forked from picolibc/picolibc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprintf-tests.c
286 lines (275 loc) · 7.29 KB
/
printf-tests.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/* Copyright © 2013 Bart Massey */
/* This program is licensed under the GPL version 2 or later.
Please see the file COPYING.GPL2 in this distribution for
license terms. */
#define _GNU_SOURCE
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <math.h>
#include <wchar.h>
#include <locale.h>
#ifndef TINY_STDIO
#define printf_float(x) ((double) (x))
#ifdef _NANO_FORMATTED_IO
#ifndef NO_FLOATING_POINT
extern int _printf_float();
extern int _scanf_float();
int (*_reference_printf_float)() = _printf_float;
int (*_reference_scanf_float)() = _scanf_float;
#define TEST_ASPRINTF
#endif
#endif
#endif
#define PRINTF_BUF_SIZE 512
static char buf[PRINTF_BUF_SIZE];
static wchar_t wbuf[PRINTF_BUF_SIZE];
static void failmsg(int serial, char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
printf("test %d failed: ", serial);
vprintf(fmt, ap);
printf("\n");
va_end(ap);
}
#ifdef __GNUC__
#pragma GCC diagnostic ignored "-Wpragmas"
#pragma GCC diagnostic ignored "-Wunknown-warning-option"
/* 'bsize' is used directly with malloc/realloc which confuses -fanalyzer */
#pragma GCC diagnostic ignored "-Wanalyzer-va-arg-type-mismatch"
#pragma GCC diagnostic ignored "-Wanalyzer-va-list-exhausted"
#endif
static int test(int serial, char *expect, char *fmt, ...) {
va_list ap;
char *abuf = NULL;
va_start(ap, fmt);
int n;
#ifdef TEST_ASPRINTF
int an;
va_list aap;
va_copy(aap, ap);
#endif
#ifndef NO_FLOATING_POINT
# ifdef _HAS_IO_FLOAT
uint32_t dv;
# else
double dv;
# endif
#ifndef NO_LONG_DOUBLE
long double ldv;
#endif
char *star;
char *long_double;
#endif
switch (fmt[strlen(fmt)-1]) {
case 'e':
case 'E':
case 'f':
case 'F':
case 'g':
case 'G':
case 'a':
case 'A':
#ifdef NO_FLOATING_POINT
return 0;
#else
star = strchr(fmt, '*');
long_double = strchr(fmt, 'L');
if (star) {
if (strchr(star+1, '*')) {
int iv1 = va_arg(ap, int);
int iv2 = va_arg(ap, int);
#ifndef NO_LONG_DOUBLE
if (long_double) {
ldv = va_arg(ap, long double);
n = snprintf(buf, PRINTF_BUF_SIZE, fmt, iv1, iv2, ldv);
#ifdef TEST_ASPRINTF
an = asprintf(&abuf, fmt, iv1, iv2, ldv);
#endif
} else
#endif
{
dv = va_arg(ap, __typeof(dv));
n = snprintf(buf, PRINTF_BUF_SIZE, fmt, iv1, iv2, dv);
#ifdef TEST_ASPRINTF
an = asprintf(&abuf, fmt, iv1, iv2, dv);
#endif
}
} else {
int iv = va_arg(ap, int);
#ifndef NO_LONG_DOUBLE
if (long_double) {
ldv = va_arg(ap, long double);
n = snprintf(buf, PRINTF_BUF_SIZE, fmt, iv, ldv);
#ifdef TEST_ASPRINTF
an = asprintf(&abuf, fmt, iv, ldv);
#endif
} else
#endif
{
dv = va_arg(ap, __typeof(dv));
n = snprintf(buf, PRINTF_BUF_SIZE, fmt, iv, dv);
#ifdef TEST_ASPRINTF
an = asprintf(&abuf, fmt, iv, dv);
#endif
}
}
} else {
#ifndef NO_LONG_DOUBLE
if (long_double) {
ldv = va_arg(ap, long double);
n = snprintf(buf, PRINTF_BUF_SIZE, fmt, ldv);
#ifdef TEST_ASPRINTF
an = asprintf(&abuf, fmt, ldv);
#endif
} else
#endif
{
dv = va_arg(ap, __typeof(dv));
n = snprintf(buf, PRINTF_BUF_SIZE, fmt, dv);
#ifdef TEST_ASPRINTF
an = asprintf(&abuf, fmt, dv);
#endif
}
}
break;
#endif
default:
n = vsnprintf(buf, PRINTF_BUF_SIZE, fmt, ap);
#ifdef TEST_ASPRINTF
an = vasprintf(&abuf, fmt, aap);
#endif
break;
}
va_end(ap);
#ifdef TEST_ASPRINTF
va_end(aap);
#endif
// printf("serial %d expect \"%s\" got \"%s\"\n", serial, expect, buf);
if (n >= PRINTF_BUF_SIZE) {
failmsg(serial, "buffer overflow");
free(abuf);
return 1;
}
if (n != (int) strlen(expect)) {
failmsg(serial, "expected \"%s\" (%d), got \"%s\" (%d)",
expect, (int) strlen(expect), buf, n);
free(abuf);
return 1;
}
if (strcmp(buf, expect)) {
failmsg(serial, "expected \"%s\", got \"%s\"", expect, buf);
free(abuf);
return 1;
}
#ifdef TEST_ASPRINTF
if (an != n) {
failmsg(serial, "asprintf return %d sprintf return %d\n", an, n);
free(abuf);
return 1;
}
if (strcmp(abuf, buf)) {
failmsg(serial, "sprintf return %s asprintf return %s\n", buf, abuf);
free(abuf);
return 1;
}
free(abuf);
#endif
return 0;
}
static void failmsgw(int serial, wchar_t *fmt, ...) {
va_list ap;
va_start(ap, fmt);
printf("test %d failed: ", serial);
static wchar_t f_wbuf[PRINTF_BUF_SIZE];
static char f_buf[PRINTF_BUF_SIZE];
vswprintf(f_wbuf, PRINTF_BUF_SIZE, fmt, ap);
wcstombs(f_buf, f_wbuf, PRINTF_BUF_SIZE);
printf("%s\n", f_buf);
va_end(ap);
}
static int testw(int serial, wchar_t *expect, wchar_t *fmt, ...) {
va_list ap;
wchar_t *abuf = NULL;
va_start(ap, fmt);
int n;
#ifdef TEST_ASPRINTF
int an;
va_list aap;
va_copy(aap, ap);
#endif
#ifndef NO_FLOATING_POINT
# ifdef _HAS_IO_FLOAT
uint32_t dv;
# else
double dv;
# endif
wchar_t *star;
#endif
switch (fmt[wcslen(fmt)-1]) {
case 'e':
case 'E':
case 'f':
case 'F':
case 'g':
case 'G':
case 'a':
case 'A':
#ifdef NO_FLOATING_POINT
return 0;
#else
star = wcschr(fmt, '*');
if (star) {
if (wcschr(star+1, '*')) {
int iv1 = va_arg(ap, int);
int iv2 = va_arg(ap, int);
dv = va_arg(ap, __typeof(dv));
n = swprintf(wbuf, PRINTF_BUF_SIZE, fmt, iv1, iv2, dv);
} else {
int iv = va_arg(ap, int);
dv = va_arg(ap, __typeof(dv));
n = swprintf(wbuf, PRINTF_BUF_SIZE, fmt, iv, dv);
}
} else {
dv = va_arg(ap, __typeof(dv));
n = swprintf(wbuf, PRINTF_BUF_SIZE, fmt, dv);
}
break;
#endif
default:
n = vswprintf(wbuf, PRINTF_BUF_SIZE, fmt, ap);
break;
}
va_end(ap);
// printf("serial %d expect \"%s\" got \"%s\"\n", serial, expect, wbuf);
if (n >= PRINTF_BUF_SIZE) {
failmsgw(serial, L"buffer overflow");
free(abuf);
return 1;
}
if (n != (int) wcslen(expect)) {
failmsgw(serial, L"expected \"%s\" (%d), got \"%s\" (%d)",
expect, wcslen(expect), wbuf, n);
free(abuf);
return 1;
}
if (wcscmp(wbuf, expect)) {
failmsgw(serial, L"expected \"%ls\", got \"%ls\"", expect, wbuf);
free(abuf);
return 1;
}
return 0;
}
int main(void) {
int result = 0;
#if !defined(__PICOLIBC__) || defined(_MB_CAPABLE)
if (!setlocale(LC_CTYPE, "C.UTF-8")) {
printf("setlocale(LC_CTYPE, \"C.UTF-8\") failed\n");
return 1;
}
#endif
#include "testcases.c"
return result;
}