|
12 | 12 | #include <boost/decimal/fmt_format.hpp> |
13 | 13 | #include <boost/core/lightweight_test.hpp> |
14 | 14 | #include <limits> |
| 15 | +#include <array> |
| 16 | +#include <cctype> |
| 17 | +#include <string> |
15 | 18 |
|
16 | 19 | using namespace boost::decimal; |
17 | 20 |
|
@@ -222,6 +225,42 @@ void test_with_string() |
222 | 225 | BOOST_TEST_EQ(fmt::format("Height is: {} meters", T {2}), "Height is: 2 meters"); |
223 | 226 | } |
224 | 227 |
|
| 228 | +template <typename T> |
| 229 | +void test_cohort_preservation() |
| 230 | +{ |
| 231 | + const std::array<T, 7> decimals = { |
| 232 | + T {5, 4}, |
| 233 | + T {50, 3}, |
| 234 | + T {500, 2}, |
| 235 | + T {5000, 1}, |
| 236 | + T {50000, 0}, |
| 237 | + T {500000, -1}, |
| 238 | + T {5000000, -2}, |
| 239 | + }; |
| 240 | + |
| 241 | + const std::array<const char*, 7> result_strings = { |
| 242 | + "5e+04", |
| 243 | + "5.0e+04", |
| 244 | + "5.00e+04", |
| 245 | + "5.000e+04", |
| 246 | + "5.0000e+04", |
| 247 | + "5.00000e+04", |
| 248 | + "5.000000e+04", |
| 249 | + }; |
| 250 | + |
| 251 | + for (std::size_t i {}; i < decimals.size(); ++i) |
| 252 | + { |
| 253 | + BOOST_TEST_CSTR_EQ(fmt::format("{:a}", decimals[i]).c_str(), result_strings[i]); |
| 254 | + |
| 255 | + std::string s {result_strings[i]}; |
| 256 | + std::transform(s.begin(), s.end(), s.begin(), |
| 257 | + [](unsigned char c) |
| 258 | + { return std::toupper(c); }); |
| 259 | + |
| 260 | + BOOST_TEST_CSTR_EQ(fmt::format("{:A}", decimals[i]).c_str(), s.c_str()); |
| 261 | + } |
| 262 | +} |
| 263 | + |
225 | 264 | #ifdef _MSC_VER |
226 | 265 | #pragma warning(pop) |
227 | 266 | #endif |
@@ -263,6 +302,10 @@ int main() |
263 | 302 | test_with_string<decimal128_t>(); |
264 | 303 | test_with_string<decimal_fast128_t>(); |
265 | 304 |
|
| 305 | + test_cohort_preservation<decimal32_t>(); |
| 306 | + test_cohort_preservation<decimal64_t>(); |
| 307 | + test_cohort_preservation<decimal128_t>(); |
| 308 | + |
266 | 309 | return boost::report_errors(); |
267 | 310 | } |
268 | 311 |
|
|
0 commit comments