]>
Commit | Line | Data |
---|---|---|
99dee823 | 1 | // Copyright (C) 2020-2021 Free Software Foundation, Inc. |
3c57e692 PP |
2 | // |
3 | // This file is part of the GNU ISO C++ Library. This library is free | |
4 | // software; you can redistribute it and/or modify it under the | |
5 | // terms of the GNU General Public License as published by the | |
6 | // Free Software Foundation; either version 3, or (at your option) | |
7 | // any later version. | |
8 | ||
9 | // This library is distributed in the hope that it will be useful, | |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | // GNU General Public License for more details. | |
13 | ||
14 | // You should have received a copy of the GNU General Public License along | |
15 | // with this library; see the file COPYING3. If not see | |
16 | // <http://www.gnu.org/licenses/>. | |
17 | ||
18 | // <charconv> is supported in C++14 as a GNU extension, but this test uses C++17 | |
19 | // hexadecimal floating-point literals. | |
20 | // { dg-do run { target c++17 } } | |
6a31d47e | 21 | // { dg-require-effective-target ieee-floats } |
3c57e692 PP |
22 | |
23 | #include <charconv> | |
24 | ||
25 | #include <cmath> | |
26 | #include <cstring> | |
27 | #include <iterator> | |
70aa0e6e | 28 | #include <optional> |
3c57e692 PP |
29 | #include <limits> |
30 | ||
31 | #include <testsuite_hooks.h> | |
32 | ||
33 | using namespace std; | |
34 | ||
19f3c433 PP |
35 | namespace detail |
36 | { | |
37 | long double | |
38 | nextupl(long double x) | |
39 | { return nexttowardl(x, numeric_limits<long double>::infinity()); } | |
40 | ||
41 | long double | |
42 | nextdownl(long double x) | |
43 | { return nexttowardl(x, -numeric_limits<long double>::infinity()); } | |
44 | } | |
45 | ||
3c57e692 PP |
46 | // The long double overloads of std::to_chars currently just go through printf |
47 | // (except for the hexadecimal formatting). | |
48 | ||
49 | // Test our hand-written hexadecimal formatting implementation. | |
50 | void | |
51 | test01() | |
52 | { | |
70aa0e6e PP |
53 | // Verifies correctness of the hexadecimal form [BEGIN,END) for VALUE by |
54 | // round-tripping it through from_chars (if available). | |
55 | auto verify_via_from_chars = [] (char *begin, char *end, long double value) { | |
56 | #if __cpp_lib_to_chars >= 201611L || _GLIBCXX_HAVE_USELOCALE | |
57 | long double roundtrip; | |
58 | auto result = from_chars(begin, end, roundtrip, chars_format::hex); | |
59 | VERIFY( result.ec == errc{} ); | |
60 | VERIFY( result.ptr == end ); | |
61 | VERIFY( roundtrip == value ); | |
62 | #endif | |
63 | }; | |
64 | ||
65 | // Verifies correctness of the null-terminated hexadecimal form at BEGIN | |
66 | // for VALUE and PRECISION by comparing it with the output of printf's %La | |
67 | // conversion specifier. | |
68 | auto verify_via_printf = [] (char *begin, long double value, | |
69 | optional<int> precision = nullopt) { | |
70 | char printf_buffer[1024] = {}; | |
71 | if (precision.has_value()) | |
72 | sprintf(printf_buffer, "%.*La", precision.value(), value); | |
73 | else | |
74 | sprintf(printf_buffer, "%La", value); | |
75 | ||
76 | // Only compare with the output of printf if the leading hex digits agree. | |
77 | // If the leading hex digit of our form doesn't agree with that of printf, | |
78 | // then the two forms may still be equivalent (e.g. 1.1p+0 vs 8.8p-3). But | |
79 | // if the leading hex digits do agree, then we do expect the two forms to be | |
80 | // the same. | |
81 | if (printf_buffer[strlen("0x")] == begin[0]) | |
82 | VERIFY( !strcmp(begin, printf_buffer+strlen("0x")) ); | |
83 | }; | |
84 | ||
3c57e692 | 85 | const long double hex_testcases[] |
19f3c433 PP |
86 | = { detail::nextdownl(numeric_limits<long double>::max()), |
87 | detail::nextupl(numeric_limits<long double>::min()), | |
3c57e692 PP |
88 | 42.0L, |
89 | 0x1.2p+0L, | |
90 | 0x1.23p+0L, | |
91 | 0x1.234p+0L, | |
92 | 0x1.2345p+0L, | |
93 | 0x1.23456p+0L, | |
94 | 0x1.234567p+0L, | |
95 | 0x1.2345678p+0L, | |
96 | 0x1.23456789p+0L, | |
97 | 0x1.23456789p+0L, | |
98 | 0x1.23456789ap+0L, | |
99 | 0x1.23456789abp+0L, | |
100 | 0x1.23456789abcp+0L, | |
101 | 0x1.23456789abcdp+0L, | |
102 | 0x1.23456789abcdep+0L, | |
103 | 0x1.23456789abcdefp+0L, | |
104 | 0x1.23456789abcdef0p+0L, | |
105 | 0x1.23456789abcdef01p+0L, | |
106 | 0x1.23456789abcdef012p+0L, | |
107 | 0x1.23456789abcdef0123p+0L, | |
108 | 0x1.23456789abcdef01234p+0L, | |
109 | 0x1.23456789abcdef012345p+0L, | |
110 | 0x1.23456789abcdef0123456p+0L, | |
111 | 0x1.23456789abcdef01234567p+0L, | |
112 | 0x1.23456789abcdef012345678p+0L, | |
113 | 0x1.23456789abcdef0123456789p+0L, | |
114 | 0x1.23456789abcdef0123456789ap+0L, | |
115 | 0x1.23456789abcdef0123456789abp+0L, | |
116 | 0x1.23456789abcdef0123456789abcp+0L, | |
117 | 0x1.23456789abcdef0123456789abcdp+0L, | |
118 | }; | |
119 | ||
120 | for (int exponent : {-11000, -3000, -300, -50, -7, 0, 7, 50, 300, 3000, 11000}) | |
121 | for (long double testcase : hex_testcases) | |
122 | { | |
123 | testcase = ldexpl(testcase, exponent); | |
124 | if (testcase == 0.0L || isinf(testcase)) | |
125 | continue; | |
126 | ||
70aa0e6e | 127 | char to_chars_buffer[1024] = {}; |
3c57e692 PP |
128 | auto result = to_chars(begin(to_chars_buffer), end(to_chars_buffer), |
129 | testcase, chars_format::hex); | |
130 | VERIFY( result.ec == errc{} ); | |
131 | *result.ptr = '\0'; | |
70aa0e6e PP |
132 | verify_via_from_chars(begin(to_chars_buffer), result.ptr, testcase); |
133 | verify_via_printf(to_chars_buffer, testcase); | |
3c57e692 | 134 | |
70aa0e6e PP |
135 | // Verify the nearby values, and also check they have a different |
136 | // shortest form. | |
137 | for (long double nearby | |
138 | : { detail::nextdownl(testcase), detail::nextupl(testcase) }) | |
3c57e692 | 139 | { |
70aa0e6e PP |
140 | char nearby_buffer[1024] = {}; |
141 | result = to_chars(begin(nearby_buffer), end(nearby_buffer), | |
142 | nearby, chars_format::hex); | |
3c57e692 PP |
143 | VERIFY( result.ec == errc{} ); |
144 | *result.ptr = '\0'; | |
70aa0e6e PP |
145 | VERIFY( strcmp(nearby_buffer, to_chars_buffer) != 0); |
146 | verify_via_from_chars(begin(nearby_buffer), result.ptr, nearby); | |
147 | verify_via_printf(nearby_buffer, nearby); | |
3c57e692 PP |
148 | } |
149 | ||
150 | for (int precision = -1; precision < 50; precision++) | |
151 | { | |
152 | result = to_chars(begin(to_chars_buffer), end(to_chars_buffer), | |
153 | testcase, chars_format::hex, precision); | |
154 | VERIFY( result.ec == errc{} ); | |
155 | *result.ptr = '\0'; | |
70aa0e6e | 156 | verify_via_printf(to_chars_buffer, testcase, precision); |
3c57e692 PP |
157 | } |
158 | } | |
159 | } | |
160 | ||
161 | // Test the rest of the formatting modes, which go through printf. | |
162 | void | |
163 | test02() | |
164 | { | |
165 | const long double growth_factor = 1.442695040888963407359924681001892137L; | |
166 | for (chars_format fmt : {chars_format::fixed, chars_format::scientific, | |
167 | chars_format::general}) | |
168 | for (long double __value = 1.0L, count = 0; !isinf(__value); | |
169 | ++count <= 100.0L ? __value *= growth_factor : __value *= __value) | |
170 | for (const long double value : {__value, 1.0L/__value}) | |
171 | { | |
172 | for (const int precision : {-1, 0, 10, 100, 10000}) | |
173 | { | |
174 | const char* const printf_specifier | |
175 | = (fmt == chars_format::fixed ? "%.*Lf" | |
176 | : fmt == chars_format::scientific ? "%.*Le" | |
177 | : fmt == chars_format::general ? "%.*Lg" | |
178 | : nullptr); | |
179 | unsigned output_length = snprintf(nullptr, 0, printf_specifier, | |
180 | precision, value); | |
181 | ||
182 | char printf_buffer[output_length+1]; | |
183 | snprintf(printf_buffer, output_length+1, printf_specifier, | |
184 | precision, value); | |
185 | ||
186 | char to_chars_buffer[output_length]; | |
187 | auto result = to_chars(to_chars_buffer, | |
188 | to_chars_buffer+output_length, | |
189 | value, fmt, precision); | |
190 | VERIFY( result.ec == errc{} ); | |
191 | VERIFY( !memcmp(printf_buffer, to_chars_buffer, output_length) ); | |
192 | ||
193 | result = to_chars(to_chars_buffer, | |
194 | to_chars_buffer+output_length-1, | |
195 | value, fmt, precision); | |
196 | VERIFY( result.ec == errc::value_too_large ); | |
197 | } | |
198 | ||
199 | // Verify that the nearby values have a different shortest form. | |
200 | char to_chars_buffer[50000]; | |
201 | auto result = to_chars(begin(to_chars_buffer), end(to_chars_buffer), | |
202 | value, fmt); | |
203 | VERIFY( result.ec == errc{} ); | |
204 | *result.ptr = '\0'; | |
205 | char nearby_buffer[50000]; | |
206 | { | |
19f3c433 | 207 | const long double smaller = detail::nextdownl(value); |
3c57e692 PP |
208 | result = to_chars(begin(nearby_buffer), end(nearby_buffer), |
209 | smaller, fmt); | |
210 | VERIFY( result.ec == errc{} ); | |
211 | *result.ptr = '\0'; | |
212 | VERIFY( strcmp(to_chars_buffer, nearby_buffer) != 0 ); | |
213 | } | |
214 | ||
215 | { | |
19f3c433 | 216 | long double larger = detail::nextupl(value); |
3c57e692 PP |
217 | result = to_chars(begin(nearby_buffer), end(nearby_buffer), |
218 | larger, fmt); | |
219 | VERIFY( result.ec == errc{} ); | |
220 | *result.ptr = '\0'; | |
221 | VERIFY( strcmp(to_chars_buffer, nearby_buffer) != 0 ); | |
222 | } | |
223 | } | |
224 | } | |
225 | ||
226 | int | |
227 | main() | |
228 | { | |
229 | test01(); | |
230 | test02(); | |
231 | } |