1 /* Test for ldbl-128ibm strtold overflow to infinity (bug 14551).
2    Copyright (C) 2015-2022 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9 
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <https://www.gnu.org/licenses/>.  */
18 
19 #include <errno.h>
20 #include <fenv.h>
21 #include <math.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 
25 static int
test_strtold_value(const char * s,double exp_hi,double exp_lo,int exp_exc,int exp_errno)26 test_strtold_value (const char *s, double exp_hi, double exp_lo, int exp_exc,
27 		    int exp_errno)
28 {
29   int result = 0;
30   union { long double ld; double d[2]; } x;
31   feclearexcept (FE_ALL_EXCEPT);
32   errno = 0;
33   x.ld = strtold (s, NULL);
34   int exc = fetestexcept (FE_ALL_EXCEPT);
35   int new_errno = errno;
36   printf ("strtold (\"%s\") returned (%a, %a), exceptions 0x%x, errno %d\n",
37 	  s, x.d[0], x.d[1], exc, new_errno);
38   if (x.d[0] == exp_hi)
39     printf ("PASS: strtold (\"%s\") high == %a\n", s, exp_hi);
40   else
41     {
42       printf ("FAIL: strtold (\"%s\") high == %a\n", s, exp_hi);
43       result = 1;
44     }
45   if (x.d[1] == exp_lo)
46     printf ("PASS: strtold (\"%s\") low == %a\n", s, exp_lo);
47   else
48     {
49       printf ("FAIL: strtold (\"%s\") low == %a\n", s, exp_lo);
50       result = 1;
51     }
52   if (exc == exp_exc)
53     printf ("PASS: strtold (\"%s\") exceptions 0x%x\n", s, exp_exc);
54   else
55     {
56       printf ("FAIL: strtold (\"%s\") exceptions 0x%x\n", s, exp_exc);
57       result = 1;
58     }
59   if (new_errno == exp_errno)
60     printf ("PASS: strtold (\"%s\") errno %d\n", s, exp_errno);
61   else
62     {
63       printf ("FAIL: strtold (\"%s\") errno %d\n", s, exp_errno);
64       result = 1;
65     }
66   return result;
67 }
68 
69 static int
do_test(void)70 do_test (void)
71 {
72   int result = 0;
73   result |= test_strtold_value ("0x1.fffffffffffff8p+1023", INFINITY, 0,
74 				FE_OVERFLOW | FE_INEXACT, ERANGE);
75   result |= test_strtold_value ("-0x1.fffffffffffff8p+1023", -INFINITY, 0,
76 				FE_OVERFLOW | FE_INEXACT, ERANGE);
77   result |= test_strtold_value ("0x1.ffffffffffffffp+1023", INFINITY, 0,
78 				FE_OVERFLOW | FE_INEXACT, ERANGE);
79   result |= test_strtold_value ("-0x1.ffffffffffffffp+1023", -INFINITY, 0,
80 				FE_OVERFLOW | FE_INEXACT, ERANGE);
81   return result;
82 }
83 
84 #define TEST_FUNCTION do_test ()
85 #include "../../../test-skeleton.c"
86