Skip to content

Commit 3321476

Browse files
committed
Add basic testing of cohort preserving {fmt} formatting
1 parent 501f528 commit 3321476

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

test/test_format_fmtlib.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
#include <boost/decimal/fmt_format.hpp>
1313
#include <boost/core/lightweight_test.hpp>
1414
#include <limits>
15+
#include <array>
16+
#include <cctype>
17+
#include <string>
1518

1619
using namespace boost::decimal;
1720

@@ -222,6 +225,42 @@ void test_with_string()
222225
BOOST_TEST_EQ(fmt::format("Height is: {} meters", T {2}), "Height is: 2 meters");
223226
}
224227

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+
225264
#ifdef _MSC_VER
226265
#pragma warning(pop)
227266
#endif
@@ -263,6 +302,10 @@ int main()
263302
test_with_string<decimal128_t>();
264303
test_with_string<decimal_fast128_t>();
265304

305+
test_cohort_preservation<decimal32_t>();
306+
test_cohort_preservation<decimal64_t>();
307+
test_cohort_preservation<decimal128_t>();
308+
266309
return boost::report_errors();
267310
}
268311

0 commit comments

Comments
 (0)