Skip to content

Commit 250345d

Browse files
committed
Add basic testing of cohort preserving <format>
1 parent 3321476 commit 250345d

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

test/test_format.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
#include <boost/decimal.hpp>
66
#include <boost/core/lightweight_test.hpp>
77
#include <limits>
8+
#include <array>
9+
#include <cctype>
10+
#include <string>
811

912
using namespace boost::decimal;
1013

@@ -197,6 +200,42 @@ void test_with_string()
197200
BOOST_TEST_EQ(std::format("Height is: {} meters", T {2}), "Height is: 2 meters");
198201
}
199202

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+
200239
int main()
201240
{
202241
test_general<decimal32_t>();
@@ -234,6 +273,10 @@ int main()
234273
test_with_string<decimal128_t>();
235274
test_with_string<decimal_fast128_t>();
236275

276+
test_cohort_preservation<decimal32_t>();
277+
test_cohort_preservation<decimal64_t>();
278+
test_cohort_preservation<decimal128_t>();
279+
237280
return boost::report_errors();
238281
}
239282

0 commit comments

Comments
 (0)