strtoul test fix, infrastructure changes
[libc-test] / src / stdio / fscanf.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <limits.h>
6 #include <unistd.h>
7 #include "test.h"
8
9 #define T(r, f, x, m) do { \
10         r = (f); \
11         if (r != (x)) \
12                 error("%s failed (got %d, expected %d, errno \"%s\") (%s)\n", \
13                         #f, r, x, errno ? strerror(errno) : "", m); \
14         errno = 0; \
15 } while (0)
16
17 static void S(const char *s, const char *x, const char *m) {
18         if (strcmp(s, x) != 0)
19                 error("got [%s], expected [%s] (%s)\n", s, x, m);
20 }
21
22 void test_scanf_long(void) {
23         enum {n = 1<<21};
24         char *s = malloc(n+1);
25         int i;
26         int r;
27
28         for (i = 0; i < n; i++) s[i] = '1';
29         s[n] = 0;
30         r = sscanf(s, "%d", &i);
31         free(s);
32 }
33
34 void test_fscanf(void) {
35         int i, x, y;
36         char a[100], b[100];
37         int p[2];
38         FILE *f;
39
40         T(i, pipe(p), 0, "open pipe");
41         T(i, !(f = fdopen(p[0], "rb")), 0, "fdopen pipe");
42         if (!f) {
43                 close(p[0]);
44                 close(p[1]);
45                 return;
46         }
47
48         T(i, write(p[1], "hello, world\n", 13), 13, "write to pipe");
49         T(i, fscanf(f, "%s %[own]", a, b), 2, "");
50         S(a, "hello,", "wrong result for %s");
51         S(b, "wo", "wrong result for %[own]");
52         T(i, fgetc(f), 'r', "fgetc 'r'");
53
54         T(i, write(p[1], " 0x12 0x34", 10), 10, "");
55         T(i, fscanf(f, "ld %5i%2i", &x, &y), 1, "");
56         T(i, x, 0x12, "");
57         T(i, fgetc(f), '3', "fgetc '3'");
58
59         fclose(f);
60         close(p[1]);
61 }