add test for strftime
[libc-test.git] / src / functional / fscanf.c
blob17fdb64143c50b62bfaf473128cb970cf549c343
1 #include <stdio.h>
2 #include <string.h>
3 #include <errno.h>
4 #include <limits.h>
5 #include <unistd.h>
6 #include "test.h"
8 #define TEST(r, f, x, m) ( \
9 errno=0, ((r) = (f)) == (x) || \
10 (t_error("%s failed (" m ")\n", #f, r, x, strerror(errno)), 0) )
12 #define TEST_S(s, x, m) ( \
13 !strcmp((s),(x)) || \
14 (t_error("[%s] != [%s] (%s)\n", s, x, m), 0) )
16 static FILE *writetemp(const char *data)
18 FILE *f = tmpfile();
19 if (!f) return 0;
20 if (!fwrite(data, strlen(data), 1, f)) {
21 fclose(f);
22 return 0;
24 rewind(f);
25 return f;
28 int main(void)
30 int i, x, y;
31 double u;
32 char a[100], b[100];
33 FILE *f;
34 int p[2];
36 TEST(i, pipe(p), 0, "failed to open pipe %d!=%d (%s)");
37 TEST(i, !(f = fdopen(p[0], "rb")), 0, "failed to fdopen pipe %d!=%d (%s)");
39 if (!f) {
40 close(p[0]);
41 close(p[1]);
42 return 1;
45 TEST(i, write(p[1], "hello, world\n", 13), 13, "write error %d!=%d (%s)");
46 TEST(i, fscanf(f, "%s %[own]", a, b), 2, "got %d fields, expected %d");
47 TEST_S(a, "hello,", "wrong result for %s");
48 TEST_S(b, "wo", "wrong result for %[own]");
49 TEST(i, fgetc(f), 'r', "'%c' != '%c') (%s)");
51 TEST(i, write(p[1], " 0x12 0x34", 10), 10, "write error %d!=%d (%s)");
52 TEST(i, fscanf(f, "ld %5i%2i", &x, &y), 1, "got %d fields, expected %d");
53 TEST(i, x, 0x12, "%d != %d");
54 TEST(i, fgetc(f), '3', "'%c' != '%c'");
56 fclose(f);
57 close(p[1]);
59 TEST(i, !!(f=writetemp(" 42")), 1, "failed to make temp file");
60 if (f) {
61 x=y=-1;
62 TEST(i, fscanf(f, " %n%*d%n", &x, &y), 0, "%d != %d");
63 TEST(i, x, 6, "%d != %d");
64 TEST(i, y, 8, "%d != %d");
65 TEST(i, ftell(f), 8, "%d != %d");
66 TEST(i, !!feof(f), 1, "%d != %d");
67 fclose(f);
70 TEST(i, !!(f=writetemp("[abc123]....x")), 1, "failed to make temp file");
71 if (f) {
72 x=y=-1;
73 TEST(i, fscanf(f, "%10[^]]%n%10[].]%n", a, &x, b, &y), 2, "%d != %d");
74 TEST_S(a, "[abc123", "wrong result for %[^]]");
75 TEST_S(b, "]....", "wrong result for %[].]");
76 TEST(i, x, 7, "%d != %d");
77 TEST(i, y, 12, "%d != %d");
78 TEST(i, ftell(f), 12, "%d != %d");
79 TEST(i, feof(f), 0, "%d != %d");
80 TEST(i, fgetc(f), 'x', "%d != %d");
81 fclose(f);
84 TEST(i, !!(f=writetemp("0x1p 12")), 1, "failed to make temp file");
85 if (f) {
86 x=y=-1;
87 u=-1;
88 TEST(i, fscanf(f, "%lf%n %d", &u, &x, &y), 0, "%d != %d");
89 TEST(u, u, -1.0, "%g != %g");
90 TEST(i, x, -1, "%d != %d");
91 TEST(i, y, -1, "%d != %d");
92 TEST(i, ftell(f), 4, "%d != %d");
93 TEST(i, feof(f), 0, "%d != %d");
94 TEST(i, fgetc(f), ' ', "%d != %d");
95 rewind(f);
96 TEST(i, fgetc(f), '0', "%d != %d");
97 TEST(i, fgetc(f), 'x', "%d != %d");
98 TEST(i, fscanf(f, "%lf%n%c %d", &u, &x, a, &y), 3, "%d != %d");
99 TEST(u, u, 1.0, "%g != %g");
100 TEST(i, x, 1, "%d != %d");
101 TEST(i, a[0], 'p', "%d != %d");
102 TEST(i, y, 12, "%d != %d");
103 TEST(i, ftell(f), 7, "%d != %d");
104 TEST(i, !!feof(f), 1, "%d != %d");
105 fclose(f);
108 TEST(i, !!(f=writetemp("0x.1p4 012")), 1, "failed to make temp file");
109 if (f) {
110 x=y=-1;
111 u=-1;
112 TEST(i, fscanf(f, "%lf%n %i", &u, &x, &y), 2, "%d != %d");
113 TEST(u, u, 1.0, "%g != %g");
114 TEST(i, x, 6, "%d != %d");
115 TEST(i, y, 10, "%d != %d");
116 TEST(i, ftell(f), 13, "%d != %d");
117 TEST(i, !!feof(f), 1, "%d != %d");
118 fclose(f);
121 TEST(i, !!(f=writetemp("0xx")), 1, "failed to make temp file");
122 if (f) {
123 x=y=-1;
124 TEST(i, fscanf(f, "%x%n", &x, &y), 0, "%d != %d");
125 TEST(i, x, -1, "%d != %d");
126 TEST(i, y, -1, "%d != %d");
127 TEST(i, ftell(f), 2, "%d != %d");
128 TEST(i, feof(f), 0, "%d != %d");
129 fclose(f);
132 return t_status;