openat: don’t close (-1)
[gnulib.git] / tests / test-strtold.h
blob580701dcc9acf00ad735163ff8fbb3d2837d181e
1 /* Test of strtold() and c_strtold() 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 /* Written by Eric Blake. */
19 static void
20 test_function (long double (*my_strtold) (const char *, char **))
22 /* Subject sequence empty or invalid. */
24 const char input[] = "";
25 char *ptr;
26 long double result;
27 errno = 0;
28 result = my_strtold (input, &ptr);
29 ASSERT (result == 0.0L);
30 ASSERT (!signbit (result));
31 ASSERT (ptr == input);
32 ASSERT (errno == 0 || errno == EINVAL);
35 const char input[] = " ";
36 char *ptr;
37 long double result;
38 errno = 0;
39 result = my_strtold (input, &ptr);
40 ASSERT (result == 0.0L);
41 ASSERT (!signbit (result));
42 ASSERT (ptr == input);
43 ASSERT (errno == 0 || errno == EINVAL);
46 const char input[] = " +";
47 char *ptr;
48 long double result;
49 errno = 0;
50 result = my_strtold (input, &ptr);
51 ASSERT (result == 0.0L);
52 ASSERT (!signbit (result));
53 ASSERT (ptr == input);
54 ASSERT (errno == 0 || errno == EINVAL);
57 const char input[] = " .";
58 char *ptr;
59 long double result;
60 errno = 0;
61 result = my_strtold (input, &ptr);
62 ASSERT (result == 0.0L);
63 ASSERT (!signbit (result));
64 ASSERT (ptr == input);
65 ASSERT (errno == 0 || errno == EINVAL);
68 const char input[] = " .e0";
69 char *ptr;
70 long double result;
71 errno = 0;
72 result = my_strtold (input, &ptr);
73 ASSERT (result == 0.0L);
74 ASSERT (!signbit (result));
75 ASSERT (ptr == input); /* IRIX 6.5 */
76 ASSERT (errno == 0 || errno == EINVAL);
79 const char input[] = " +.e-0";
80 char *ptr;
81 long double result;
82 errno = 0;
83 result = my_strtold (input, &ptr);
84 ASSERT (result == 0.0L);
85 ASSERT (!signbit (result));
86 ASSERT (ptr == input); /* IRIX 6.5 */
87 ASSERT (errno == 0 || errno == EINVAL);
90 const char input[] = " in";
91 char *ptr;
92 long double result;
93 errno = 0;
94 result = my_strtold (input, &ptr);
95 ASSERT (result == 0.0L);
96 ASSERT (!signbit (result));
97 ASSERT (ptr == input);
98 ASSERT (errno == 0 || errno == EINVAL);
101 const char input[] = " na";
102 char *ptr;
103 long double result;
104 errno = 0;
105 result = my_strtold (input, &ptr);
106 ASSERT (result == 0.0L);
107 ASSERT (!signbit (result));
108 ASSERT (ptr == input);
109 ASSERT (errno == 0 || errno == EINVAL);
112 /* Simple floating point values. */
114 const char input[] = "1";
115 char *ptr;
116 long double result;
117 errno = 0;
118 result = my_strtold (input, &ptr);
119 ASSERT (result == 1.0L);
120 ASSERT (ptr == input + 1);
121 ASSERT (errno == 0);
124 const char input[] = "1.";
125 char *ptr;
126 long double result;
127 errno = 0;
128 result = my_strtold (input, &ptr);
129 ASSERT (result == 1.0L);
130 ASSERT (ptr == input + 2);
131 ASSERT (errno == 0);
134 const char input[] = ".5";
135 char *ptr;
136 long double result;
137 errno = 0;
138 result = my_strtold (input, &ptr);
139 ASSERT (result == 0.5L);
140 ASSERT (ptr == input + 2);
141 ASSERT (errno == 0);
144 const char input[] = " 1";
145 char *ptr;
146 long double result;
147 errno = 0;
148 result = my_strtold (input, &ptr);
149 ASSERT (result == 1.0L);
150 ASSERT (ptr == input + 2);
151 ASSERT (errno == 0);
154 const char input[] = "+1";
155 char *ptr;
156 long double result;
157 errno = 0;
158 result = my_strtold (input, &ptr);
159 ASSERT (result == 1.0L);
160 ASSERT (ptr == input + 2);
161 ASSERT (errno == 0);
164 const char input[] = "-1";
165 char *ptr;
166 long double result;
167 errno = 0;
168 result = my_strtold (input, &ptr);
169 ASSERT (result == -1.0L);
170 ASSERT (ptr == input + 2);
171 ASSERT (errno == 0);
174 const char input[] = "1e0";
175 char *ptr;
176 long double result;
177 errno = 0;
178 result = my_strtold (input, &ptr);
179 ASSERT (result == 1.0L);
180 ASSERT (ptr == input + 3);
181 ASSERT (errno == 0);
184 const char input[] = "1e+0";
185 char *ptr;
186 long double result;
187 errno = 0;
188 result = my_strtold (input, &ptr);
189 ASSERT (result == 1.0L);
190 ASSERT (ptr == input + 4);
191 ASSERT (errno == 0);
194 const char input[] = "1e-0";
195 char *ptr;
196 long double result;
197 errno = 0;
198 result = my_strtold (input, &ptr);
199 ASSERT (result == 1.0L);
200 ASSERT (ptr == input + 4);
201 ASSERT (errno == 0);
204 const char input[] = "1e1";
205 char *ptr;
206 long double result;
207 errno = 0;
208 result = my_strtold (input, &ptr);
209 ASSERT (result == 10.0L);
210 ASSERT (ptr == input + 3);
211 ASSERT (errno == 0);
214 const char input[] = "5e-1";
215 char *ptr;
216 long double result;
217 errno = 0;
218 result = my_strtold (input, &ptr);
219 ASSERT (result == 0.5L);
220 ASSERT (ptr == input + 4);
221 ASSERT (errno == 0);
224 /* Zero. */
226 const char input[] = "0";
227 char *ptr;
228 long double result;
229 errno = 0;
230 result = my_strtold (input, &ptr);
231 ASSERT (result == 0.0L);
232 ASSERT (!signbit (result));
233 ASSERT (ptr == input + 1);
234 ASSERT (errno == 0);
237 const char input[] = ".0";
238 char *ptr;
239 long double result;
240 errno = 0;
241 result = my_strtold (input, &ptr);
242 ASSERT (result == 0.0L);
243 ASSERT (!signbit (result));
244 ASSERT (ptr == input + 2);
245 ASSERT (errno == 0);
248 const char input[] = "0e0";
249 char *ptr;
250 long double result;
251 errno = 0;
252 result = my_strtold (input, &ptr);
253 ASSERT (result == 0.0L);
254 ASSERT (!signbit (result));
255 ASSERT (ptr == input + 3);
256 ASSERT (errno == 0);
259 const char input[] = "0e+9999999";
260 char *ptr;
261 long double result;
262 errno = 0;
263 result = my_strtold (input, &ptr);
264 ASSERT (result == 0.0L);
265 ASSERT (!signbit (result));
266 ASSERT (ptr == input + 10);
267 ASSERT (errno == 0);
270 const char input[] = "0e-9999999";
271 char *ptr;
272 long double result;
273 errno = 0;
274 result = my_strtold (input, &ptr);
275 ASSERT (result == 0.0L);
276 ASSERT (!signbit (result));
277 ASSERT (ptr == input + 10);
278 ASSERT (errno == 0);
281 const char input[] = "-0";
282 char *ptr;
283 long double result;
284 errno = 0;
285 result = my_strtold (input, &ptr);
286 ASSERT (result == 0.0L);
287 ASSERT (!!signbit (result) == !!signbit (minus_zerol)); /* IRIX 6.5 */
288 ASSERT (ptr == input + 2);
289 ASSERT (errno == 0);
292 /* Suffixes. */
294 const char input[] = "1f";
295 char *ptr;
296 long double result;
297 errno = 0;
298 result = my_strtold (input, &ptr);
299 ASSERT (result == 1.0L);
300 ASSERT (ptr == input + 1);
301 ASSERT (errno == 0);
304 const char input[] = "1.f";
305 char *ptr;
306 long double result;
307 errno = 0;
308 result = my_strtold (input, &ptr);
309 ASSERT (result == 1.0L);
310 ASSERT (ptr == input + 2);
311 ASSERT (errno == 0);
314 const char input[] = "1e";
315 char *ptr;
316 long double result;
317 errno = 0;
318 result = my_strtold (input, &ptr);
319 ASSERT (result == 1.0L);
320 ASSERT (ptr == input + 1);
321 ASSERT (errno == 0);
324 const char input[] = "1e+";
325 char *ptr;
326 long double result;
327 errno = 0;
328 result = my_strtold (input, &ptr);
329 ASSERT (result == 1.0L);
330 ASSERT (ptr == input + 1);
331 ASSERT (errno == 0);
334 const char input[] = "1e-";
335 char *ptr;
336 long double result;
337 errno = 0;
338 result = my_strtold (input, &ptr);
339 ASSERT (result == 1.0L);
340 ASSERT (ptr == input + 1);
341 ASSERT (errno == 0);
344 const char input[] = "1E 2";
345 char *ptr;
346 long double result;
347 errno = 0;
348 result = my_strtold (input, &ptr);
349 ASSERT (result == 1.0L); /* HP-UX 11.31/ia64, IRIX 6.5 */
350 ASSERT (ptr == input + 1); /* HP-UX 11.31/ia64, IRIX 6.5 */
351 ASSERT (errno == 0);
354 const char input[] = "0x";
355 char *ptr;
356 long double result;
357 errno = 0;
358 result = my_strtold (input, &ptr);
359 ASSERT (result == 0.0L);
360 ASSERT (!signbit (result));
361 ASSERT (ptr == input + 1); /* glibc-2.3.2, Mac OS X 10.5, Haiku */
362 ASSERT (errno == 0);
365 const char input[] = "00x1";
366 char *ptr;
367 long double result;
368 errno = 0;
369 result = my_strtold (input, &ptr);
370 ASSERT (result == 0.0L);
371 ASSERT (!signbit (result));
372 ASSERT (ptr == input + 2);
373 ASSERT (errno == 0);
376 const char input[] = "-0x";
377 char *ptr;
378 long double result;
379 errno = 0;
380 result = my_strtold (input, &ptr);
381 ASSERT (result == 0.0L);
382 ASSERT (!!signbit (result) == !!signbit (minus_zerol)); /* Mac OS X 10.5, IRIX 6.5 */
383 ASSERT (ptr == input + 2); /* glibc-2.3.2, Mac OS X 10.5, Haiku */
384 ASSERT (errno == 0);
387 const char input[] = "0xg";
388 char *ptr;
389 long double result;
390 errno = 0;
391 result = my_strtold (input, &ptr);
392 ASSERT (result == 0.0L);
393 ASSERT (!signbit (result));
394 ASSERT (ptr == input + 1); /* glibc-2.3.2, Mac OS X 10.5, Haiku */
395 ASSERT (errno == 0);
398 const char input[] = "0xp";
399 char *ptr;
400 long double result;
401 errno = 0;
402 result = my_strtold (input, &ptr);
403 ASSERT (result == 0.0L);
404 ASSERT (!signbit (result));
405 ASSERT (ptr == input + 1); /* glibc-2.3.2, Mac OS X 10.5, Haiku */
406 ASSERT (errno == 0);
409 const char input[] = "0XP";
410 char *ptr;
411 long double result;
412 errno = 0;
413 result = my_strtold (input, &ptr);
414 ASSERT (result == 0.0L);
415 ASSERT (!signbit (result));
416 ASSERT (ptr == input + 1); /* glibc-2.3.2, Mac OS X 10.5, Haiku */
417 ASSERT (errno == 0);
420 const char input[] = "0x.";
421 char *ptr;
422 long double result;
423 errno = 0;
424 result = my_strtold (input, &ptr);
425 ASSERT (result == 0.0L);
426 ASSERT (!signbit (result));
427 ASSERT (ptr == input + 1); /* glibc-2.3.2, Mac OS X 10.5, Haiku */
428 ASSERT (errno == 0);
431 const char input[] = "0xp+";
432 char *ptr;
433 long double result;
434 errno = 0;
435 result = my_strtold (input, &ptr);
436 ASSERT (result == 0.0L);
437 ASSERT (!signbit (result));
438 ASSERT (ptr == input + 1); /* glibc-2.3.2, Mac OS X 10.5, Haiku */
439 ASSERT (errno == 0);
442 const char input[] = "0xp+1";
443 char *ptr;
444 long double result;
445 errno = 0;
446 result = my_strtold (input, &ptr);
447 ASSERT (result == 0.0L);
448 ASSERT (!signbit (result));
449 ASSERT (ptr == input + 1); /* glibc-2.3.2, Mac OS X 10.5, Haiku */
450 ASSERT (errno == 0);
453 const char input[] = "0x.p+1";
454 char *ptr;
455 long double result;
456 errno = 0;
457 result = my_strtold (input, &ptr);
458 ASSERT (result == 0.0L);
459 ASSERT (!signbit (result));
460 ASSERT (ptr == input + 1); /* glibc-2.3.2, Mac OS X 10.5, Haiku */
461 ASSERT (errno == 0);
464 const char input[] = "1p+1";
465 char *ptr;
466 long double result;
467 errno = 0;
468 result = my_strtold (input, &ptr);
469 ASSERT (result == 1.0L);
470 ASSERT (ptr == input + 1);
471 ASSERT (errno == 0);
474 const char input[] = "1P+1";
475 char *ptr;
476 long double result;
477 errno = 0;
478 result = my_strtold (input, &ptr);
479 ASSERT (result == 1.0L);
480 ASSERT (ptr == input + 1);
481 ASSERT (errno == 0);
484 /* Overflow. */
486 const char input[] = "1e6000";
487 char *ptr;
488 long double result;
489 errno = 0;
490 result = my_strtold (input, &ptr);
491 ASSERT (result == HUGE_VALL);
492 ASSERT (ptr == input + 6);
493 ASSERT (errno == ERANGE);
496 const char input[] = "1E1000000";
497 char *ptr;
498 long double result;
499 errno = 0;
500 result = my_strtold (input, &ptr);
501 ASSERT (result == HUGE_VALL);
502 ASSERT (ptr == input + 9);
503 ASSERT (errno == ERANGE);
506 const char input[] = "-1E1000000";
507 char *ptr;
508 long double result;
509 errno = 0;
510 result = my_strtold (input, &ptr);
511 ASSERT (result == -HUGE_VALL);
512 ASSERT (ptr == input + 10);
513 ASSERT (errno == ERANGE);
516 /* Gradual underflow, resulting in a denormalized number. */
518 #if LDBL_MAX_EXP > 10000
519 const char input[] = "1e-4950";
520 #else
521 const char input[] = "1e-320";
522 #endif
523 char *ptr;
524 long double result;
525 errno = 0;
526 result = my_strtold (input, &ptr);
527 ASSERT (0.0L < result && result <= LDBL_MIN);
528 ASSERT (ptr == input + strlen (input));
529 #if !defined _MSC_VER
530 ASSERT (errno == ERANGE);
531 #endif
534 #if LDBL_MAX_EXP > 10000
535 const char input[] = "-1e-4950";
536 #else
537 const char input[] = "-1e-320";
538 #endif
539 char *ptr;
540 long double result;
541 errno = 0;
542 result = my_strtold (input, &ptr);
543 ASSERT (-LDBL_MIN <= result && result < 0.0L);
544 ASSERT (ptr == input + strlen (input));
545 #if !defined _MSC_VER
546 ASSERT (errno == ERANGE);
547 #endif
550 /* Flush-to-zero underflow. */
552 const char input[] = "1E-100000";
553 char *ptr;
554 long double result;
555 errno = 0;
556 result = my_strtold (input, &ptr);
557 #if 1
558 ASSERT (result == 0.0L);
559 #else /* This is all that ISO C guarantees. */
560 ASSERT (0.0L <= result && result <= LDBL_MIN);
561 #endif
562 ASSERT (!signbit (result));
563 ASSERT (ptr == input + 9);
564 ASSERT (errno == ERANGE);
567 const char input[] = "-1E-100000";
568 char *ptr;
569 long double result;
570 errno = 0;
571 result = my_strtold (input, &ptr);
572 #if 1
573 ASSERT (result == 0.0L);
574 #else /* This is all that ISO C guarantees. */
575 ASSERT (-LDBL_MIN <= result && result <= 0.0L);
576 #endif
577 /* Negative underflow. Expect a negative sign, although POSIX allows +0.0L.
578 See also <https://sourceware.org/bugzilla/show_bug.cgi?id=5995>. */
579 ASSERT (!!signbit (result) == !!signbit (minus_zerol)); /* glibc-2.3.2, Haiku */
580 ASSERT (ptr == input + 10);
581 ASSERT (errno == ERANGE);
584 /* Space before the exponent. */
586 const char input[] = "1E 1000000";
587 char *ptr;
588 long double result;
589 errno = 0;
590 result = my_strtold (input, &ptr);
591 ASSERT (result == 1.0L); /* HP-UX 11.31/ia64, IRIX 6.5 */
592 ASSERT (ptr == input + 1); /* HP-UX 11.31/ia64, IRIX 6.5 */
593 ASSERT (errno == 0); /* HP-UX 11.31/ia64, IRIX 6.5 */
596 const char input[] = "0x1P 1000000";
597 char *ptr;
598 long double result;
599 errno = 0;
600 result = my_strtold (input, &ptr);
601 ASSERT (result == 1.0L); /* HP-UX 11.31/ia64, IRIX 6.5, mingw */
602 ASSERT (ptr == input + 3); /* HP-UX 11.31/ia64, IRIX 6.5, mingw */
603 ASSERT (errno == 0);
606 /* Infinity. */
608 const char input[] = "iNf";
609 char *ptr;
610 long double result;
611 errno = 0;
612 result = my_strtold (input, &ptr);
613 ASSERT (result == HUGE_VALL); /* IRIX 6.5 */
614 ASSERT (ptr == input + 3); /* IRIX 6.5 */
615 ASSERT (errno == 0); /* HP-UX 11.31/ia64 */
618 const char input[] = "-InF";
619 char *ptr;
620 long double result;
621 errno = 0;
622 result = my_strtold (input, &ptr);
623 ASSERT (result == -HUGE_VALL); /* IRIX 6.5 */
624 ASSERT (ptr == input + 4); /* IRIX 6.5 */
625 ASSERT (errno == 0); /* HP-UX 11.31/ia64 */
628 const char input[] = "infinite";
629 char *ptr;
630 long double result;
631 errno = 0;
632 result = my_strtold (input, &ptr);
633 ASSERT (result == HUGE_VALL); /* IRIX 6.5 */
634 ASSERT (ptr == input + 3); /* IRIX 6.5 */
635 ASSERT (errno == 0); /* HP-UX 11.31/ia64 */
638 const char input[] = "infinitY";
639 char *ptr;
640 long double result;
641 errno = 0;
642 result = my_strtold (input, &ptr);
643 ASSERT (result == HUGE_VALL); /* IRIX 6.5 */
644 ASSERT (ptr == input + 8); /* IRIX 6.5 */
645 ASSERT (errno == 0); /* HP-UX 11.31/ia64 */
648 const char input[] = "infinitY.";
649 char *ptr;
650 long double result;
651 errno = 0;
652 result = my_strtold (input, &ptr);
653 ASSERT (result == HUGE_VALL); /* IRIX 6.5 */
654 ASSERT (ptr == input + 8); /* IRIX 6.5 */
655 ASSERT (errno == 0); /* HP-UX 11.31/ia64 */
658 /* NaN. Some processors set the sign bit of the default NaN, so all
659 we check is that using a sign changes the result. */
661 const char input[] = "-nan";
662 char *ptr1;
663 char *ptr2;
664 long double result1;
665 long double result2;
666 errno = 0;
667 result1 = my_strtold (input, &ptr1);
668 result2 = my_strtold (input + 1, &ptr2);
669 #if 1 /* All known CPUs support NaNs. */
670 ASSERT (isnanl (result1)); /* IRIX 6.5 */
671 ASSERT (isnanl (result2)); /* IRIX 6.5 */
672 # if 0
673 /* Sign bits of NaN is a portability sticking point, not worth
674 worrying about. */
675 ASSERT (!!signbit (result1) != !!signbit (result2));
676 # endif
677 ASSERT (ptr1 == input + 4); /* IRIX 6.5 */
678 ASSERT (ptr2 == input + 4); /* IRIX 6.5 */
679 ASSERT (errno == 0); /* HP-UX 11.31/ia64 */
680 #else
681 ASSERT (result1 == 0.0L);
682 ASSERT (result2 == 0.0L);
683 ASSERT (!signbit (result1));
684 ASSERT (!signbit (result2));
685 ASSERT (ptr1 == input);
686 ASSERT (ptr2 == input + 1);
687 ASSERT (errno == 0 || errno == EINVAL);
688 #endif
691 const char input[] = "+nan(";
692 char *ptr1;
693 char *ptr2;
694 long double result1;
695 long double result2;
696 errno = 0;
697 result1 = my_strtold (input, &ptr1);
698 result2 = my_strtold (input + 1, &ptr2);
699 #if 1 /* All known CPUs support NaNs. */
700 ASSERT (isnanl (result1)); /* IRIX 6.5 */
701 ASSERT (isnanl (result2)); /* IRIX 6.5 */
702 ASSERT (!!signbit (result1) == !!signbit (result2));
703 ASSERT (ptr1 == input + 4); /* Mac OS X 10.5, IRIX 6.5 */
704 ASSERT (ptr2 == input + 4); /* Mac OS X 10.5, IRIX 6.5 */
705 ASSERT (errno == 0); /* HP-UX 11.31/ia64 */
706 #else
707 ASSERT (result1 == 0.0L);
708 ASSERT (result2 == 0.0L);
709 ASSERT (!signbit (result1));
710 ASSERT (!signbit (result2));
711 ASSERT (ptr1 == input);
712 ASSERT (ptr2 == input + 1);
713 ASSERT (errno == 0 || errno == EINVAL);
714 #endif
717 const char input[] = "-nan()";
718 char *ptr1;
719 char *ptr2;
720 long double result1;
721 long double result2;
722 errno = 0;
723 result1 = my_strtold (input, &ptr1);
724 result2 = my_strtold (input + 1, &ptr2);
725 #if 1 /* All known CPUs support NaNs. */
726 ASSERT (isnanl (result1)); /* IRIX 6.5 */
727 ASSERT (isnanl (result2)); /* IRIX 6.5 */
728 # if 0
729 /* Sign bits of NaN is a portability sticking point, not worth
730 worrying about. */
731 ASSERT (!!signbit (result1) != !!signbit (result2)); /* glibc-2.3.2, musl libc, OpenBSD 6.0, IRIX 6.5, mingw, Haiku */
732 # endif
733 ASSERT (ptr1 == input + 6); /* glibc-2.3.2, IRIX 6.5, mingw, Haiku */
734 ASSERT (ptr2 == input + 6); /* glibc-2.3.2, IRIX 6.5, mingw, Haiku */
735 ASSERT (errno == 0); /* HP-UX 11.31/ia64, Haiku */
736 #else
737 ASSERT (result1 == 0.0L);
738 ASSERT (result2 == 0.0L);
739 ASSERT (!signbit (result1));
740 ASSERT (!signbit (result2));
741 ASSERT (ptr1 == input);
742 ASSERT (ptr2 == input + 1);
743 ASSERT (errno == 0 || errno == EINVAL);
744 #endif
747 const char input[] = " nan().";
748 char *ptr;
749 long double result;
750 errno = 0;
751 result = my_strtold (input, &ptr);
752 #if 1 /* All known CPUs support NaNs. */
753 ASSERT (isnanl (result)); /* IRIX 6.5 */
754 ASSERT (ptr == input + 6); /* glibc-2.3.2, IRIX 6.5, mingw, Haiku */
755 ASSERT (errno == 0); /* HP-UX 11.31/ia64, Haiku */
756 #else
757 ASSERT (result == 0.0L);
758 ASSERT (!signbit (result));
759 ASSERT (ptr == input);
760 ASSERT (errno == 0 || errno == EINVAL);
761 #endif
764 /* The behavior of nan(0) is implementation-defined, but all
765 implementations we know of which handle optional
766 n-char-sequences handle nan(0) the same as nan(). */
767 const char input[] = "-nan(0).";
768 char *ptr1;
769 char *ptr2;
770 long double result1;
771 long double result2;
772 errno = 0;
773 result1 = my_strtold (input, &ptr1);
774 result2 = my_strtold (input + 1, &ptr2);
775 #if 1 /* All known CPUs support NaNs. */
776 ASSERT (isnanl (result1)); /* IRIX 6.5 */
777 ASSERT (isnanl (result2)); /* IRIX 6.5 */
778 # if 0
779 /* Sign bits of NaN is a portability sticking point, not worth
780 worrying about. */
781 ASSERT (!!signbit (result1) != !!signbit (result2));
782 # endif
783 ASSERT (ptr1 == input + 7); /* glibc-2.3.2, IRIX 6.5, mingw, Haiku */
784 ASSERT (ptr2 == input + 7); /* glibc-2.3.2, IRIX 6.5, mingw, Haiku */
785 ASSERT (errno == 0); /* HP-UX 11.31/ia64 */
786 #else
787 ASSERT (result1 == 0.0L);
788 ASSERT (result2 == 0.0L);
789 ASSERT (!signbit (result1));
790 ASSERT (!signbit (result2));
791 ASSERT (ptr1 == input);
792 ASSERT (ptr2 == input + 1);
793 ASSERT (errno == 0 || errno == EINVAL);
794 #endif
797 /* Hex. */
799 const char input[] = "0xa";
800 char *ptr;
801 long double result;
802 errno = 0;
803 result = my_strtold (input, &ptr);
804 ASSERT (result == 10.0L); /* IRIX 6.5, mingw */
805 ASSERT (ptr == input + 3); /* IRIX 6.5, mingw */
806 ASSERT (errno == 0);
809 const char input[] = "0XA";
810 char *ptr;
811 long double result;
812 errno = 0;
813 result = my_strtold (input, &ptr);
814 ASSERT (result == 10.0L); /* IRIX 6.5, mingw */
815 ASSERT (ptr == input + 3); /* IRIX 6.5, mingw */
816 ASSERT (errno == 0);
819 const char input[] = "0x1p";
820 char *ptr;
821 long double result;
822 errno = 0;
823 result = my_strtold (input, &ptr);
824 ASSERT (result == 1.0L); /* IRIX 6.5, mingw */
825 ASSERT (ptr == input + 3); /* IRIX 6.5, mingw */
826 ASSERT (errno == 0);
829 const char input[] = "0x1p+";
830 char *ptr;
831 long double result;
832 errno = 0;
833 result = my_strtold (input, &ptr);
834 ASSERT (result == 1.0L); /* IRIX 6.5, mingw */
835 ASSERT (ptr == input + 3); /* IRIX 6.5, mingw */
836 ASSERT (errno == 0);
839 const char input[] = "0x1P+";
840 char *ptr;
841 long double result;
842 errno = 0;
843 result = my_strtold (input, &ptr);
844 ASSERT (result == 1.0L); /* IRIX 6.5, mingw */
845 ASSERT (ptr == input + 3); /* IRIX 6.5, mingw */
846 ASSERT (errno == 0);
849 const char input[] = "0x1p+1";
850 char *ptr;
851 long double result;
852 errno = 0;
853 result = my_strtold (input, &ptr);
854 ASSERT (result == 2.0L); /* IRIX 6.5, mingw */
855 ASSERT (ptr == input + 6); /* IRIX 6.5, mingw */
856 ASSERT (errno == 0);
859 const char input[] = "0X1P+1";
860 char *ptr;
861 long double result;
862 errno = 0;
863 result = my_strtold (input, &ptr);
864 ASSERT (result == 2.0L); /* IRIX 6.5, mingw */
865 ASSERT (ptr == input + 6); /* IRIX 6.5, mingw */
866 ASSERT (errno == 0);
869 const char input[] = "0x1p+1a";
870 char *ptr;
871 long double result;
872 errno = 0;
873 result = my_strtold (input, &ptr);
874 ASSERT (result == 2.0L); /* IRIX 6.5, mingw */
875 ASSERT (ptr == input + 6); /* IRIX 6.5, mingw */
876 ASSERT (errno == 0);
879 const char input[] = "0x1p 2";
880 char *ptr;
881 long double result;
882 errno = 0;
883 result = my_strtold (input, &ptr);
884 ASSERT (result == 1.0L); /* HP-UX 11.31/ia64, IRIX 6.5, mingw */
885 ASSERT (ptr == input + 3); /* HP-UX 11.31/ia64, IRIX 6.5, mingw */
886 ASSERT (errno == 0);
889 /* Large buffers. */
891 size_t m = 1000000;
892 char *input = malloc (m + 1);
893 if (input)
895 char *ptr;
896 long double result;
897 memset (input, '\t', m - 1);
898 input[m - 1] = '1';
899 input[m] = '\0';
900 errno = 0;
901 result = my_strtold (input, &ptr);
902 ASSERT (result == 1.0L);
903 ASSERT (ptr == input + m);
904 ASSERT (errno == 0);
906 free (input);
909 size_t m = 1000000;
910 char *input = malloc (m + 1);
911 if (input)
913 char *ptr;
914 long double result;
915 memset (input, '0', m - 1);
916 input[m - 1] = '1';
917 input[m] = '\0';
918 errno = 0;
919 result = my_strtold (input, &ptr);
920 ASSERT (result == 1.0L);
921 ASSERT (ptr == input + m);
922 ASSERT (errno == 0);
924 free (input);
926 #if 0
927 /* Newlib has an artificial limit of 20000 for the exponent. TODO -
928 gnulib should fix this. */
930 size_t m = 1000000;
931 char *input = malloc (m + 1);
932 if (input)
934 char *ptr;
935 long double result;
936 input[0] = '.';
937 memset (input + 1, '0', m - 10);
938 input[m - 9] = '1';
939 input[m - 8] = 'e';
940 input[m - 7] = '+';
941 input[m - 6] = '9';
942 input[m - 5] = '9';
943 input[m - 4] = '9';
944 input[m - 3] = '9';
945 input[m - 2] = '9';
946 input[m - 1] = '1';
947 input[m] = '\0';
948 errno = 0;
949 result = my_strtold (input, &ptr);
950 ASSERT (result == 1.0L); /* Mac OS X 10.5, FreeBSD 11.0, NetBSD 7.1, OpenBSD 6.0, Minix 3.3, IRIX 6.5, mingw */
951 ASSERT (ptr == input + m);
952 ASSERT (errno == 0); /* Mac OS X 10.5, FreeBSD 11.0, NetBSD 7.1, OpenBSD 6.0, Minix 3.3, IRIX 6.5, mingw */
954 free (input);
957 size_t m = 1000000;
958 char *input = malloc (m + 1);
959 if (input)
961 char *ptr;
962 long double result;
963 input[0] = '1';
964 memset (input + 1, '0', m - 9);
965 input[m - 8] = 'e';
966 input[m - 7] = '-';
967 input[m - 6] = '9';
968 input[m - 5] = '9';
969 input[m - 4] = '9';
970 input[m - 3] = '9';
971 input[m - 2] = '9';
972 input[m - 1] = '1';
973 input[m] = '\0';
974 errno = 0;
975 result = my_strtold (input, &ptr);
976 ASSERT (result == 1.0L); /* Mac OS X 10.5, FreeBSD 11.0, NetBSD 7.1, OpenBSD 6.0, Minix 3.3, IRIX 6.5, mingw */
977 ASSERT (ptr == input + m);
978 ASSERT (errno == 0); /* Mac OS X 10.5, FreeBSD 11.0, NetBSD 7.1, OpenBSD 6.0, Minix 3.3, IRIX 6.5, mingw */
980 free (input);
982 #endif
984 size_t m = 1000000;
985 char *input = malloc (m + 1);
986 if (input)
988 char *ptr;
989 long double result;
990 input[0] = '-';
991 input[1] = '0';
992 input[2] = 'e';
993 input[3] = '1';
994 memset (input + 4, '0', m - 3);
995 input[m] = '\0';
996 errno = 0;
997 result = my_strtold (input, &ptr);
998 ASSERT (result == 0.0L);
999 ASSERT (!!signbit (result) == !!signbit (minus_zerol)); /* IRIX 6.5 */
1000 ASSERT (ptr == input + m);
1001 ASSERT (errno == 0);
1003 free (input);
1006 /* Rounding. */
1007 /* TODO - is it worth some tests of rounding for typical IEEE corner
1008 cases, such as .5 ULP rounding up to the smallest denormal and
1009 not causing underflow, or LDBL_MIN - .5 ULP not causing an
1010 infinite loop? */