Debian Codegraph @

libstdc++-v3/testsuite/20_util/from_chars/7.cc

raw
1 // Testcases for binary64 hexfloat std::from_chars.
2 // { dg-do run { target c++17 } }
3 // { dg-require-effective-target ieee_floats }
4
5 #include <charconv>
6
7 #include <cfenv>
8 #include <cmath>
9 #include <cstring>
10 #include <cstdio>
11 #include <limits>
12 #include <testsuite_hooks.h>
13
14 struct testcase {
15 const char* input;
16 size_t correct_idx;
17 std::errc correct_ec;
18 double correct_value;
19 };
20
21 constexpr testcase testcases[] = {
22 { "1.fffffffffffff8p0", 18, {}, 0x1.fffffffffffff8p0 },
23 { "0.fffffffffffff8p-1022", 22, std::errc{}, 0x0.fffffffffffffep-1022 },
24 { "inf", 3, {}, std::numeric_limits<double>::infinity() },
25 { "inff", 3, {}, std::numeric_limits<double>::infinity() },
26 { "-inf", 4, {}, -std::numeric_limits<double>::infinity() },
27 { "-inff", 4, {}, -std::numeric_limits<double>::infinity() },
28 { "NAN", 3, {}, std::numeric_limits<double>::quiet_NaN() },
29 { "-NAN", 4, {}, std::numeric_limits<double>::quiet_NaN() },
30 { "NAN()", 5, {}, std::numeric_limits<double>::quiet_NaN() },
31 { "-NAN()", 6, {}, std::numeric_limits<double>::quiet_NaN() },
32 { "-NAN(test)", 10, {}, std::numeric_limits<double>::quiet_NaN() },
33 { "-NAN(test", 4, {}, std::numeric_limits<double>::quiet_NaN() },
34 { "-NAN(", 4, {}, std::numeric_limits<double>::quiet_NaN() },
35 { "0.000000000000001p-100000000000000000", 37, std::errc::result_out_of_range, 0 },
36 { "-lol", 0, std::errc::invalid_argument, 1 },
37 { " 0", 0, std::errc::invalid_argument, 1 },
38 { "", 0, std::errc::invalid_argument, 0 },
39 { "1", 1, {}, 1 },
40 { "2", 1, {}, 2 },
41 { "3", 1, {}, 3 },
42 { "4", 1, {}, 4 },
43 { "5", 1, {}, 5 },
44 { "6", 1, {}, 6 },
45 { "7", 1, {}, 7 },
46 { "8", 1, {}, 8 },
47 { "9", 1, {}, 9 },
48 { "a", 1, {}, 0xa },
49 { "b", 1, {}, 0xb },
50 { "c", 1, {}, 0xc },
51 { "d", 1, {}, 0xd },
52 { "e", 1, {}, 0xe },
53 { "f", 1, {}, 0xf },
54 { "0.000000000000000000000000000000000000000000001p-1022", 53,
55 std::errc::result_out_of_range, 0 },
56 { "0.00000000000000p-1022", 22, {}, 0 },
57 { "0.00000000000009", 16, {}, 0x0.00000000000009p0 },
58 { "0.0", 3, {}, 0 },
59 { "1p10000000000000000000000", 25, std::errc::result_out_of_range, 0 },
60 { "-0.0", 4, {}, -0.0 },
61 { "0.00000000000000", 16, {}, 0 },
62 { "0.0000000000000p-1022", 21, {}, 0 },
63 { ".", 0, std::errc::invalid_argument, 0 },
64 { "-.", 0, std::errc::invalid_argument, 0 },
65 { "0", 1, {}, 0 },
66 { "00", 2, {}, 0 },
67 { "00.", 3, {}, 0 },
68 { "0.", 2, {}, 0 },
69 { "1.ffffFFFFFFFFFF", 16, {}, 0x2 },
70 { "1.ffffffffffffff", 16, {}, 0x2 },
71 { "1.00000000000029", 16, {}, 0x1.0000000000003p0 },
72 { "0.00000000000008p-1022", 22, std::errc::result_out_of_range, 0 },
73 { "1.fffffffffffffp-1023", 21, {}, 0x1p-1022 },
74 { "1.fffffffffffff8p+1023", 22, std::errc::result_out_of_range, 0 },
75 { "0.ffffffffffffe8p-1022", 22, {}, 0x0.ffffffffffffep-1022 },
76 { "2.11111111111111", 16, {}, 0x1.0888888888889p+1 },
77 { "1.1111111111111", 15, {}, 0x1.1111111111111p0 },
78 { "1.11111111111111", 16, {}, 0x1.1111111111111p0 },
79 { "1.11111111111118", 16, {}, 0x1.1111111111112p0 },
80 { "1.11111111111128", 16, {}, 0x1.1111111111112p0 },
81 { "1.1111111111112801", 18, {}, 0x1.1111111111113p0 },
82 { "1.08888888888888", 16, {}, 0x1.0888888888888p0 },
83 { "1.088888888888888", 17, {}, 0x1.0888888888889p0 },
84 { "2.00000000000029", 16, {}, 0x2.0000000000002p0 },
85 { "0.ffffffffffffep-1022", 21, {}, 0x0.ffffffffffffep-1022 },
86 { "3.ffffffffffffep-1024", 21, {}, 0x1p-1022 },
87 { "1.00000000000008p+0", 19, {}, 1 },
88 { "1p-1023", 7, {}, 0x0.8p-1022 },
89 { "1p-1022", 7, {}, 0x1p-1022 },
90 { "1.1p-1033", 9, {}, 0x1.1p-1033 }, // 0.0022p-1022
91 { "22p-1038", 8, {}, 0x1.1p-1033 },
92 { "5", 1, {}, 0x5 },
93 { "a", 1, {}, 0xa },
94 { "1", 1, {}, 1.0 },
95 { "1p1", 3, {}, 0x1p1 },
96 { "1p-1", 4, {}, 0x1p-1 },
97 { "0", 1, {}, 0.0 },
98 { "A", 1, {}, 0xA },
99 { "1.ABCDEFP+10", 12, {}, 0x1.ABCDEFP+10 },
100 { "-1", 2, {}, -1.0 },
101 { "-0", 2, {}, -0.0 },
102 { "42", 2, {}, 0x42p0 },
103 { "-42", 3, {}, -0x42p0 },
104 { ".1", 2, {}, 0x0.1p0 },
105 { "22p-1000", 8, {}, 0x22p-1000 },
106 { ".0000008", 8, {}, 0x.0000008p0 },
107 { ".0000008p-1022", 14, {}, 0x.0000008p-1022 },
108 { "1p-1074", 7, {}, 0x.0000000000001p-1022 },
109 { "9999999999999", 13, {}, 0x9999999999999p0 },
110 { "1.000000000000a000", 18, {}, 0x1.000000000000ap0 },
111 { "1.000000000000a001", 18, {}, 0x1.000000000000ap0 },
112 { "1.000000000000a800", 18, {}, 0x1.000000000000ap0 },
113 { "1.000000000000a801", 18, {}, 0x1.000000000000bp0 },
114 { "1.000000000000b800", 18, {}, 0x1.000000000000cp0 },
115 { "000000", 6, {}, 0x0 },
116 { "1p", 1, {}, 0x1 },
117 { "0p99999999999999999999", 22, {}, 0 },
118 { "1p99999999999999999999", 22, std::errc::result_out_of_range, 0 },
119 { "0p-99999999999999999999", 23, {}, 0 },
120 { "1p-99999999999999999999", 23, std::errc::result_out_of_range, 0 },
121 { "99999999999999999999999", 23, {}, 0x99999999999999999999999p0 },
122 { "-1.fffffffffffffp-1023", 22, {}, -0x1p-1022 },
123 { "1.337", 5, {}, 0x1.337p0 },
124 };
125
126 void
127 test01()
128 {
129 for (auto [input,correct_idx,correct_ec,correct_value] : testcases)
130 {
131 double value;
132 auto [ptr,ec] = std::from_chars(input, input+strlen(input),
133 value, std::chars_format::hex);
134 VERIFY( ptr == input + correct_idx );
135 VERIFY( ec == correct_ec );
136 if (ec == std::errc{})
137 {
138 if (std::isnan(value) || std::isnan(correct_value))
139 VERIFY( std::isnan(value) && std::isnan(correct_value) );
140 else
141 {
142 VERIFY( value == correct_value );
143 VERIFY( !memcmp(&value, &correct_value, sizeof(double)) );
144 }
145 }
146 }
147 }
148
149 int
150 main()
151 {
152 test01();
153 }