@@ -30,6 +30,35 @@ void test_to_chars_scientific(const ResultsType& decimals, const StringsType& st
3030 }
3131}
3232
33+ // The cohorts will compare equal regardless so here we check bit-wise equality to be a successful roundtrip
34+ template <typename T, std::size_t N, typename StringsType>
35+ void test_roundtrip (const std::array<T, N>& decimals, const std::array<StringsType, N>& strings)
36+ {
37+ using bit_type = std::conditional_t <std::is_same<T, decimal32_t >::value, std::uint32_t ,
38+ std::conditional_t <std::is_same<T, decimal64_t >::value, std::uint64_t , boost::int128::uint128_t >>;
39+
40+ for (std::size_t i {}; i < decimals.size (); ++i)
41+ {
42+ bit_type initial_bits;
43+ std::memcpy (&initial_bits, &decimals[i], sizeof (initial_bits));
44+
45+ char buffer[64 ] {};
46+ const auto r {to_chars (buffer, buffer + sizeof (buffer), decimals[i], chars_format::cohort_preserving_scientific)};
47+ BOOST_TEST (r);
48+ *r.ptr = ' \0 ' ;
49+ BOOST_TEST_CSTR_EQ (buffer, strings[i]);
50+
51+ T return_val;
52+ const auto return_r {from_chars (buffer, buffer + sizeof (buffer), return_val, chars_format::cohort_preserving_scientific)};
53+ BOOST_TEST (return_r);
54+
55+ bit_type return_bits;
56+ std::memcpy (&return_bits, &return_val, sizeof (return_bits));
57+
58+ BOOST_TEST_EQ (initial_bits, return_bits);
59+ }
60+ }
61+
3362template <typename T>
3463const std::array<T, 7 > decimals = {
3564 T{3 , 2 },
@@ -101,5 +130,17 @@ int main()
101130 test_to_chars_scientific (negative_values<decimal64_t >, negative_values_strings);
102131 test_to_chars_scientific (negative_values<decimal128_t >, negative_values_strings);
103132
133+ test_roundtrip (decimals<decimal32_t >, strings);
134+ test_roundtrip (decimals<decimal64_t >, strings);
135+ test_roundtrip (decimals<decimal128_t >, strings);
136+
137+ test_roundtrip (decimals_with_exp<decimal32_t >, decimals_with_exp_strings);
138+ test_roundtrip (decimals_with_exp<decimal64_t >, decimals_with_exp_strings);
139+ test_roundtrip (decimals_with_exp<decimal128_t >, decimals_with_exp_strings);
140+
141+ test_roundtrip (negative_values<decimal32_t >, negative_values_strings);
142+ test_roundtrip (negative_values<decimal64_t >, negative_values_strings);
143+ test_roundtrip (negative_values<decimal128_t >, negative_values_strings);
144+
104145 return boost::report_errors ();
105146}
0 commit comments