add test for strftime
[libc-test.git] / src / functional / fwscanf.c
blob18b68e8686f925dc8a16e3c29443bd06a83c59c0
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"
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) )
13 #define TEST_S(s, x, m) ( \
14 !strcmp((s),(x)) || \
15 (t_error("[%s] != [%s] (%s)\n", s, x, m), 0) )
17 static FILE *writetemp(const char *data)
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;
27 if (lseek(fileno(f), 0, SEEK_SET) != 0) {
28 t_error("lseek: %s\n", strerror(errno));
29 fclose(f);
30 return 0;
32 return f;
35 int main(void)
37 int i, x, y;
38 double u;
39 char a[100], b[100];
40 FILE *f;
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);
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);
67 TEST(i, !!(f=writetemp("0x1p 12")), 1, "failed to make temp file");
68 if (f) {
69 x=y=-1;
70 u=-1;
71 TEST(i, fwscanf(f, L"%lf%n %d", &u, &x, &y), 0, "%d != %d");
72 TEST(u, u, -1.0, "%g != %g");
73 TEST(i, x, -1, "%d != %d");
74 TEST(i, y, -1, "%d != %d");
75 TEST(i, ftell(f), 4, "%d != %d");
76 TEST(i, feof(f), 0, "%d != %d");
77 TEST(i, fgetwc(f), ' ', "%d != %d");
78 rewind(f);
79 TEST(i, fgetwc(f), '0', "%d != %d");
80 TEST(i, fgetwc(f), 'x', "%d != %d");
81 TEST(i, fwscanf(f, L"%lf%n%c %d", &u, &x, a, &y), 3, "%d != %d");
82 TEST(u, u, 1.0, "%g != %g");
83 TEST(i, x, 1, "%d != %d");
84 TEST(i, a[0], 'p', "%d != %d");
85 TEST(i, y, 12, "%d != %d");
86 TEST(i, ftell(f), 7, "%d != %d");
87 TEST(i, !!feof(f), 1, "%d != %d");
88 fclose(f);
91 TEST(i, !!(f=writetemp("0x.1p4 012")), 1, "failed to make temp file");
92 if (f) {
93 x=y=-1;
94 u=-1;
95 TEST(i, fwscanf(f, L"%lf%n %i", &u, &x, &y), 2, "%d != %d");
96 TEST(u, u, 1.0, "%g != %g");
97 TEST(i, x, 6, "%d != %d");
98 TEST(i, y, 10, "%d != %d");
99 TEST(i, ftell(f), 13, "%d != %d");
100 TEST(i, !!feof(f), 1, "%d != %d");
101 fclose(f);
104 TEST(i, !!(f=writetemp("0xx")), 1, "failed to make temp file");
105 if (f) {
106 x=y=-1;
107 TEST(i, fwscanf(f, L"%x%n", &x, &y), 0, "%d != %d");
108 TEST(i, x, -1, "%d != %d");
109 TEST(i, y, -1, "%d != %d");
110 TEST(i, ftell(f), 2, "%d != %d");
111 TEST(i, feof(f), 0, "%d != %d");
112 fclose(f);
115 return t_status;