1 // Copyright (c) Microsoft Corporation.
2 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3 
4 
5 // Copyright 2018 Ulf Adams
6 // Copyright (c) Microsoft Corporation. All rights reserved.
7 
8 // Boost Software License - Version 1.0 - August 17th, 2003
9 
10 // Permission is hereby granted, free of charge, to any person or organization
11 // obtaining a copy of the software and accompanying documentation covered by
12 // this license (the "Software") to use, reproduce, display, distribute,
13 // execute, and transmit the Software, and to prepare derivative works of the
14 // Software, and to permit third-parties to whom the Software is furnished to
15 // do so, all subject to the following:
16 
17 // The copyright notices in the Software and this entire statement, including
18 // the above license grant, this restriction and the following disclaimer,
19 // must be included in all copies of the Software, in whole or in part, and
20 // all derivative works of the Software, unless such copies or derivative
21 // works are solely in the form of machine-executable object code generated by
22 // a source language processor.
23 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
27 // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
28 // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
29 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30 // DEALINGS IN THE SOFTWARE.
31 
32 
33 // This file contains test cases derived from:
34 // https://github.com/ulfjack/ryu
35 // See xcharconv_ryu.h for the exact commit.
36 // (Keep the cgmanifest.json commitHash in sync.)
37 
38 
39 #ifndef DOUBLE_SCIENTIFIC_PRECISION_TO_CHARS_TEST_CASES_1_HPP
40 #define DOUBLE_SCIENTIFIC_PRECISION_TO_CHARS_TEST_CASES_1_HPP
41 
42 #include <charconv>
43 
44 #include "test.hpp"
45 using namespace std;
46 
47 inline constexpr DoublePrecisionToCharsTestCase double_scientific_precision_to_chars_test_cases_1[] = {
48     // Test special cases (zero, inf, nan) and an ordinary case. Also test negative signs.
49     {0.0, chars_format::scientific, 4, "0.0000e+00"},
50     {-0.0, chars_format::scientific, 4, "-0.0000e+00"},
51     {double_inf, chars_format::scientific, 4, "inf"},
52     {-double_inf, chars_format::scientific, 4, "-inf"},
53     {double_nan, chars_format::scientific, 4, "nan"},
54     {-double_nan, chars_format::scientific, 4, "-nan(ind)"},
55     {double_nan_payload, chars_format::scientific, 4, "nan"},
56     {-double_nan_payload, chars_format::scientific, 4, "-nan"},
57     {1.729, chars_format::scientific, 4, "1.7290e+00"},
58     {-1.729, chars_format::scientific, 4, "-1.7290e+00"},
59 
60     // Ryu Printf d2fixed_test.cc D2expTest Basic
61     {0x1.000000001869fp+211, chars_format::scientific, 62,
62         "3.29100911471548643542566484557342614975886952410844652587974656e+63"},
63 
64     // Ryu Printf d2fixed_test.cc D2expTest Zero
65     {0.0, chars_format::scientific, 4, "0.0000e+00"},
66     {0.0, chars_format::scientific, 3, "0.000e+00"},
67     {0.0, chars_format::scientific, 2, "0.00e+00"},
68     {0.0, chars_format::scientific, 1, "0.0e+00"},
69     {0.0, chars_format::scientific, 0, "0e+00"},
70 
71     // Ryu Printf d2fixed_test.cc D2expTest MinMax
72     {0x0.0000000000001p-1022, chars_format::scientific, 750,
73         "4.9406564584124654417656879286822137236505980261432476442558568250067550727020875186529983"
74         "636163599237979656469544571773092665671035593979639877479601078187812630071319031140452784"
75         "581716784898210368871863605699873072305000638740915356498438731247339727316961514003171538"
76         "539807412623856559117102665855668676818703956031062493194527159149245532930545654440112748"
77         "012970999954193198940908041656332452475714786901472678015935523861155013480352649347201937"
78         "902681071074917033322268447533357208324319360923828934583680601060115061698097530783422773"
79         "183292479049825247307763759272478746560847782037344696995336470179726777175851256605511991"
80         "315048911014510378627381672509558373897335989936648099411642057026370902792427675445652290"
81         "87538682506419718265533447265625e-324"},
82 
83     {0x1.fffffffffffffp+1023, chars_format::scientific, 308,
84         "1.7976931348623157081452742373170435679807056752584499659891747680315726078002853876058955"
85         "863276687817154045895351438246423432132688946418276846754670353751698604991057655128207624"
86         "549009038932894407586850845513394230458323690322294816580855933212334827479782620414472316"
87         "8738177180919299881250404026184124858368e+308"},
88 
89     // Test more corner cases.
90     {0x0.fffffffffffffp-1022, chars_format::scientific, 766,
91         "2."
92         "2250738585072008890245868760858598876504231122409594654935248025624400092282356951787758888037591552642309"
93         "7809504343120858773871583572918219930202943792242235598198275012420417889695713117910822610439719796040004"
94         "5489739193807919893608152561311337614984204327175103362739154978273159414382813627511383860409424946494228"
95         "6316695429105080201815926642134996606517803095075913058719846423906068637102005108723282784678843631944515"
96         "8661350412234790147923695852083215976210663754016137365830441936037147783553066828345356340050740730401356"
97         "0296804637591858316312422452159926254649430083685186171942241764645513713542013221703137049658321015465406"
98         "8035397417906022589503023501937519773030945763173210852507299305089761582519159720757232455434770912461317"
99         "493580281734466552734375e-308"}, // max subnormal
100     {0x1p-1022, chars_format::scientific, 714,
101         "2."
102         "2250738585072013830902327173324040642192159804623318305533274168872044348139181958542831590125110205640673"
103         "3973103581100515243416155346010885601238537771882113077799353200233047961014744258363607192156504694250373"
104         "4208375250806650616658158948720491179968591639648500635908770118304874799780887753749949451580451605050915"
105         "3998565824708186451135379358049921159810857660519924333521143523901487956996095912888916029926415110634663"
106         "1339366347758651302937176204732563178148566435087212282863764204484681140761391147706280168985324411002416"
107         "1447421618567166150540154285084716752901903161322778896729707373123334086988983175067838846926092773977972"
108         "858659654941091369095406136467568702398678315290680984617210924625396728515625e-308"}, // min normal
109 
110     // Ryu Printf d2fixed_test.cc D2expTest RoundToEven
111     {0.125, chars_format::scientific, 2, "1.25e-01"},
112     {0.125, chars_format::scientific, 1, "1.2e-01"},
113     {0.375, chars_format::scientific, 2, "3.75e-01"},
114     {0.375, chars_format::scientific, 1, "3.8e-01"},
115 
116     // Ryu Printf d2fixed_test.cc D2expTest RoundToEvenInteger
117     {2.5, chars_format::scientific, 1, "2.5e+00"},
118     {2.5, chars_format::scientific, 0, "2e+00"},
119     {3.5, chars_format::scientific, 1, "3.5e+00"},
120     {3.5, chars_format::scientific, 0, "4e+00"},
121 
122     // Ryu Printf d2fixed_test.cc D2expTest NonRoundToEvenScenarios
123     {0.748046875, chars_format::scientific, 2, "7.48e-01"},
124     {0.748046875, chars_format::scientific, 1, "7.5e-01"},
125     {0.748046875, chars_format::scientific, 0, "7e-01"}, // 0.75 would round to "8e-01", but this is smaller
126 
127     {0.2509765625, chars_format::scientific, 2, "2.51e-01"},
128     {0.2509765625, chars_format::scientific, 1, "2.5e-01"},
129     {0.2509765625, chars_format::scientific, 0, "3e-01"}, // 0.25 would round to "2e-01", but this is larger
130 
131     {0x1.0000000000001p-2, chars_format::scientific, 53, "2.50000000000000055511151231257827021181583404541015625e-01"},
132     {0x1.0000000000001p-2, chars_format::scientific, 2, "2.50e-01"},
133     {0x1.0000000000001p-2, chars_format::scientific, 1, "2.5e-01"},
134     {0x1.0000000000001p-2, chars_format::scientific, 0,
135         "3e-01"}, // 0.25 would round to "2e-01", but this is larger (again)
136 
137     // More rounding tests.
138     {9.5, chars_format::scientific, 1, "9.5e+00"},
139     {9.5, chars_format::scientific, 0, "1e+01"},
140     {10.5, chars_format::scientific, 2, "1.05e+01"},
141     {10.5, chars_format::scientific, 1, "1.0e+01"},
142 
143     {1.241, chars_format::scientific, 3, "1.241e+00"},
144     {1.241, chars_format::scientific, 1, "1.2e+00"},
145     {1.251, chars_format::scientific, 3, "1.251e+00"},
146     {1.251, chars_format::scientific, 1, "1.3e+00"},
147     {1.261, chars_format::scientific, 3, "1.261e+00"},
148     {1.261, chars_format::scientific, 1, "1.3e+00"},
149     {1.341, chars_format::scientific, 3, "1.341e+00"},
150     {1.341, chars_format::scientific, 1, "1.3e+00"},
151     {1.351, chars_format::scientific, 3, "1.351e+00"},
152     {1.351, chars_format::scientific, 1, "1.4e+00"},
153     {1.361, chars_format::scientific, 3, "1.361e+00"},
154     {1.361, chars_format::scientific, 1, "1.4e+00"},
155 
156     {2.41, chars_format::scientific, 2, "2.41e+00"},
157     {2.41, chars_format::scientific, 0, "2e+00"},
158     {2.51, chars_format::scientific, 2, "2.51e+00"},
159     {2.51, chars_format::scientific, 0, "3e+00"},
160     {2.61, chars_format::scientific, 2, "2.61e+00"},
161     {2.61, chars_format::scientific, 0, "3e+00"},
162     {3.41, chars_format::scientific, 2, "3.41e+00"},
163     {3.41, chars_format::scientific, 0, "3e+00"},
164     {3.51, chars_format::scientific, 2, "3.51e+00"},
165     {3.51, chars_format::scientific, 0, "4e+00"},
166     {3.61, chars_format::scientific, 2, "3.61e+00"},
167     {3.61, chars_format::scientific, 0, "4e+00"},
168 
169     // Ryu Printf d2fixed_test.cc D2expTest VaryingPrecision
170     {1729.142857142857, chars_format::scientific, 50, "1.72914285714285711037518922239542007446289062500000e+03"},
171     {1729.142857142857, chars_format::scientific, 49, "1.7291428571428571103751892223954200744628906250000e+03"},
172     {1729.142857142857, chars_format::scientific, 48, "1.729142857142857110375189222395420074462890625000e+03"},
173     {1729.142857142857, chars_format::scientific, 47, "1.72914285714285711037518922239542007446289062500e+03"},
174     {1729.142857142857, chars_format::scientific, 46, "1.7291428571428571103751892223954200744628906250e+03"},
175     {1729.142857142857, chars_format::scientific, 45, "1.729142857142857110375189222395420074462890625e+03"},
176     {1729.142857142857, chars_format::scientific, 44, "1.72914285714285711037518922239542007446289062e+03"},
177     {1729.142857142857, chars_format::scientific, 43, "1.7291428571428571103751892223954200744628906e+03"},
178     {1729.142857142857, chars_format::scientific, 42, "1.729142857142857110375189222395420074462891e+03"},
179     {1729.142857142857, chars_format::scientific, 41, "1.72914285714285711037518922239542007446289e+03"},
180     {1729.142857142857, chars_format::scientific, 40, "1.7291428571428571103751892223954200744629e+03"},
181     {1729.142857142857, chars_format::scientific, 39, "1.729142857142857110375189222395420074463e+03"},
182     {1729.142857142857, chars_format::scientific, 38, "1.72914285714285711037518922239542007446e+03"},
183     {1729.142857142857, chars_format::scientific, 37, "1.7291428571428571103751892223954200745e+03"},
184     {1729.142857142857, chars_format::scientific, 36, "1.729142857142857110375189222395420074e+03"},
185     {1729.142857142857, chars_format::scientific, 35, "1.72914285714285711037518922239542007e+03"},
186     {1729.142857142857, chars_format::scientific, 34, "1.7291428571428571103751892223954201e+03"},
187     {1729.142857142857, chars_format::scientific, 33, "1.729142857142857110375189222395420e+03"},
188     {1729.142857142857, chars_format::scientific, 32, "1.72914285714285711037518922239542e+03"},
189     {1729.142857142857, chars_format::scientific, 31, "1.7291428571428571103751892223954e+03"},
190     {1729.142857142857, chars_format::scientific, 30, "1.729142857142857110375189222395e+03"},
191     {1729.142857142857, chars_format::scientific, 29, "1.72914285714285711037518922240e+03"},
192     {1729.142857142857, chars_format::scientific, 28, "1.7291428571428571103751892224e+03"},
193     {1729.142857142857, chars_format::scientific, 27, "1.729142857142857110375189222e+03"},
194     {1729.142857142857, chars_format::scientific, 26, "1.72914285714285711037518922e+03"},
195     {1729.142857142857, chars_format::scientific, 25, "1.7291428571428571103751892e+03"},
196     {1729.142857142857, chars_format::scientific, 24, "1.729142857142857110375189e+03"},
197     {1729.142857142857, chars_format::scientific, 23, "1.72914285714285711037519e+03"},
198     {1729.142857142857, chars_format::scientific, 22, "1.7291428571428571103752e+03"},
199     {1729.142857142857, chars_format::scientific, 21, "1.729142857142857110375e+03"},
200     {1729.142857142857, chars_format::scientific, 20, "1.72914285714285711038e+03"},
201     {1729.142857142857, chars_format::scientific, 19, "1.7291428571428571104e+03"},
202     {1729.142857142857, chars_format::scientific, 18, "1.729142857142857110e+03"},
203     {1729.142857142857, chars_format::scientific, 17, "1.72914285714285711e+03"},
204     {1729.142857142857, chars_format::scientific, 16, "1.7291428571428571e+03"},
205     {1729.142857142857, chars_format::scientific, 15, "1.729142857142857e+03"},
206     {1729.142857142857, chars_format::scientific, 14, "1.72914285714286e+03"},
207     {1729.142857142857, chars_format::scientific, 13, "1.7291428571429e+03"},
208     {1729.142857142857, chars_format::scientific, 12, "1.729142857143e+03"},
209     {1729.142857142857, chars_format::scientific, 11, "1.72914285714e+03"},
210     {1729.142857142857, chars_format::scientific, 10, "1.7291428571e+03"},
211     {1729.142857142857, chars_format::scientific, 9, "1.729142857e+03"},
212     {1729.142857142857, chars_format::scientific, 8, "1.72914286e+03"},
213     {1729.142857142857, chars_format::scientific, 7, "1.7291429e+03"},
214     {1729.142857142857, chars_format::scientific, 6, "1.729143e+03"},
215     {1729.142857142857, chars_format::scientific, 5, "1.72914e+03"},
216     {1729.142857142857, chars_format::scientific, 4, "1.7291e+03"},
217     {1729.142857142857, chars_format::scientific, 3, "1.729e+03"},
218     {1729.142857142857, chars_format::scientific, 2, "1.73e+03"},
219     {1729.142857142857, chars_format::scientific, 1, "1.7e+03"},
220     {1729.142857142857, chars_format::scientific, 0, "2e+03"},
221 
222     // Negative precision requests 6 digits of precision.
223     {1729.142857142857, chars_format::scientific, -1, "1.729143e+03"},
224     {1729.142857142857, chars_format::scientific, -2, "1.729143e+03"},
225     {1729.142857142857, chars_format::scientific, -3, "1.729143e+03"},
226 
227     // Ryu Printf d2fixed_test.cc D2expTest Carrying
228     {2.0009, chars_format::scientific, 4, "2.0009e+00"},
229     {2.0009, chars_format::scientific, 3, "2.001e+00"},
230     {2.0029, chars_format::scientific, 4, "2.0029e+00"},
231     {2.0029, chars_format::scientific, 3, "2.003e+00"},
232     {2.0099, chars_format::scientific, 4, "2.0099e+00"},
233     {2.0099, chars_format::scientific, 3, "2.010e+00"},
234     {2.0299, chars_format::scientific, 4, "2.0299e+00"},
235     {2.0299, chars_format::scientific, 3, "2.030e+00"},
236     {2.0999, chars_format::scientific, 4, "2.0999e+00"},
237     {2.0999, chars_format::scientific, 3, "2.100e+00"},
238     {2.2999, chars_format::scientific, 4, "2.2999e+00"},
239     {2.2999, chars_format::scientific, 3, "2.300e+00"},
240     {2.9999, chars_format::scientific, 4, "2.9999e+00"},
241     {2.9999, chars_format::scientific, 3, "3.000e+00"},
242     {9.9999, chars_format::scientific, 4, "9.9999e+00"},
243     {9.9999, chars_format::scientific, 3, "1.000e+01"},
244 
245     {2.09, chars_format::scientific, 2, "2.09e+00"},
246     {2.09, chars_format::scientific, 1, "2.1e+00"},
247     {2.29, chars_format::scientific, 2, "2.29e+00"},
248     {2.29, chars_format::scientific, 1, "2.3e+00"},
249     {2.99, chars_format::scientific, 2, "2.99e+00"},
250     {2.99, chars_format::scientific, 1, "3.0e+00"},
251     {9.99, chars_format::scientific, 2, "9.99e+00"},
252     {9.99, chars_format::scientific, 1, "1.0e+01"},
253 
254     {2.9, chars_format::scientific, 1, "2.9e+00"},
255     {2.9, chars_format::scientific, 0, "3e+00"},
256     {9.9, chars_format::scientific, 1, "9.9e+00"},
257     {9.9, chars_format::scientific, 0, "1e+01"},
258 
259     // Ryu Printf d2fixed_test.cc D2expTest Exponents
260     {9.99e-100, chars_format::scientific, 2, "9.99e-100"},
261     {9.99e-99, chars_format::scientific, 2, "9.99e-99"},
262     {9.99e-10, chars_format::scientific, 2, "9.99e-10"},
263     {9.99e-09, chars_format::scientific, 2, "9.99e-09"},
264     {9.99e-01, chars_format::scientific, 2, "9.99e-01"},
265     {9.99e+00, chars_format::scientific, 2, "9.99e+00"},
266     {9.99e+01, chars_format::scientific, 2, "9.99e+01"},
267     {9.99e+09, chars_format::scientific, 2, "9.99e+09"},
268     {9.99e+10, chars_format::scientific, 2, "9.99e+10"},
269     {9.99e+99, chars_format::scientific, 2, "9.99e+99"},
270     {9.99e+100, chars_format::scientific, 2, "9.99e+100"},
271 
272     {9.99e-100, chars_format::scientific, 1, "1.0e-99"},
273     {9.99e-99, chars_format::scientific, 1, "1.0e-98"},
274     {9.99e-10, chars_format::scientific, 1, "1.0e-09"},
275     {9.99e-09, chars_format::scientific, 1, "1.0e-08"},
276     {9.99e-01, chars_format::scientific, 1, "1.0e+00"},
277     {9.99e+00, chars_format::scientific, 1, "1.0e+01"},
278     {9.99e+01, chars_format::scientific, 1, "1.0e+02"},
279     {9.99e+09, chars_format::scientific, 1, "1.0e+10"},
280     {9.99e+10, chars_format::scientific, 1, "1.0e+11"},
281     {9.99e+99, chars_format::scientific, 1, "1.0e+100"},
282     {9.99e+100, chars_format::scientific, 1, "1.0e+101"},
283 
284     // Ryu Printf d2fixed_test.cc D2expTest PrintDecimalPoint
285     // These values exercise each codepath.
286     {1e+54, chars_format::scientific, 0, "1e+54"},
287     {1e+54, chars_format::scientific, 1, "1.0e+54"},
288     {1e-63, chars_format::scientific, 0, "1e-63"},
289     {1e-63, chars_format::scientific, 1, "1.0e-63"},
290     {1e+83, chars_format::scientific, 0, "1e+83"},
291     {1e+83, chars_format::scientific, 1, "1.0e+83"},
292 
293     // The UCRT had trouble with rounding this value. charconv was never affected, but let's test it anyways.
294     {0x1.88e2d605edc3dp+345, chars_format::scientific, 104,
295         "1.09995565999999994887854821710219658911365648587951921896774663603198787416706536331386569598149846892544e+"
296         "104"},
297     {0x1.88e2d605edc3dp+345, chars_format::scientific, 18, "1.099955659999999949e+104"},
298     {0x1.88e2d605edc3dp+345, chars_format::scientific, 17, "1.09995565999999995e+104"},
299     {0x1.88e2d605edc3dp+345, chars_format::scientific, 16, "1.0999556599999999e+104"},
300     {0x1.88e2d605edc3dp+345, chars_format::scientific, 15, "1.099955660000000e+104"},
301     {0x1.88e2d605edc3dp+345, chars_format::scientific, 14, "1.09995566000000e+104"},
302     {0x1.88e2d605edc3dp+345, chars_format::scientific, 13, "1.0999556600000e+104"},
303     {0x1.88e2d605edc3dp+345, chars_format::scientific, 12, "1.099955660000e+104"},
304     {0x1.88e2d605edc3dp+345, chars_format::scientific, 11, "1.09995566000e+104"},
305     {0x1.88e2d605edc3dp+345, chars_format::scientific, 10, "1.0999556600e+104"},
306     {0x1.88e2d605edc3dp+345, chars_format::scientific, 9, "1.099955660e+104"},
307     {0x1.88e2d605edc3dp+345, chars_format::scientific, 8, "1.09995566e+104"},
308     {0x1.88e2d605edc3dp+345, chars_format::scientific, 7, "1.0999557e+104"},
309     {0x1.88e2d605edc3dp+345, chars_format::scientific, 6, "1.099956e+104"},
310     {0x1.88e2d605edc3dp+345, chars_format::scientific, 5, "1.09996e+104"},
311     {0x1.88e2d605edc3dp+345, chars_format::scientific, 4, "1.1000e+104"},
312     {0x1.88e2d605edc3dp+345, chars_format::scientific, 3, "1.100e+104"},
313     {0x1.88e2d605edc3dp+345, chars_format::scientific, 2, "1.10e+104"},
314     {0x1.88e2d605edc3dp+345, chars_format::scientific, 1, "1.1e+104"},
315     {0x1.88e2d605edc3dp+345, chars_format::scientific, 0, "1e+104"},
316 
317     // More cases that the UCRT had trouble with (e.g. DevCom-1093399).
318     {0x1.8p+62, chars_format::scientific, 16, "6.9175290276410819e+18"},
319     {0x1.0a2742p+17, chars_format::scientific, 5, "1.36271e+05"},
320     {0x1.f8b0f962cdffbp+205, chars_format::scientific, 13, "1.0137595739223e+62"},
321     {0x1.f8b0f962cdffbp+205, chars_format::scientific, 16, "1.0137595739222531e+62"},
322     {0x1.f8b0f962cdffbp+205, chars_format::scientific, 50, "1.01375957392225305727423222620636224221808910954041e+62"},
323     {0x1.f8b0f962cdffbp+205, chars_format::scientific, 54,
324         "1.013759573922253057274232226206362242218089109540405973e+62"},
325 };
326 
327 #endif // DOUBLE_SCIENTIFIC_PRECISION_TO_CHARS_TEST_CASES_1_HPP
328