(root)/
glibc-2.38/
stdio-common/
tst-sprintf3.c
       1  /* Copyright (C) 2012-2023 Free Software Foundation, Inc.
       2     This file is part of the GNU C Library.
       3  
       4     The GNU C Library is free software; you can redistribute it and/or
       5     modify it under the terms of the GNU Lesser General Public
       6     License as published by the Free Software Foundation; either
       7     version 2.1 of the License, or (at your option) any later version.
       8  
       9     The GNU C Library 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 GNU
      12     Lesser General Public License for more details.
      13  
      14     You should have received a copy of the GNU Lesser General Public
      15     License along with the GNU C Library; if not, see
      16     <https://www.gnu.org/licenses/>.  */
      17  
      18  /* Test bug #13941.  */
      19  
      20  #include <float.h>
      21  #include <math.h>
      22  #include <stdio.h>
      23  #include <string.h>
      24  
      25  static int
      26  do_test (void)
      27  {
      28  #if LDBL_MANT_DIG >= 106
      29    volatile union { long double l; long long x[2]; } u, v;
      30    char buf[64];
      31  #endif
      32    int result = 0;
      33  
      34  #if LDBL_MANT_DIG == 106 || LDBL_MANT_DIG == 113
      35  # define COMPARE_LDBL(u, v) \
      36    ((u).l == (v).l && (u).x[0] == (v).x[0] && (u).x[1] == (v).x[1])
      37  #else
      38  # define COMPARE_LDBL(u, v) ((u).l == (v).l)
      39  #endif
      40  
      41  #define TEST_N(val, n) \
      42    do									   \
      43      {									   \
      44        u.l = (val);							   \
      45        snprintf (buf, sizeof buf, "%." #n "LgL", u.l);			   \
      46        if (strcmp (buf, #val) != 0)					   \
      47  	{								   \
      48  	  printf ("Error on line %d: %s != %s\n", __LINE__, buf, #val);	   \
      49  	  result = 1;							   \
      50  	}								   \
      51        if (sscanf (#val, "%Lg", &v.l) != 1 || !COMPARE_LDBL (u, v))	   \
      52  	{								   \
      53  	  printf ("Error sscanf on line %d: %." #n "Lg != %." #n "Lg\n",   \
      54  		  __LINE__, u.l, v.l);					   \
      55  	  result = 1;							   \
      56  	}								   \
      57        /* printf ("%s %Lg %016Lx %016Lx\n", #val, u.l, u.x[0], u.x[1]); */  \
      58      }									   \
      59    while (0)
      60  
      61  #define TEST(val) TEST_N (val,30)
      62  
      63  #if LDBL_MANT_DIG >= 106
      64  # if LDBL_MANT_DIG == 106
      65    TEST (2.22507385850719347803989925739e-308L);
      66    TEST (2.22507385850719397210554509863e-308L);
      67    TEST (2.22507385850720088902458687609e-308L);
      68  
      69    /* Verify precision is not lost for long doubles
      70       of the form +1.pN,-1.pM.  */
      71    TEST_N (3.32306998946228968225951765070082e+35L, 34);
      72  # endif
      73    TEST (2.22507385850720138309023271733e-308L);
      74    TEST (2.22507385850720187715587855858e-308L);
      75    TEST (2.2250738585074419930597574044e-308L);
      76    TEST (4.45014771701440227211481959342e-308L);
      77    TEST (4.45014771701440276618046543466e-308L);
      78    TEST (4.45014771701440375431175711716e-308L);
      79    TEST (4.45014771701440474244304879965e-308L);
      80    TEST (7.12023634722304600689881138745e-307L);
      81    TEST (1.13923781555569064960474854133e-305L);
      82    TEST (1.13777777777777776389998996996L);
      83    TEST (1.13777777777777765287768750745L);
      84    TEST (20988295479420645138.2044444444L);
      85    TEST (20988295479420643090.2044444444L);
      86    TEST (2.14668699894294423266045294316e-292L);
      87  # if LDBL_MANT_DIG == 106
      88    TEST (-2.35993711055432139266626434123e-292L);
      89    TEST (6.26323524637968345414769634658e-302L);
      90    TEST (1.49327164802066885331814201989e-308L);
      91    TEST (3.71834550652787023640837473722e-308L);
      92    TEST (9.51896449671134907001349268087e-306L);
      93  # endif
      94  #endif
      95    return result;
      96  }
      97  
      98  #define TEST_FUNCTION do_test ()
      99  #include "../test-skeleton.c"