1 #include <stdio.h>
2 #include <string.h>
3 #include <errno.h>
4 #include <limits.h>
5 #include <unistd.h>
6 #include <wchar.h>
7 #include "test.h"
8
9 #define TEST(r, f, x, m) ( \
10 errno=0, ((r) = (f)) == (x) || \
11 (t_error("%s failed (" m ")\n", #f, r, x, strerror(errno)), 0) )
12
13 #define TEST_S(s, x, m) ( \
14 !strcmp((s),(x)) || \
15 (t_error("[%s] != [%s] (%s)\n", s, x, m), 0) )
16
writetemp(const char * data)17 static FILE *writetemp(const char *data)
18 {
19 FILE *f = tmpfile();
20 size_t n = strlen(data);
21 if (!f) return 0;
22 if (write(fileno(f), data, n) != n) {
23 t_error("write: %s\n", strerror(errno));
24 fclose(f);
25 return 0;
26 }
27 if (lseek(fileno(f), 0, SEEK_SET) != 0) {
28 t_error("lseek: %s\n", strerror(errno));
29 fclose(f);
30 return 0;
31 }
32 return f;
33 }
34
main(void)35 int main(void)
36 {
37 int i, x, y;
38 double u;
39 char a[100], b[100];
40 FILE *f;
41
42 TEST(i, !!(f=writetemp(" 42")), 1, "failed to make temp file");
43 if (f) {
44 x=y=-1;
45 TEST(i, fwscanf(f, L" %n%*d%n", &x, &y), 0, "%d != %d");
46 TEST(i, x, 6, "%d != %d");
47 TEST(i, y, 8, "%d != %d");
48 TEST(i, ftell(f), 8, "%d != %d");
49 TEST(i, !!feof(f), 1, "%d != %d");
50 fclose(f);
51 }
52
53 TEST(i, !!(f=writetemp("[abc123]....x")), 1, "failed to make temp file");
54 if (f) {
55 x=y=-1;
56 TEST(i, fwscanf(f, L"%10[^]]%n%10[].]%n", a, &x, b, &y), 2, "%d != %d");
57 TEST_S(a, "[abc123", "wrong result for %[^]]");
58 TEST_S(b, "]....", "wrong result for %[].]");
59 TEST(i, x, 7, "%d != %d");
60 TEST(i, y, 12, "%d != %d");
61 TEST(i, ftell(f), 12, "%d != %d");
62 TEST(i, feof(f), 0, "%d != %d");
63 TEST(i, fgetwc(f), 'x', "%d != %d");
64 fclose(f);
65 }
66
67 /* Comment it because musl parse "0x1p" to "0x1" now rather than treat it as abnormal format.
68 TEST(i, !!(f=writetemp("0x1p 12")), 1, "failed to make temp file");
69 if (f) {
70 x=y=-1;
71 u=-1;
72 TEST(i, fwscanf(f, L"%lf%n %d", &u, &x, &y), 0, "%d != %d");
73 TEST(u, u, -1.0, "%g != %g");
74 TEST(i, x, -1, "%d != %d");
75 TEST(i, y, -1, "%d != %d");
76 TEST(i, ftell(f), 4, "%d != %d");
77 TEST(i, feof(f), 0, "%d != %d");
78 TEST(i, fgetwc(f), ' ', "%d != %d");
79 rewind(f);
80 TEST(i, fgetwc(f), '0', "%d != %d");
81 TEST(i, fgetwc(f), 'x', "%d != %d");
82 TEST(i, fwscanf(f, L"%lf%n%c %d", &u, &x, a, &y), 3, "%d != %d");
83 TEST(u, u, 1.0, "%g != %g");
84 TEST(i, x, 1, "%d != %d");
85 TEST(i, a[0], 'p', "%d != %d");
86 TEST(i, y, 12, "%d != %d");
87 TEST(i, ftell(f), 7, "%d != %d");
88 TEST(i, !!feof(f), 1, "%d != %d");
89 fclose(f);
90 }
91 */
92
93 TEST(i, !!(f=writetemp("0x.1p4 012")), 1, "failed to make temp file");
94 if (f) {
95 x=y=-1;
96 u=-1;
97 TEST(i, fwscanf(f, L"%lf%n %i", &u, &x, &y), 2, "%d != %d");
98 TEST(u, u, 1.0, "%g != %g");
99 TEST(i, x, 6, "%d != %d");
100 TEST(i, y, 10, "%d != %d");
101 TEST(i, ftell(f), 13, "%d != %d");
102 TEST(i, !!feof(f), 1, "%d != %d");
103 fclose(f);
104 }
105
106 TEST(i, !!(f=writetemp("0xx")), 1, "failed to make temp file");
107 if (f) {
108 x=y=-1;
109 TEST(i, fwscanf(f, L"%x%n", &x, &y), 0, "%d != %d");
110 TEST(i, x, -1, "%d != %d");
111 TEST(i, y, -1, "%d != %d");
112 TEST(i, ftell(f), 2, "%d != %d");
113 TEST(i, feof(f), 0, "%d != %d");
114 fclose(f);
115 }
116
117 return t_status;
118 }
119