| 1 |
// Copyright (c) Microsoft Corporation. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 |
|
| 4 |
#ifndef FLOAT_FROM_CHARS_TEST_CASES_HPP |
| 5 |
#define FLOAT_FROM_CHARS_TEST_CASES_HPP |
| 6 |
|
| 7 |
#include <charconv> |
| 8 |
#include <stddef.h> |
| 9 |
#include <system_error> |
| 10 |
using namespace std; |
| 11 |
|
| 12 |
inline constexpr FloatFromCharsTestCase float_from_chars_test_cases[] = { |
| 13 |
{"1.a0000400", chars_format::hex, 10, errc{}, 0x1.a00004p0f}, // exact |
| 14 |
{"1.a0000401", chars_format::hex, 10, errc{}, 0x1.a00004p0f}, // below midpoint, round down |
| 15 |
{"1.a0000500", chars_format::hex, 10, errc{}, 0x1.a00004p0f}, // midpoint, round down to even |
| 16 |
{"1.a0000501", chars_format::hex, 10, errc{}, 0x1.a00006p0f}, // above midpoint, round up |
| 17 |
{"1.a0000600", chars_format::hex, 10, errc{}, 0x1.a00006p0f}, // exact |
| 18 |
{"1.a0000601", chars_format::hex, 10, errc{}, 0x1.a00006p0f}, // below midpoint, round down |
| 19 |
{"1.a0000700", chars_format::hex, 10, errc{}, 0x1.a00008p0f}, // midpoint, round up to even |
| 20 |
{"1.a0000701", chars_format::hex, 10, errc{}, 0x1.a00008p0f}, // above midpoint, round up |
| 21 |
|
| 22 |
{"1.0000040", chars_format::hex, 9, errc{}, 0x1.000004p0f}, // exact |
| 23 |
{"1.0000041", chars_format::hex, 9, errc{}, 0x1.000004p0f}, // below midpoint, round down |
| 24 |
{"1.0000050", chars_format::hex, 9, errc{}, 0x1.000004p0f}, // midpoint, round down to even |
| 25 |
{"1.0000051", chars_format::hex, 9, errc{}, 0x1.000006p0f}, // above midpoint, round up |
| 26 |
{"1.0000060", chars_format::hex, 9, errc{}, 0x1.000006p0f}, // exact |
| 27 |
{"1.0000061", chars_format::hex, 9, errc{}, 0x1.000006p0f}, // below midpoint, round down |
| 28 |
{"1.0000070", chars_format::hex, 9, errc{}, 0x1.000008p0f}, // midpoint, round up to even |
| 29 |
{"1.0000071", chars_format::hex, 9, errc{}, 0x1.000008p0f}, // above midpoint, round up |
| 30 |
|
| 31 |
{"1.0000002384185791015625000000", chars_format::general, 30, errc{}, 0x1.000004p0f}, // exact |
| 32 |
{"1.0000002421438694000244140625", chars_format::general, 30, errc{}, 0x1.000004p0f}, // below midpoint, round down |
| 33 |
{"1.0000002980232238769531249999", chars_format::general, 30, errc{}, 0x1.000004p0f}, // below midpoint, round down |
| 34 |
{"1.0000002980232238769531250000", chars_format::general, 30, errc{}, |
| 35 |
0x1.000004p0f}, // midpoint, round down to even |
| 36 |
{"1.0000002980232238769531250001", chars_format::general, 30, errc{}, 0x1.000006p0f}, // above midpoint, round up |
| 37 |
{"1.0000003017485141754150390625", chars_format::general, 30, errc{}, 0x1.000006p0f}, // above midpoint, round up |
| 38 |
{"1.0000003576278686523437500000", chars_format::general, 30, errc{}, 0x1.000006p0f}, // exact |
| 39 |
{"1.0000003613531589508056640625", chars_format::general, 30, errc{}, 0x1.000006p0f}, // below midpoint, round down |
| 40 |
{"1.0000004172325134277343749999", chars_format::general, 30, errc{}, 0x1.000006p0f}, // below midpoint, round down |
| 41 |
{"1.0000004172325134277343750000", chars_format::general, 30, errc{}, 0x1.000008p0f}, // midpoint, round up to even |
| 42 |
{"1.0000004172325134277343750001", chars_format::general, 30, errc{}, 0x1.000008p0f}, // above midpoint, round up |
| 43 |
{"1.0000004209578037261962890625", chars_format::general, 30, errc{}, 0x1.000008p0f}, // above midpoint, round up |
| 44 |
|
| 45 |
// VSO-838635 "<charconv>: from_chars() mishandles certain subnormals" |
| 46 |
// This bug didn't actually affect float, but we should have similar test cases. |
| 47 |
// These values change on half-ulp boundaries: |
| 48 |
// 1 * 2^-150 ~= 7.01e-46 (half-ulp between zero and min subnormal) |
| 49 |
// 2 * 2^-150 ~= 1.40e-45 (min subnormal) |
| 50 |
// 3 * 2^-150 ~= 2.10e-45 (half-ulp between min subnormal and next subnormal) |
| 51 |
// 4 * 2^-150 ~= 2.80e-45 (next subnormal) |
| 52 |
{"6." |
| 53 |
"6666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666" |
| 54 |
"6666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666" |
| 55 |
"6666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666" |
| 56 |
"6666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666" |
| 57 |
"6666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666" |
| 58 |
"6666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666" |
| 59 |
"6666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666" |
| 60 |
"6666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666" |
| 61 |
"6666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666" |
| 62 |
"6666666666666666666e-46", |
| 63 |
chars_format::scientific, 1006, errc::result_out_of_range, 0x0.000000p+0f}, |
| 64 |
{"7." |
| 65 |
"7777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777" |
| 66 |
"7777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777" |
| 67 |
"7777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777" |
| 68 |
"7777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777" |
| 69 |
"7777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777" |
| 70 |
"7777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777" |
| 71 |
"7777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777" |
| 72 |
"7777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777" |
| 73 |
"7777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777" |
| 74 |
"7777777777777777777e-46", |
| 75 |
chars_format::scientific, 1006, errc{}, 0x0.000002p-126f}, |
| 76 |
{"8." |
| 77 |
"8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888" |
| 78 |
"8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888" |
| 79 |
"8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888" |
| 80 |
"8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888" |
| 81 |
"8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888" |
| 82 |
"8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888" |
| 83 |
"8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888" |
| 84 |
"8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888" |
| 85 |
"8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888" |
| 86 |
"8888888888888888888e-46", |
| 87 |
chars_format::scientific, 1006, errc{}, 0x0.000002p-126f}, |
| 88 |
{"9." |
| 89 |
"9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999" |
| 90 |
"9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999" |
| 91 |
"9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999" |
| 92 |
"9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999" |
| 93 |
"9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999" |
| 94 |
"9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999" |
| 95 |
"9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999" |
| 96 |
"9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999" |
| 97 |
"9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999" |
| 98 |
"9999999999999999999e-46", |
| 99 |
chars_format::scientific, 1006, errc{}, 0x0.000002p-126f}, |
| 100 |
{"1." |
| 101 |
"1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" |
| 102 |
"1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" |
| 103 |
"1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" |
| 104 |
"1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" |
| 105 |
"1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" |
| 106 |
"1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" |
| 107 |
"1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" |
| 108 |
"1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" |
| 109 |
"1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" |
| 110 |
"1111111111111111111e-45", |
| 111 |
chars_format::scientific, 1006, errc{}, 0x0.000002p-126f}, |
| 112 |
{"2." |
| 113 |
"2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222" |
| 114 |
"2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222" |
| 115 |
"2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222" |
| 116 |
"2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222" |
| 117 |
"2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222" |
| 118 |
"2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222" |
| 119 |
"2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222" |
| 120 |
"2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222" |
| 121 |
"2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222" |
| 122 |
"2222222222222222222e-45", |
| 123 |
chars_format::scientific, 1006, errc{}, 0x0.000004p-126f}, |
| 124 |
|
| 125 |
// VSO-733765 "<charconv>: [Feedback] double std::from_chars behavior on exponent out of range" |
| 126 |
// LWG-3081 "Floating point from_chars API does not distinguish between overflow and underflow" |
| 127 |
// These test cases exercise every overflow/underflow codepath. |
| 128 |
{"1e+1000", chars_format::scientific, 7, errc::result_out_of_range, float_inf}, |
| 129 |
{"1e-1000", chars_format::scientific, 7, errc::result_out_of_range, 0.0f}, |
| 130 |
{"1.ffffffp+127", chars_format::hex, 13, errc::result_out_of_range, float_inf}, |
| 131 |
{"1e+2000", chars_format::scientific, 7, errc::result_out_of_range, float_inf}, |
| 132 |
{"1e-2000", chars_format::scientific, 7, errc::result_out_of_range, 0.0f}, |
| 133 |
{"1e+9999", chars_format::scientific, 7, errc::result_out_of_range, float_inf}, |
| 134 |
{"1e-9999", chars_format::scientific, 7, errc::result_out_of_range, 0.0f}, |
| 135 |
{"10e+5199", chars_format::scientific, 8, errc::result_out_of_range, float_inf}, |
| 136 |
{"0.001e-5199", chars_format::scientific, 11, errc::result_out_of_range, 0.0f}, |
| 137 |
}; |
| 138 |
|
| 139 |
#endif // FLOAT_FROM_CHARS_TEST_CASES_HPP |