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
25static int
26test_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 (nptr: s, NULL);
34 int exc = fetestexcept (FE_ALL_EXCEPT);
35 int new_errno = errno;
36 printf (format: "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 (format: "PASS: strtold (\"%s\") high == %a\n", s, exp_hi);
40 else
41 {
42 printf (format: "FAIL: strtold (\"%s\") high == %a\n", s, exp_hi);
43 result = 1;
44 }
45 if (x.d[1] == exp_lo)
46 printf (format: "PASS: strtold (\"%s\") low == %a\n", s, exp_lo);
47 else
48 {
49 printf (format: "FAIL: strtold (\"%s\") low == %a\n", s, exp_lo);
50 result = 1;
51 }
52 if (exc == exp_exc)
53 printf (format: "PASS: strtold (\"%s\") exceptions 0x%x\n", s, exp_exc);
54 else
55 {
56 printf (format: "FAIL: strtold (\"%s\") exceptions 0x%x\n", s, exp_exc);
57 result = 1;
58 }
59 if (new_errno == exp_errno)
60 printf (format: "PASS: strtold (\"%s\") errno %d\n", s, exp_errno);
61 else
62 {
63 printf (format: "FAIL: strtold (\"%s\") errno %d\n", s, exp_errno);
64 result = 1;
65 }
66 return result;
67}
68
69static int
70do_test (void)
71{
72 int result = 0;
73 result |= test_strtold_value (s: "0x1.fffffffffffff8p+1023", INFINITY, exp_lo: 0,
74 FE_OVERFLOW | FE_INEXACT, ERANGE);
75 result |= test_strtold_value (s: "-0x1.fffffffffffff8p+1023", exp_hi: -INFINITY, exp_lo: 0,
76 FE_OVERFLOW | FE_INEXACT, ERANGE);
77 result |= test_strtold_value (s: "0x1.ffffffffffffffp+1023", INFINITY, exp_lo: 0,
78 FE_OVERFLOW | FE_INEXACT, ERANGE);
79 result |= test_strtold_value (s: "-0x1.ffffffffffffffp+1023", exp_hi: -INFINITY, exp_lo: 0,
80 FE_OVERFLOW | FE_INEXACT, ERANGE);
81 return result;
82}
83
84#define TEST_FUNCTION do_test ()
85#include "../../../test-skeleton.c"
86

source code of glibc/sysdeps/ieee754/ldbl-128ibm/tst-strtold-ldbl-128ibm.c