1 /* Test scanf functions with C2X binary integers.
2 Copyright (C) 2022-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
23 #include <support/check.h>
24 #include <support/xstdio.h>
26 #define CONCAT_(X, Y, Z) X ## Y ## Z
27 #define CONCAT(X, Y, Z) CONCAT_ (X, Y, Z)
28 #define FNX(FN1, FN2) CONCAT (FN1, FNW, FN2)
33 #define INFILE OBJPFX "/tst-" STDX "scanf-binary-" STD "-in"
36 wrap_vfscanf (FILE *fp
, const CHAR
*format
, ...)
39 va_start (ap
, format
);
40 int ret
= FNX (vf
, scanf
) (fp
, format
, ap
);
46 wrap_vscanf (const CHAR
*format
, ...)
49 va_start (ap
, format
);
50 int ret
= FNX (v
, scanf
) (format
, ap
);
56 wrap_vsscanf (const CHAR
*s
, const CHAR
*format
, ...)
59 va_start (ap
, format
);
60 int ret
= FNX (vs
, scanf
) (s
, format
, ap
);
66 one_check (const CHAR
*s
, int expected
, char expected_c
)
74 fp
= xfopen (INFILE
, "w");
75 ret
= FNX (fput
, s
) (s
, fp
);
76 TEST_VERIFY_EXIT (0 <= ret
);
82 expected_c
= s
[0] == L_('-') ? s
[2] : s
[1];
85 ret
= FNX (s
, scanf
) (s
, L_("%i %c"), &ret_i
, &ret_c
);
86 TEST_COMPARE (ret
, 2);
87 TEST_COMPARE (ret_i
, expected
);
88 TEST_COMPARE (ret_c
, expected_c
);
89 fp
= xfopen (INFILE
, "r");
90 ret
= FNX (f
, scanf
) (fp
, L_("%i %c"), &ret_i
, &ret_c
);
91 TEST_COMPARE (ret
, 2);
92 TEST_COMPARE (ret_i
, expected
);
93 TEST_COMPARE (ret_c
, expected_c
);
95 fp
= xfreopen (INFILE
, "r", stdin
);
96 ret
= FNX (, scanf
) (L_("%i %c"), &ret_i
, &ret_c
);
97 TEST_COMPARE (ret
, 2);
98 TEST_COMPARE (ret_i
, expected
);
99 TEST_COMPARE (ret_c
, expected_c
);
100 ret
= wrap_vsscanf (s
, L_("%i %c"), &ret_i
, &ret_c
);
101 TEST_COMPARE (ret
, 2);
102 TEST_COMPARE (ret_i
, expected
);
103 TEST_COMPARE (ret_c
, expected_c
);
104 fp
= xfopen (INFILE
, "r");
105 ret
= wrap_vfscanf (fp
, L_("%i %c"), &ret_i
, &ret_c
);
106 TEST_COMPARE (ret
, 2);
107 TEST_COMPARE (ret_i
, expected
);
108 TEST_COMPARE (ret_c
, expected_c
);
110 fp
= xfreopen (INFILE
, "r", stdin
);
111 ret
= wrap_vscanf (L_("%i %c"), &ret_i
, &ret_c
);
112 TEST_COMPARE (ret
, 2);
113 TEST_COMPARE (ret_i
, expected
);
114 TEST_COMPARE (ret_c
, expected_c
);
116 ret
= FNX (s
, scanf
) (s
, L_("%li %c"), &ret_l
, &ret_c
);
117 TEST_COMPARE (ret
, 2);
118 TEST_COMPARE (ret_l
, expected
);
119 TEST_COMPARE (ret_c
, expected_c
);
120 fp
= xfopen (INFILE
, "r");
121 ret
= FNX (f
, scanf
) (fp
, L_("%li %c"), &ret_l
, &ret_c
);
122 TEST_COMPARE (ret
, 2);
123 TEST_COMPARE (ret_l
, expected
);
124 TEST_COMPARE (ret_c
, expected_c
);
126 fp
= xfreopen (INFILE
, "r", stdin
);
127 ret
= FNX (, scanf
) (L_("%li %c"), &ret_l
, &ret_c
);
128 TEST_COMPARE (ret
, 2);
129 TEST_COMPARE (ret_l
, expected
);
130 TEST_COMPARE (ret_c
, expected_c
);
131 ret
= wrap_vsscanf (s
, L_("%li %c"), &ret_l
, &ret_c
);
132 TEST_COMPARE (ret
, 2);
133 TEST_COMPARE (ret_l
, expected
);
134 TEST_COMPARE (ret_c
, expected_c
);
135 fp
= xfopen (INFILE
, "r");
136 ret
= wrap_vfscanf (fp
, L_("%li %c"), &ret_l
, &ret_c
);
137 TEST_COMPARE (ret
, 2);
138 TEST_COMPARE (ret_l
, expected
);
139 TEST_COMPARE (ret_c
, expected_c
);
141 fp
= xfreopen (INFILE
, "r", stdin
);
142 ret
= wrap_vscanf (L_("%li %c"), &ret_l
, &ret_c
);
143 TEST_COMPARE (ret
, 2);
144 TEST_COMPARE (ret_l
, expected
);
145 TEST_COMPARE (ret_c
, expected_c
);
147 ret
= FNX (s
, scanf
) (s
, L_("%lli %c"), &ret_ll
, &ret_c
);
148 TEST_COMPARE (ret
, 2);
149 TEST_COMPARE (ret_ll
, expected
);
150 TEST_COMPARE (ret_c
, expected_c
);
151 fp
= xfopen (INFILE
, "r");
152 ret
= FNX (f
, scanf
) (fp
, L_("%lli %c"), &ret_ll
, &ret_c
);
153 TEST_COMPARE (ret
, 2);
154 TEST_COMPARE (ret_ll
, expected
);
155 TEST_COMPARE (ret_c
, expected_c
);
157 fp
= xfreopen (INFILE
, "r", stdin
);
158 ret
= FNX (, scanf
) (L_("%lli %c"), &ret_ll
, &ret_c
);
159 TEST_COMPARE (ret
, 2);
160 TEST_COMPARE (ret_ll
, expected
);
161 TEST_COMPARE (ret_c
, expected_c
);
162 ret
= wrap_vsscanf (s
, L_("%lli %c"), &ret_ll
, &ret_c
);
163 TEST_COMPARE (ret
, 2);
164 TEST_COMPARE (ret_ll
, expected
);
165 TEST_COMPARE (ret_c
, expected_c
);
166 fp
= xfopen (INFILE
, "r");
167 ret
= wrap_vfscanf (fp
, L_("%lli %c"), &ret_ll
, &ret_c
);
168 TEST_COMPARE (ret
, 2);
169 TEST_COMPARE (ret_ll
, expected
);
170 TEST_COMPARE (ret_c
, expected_c
);
172 fp
= xfreopen (INFILE
, "r", stdin
);
173 ret
= wrap_vscanf (L_("%lli %c"), &ret_ll
, &ret_c
);
174 TEST_COMPARE (ret
, 2);
175 TEST_COMPARE (ret_ll
, expected
);
176 TEST_COMPARE (ret_c
, expected_c
);
182 one_check (L_("0b101 x"), 5, 'x');
183 one_check (L_("0B101 x"), 5, 'x');
184 one_check (L_("-0b11111 y"), -31, 'y');
185 one_check (L_("-0B11111 y"), -31, 'y');
189 #include <support/test-driver.c>