|
5 | 5 | #include <boost/decimal.hpp> |
6 | 6 | #include <boost/core/lightweight_test.hpp> |
7 | 7 | #include <limits> |
| 8 | +#include <array> |
| 9 | +#include <cctype> |
| 10 | +#include <string> |
8 | 11 |
|
9 | 12 | using namespace boost::decimal; |
10 | 13 |
|
@@ -197,6 +200,42 @@ void test_with_string() |
197 | 200 | BOOST_TEST_EQ(std::format("Height is: {} meters", T {2}), "Height is: 2 meters"); |
198 | 201 | } |
199 | 202 |
|
| 203 | +template <typename T> |
| 204 | +void test_cohort_preservation() |
| 205 | +{ |
| 206 | + const std::array<T, 7> decimals = { |
| 207 | + T {5, 4}, |
| 208 | + T {50, 3}, |
| 209 | + T {500, 2}, |
| 210 | + T {5000, 1}, |
| 211 | + T {50000, 0}, |
| 212 | + T {500000, -1}, |
| 213 | + T {5000000, -2}, |
| 214 | + }; |
| 215 | + |
| 216 | + const std::array<const char*, 7> result_strings = { |
| 217 | + "5e+04", |
| 218 | + "5.0e+04", |
| 219 | + "5.00e+04", |
| 220 | + "5.000e+04", |
| 221 | + "5.0000e+04", |
| 222 | + "5.00000e+04", |
| 223 | + "5.000000e+04", |
| 224 | + }; |
| 225 | + |
| 226 | + for (std::size_t i {}; i < decimals.size(); ++i) |
| 227 | + { |
| 228 | + BOOST_TEST_CSTR_EQ(std::format("{:a}", decimals[i]).c_str(), result_strings[i]); |
| 229 | + |
| 230 | + std::string s {result_strings[i]}; |
| 231 | + std::transform(s.begin(), s.end(), s.begin(), |
| 232 | + [](unsigned char c) |
| 233 | + { return std::toupper(c); }); |
| 234 | + |
| 235 | + BOOST_TEST_CSTR_EQ(std::format("{:A}", decimals[i]).c_str(), s.c_str()); |
| 236 | + } |
| 237 | +} |
| 238 | + |
200 | 239 | int main() |
201 | 240 | { |
202 | 241 | test_general<decimal32_t>(); |
@@ -234,6 +273,10 @@ int main() |
234 | 273 | test_with_string<decimal128_t>(); |
235 | 274 | test_with_string<decimal_fast128_t>(); |
236 | 275 |
|
| 276 | + test_cohort_preservation<decimal32_t>(); |
| 277 | + test_cohort_preservation<decimal64_t>(); |
| 278 | + test_cohort_preservation<decimal128_t>(); |
| 279 | + |
237 | 280 | return boost::report_errors(); |
238 | 281 | } |
239 | 282 |
|
|
0 commit comments