(root)/
glibc-2.38/
sysdeps/
ieee754/
ldbl-128ibm-compat/
test-scanf-ldbl-compat-template.c
       1  /* Test for the long double variants of *scanf functions.
       2     Copyright (C) 2019-2023 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 <stdarg.h>
      20  #include <stdint.h>
      21  #include <stdio.h>
      22  #include <string.h>
      23  #include <wchar.h>
      24  
      25  #include <support/check.h>
      26  
      27  #define CLEAR_VARGS							\
      28    va_start (args, format);						\
      29    ldptr = va_arg (args, long double *);					\
      30    fptr = va_arg (args, float *);					\
      31    *ldptr = 0;								\
      32    *fptr = 0;								\
      33    va_end (args);
      34  
      35  #define CHECK_VARGS							\
      36    va_start (args, format);						\
      37    ldptr = va_arg (args, long double *);					\
      38    fptr = va_arg (args, float *);					\
      39    va_end (args);							\
      40    if (*ldptr == -1 && *fptr == -2 && ret == 2)				\
      41      printf ("OK");							\
      42    else									\
      43      printf ("ERROR (%Lf %f %d)", *ldptr, *fptr, ret);			\
      44    printf ("\n");
      45  
      46  #define CLEAR_VALUE							\
      47    ld = 0;								\
      48    f = 0;
      49  
      50  #define CHECK_VALUE							\
      51    if (ld == -1 && f == -2 && ret == 2)					\
      52      printf ("OK");							\
      53    else									\
      54      printf ("ERROR (%Lf %f %d)", ld, f, ret);				\
      55    printf ("\n");
      56  
      57  static void
      58  do_test_call (FILE *stream, CHAR *string, const CHAR *format, ...)
      59  {
      60    float f;
      61    long double ld;
      62    float *fptr;
      63    long double *ldptr;
      64    va_list args;
      65    int ret;
      66  
      67    CLEAR_VALUE
      68    printf ("fscanf: ");
      69    ret = FSCANF (stream, format, &ld, &f);
      70    CHECK_VALUE
      71  
      72    CLEAR_VALUE
      73    printf ("scanf: ");
      74    ret = SCANF (format, &ld, &f);
      75    CHECK_VALUE
      76  
      77    CLEAR_VALUE
      78    printf ("sscanf: ");
      79    ret = SSCANF (string, format, &ld, &f);
      80    CHECK_VALUE
      81  
      82    CLEAR_VARGS
      83    printf ("vfscanf: ");
      84    va_start (args, format);
      85    ret = VFSCANF (stream, format, args);
      86    va_end (args);
      87    CHECK_VARGS
      88  
      89    CLEAR_VARGS
      90    printf ("vscanf: ");
      91    va_start (args, format);
      92    ret = VSCANF (format, args);
      93    va_end (args);
      94    CHECK_VARGS
      95  
      96    CLEAR_VARGS
      97    printf ("vsscanf: ");
      98    va_start (args, format);
      99    ret = VSSCANF (string, format, args);
     100    va_end (args);
     101    CHECK_VARGS
     102  }
     103  
     104  static int
     105  do_test (void)
     106  {
     107    CHAR string[256];
     108    float f;
     109    long double ld;
     110  
     111    /* Scan in decimal notation.  */
     112    STRCPY (string,
     113  	  L ("-1.0 -2.0\n")
     114  	  L ("-1.0 -2.0\n") );
     115    do_test_call (stdin, string, L("%Lf %f"), &ld, &f);
     116  
     117    /* Scan in hexadecimal notation.  */
     118    STRCPY (string,
     119  	  L ("-0x1.0p+0 -0x2.0p+0\n")
     120  	  L ("-0x1.0p+0 -0x2.0p+0\n") );
     121    /* For ISO C99, scan the single-precision value with "%as" to test
     122       that __isoc99_*scanf ignores the 's'.  For DEPRECATED_SCANF, do not
     123       use "%as", because that would try to scan a string and allocate
     124       space for it.  */
     125  #if __GLIBC_USE (DEPRECATED_SCANF)
     126  # define FMT "%La %a"
     127  #else
     128  # define FMT "%La %as"
     129  #endif
     130    do_test_call (stdin, string, L(FMT), &ld, &f);
     131  
     132    return 0;
     133  }
     134  
     135  #include <support/test-driver.c>