openat: don’t close (-1)
[gnulib.git] / tests / test-xstrtold.c
bloba8788ca100c8fbca1943cc1fa7f83c4ba090dfbd
1 /* Test of xstrtold() in the "C" locale.
2 Copyright (C) 2008-2024 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program 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.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 #include <config.h>
19 /* Specification. */
20 #include "xstrtod.h"
22 #include <errno.h>
23 #include <float.h>
24 #include <math.h>
25 #include <string.h>
27 #include "isnanl-nolibm.h"
28 #include "minus-zero.h"
29 #include "macros.h"
31 #define UNINIT 0.26149721284764278375542683860869586L
33 int
34 main ()
36 /* Subject sequence empty or invalid. */
38 const char input[] = "";
40 const char *ptr;
41 long double result = UNINIT;
42 bool ok = xstrtold (input, &ptr, &result, strtold);
43 ASSERT (!ok);
44 ASSERT (result == 0.0L);
45 ASSERT (!signbit (result));
46 ASSERT (ptr == input);
47 ASSERT (errno == 0 || errno == EINVAL);
50 long double result = UNINIT;
51 bool ok = xstrtold (input, NULL, &result, strtold);
52 ASSERT (!ok);
53 ASSERT (result == 0.0L);
54 ASSERT (!signbit (result));
55 ASSERT (errno == 0 || errno == EINVAL);
59 const char input[] = " ";
61 const char *ptr;
62 long double result = UNINIT;
63 bool ok = xstrtold (input, &ptr, &result, strtold);
64 ASSERT (!ok);
65 ASSERT (result == 0.0L);
66 ASSERT (!signbit (result));
67 ASSERT (ptr == input);
68 ASSERT (errno == 0 || errno == EINVAL);
71 long double result = UNINIT;
72 bool ok = xstrtold (input, NULL, &result, strtold);
73 ASSERT (!ok);
74 ASSERT (result == 0.0L);
75 ASSERT (!signbit (result));
76 ASSERT (errno == 0 || errno == EINVAL);
80 const char input[] = " +";
82 const char *ptr;
83 long double result = UNINIT;
84 bool ok = xstrtold (input, &ptr, &result, strtold);
85 ASSERT (!ok);
86 ASSERT (result == 0.0L);
87 ASSERT (!signbit (result));
88 ASSERT (ptr == input);
89 ASSERT (errno == 0 || errno == EINVAL);
92 long double result = UNINIT;
93 bool ok = xstrtold (input, NULL, &result, strtold);
94 ASSERT (!ok);
95 ASSERT (result == 0.0L);
96 ASSERT (!signbit (result));
97 ASSERT (errno == 0 || errno == EINVAL);
101 const char input[] = " .";
103 const char *ptr;
104 long double result = UNINIT;
105 bool ok = xstrtold (input, &ptr, &result, strtold);
106 ASSERT (!ok);
107 ASSERT (result == 0.0L);
108 ASSERT (!signbit (result));
109 ASSERT (ptr == input);
110 ASSERT (errno == 0 || errno == EINVAL);
113 long double result = UNINIT;
114 bool ok = xstrtold (input, NULL, &result, strtold);
115 ASSERT (!ok);
116 ASSERT (result == 0.0L);
117 ASSERT (!signbit (result));
118 ASSERT (errno == 0 || errno == EINVAL);
122 const char input[] = " .e0";
124 const char *ptr;
125 long double result = UNINIT;
126 bool ok = xstrtold (input, &ptr, &result, strtold);
127 ASSERT (!ok);
128 ASSERT (result == 0.0L);
129 ASSERT (!signbit (result));
130 ASSERT (ptr == input);
131 ASSERT (errno == 0 || errno == EINVAL);
134 long double result = UNINIT;
135 bool ok = xstrtold (input, NULL, &result, strtold);
136 ASSERT (!ok);
137 ASSERT (result == 0.0L);
138 ASSERT (!signbit (result));
139 ASSERT (errno == 0 || errno == EINVAL);
143 const char input[] = " +.e-0";
145 const char *ptr;
146 long double result = UNINIT;
147 bool ok = xstrtold (input, &ptr, &result, strtold);
148 ASSERT (!ok);
149 ASSERT (result == 0.0L);
150 ASSERT (!signbit (result));
151 ASSERT (ptr == input);
152 ASSERT (errno == 0 || errno == EINVAL);
155 long double result = UNINIT;
156 bool ok = xstrtold (input, NULL, &result, strtold);
157 ASSERT (!ok);
158 ASSERT (result == 0.0L);
159 ASSERT (!signbit (result));
160 ASSERT (errno == 0 || errno == EINVAL);
164 const char input[] = " in";
166 const char *ptr;
167 long double result = UNINIT;
168 bool ok = xstrtold (input, &ptr, &result, strtold);
169 ASSERT (!ok);
170 ASSERT (result == 0.0L);
171 ASSERT (!signbit (result));
172 ASSERT (ptr == input);
173 ASSERT (errno == 0 || errno == EINVAL);
176 long double result = UNINIT;
177 bool ok = xstrtold (input, NULL, &result, strtold);
178 ASSERT (!ok);
179 ASSERT (result == 0.0L);
180 ASSERT (!signbit (result));
181 ASSERT (errno == 0 || errno == EINVAL);
185 const char input[] = " na";
187 const char *ptr;
188 long double result = UNINIT;
189 bool ok = xstrtold (input, &ptr, &result, strtold);
190 ASSERT (!ok);
191 ASSERT (result == 0.0L);
192 ASSERT (!signbit (result));
193 ASSERT (ptr == input);
194 ASSERT (errno == 0 || errno == EINVAL);
197 long double result = UNINIT;
198 bool ok = xstrtold (input, NULL, &result, strtold);
199 ASSERT (!ok);
200 ASSERT (result == 0.0L);
201 ASSERT (!signbit (result));
202 ASSERT (errno == 0 || errno == EINVAL);
206 /* Simple floating point values. */
208 const char input[] = "1";
209 const char *ptr;
210 long double result = UNINIT;
211 bool ok = xstrtold (input, &ptr, &result, strtold);
212 ASSERT (ok);
213 ASSERT (result == 1.0L);
214 ASSERT (ptr == input + 1);
215 ASSERT (errno == 0);
218 const char input[] = "1.";
219 const char *ptr;
220 long double result = UNINIT;
221 bool ok = xstrtold (input, &ptr, &result, strtold);
222 ASSERT (ok);
223 ASSERT (result == 1.0L);
224 ASSERT (ptr == input + 2);
225 ASSERT (errno == 0);
228 const char input[] = ".5";
229 const char *ptr;
230 long double result = UNINIT;
231 bool ok = xstrtold (input, &ptr, &result, strtold);
232 ASSERT (ok);
233 ASSERT (result == 0.5L);
234 ASSERT (ptr == input + 2);
235 ASSERT (errno == 0);
238 const char input[] = " 1";
240 const char *ptr;
241 long double result = UNINIT;
242 bool ok = xstrtold (input, &ptr, &result, strtold);
243 ASSERT (ok);
244 ASSERT (result == 1.0L);
245 ASSERT (ptr == input + 2);
246 ASSERT (errno == 0);
249 long double result = UNINIT;
250 bool ok = xstrtold (input, NULL, &result, strtold);
251 ASSERT (ok);
252 ASSERT (result == 1.0L);
253 ASSERT (errno == 0);
257 const char input[] = "+1";
258 const char *ptr;
259 long double result = UNINIT;
260 bool ok = xstrtold (input, &ptr, &result, strtold);
261 ASSERT (ok);
262 ASSERT (result == 1.0L);
263 ASSERT (ptr == input + 2);
264 ASSERT (errno == 0);
267 const char input[] = "-1";
268 const char *ptr;
269 long double result = UNINIT;
270 bool ok = xstrtold (input, &ptr, &result, strtold);
271 ASSERT (ok);
272 ASSERT (result == -1.0L);
273 ASSERT (ptr == input + 2);
274 ASSERT (errno == 0);
277 const char input[] = "1e0";
278 const char *ptr;
279 long double result = UNINIT;
280 bool ok = xstrtold (input, &ptr, &result, strtold);
281 ASSERT (ok);
282 ASSERT (result == 1.0L);
283 ASSERT (ptr == input + 3);
284 ASSERT (errno == 0);
287 const char input[] = "1e+0";
288 const char *ptr;
289 long double result = UNINIT;
290 bool ok = xstrtold (input, &ptr, &result, strtold);
291 ASSERT (ok);
292 ASSERT (result == 1.0L);
293 ASSERT (ptr == input + 4);
294 ASSERT (errno == 0);
297 const char input[] = "1e-0";
298 const char *ptr;
299 long double result = UNINIT;
300 bool ok = xstrtold (input, &ptr, &result, strtold);
301 ASSERT (ok);
302 ASSERT (result == 1.0L);
303 ASSERT (ptr == input + 4);
304 ASSERT (errno == 0);
307 const char input[] = "1e1";
308 const char *ptr;
309 long double result = UNINIT;
310 bool ok = xstrtold (input, &ptr, &result, strtold);
311 ASSERT (ok);
312 ASSERT (result == 10.0L);
313 ASSERT (ptr == input + 3);
314 ASSERT (errno == 0);
317 const char input[] = "5e-1";
318 const char *ptr;
319 long double result = UNINIT;
320 bool ok = xstrtold (input, &ptr, &result, strtold);
321 ASSERT (ok);
322 ASSERT (result == 0.5L);
323 ASSERT (ptr == input + 4);
324 ASSERT (errno == 0);
327 /* Zero. */
329 const char input[] = "0";
330 const char *ptr;
331 long double result = UNINIT;
332 bool ok = xstrtold (input, &ptr, &result, strtold);
333 ASSERT (ok);
334 ASSERT (result == 0.0L);
335 ASSERT (!signbit (result));
336 ASSERT (ptr == input + 1);
337 ASSERT (errno == 0);
340 const char input[] = ".0";
341 const char *ptr;
342 long double result = UNINIT;
343 bool ok = xstrtold (input, &ptr, &result, strtold);
344 ASSERT (ok);
345 ASSERT (result == 0.0L);
346 ASSERT (!signbit (result));
347 ASSERT (ptr == input + 2);
348 ASSERT (errno == 0);
351 const char input[] = "0e0";
352 const char *ptr;
353 long double result = UNINIT;
354 bool ok = xstrtold (input, &ptr, &result, strtold);
355 ASSERT (ok);
356 ASSERT (result == 0.0L);
357 ASSERT (!signbit (result));
358 ASSERT (ptr == input + 3);
359 ASSERT (errno == 0);
362 const char input[] = "0e+9999999";
363 const char *ptr;
364 long double result = UNINIT;
365 bool ok = xstrtold (input, &ptr, &result, strtold);
366 ASSERT (ok);
367 ASSERT (result == 0.0L);
368 ASSERT (!signbit (result));
369 ASSERT (ptr == input + 10);
370 ASSERT (errno == 0);
373 const char input[] = "0e-9999999";
374 const char *ptr;
375 long double result = UNINIT;
376 bool ok = xstrtold (input, &ptr, &result, strtold);
377 ASSERT (ok);
378 ASSERT (result == 0.0L);
379 ASSERT (!signbit (result));
380 ASSERT (ptr == input + 10);
381 ASSERT (errno == 0);
384 const char input[] = "-0";
385 const char *ptr;
386 long double result = UNINIT;
387 bool ok = xstrtold (input, &ptr, &result, strtold);
388 ASSERT (ok);
389 ASSERT (result == 0.0L);
390 ASSERT (!!signbit (result) == !!signbit (minus_zerol));
391 ASSERT (ptr == input + 2);
392 ASSERT (errno == 0);
395 /* Suffixes. */
397 const char input[] = "1f";
398 const char *ptr;
399 long double result = UNINIT;
400 bool ok = xstrtold (input, &ptr, &result, strtold);
401 ASSERT (ok);
402 ASSERT (result == 1.0L);
403 ASSERT (ptr == input + 1);
404 ASSERT (errno == 0);
407 const char input[] = "1.f";
408 const char *ptr;
409 long double result = UNINIT;
410 bool ok = xstrtold (input, &ptr, &result, strtold);
411 ASSERT (ok);
412 ASSERT (result == 1.0L);
413 ASSERT (ptr == input + 2);
414 ASSERT (errno == 0);
417 const char input[] = "1e";
419 const char *ptr;
420 long double result = UNINIT;
421 bool ok = xstrtold (input, &ptr, &result, strtold);
422 ASSERT (ok);
423 ASSERT (result == 1.0L);
424 ASSERT (ptr == input + 1);
425 ASSERT (errno == 0);
428 long double result = UNINIT;
429 bool ok = xstrtold (input, NULL, &result, strtold);
430 ASSERT (!ok);
431 ASSERT (result == 1.0L);
432 ASSERT (errno == 0);
436 const char input[] = "1e+";
438 const char *ptr;
439 long double result = UNINIT;
440 bool ok = xstrtold (input, &ptr, &result, strtold);
441 ASSERT (ok);
442 ASSERT (result == 1.0L);
443 ASSERT (ptr == input + 1);
444 ASSERT (errno == 0);
447 long double result = UNINIT;
448 bool ok = xstrtold (input, NULL, &result, strtold);
449 ASSERT (!ok);
450 ASSERT (result == 1.0L);
451 ASSERT (errno == 0);
455 const char input[] = "1e-";
457 const char *ptr;
458 long double result = UNINIT;
459 bool ok = xstrtold (input, &ptr, &result, strtold);
460 ASSERT (ok);
461 ASSERT (result == 1.0L);
462 ASSERT (ptr == input + 1);
463 ASSERT (errno == 0);
466 long double result = UNINIT;
467 bool ok = xstrtold (input, NULL, &result, strtold);
468 ASSERT (!ok);
469 ASSERT (result == 1.0L);
470 ASSERT (errno == 0);
474 const char input[] = "1E 2";
476 const char *ptr;
477 long double result = UNINIT;
478 bool ok = xstrtold (input, &ptr, &result, strtold);
479 ASSERT (ok);
480 ASSERT (result == 1.0L);
481 ASSERT (ptr == input + 1);
482 ASSERT (errno == 0);
485 long double result = UNINIT;
486 bool ok = xstrtold (input, NULL, &result, strtold);
487 ASSERT (!ok);
488 ASSERT (result == 1.0L);
489 ASSERT (errno == 0);
493 const char input[] = "0x";
494 const char *ptr;
495 long double result = UNINIT;
496 bool ok = xstrtold (input, &ptr, &result, strtold);
497 ASSERT (ok);
498 ASSERT (result == 0.0L);
499 ASSERT (!signbit (result));
500 ASSERT (ptr == input + 1);
501 ASSERT (errno == 0);
504 const char input[] = "00x1";
505 const char *ptr;
506 long double result = UNINIT;
507 bool ok = xstrtold (input, &ptr, &result, strtold);
508 ASSERT (ok);
509 ASSERT (result == 0.0L);
510 ASSERT (!signbit (result));
511 ASSERT (ptr == input + 2);
512 ASSERT (errno == 0);
515 const char input[] = "-0x";
516 const char *ptr;
517 long double result = UNINIT;
518 bool ok = xstrtold (input, &ptr, &result, strtold);
519 ASSERT (ok);
520 ASSERT (result == 0.0L);
521 ASSERT (!!signbit (result) == !!signbit (minus_zerol));
522 ASSERT (ptr == input + 2);
523 ASSERT (errno == 0);
526 const char input[] = "0xg";
527 const char *ptr;
528 long double result = UNINIT;
529 bool ok = xstrtold (input, &ptr, &result, strtold);
530 ASSERT (ok);
531 ASSERT (result == 0.0L);
532 ASSERT (!signbit (result));
533 ASSERT (ptr == input + 1);
534 ASSERT (errno == 0);
537 const char input[] = "0xp";
538 const char *ptr;
539 long double result = UNINIT;
540 bool ok = xstrtold (input, &ptr, &result, strtold);
541 ASSERT (ok);
542 ASSERT (result == 0.0L);
543 ASSERT (!signbit (result));
544 ASSERT (ptr == input + 1);
545 ASSERT (errno == 0);
548 const char input[] = "0XP";
549 const char *ptr;
550 long double result = UNINIT;
551 bool ok = xstrtold (input, &ptr, &result, strtold);
552 ASSERT (ok);
553 ASSERT (result == 0.0L);
554 ASSERT (!signbit (result));
555 ASSERT (ptr == input + 1);
556 ASSERT (errno == 0);
559 const char input[] = "0x.";
560 const char *ptr;
561 long double result = UNINIT;
562 bool ok = xstrtold (input, &ptr, &result, strtold);
563 ASSERT (ok);
564 ASSERT (result == 0.0L);
565 ASSERT (!signbit (result));
566 ASSERT (ptr == input + 1);
567 ASSERT (errno == 0);
570 const char input[] = "0xp+";
571 const char *ptr;
572 long double result = UNINIT;
573 bool ok = xstrtold (input, &ptr, &result, strtold);
574 ASSERT (ok);
575 ASSERT (result == 0.0L);
576 ASSERT (!signbit (result));
577 ASSERT (ptr == input + 1);
578 ASSERT (errno == 0);
581 const char input[] = "0xp+1";
582 const char *ptr;
583 long double result = UNINIT;
584 bool ok = xstrtold (input, &ptr, &result, strtold);
585 ASSERT (ok);
586 ASSERT (result == 0.0L);
587 ASSERT (!signbit (result));
588 ASSERT (ptr == input + 1);
589 ASSERT (errno == 0);
592 const char input[] = "0x.p+1";
593 const char *ptr;
594 long double result = UNINIT;
595 bool ok = xstrtold (input, &ptr, &result, strtold);
596 ASSERT (ok);
597 ASSERT (result == 0.0L);
598 ASSERT (!signbit (result));
599 ASSERT (ptr == input + 1);
600 ASSERT (errno == 0);
603 const char input[] = "1p+1";
604 const char *ptr;
605 long double result = UNINIT;
606 bool ok = xstrtold (input, &ptr, &result, strtold);
607 ASSERT (ok);
608 ASSERT (result == 1.0L);
609 ASSERT (ptr == input + 1);
610 ASSERT (errno == 0);
613 const char input[] = "1P+1";
614 const char *ptr;
615 long double result = UNINIT;
616 bool ok = xstrtold (input, &ptr, &result, strtold);
617 ASSERT (ok);
618 ASSERT (result == 1.0L);
619 ASSERT (ptr == input + 1);
620 ASSERT (errno == 0);
623 /* Overflow. */
625 const char input[] = "1e6000";
626 const char *ptr;
627 long double result = UNINIT;
628 bool ok = xstrtold (input, &ptr, &result, strtold);
629 ASSERT (!ok);
630 ASSERT (result == HUGE_VALL);
631 ASSERT (ptr == input + 6);
632 ASSERT (errno == ERANGE);
635 const char input[] = "1E1000000";
636 const char *ptr;
637 long double result = UNINIT;
638 bool ok = xstrtold (input, &ptr, &result, strtold);
639 ASSERT (!ok);
640 ASSERT (result == HUGE_VALL);
641 ASSERT (ptr == input + 9);
642 ASSERT (errno == ERANGE);
645 const char input[] = "-1E1000000";
646 const char *ptr;
647 long double result = UNINIT;
648 bool ok = xstrtold (input, &ptr, &result, strtold);
649 ASSERT (!ok);
650 ASSERT (result == -HUGE_VALL);
651 ASSERT (ptr == input + 10);
652 ASSERT (errno == ERANGE);
655 /* Gradual underflow, resulting in a denormalized number. */
657 #if LDBL_MAX_EXP > 10000
658 const char input[] = "1e-4950";
659 #else
660 const char input[] = "1e-320";
661 #endif
662 const char *ptr;
663 long double result = UNINIT;
664 bool ok = xstrtold (input, &ptr, &result, strtold);
665 ASSERT (ok);
666 ASSERT (0.0L < result && result <= LDBL_MIN);
667 ASSERT (ptr == input + strlen (input));
668 ASSERT (errno == 0);
671 #if LDBL_MAX_EXP > 10000
672 const char input[] = "-1e-4950";
673 #else
674 const char input[] = "-1e-320";
675 #endif
676 const char *ptr;
677 long double result = UNINIT;
678 bool ok = xstrtold (input, &ptr, &result, strtold);
679 ASSERT (ok);
680 ASSERT (-LDBL_MIN <= result && result < 0.0L);
681 ASSERT (ptr == input + strlen (input));
682 ASSERT (errno == 0);
685 /* Flush-to-zero underflow. */
687 const char input[] = "1E-100000";
688 const char *ptr;
689 long double result = UNINIT;
690 bool ok = xstrtold (input, &ptr, &result, strtold);
691 ASSERT (ok);
692 ASSERT (result == 0.0L);
693 ASSERT (!signbit (result));
694 ASSERT (ptr == input + 9);
695 ASSERT (errno == ERANGE);
698 const char input[] = "-1E-100000";
699 const char *ptr;
700 long double result = UNINIT;
701 bool ok = xstrtold (input, &ptr, &result, strtold);
702 ASSERT (ok);
703 ASSERT (result == 0.0L);
704 /* Negative underflow. Expect a negative sign, although POSIX allows +0.0L.
705 See also <https://sourceware.org/bugzilla/show_bug.cgi?id=5995>. */
706 ASSERT (!!signbit (result) == !!signbit (minus_zerol));
707 ASSERT (ptr == input + 10);
708 ASSERT (errno == ERANGE);
711 /* Space before the exponent. */
713 const char input[] = "1E 1000000";
714 const char *ptr;
715 long double result = UNINIT;
716 bool ok = xstrtold (input, &ptr, &result, strtold);
717 ASSERT (ok);
718 ASSERT (result == 1.0L);
719 ASSERT (ptr == input + 1);
720 ASSERT (errno == 0);
723 const char input[] = "0x1P 1000000";
724 const char *ptr;
725 long double result = UNINIT;
726 bool ok = xstrtold (input, &ptr, &result, strtold);
727 ASSERT (ok);
728 ASSERT (result == 1.0L);
729 ASSERT (ptr == input + 3);
730 ASSERT (errno == 0);
733 /* Infinity. */
735 const char input[] = "iNf";
736 const char *ptr;
737 long double result = UNINIT;
738 bool ok = xstrtold (input, &ptr, &result, strtold);
739 ASSERT (ok);
740 ASSERT (result == HUGE_VALL);
741 ASSERT (ptr == input + 3);
742 ASSERT (errno == 0);
745 const char input[] = "-InF";
746 const char *ptr;
747 long double result = UNINIT;
748 bool ok = xstrtold (input, &ptr, &result, strtold);
749 ASSERT (ok);
750 ASSERT (result == -HUGE_VALL);
751 ASSERT (ptr == input + 4);
752 ASSERT (errno == 0);
755 const char input[] = "infinite";
756 const char *ptr;
757 long double result = UNINIT;
758 bool ok = xstrtold (input, &ptr, &result, strtold);
759 ASSERT (ok);
760 ASSERT (result == HUGE_VALL);
761 ASSERT (ptr == input + 3);
762 ASSERT (errno == 0);
765 const char input[] = "infinitY";
766 const char *ptr;
767 long double result = UNINIT;
768 bool ok = xstrtold (input, &ptr, &result, strtold);
769 ASSERT (ok);
770 ASSERT (result == HUGE_VALL);
771 ASSERT (ptr == input + 8);
772 ASSERT (errno == 0);
775 const char input[] = "infinitY.";
776 const char *ptr;
777 long double result = UNINIT;
778 bool ok = xstrtold (input, &ptr, &result, strtold);
779 ASSERT (ok);
780 ASSERT (result == HUGE_VALL);
781 ASSERT (ptr == input + 8);
782 ASSERT (errno == 0);
785 /* NaN. Some processors set the sign bit of the default NaN, so all
786 we check is that using a sign changes the result. */
788 const char input[] = "-nan";
789 const char *ptr1;
790 const char *ptr2;
791 long double result1 = UNINIT;
792 long double result2 = UNINIT;
793 bool ok1 = xstrtold (input, &ptr1, &result1, strtold);
794 bool ok2 = xstrtold (input + 1, &ptr2, &result2, strtold);
795 ASSERT (ok1);
796 ASSERT (ok2);
797 #if 1 /* All known CPUs support NaNs. */
798 ASSERT (isnanl (result1));
799 ASSERT (isnanl (result2));
800 # if 0
801 /* Sign bits of NaN is a portability sticking point, not worth
802 worrying about. */
803 ASSERT (!!signbit (result1) != !!signbit (result2));
804 # endif
805 ASSERT (ptr1 == input + 4);
806 ASSERT (ptr2 == input + 4);
807 ASSERT (errno == 0);
808 #else
809 ASSERT (result1 == 0.0L);
810 ASSERT (result2 == 0.0L);
811 ASSERT (!signbit (result1));
812 ASSERT (!signbit (result2));
813 ASSERT (ptr1 == input);
814 ASSERT (ptr2 == input + 1);
815 ASSERT (errno == 0 || errno == EINVAL);
816 #endif
819 const char input[] = "+nan(";
820 const char *ptr1;
821 const char *ptr2;
822 long double result1 = UNINIT;
823 long double result2 = UNINIT;
824 bool ok1 = xstrtold (input, &ptr1, &result1, strtold);
825 bool ok2 = xstrtold (input + 1, &ptr2, &result2, strtold);
826 ASSERT (ok1);
827 ASSERT (ok2);
828 #if 1 /* All known CPUs support NaNs. */
829 ASSERT (isnanl (result1));
830 ASSERT (isnanl (result2));
831 ASSERT (!!signbit (result1) == !!signbit (result2));
832 ASSERT (ptr1 == input + 4);
833 ASSERT (ptr2 == input + 4);
834 ASSERT (errno == 0);
835 #else
836 ASSERT (result1 == 0.0L);
837 ASSERT (result2 == 0.0L);
838 ASSERT (!signbit (result1));
839 ASSERT (!signbit (result2));
840 ASSERT (ptr1 == input);
841 ASSERT (ptr2 == input + 1);
842 ASSERT (errno == 0 || errno == EINVAL);
843 #endif
846 const char input[] = "-nan()";
847 const char *ptr1;
848 const char *ptr2;
849 long double result1 = UNINIT;
850 long double result2 = UNINIT;
851 bool ok1 = xstrtold (input, &ptr1, &result1, strtold);
852 bool ok2 = xstrtold (input + 1, &ptr2, &result2, strtold);
853 ASSERT (ok1);
854 ASSERT (ok2);
855 #if 1 /* All known CPUs support NaNs. */
856 ASSERT (isnanl (result1));
857 ASSERT (isnanl (result2));
858 # if 0
859 /* Sign bits of NaN is a portability sticking point, not worth
860 worrying about. */
861 ASSERT (!!signbit (result1) != !!signbit (result2));
862 # endif
863 ASSERT (ptr1 == input + 6);
864 ASSERT (ptr2 == input + 6);
865 ASSERT (errno == 0);
866 #else
867 ASSERT (result1 == 0.0L);
868 ASSERT (result2 == 0.0L);
869 ASSERT (!signbit (result1));
870 ASSERT (!signbit (result2));
871 ASSERT (ptr1 == input);
872 ASSERT (ptr2 == input + 1);
873 ASSERT (errno == 0 || errno == EINVAL);
874 #endif
877 const char input[] = " nan().";
878 const char *ptr;
879 long double result = UNINIT;
880 bool ok = xstrtold (input, &ptr, &result, strtold);
881 ASSERT (ok);
882 #if 1 /* All known CPUs support NaNs. */
883 ASSERT (isnanl (result));
884 ASSERT (ptr == input + 6);
885 ASSERT (errno == 0);
886 #else
887 ASSERT (result == 0.0L);
888 ASSERT (!signbit (result));
889 ASSERT (ptr == input);
890 ASSERT (errno == 0 || errno == EINVAL);
891 #endif
894 /* The behavior of nan(0) is implementation-defined, but all
895 implementations we know of which handle optional
896 n-char-sequences handle nan(0) the same as nan(). */
897 const char input[] = "-nan(0).";
898 const char *ptr1;
899 const char *ptr2;
900 long double result1 = UNINIT;
901 long double result2 = UNINIT;
902 bool ok1 = xstrtold (input, &ptr1, &result1, strtold);
903 bool ok2 = xstrtold (input + 1, &ptr2, &result2, strtold);
904 ASSERT (ok1);
905 ASSERT (ok2);
906 #if 1 /* All known CPUs support NaNs. */
907 ASSERT (isnanl (result1));
908 ASSERT (isnanl (result2));
909 # if 0
910 /* Sign bits of NaN is a portability sticking point, not worth
911 worrying about. */
912 ASSERT (!!signbit (result1) != !!signbit (result2));
913 # endif
914 ASSERT (ptr1 == input + 7);
915 ASSERT (ptr2 == input + 7);
916 ASSERT (errno == 0);
917 #else
918 ASSERT (result1 == 0.0L);
919 ASSERT (result2 == 0.0L);
920 ASSERT (!signbit (result1));
921 ASSERT (!signbit (result2));
922 ASSERT (ptr1 == input);
923 ASSERT (ptr2 == input + 1);
924 ASSERT (errno == 0 || errno == EINVAL);
925 #endif
928 /* Hex. */
930 const char input[] = "0xa";
931 const char *ptr;
932 long double result = UNINIT;
933 bool ok = xstrtold (input, &ptr, &result, strtold);
934 ASSERT (ok);
935 ASSERT (result == 10.0L);
936 ASSERT (ptr == input + 3);
937 ASSERT (errno == 0);
940 const char input[] = "0XA";
941 const char *ptr;
942 long double result = UNINIT;
943 bool ok = xstrtold (input, &ptr, &result, strtold);
944 ASSERT (ok);
945 ASSERT (result == 10.0L);
946 ASSERT (ptr == input + 3);
947 ASSERT (errno == 0);
950 const char input[] = "0x1p";
951 const char *ptr;
952 long double result = UNINIT;
953 bool ok = xstrtold (input, &ptr, &result, strtold);
954 ASSERT (ok);
955 ASSERT (result == 1.0L);
956 ASSERT (ptr == input + 3);
957 ASSERT (errno == 0);
960 const char input[] = "0x1p+";
961 const char *ptr;
962 long double result = UNINIT;
963 bool ok = xstrtold (input, &ptr, &result, strtold);
964 ASSERT (ok);
965 ASSERT (result == 1.0L);
966 ASSERT (ptr == input + 3);
967 ASSERT (errno == 0);
970 const char input[] = "0x1P+";
971 const char *ptr;
972 long double result = UNINIT;
973 bool ok = xstrtold (input, &ptr, &result, strtold);
974 ASSERT (ok);
975 ASSERT (result == 1.0L);
976 ASSERT (ptr == input + 3);
977 ASSERT (errno == 0);
980 const char input[] = "0x1p+1";
981 const char *ptr;
982 long double result = UNINIT;
983 bool ok = xstrtold (input, &ptr, &result, strtold);
984 ASSERT (ok);
985 ASSERT (result == 2.0L);
986 ASSERT (ptr == input + 6);
987 ASSERT (errno == 0);
990 const char input[] = "0X1P+1";
991 const char *ptr;
992 long double result = UNINIT;
993 bool ok = xstrtold (input, &ptr, &result, strtold);
994 ASSERT (ok);
995 ASSERT (result == 2.0L);
996 ASSERT (ptr == input + 6);
997 ASSERT (errno == 0);
1000 const char input[] = "0x1p+1a";
1001 const char *ptr;
1002 long double result = UNINIT;
1003 bool ok = xstrtold (input, &ptr, &result, strtold);
1004 ASSERT (ok);
1005 ASSERT (result == 2.0L);
1006 ASSERT (ptr == input + 6);
1007 ASSERT (errno == 0);
1010 const char input[] = "0x1p 2";
1011 const char *ptr;
1012 long double result = UNINIT;
1013 bool ok = xstrtold (input, &ptr, &result, strtold);
1014 ASSERT (ok);
1015 ASSERT (result == 1.0L);
1016 ASSERT (ptr == input + 3);
1017 ASSERT (errno == 0);
1020 /* Large buffers. */
1022 size_t m = 1000000;
1023 char *input = malloc (m + 1);
1024 if (input)
1026 const char *ptr;
1027 long double result = UNINIT;
1028 memset (input, '\t', m - 1);
1029 input[m - 1] = '1';
1030 input[m] = '\0';
1031 bool ok = xstrtold (input, &ptr, &result, strtold);
1032 ASSERT (ok);
1033 ASSERT (result == 1.0L);
1034 ASSERT (ptr == input + m);
1035 ASSERT (errno == 0);
1037 free (input);
1040 size_t m = 1000000;
1041 char *input = malloc (m + 1);
1042 if (input)
1044 const char *ptr;
1045 long double result = UNINIT;
1046 memset (input, '0', m - 1);
1047 input[m - 1] = '1';
1048 input[m] = '\0';
1049 bool ok = xstrtold (input, &ptr, &result, strtold);
1050 ASSERT (ok);
1051 ASSERT (result == 1.0L);
1052 ASSERT (ptr == input + m);
1053 ASSERT (errno == 0);
1055 free (input);
1057 #if 0
1058 /* Newlib has an artificial limit of 20000 for the exponent. TODO -
1059 gnulib should fix this. */
1061 size_t m = 1000000;
1062 char *input = malloc (m + 1);
1063 if (input)
1065 const char *ptr;
1066 long double result = UNINIT;
1067 input[0] = '.';
1068 memset (input + 1, '0', m - 10);
1069 input[m - 9] = '1';
1070 input[m - 8] = 'e';
1071 input[m - 7] = '+';
1072 input[m - 6] = '9';
1073 input[m - 5] = '9';
1074 input[m - 4] = '9';
1075 input[m - 3] = '9';
1076 input[m - 2] = '9';
1077 input[m - 1] = '1';
1078 input[m] = '\0';
1079 bool ok = xstrtold (input, &ptr, &result, strtold);
1080 ASSERT (ok);
1081 ASSERT (result == 1.0L);
1082 ASSERT (ptr == input + m);
1083 ASSERT (errno == 0);
1085 free (input);
1088 size_t m = 1000000;
1089 char *input = malloc (m + 1);
1090 if (input)
1092 const char *ptr;
1093 long double result = UNINIT;
1094 input[0] = '1';
1095 memset (input + 1, '0', m - 9);
1096 input[m - 8] = 'e';
1097 input[m - 7] = '-';
1098 input[m - 6] = '9';
1099 input[m - 5] = '9';
1100 input[m - 4] = '9';
1101 input[m - 3] = '9';
1102 input[m - 2] = '9';
1103 input[m - 1] = '1';
1104 input[m] = '\0';
1105 bool ok = xstrtold (input, &ptr, &result, strtold);
1106 ASSERT (ok);
1107 ASSERT (result == 1.0L);
1108 ASSERT (ptr == input + m);
1109 ASSERT (errno == 0);
1111 free (input);
1113 #endif
1115 size_t m = 1000000;
1116 char *input = malloc (m + 1);
1117 if (input)
1119 const char *ptr;
1120 long double result = UNINIT;
1121 input[0] = '-';
1122 input[1] = '0';
1123 input[2] = 'e';
1124 input[3] = '1';
1125 memset (input + 4, '0', m - 3);
1126 input[m] = '\0';
1127 bool ok = xstrtold (input, &ptr, &result, strtold);
1128 ASSERT (ok);
1129 ASSERT (result == 0.0L);
1130 ASSERT (!!signbit (result) == !!signbit (minus_zerol));
1131 ASSERT (ptr == input + m);
1132 ASSERT (errno == 0);
1134 free (input);
1137 return test_exit_status;