Skip to content

Commit 6fcbde7

Browse files
committed
Test invalid from_chars values
1 parent fde22df commit 6fcbde7

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

test/test_charconv_preservation.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@ void test_roundtrip(const std::array<T, N>& decimals, const std::array<StringsTy
5959
}
6060
}
6161

62+
template <typename TargetDecimalType, typename T, std::size_t N>
63+
void test_invalid_values(const std::array<T, N>& strings)
64+
{
65+
for (std::size_t i {}; i < strings.size(); ++i)
66+
{
67+
TargetDecimalType val;
68+
const auto r {from_chars(strings[i], strings[i] + sizeof(strings[i]), val, chars_format::cohort_preserving_scientific)};
69+
BOOST_TEST(!r);
70+
}
71+
}
72+
6273
template <typename T>
6374
const std::array<T, 7> decimals = {
6475
T{3, 2},
@@ -116,6 +127,12 @@ constexpr std::array<const char*, 5> negative_values_strings = {
116127
"-3.210000e-47"
117128
};
118129

130+
constexpr std::array<const char*, 3> invalid_decimal32_strings = {
131+
"+3.2e+20",
132+
"3.421",
133+
"9.999999999999999e+05",
134+
};
135+
119136
int main()
120137
{
121138
test_to_chars_scientific(decimals<decimal32_t>, strings);
@@ -142,5 +159,12 @@ int main()
142159
test_roundtrip(negative_values<decimal64_t>, negative_values_strings);
143160
test_roundtrip(negative_values<decimal128_t>, negative_values_strings);
144161

162+
test_invalid_values<decimal32_t>(invalid_decimal32_strings);
163+
164+
// Every value for fast types are invalid
165+
test_invalid_values<decimal_fast32_t>(strings);
166+
test_invalid_values<decimal_fast64_t>(decimals_with_exp_strings);
167+
test_invalid_values<decimal_fast128_t>(negative_values_strings);
168+
145169
return boost::report_errors();
146170
}

0 commit comments

Comments
 (0)