initial commit
[glibc.git] / 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-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 <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) \
41 printf ("OK"); \
42 else \
43 printf ("ERROR (%Lf %f)", *ldptr, *fptr); \
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) \
52 printf ("OK"); \
53 else \
54 printf ("ERROR (%Lf %f)", ld, f); \
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
66 CLEAR_VALUE
67 printf ("fscanf: ");
68 FSCANF (stream, format, &ld, &f);
69 CHECK_VALUE
70
71 CLEAR_VALUE
72 printf ("scanf: ");
73 SCANF (format, &ld, &f);
74 CHECK_VALUE
75
76 CLEAR_VALUE
77 printf ("sscanf: ");
78 SSCANF (string, format, &ld, &f);
79 CHECK_VALUE
80
81 CLEAR_VARGS
82 printf ("vfscanf: ");
83 va_start (args, format);
84 VFSCANF (stream, format, args);
85 va_end (args);
86 CHECK_VARGS
87
88 CLEAR_VARGS
89 printf ("vscanf: ");
90 va_start (args, format);
91 VSCANF (format, args);
92 va_end (args);
93 CHECK_VARGS
94
95 CLEAR_VARGS
96 printf ("vsscanf: ");
97 va_start (args, format);
98 VSSCANF (string, format, args);
99 va_end (args);
100 CHECK_VARGS
101 }
102
103 static int
104 do_test (void)
105 {
106 CHAR string[256];
107 float f;
108 long double ld;
109
110 /* Scan in decimal notation. */
111 STRCPY (string,
112 L ("-1.0 -2.0\n")
113 L ("-1.0 -2.0\n") );
114 do_test_call (stdin, string, L("%Lf %f"), &ld, &f);
115
116 /* Scan in hexadecimal notation. */
117 STRCPY (string,
118 L ("-0x1.0p+0 -0x2.0p+0\n")
119 L ("-0x1.0p+0 -0x2.0p+0\n") );
120 /* For ISO C99, scan the single-precision value with "%as" to test
121 that __isoc99_*scanf ignores the 's'. For DEPRECATED_SCANF, do not
122 use "%as", because that would try to scan a string and allocate
123 space for it. */
124 #if __GLIBC_USE (DEPRECATED_SCANF)
125 # define FMT "%La %a"
126 #else
127 # define FMT "%La %as"
128 #endif
129 do_test_call (stdin, string, L(FMT), &ld, &f);
130
131 return 0;
132 }
133
134 #include <support/test-driver.c>