]> git.ipfire.org Git - thirdparty/glibc.git/blob - stdio-common/tst-scanf-binary-main.c
malloc: set NON_MAIN_ARENA flag for reclaimed memalign chunk (BZ #30101)
[thirdparty/glibc.git] / stdio-common / tst-scanf-binary-main.c
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.
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 <stdio.h>
21 #include <wchar.h>
22
23 #include <support/check.h>
24 #include <support/xstdio.h>
25
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)
29 #ifndef STDX
30 # define STDX ""
31 #endif
32
33 #define INFILE OBJPFX "/tst-" STDX "scanf-binary-" STD "-in"
34
35 static int
36 wrap_vfscanf (FILE *fp, const CHAR *format, ...)
37 {
38 va_list ap;
39 va_start (ap, format);
40 int ret = FNX (vf, scanf) (fp, format, ap);
41 va_end (ap);
42 return ret;
43 }
44
45 static int
46 wrap_vscanf (const CHAR *format, ...)
47 {
48 va_list ap;
49 va_start (ap, format);
50 int ret = FNX (v, scanf) (format, ap);
51 va_end (ap);
52 return ret;
53 }
54
55 static int
56 wrap_vsscanf (const CHAR *s, const CHAR *format, ...)
57 {
58 va_list ap;
59 va_start (ap, format);
60 int ret = FNX (vs, scanf) (s, format, ap);
61 va_end (ap);
62 return ret;
63 }
64
65 static void
66 one_check (const CHAR *s, int expected, char expected_c)
67 {
68 int ret;
69 FILE *fp;
70 int ret_i;
71 long int ret_l;
72 long long int ret_ll;
73 char ret_c;
74 fp = xfopen (INFILE, "w");
75 ret = FNX (fput, s) (s, fp);
76 TEST_VERIFY_EXIT (0 <= ret);
77 xfclose (fp);
78
79 if (!TEST_C2X)
80 {
81 expected = 0;
82 expected_c = s[0] == L_('-') ? s[2] : s[1];
83 }
84
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);
94 xfclose (fp);
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);
109 xfclose (fp);
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);
115
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);
125 xfclose (fp);
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);
140 xfclose (fp);
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);
146
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);
156 xfclose (fp);
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);
171 xfclose (fp);
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);
177 }
178
179 static int
180 do_test (void)
181 {
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');
186 return 0;
187 }
188
189 #include <support/test-driver.c>