1570af302Sopenharmony_ci#include <stdio.h>
2570af302Sopenharmony_ci#include <string.h>
3570af302Sopenharmony_ci#include <errno.h>
4570af302Sopenharmony_ci#include <limits.h>
5570af302Sopenharmony_ci#include <unistd.h>
6570af302Sopenharmony_ci#include "test.h"
7570af302Sopenharmony_ci
8570af302Sopenharmony_ci#define CONTENT "0.1234567\n"
9570af302Sopenharmony_ci#define EXPECT 0.1234567
10570af302Sopenharmony_ci#define TMP_FILE "/data/local/tmp/fscanf_test.txt"
11570af302Sopenharmony_ci#define TOTAL 515
12570af302Sopenharmony_ci
13570af302Sopenharmony_ci#define TEST(r, f, x, m) ( \
14570af302Sopenharmony_ci	errno=0, ((r) = (f)) == (x) || \
15570af302Sopenharmony_ci	(t_error("%s failed (" m ")\n", #f, r, x, strerror(errno)), 0) )
16570af302Sopenharmony_ci
17570af302Sopenharmony_ci#define TEST_S(s, x, m) ( \
18570af302Sopenharmony_ci	!strcmp((s),(x)) || \
19570af302Sopenharmony_ci	(t_error("[%s] != [%s] (%s)\n", s, x, m), 0) )
20570af302Sopenharmony_ci
21570af302Sopenharmony_cistatic FILE *writetemp(const char *data)
22570af302Sopenharmony_ci{
23570af302Sopenharmony_ci	FILE *f = tmpfile();
24570af302Sopenharmony_ci	if (!f) return 0;
25570af302Sopenharmony_ci	if (!fwrite(data, strlen(data), 1, f)) {
26570af302Sopenharmony_ci		fclose(f);
27570af302Sopenharmony_ci		return 0;
28570af302Sopenharmony_ci	}
29570af302Sopenharmony_ci	rewind(f);
30570af302Sopenharmony_ci	return f;
31570af302Sopenharmony_ci}
32570af302Sopenharmony_ci
33570af302Sopenharmony_civoid write_content()
34570af302Sopenharmony_ci{
35570af302Sopenharmony_ci	FILE *fp;
36570af302Sopenharmony_ci	fp = fopen(TMP_FILE, "w");
37570af302Sopenharmony_ci	if (fp == NULL) {
38570af302Sopenharmony_ci		t_error("FAIL %s create failed!\n", TMP_FILE);
39570af302Sopenharmony_ci	}
40570af302Sopenharmony_ci	for (size_t i = 0; i < TOTAL; i++) {
41570af302Sopenharmony_ci		fwrite(CONTENT, sizeof(char), strlen(CONTENT), fp);
42570af302Sopenharmony_ci	}
43570af302Sopenharmony_ci	fclose(fp);
44570af302Sopenharmony_ci}
45570af302Sopenharmony_ci
46570af302Sopenharmony_civoid check_content()
47570af302Sopenharmony_ci{
48570af302Sopenharmony_ci	FILE *fp;
49570af302Sopenharmony_ci	int res;
50570af302Sopenharmony_ci	double value = 0;
51570af302Sopenharmony_ci	fp = fopen(TMP_FILE, "r");
52570af302Sopenharmony_ci	if (fp == NULL) {
53570af302Sopenharmony_ci		t_error("FAIL %s open failed!\n", TMP_FILE);
54570af302Sopenharmony_ci	}
55570af302Sopenharmony_ci	for (size_t i = 0; i < TOTAL; i++) {
56570af302Sopenharmony_ci		res = fscanf(fp, "%lf", &value);
57570af302Sopenharmony_ci		if (res != 1 || value != EXPECT) {
58570af302Sopenharmony_ci			t_error("FAIL: fscanf expect %lf but get %lf, index=%d\n", EXPECT, value, i);
59570af302Sopenharmony_ci		}
60570af302Sopenharmony_ci	}
61570af302Sopenharmony_ci	fclose(fp);
62570af302Sopenharmony_ci}
63570af302Sopenharmony_ci
64570af302Sopenharmony_civoid test_read_large_data()
65570af302Sopenharmony_ci{
66570af302Sopenharmony_ci	write_content();
67570af302Sopenharmony_ci	check_content();
68570af302Sopenharmony_ci	remove(TMP_FILE);
69570af302Sopenharmony_ci}
70570af302Sopenharmony_ci
71570af302Sopenharmony_ci
72570af302Sopenharmony_ciint main(void)
73570af302Sopenharmony_ci{
74570af302Sopenharmony_ci	int i, x, y;
75570af302Sopenharmony_ci	double u;
76570af302Sopenharmony_ci	char a[100], b[100];
77570af302Sopenharmony_ci	FILE *f;
78570af302Sopenharmony_ci	int p[2];
79570af302Sopenharmony_ci
80570af302Sopenharmony_ci	TEST(i, pipe(p), 0, "failed to open pipe %d!=%d (%s)");
81570af302Sopenharmony_ci	TEST(i, !(f = fdopen(p[0], "rb")), 0, "failed to fdopen pipe %d!=%d (%s)");
82570af302Sopenharmony_ci
83570af302Sopenharmony_ci	if (!f) {
84570af302Sopenharmony_ci		close(p[0]);
85570af302Sopenharmony_ci		close(p[1]);
86570af302Sopenharmony_ci		return 1;
87570af302Sopenharmony_ci	}
88570af302Sopenharmony_ci
89570af302Sopenharmony_ci	TEST(i, write(p[1], "hello, world\n", 13), 13, "write error %d!=%d (%s)");
90570af302Sopenharmony_ci	TEST(i, fscanf(f, "%s %[own]", a, b), 2, "got %d fields, expected %d");
91570af302Sopenharmony_ci	TEST_S(a, "hello,", "wrong result for %s");
92570af302Sopenharmony_ci	TEST_S(b, "wo", "wrong result for %[own]");
93570af302Sopenharmony_ci	TEST(i, fgetc(f), 'r', "'%c' != '%c') (%s)");
94570af302Sopenharmony_ci
95570af302Sopenharmony_ci	TEST(i, write(p[1], " 0x12 0x34", 10), 10, "write error %d!=%d (%s)");
96570af302Sopenharmony_ci	TEST(i, fscanf(f, "ld %5i%2i", &x, &y), 1, "got %d fields, expected %d");
97570af302Sopenharmony_ci	TEST(i, x, 0x12, "%d != %d");
98570af302Sopenharmony_ci	TEST(i, fgetc(f), '3', "'%c' != '%c'");
99570af302Sopenharmony_ci
100570af302Sopenharmony_ci	fclose(f);
101570af302Sopenharmony_ci	close(p[1]);
102570af302Sopenharmony_ci
103570af302Sopenharmony_ci	TEST(i, !!(f=writetemp("      42")), 1, "failed to make temp file");
104570af302Sopenharmony_ci	if (f) {
105570af302Sopenharmony_ci		x=y=-1;
106570af302Sopenharmony_ci		TEST(i, fscanf(f, " %n%*d%n", &x, &y), 0, "%d != %d");
107570af302Sopenharmony_ci		TEST(i, x, 6, "%d != %d");
108570af302Sopenharmony_ci		TEST(i, y, 8, "%d != %d");
109570af302Sopenharmony_ci		TEST(i, ftell(f), 8, "%d != %d");
110570af302Sopenharmony_ci		TEST(i, !!feof(f), 1, "%d != %d");
111570af302Sopenharmony_ci		fclose(f);
112570af302Sopenharmony_ci	}
113570af302Sopenharmony_ci
114570af302Sopenharmony_ci	TEST(i, !!(f=writetemp("[abc123]....x")), 1, "failed to make temp file");
115570af302Sopenharmony_ci	if (f) {
116570af302Sopenharmony_ci		x=y=-1;
117570af302Sopenharmony_ci		TEST(i, fscanf(f, "%10[^]]%n%10[].]%n", a, &x, b, &y), 2, "%d != %d");
118570af302Sopenharmony_ci		TEST_S(a, "[abc123", "wrong result for %[^]]");
119570af302Sopenharmony_ci		TEST_S(b, "]....", "wrong result for %[].]");
120570af302Sopenharmony_ci		TEST(i, x, 7, "%d != %d");
121570af302Sopenharmony_ci		TEST(i, y, 12, "%d != %d");
122570af302Sopenharmony_ci		TEST(i, ftell(f), 12, "%d != %d");
123570af302Sopenharmony_ci		TEST(i, feof(f), 0, "%d != %d");
124570af302Sopenharmony_ci		TEST(i, fgetc(f), 'x', "%d != %d");
125570af302Sopenharmony_ci		fclose(f);
126570af302Sopenharmony_ci	}
127570af302Sopenharmony_ci
128570af302Sopenharmony_ci	/* Comment it because musl parse "0x1p" to "0x1" now rather than treat it as abnormal format.
129570af302Sopenharmony_ci	TEST(i, !!(f=writetemp("0x1p 12")), 1, "failed to make temp file");
130570af302Sopenharmony_ci	if (f) {
131570af302Sopenharmony_ci		x=y=-1;
132570af302Sopenharmony_ci		u=-1;
133570af302Sopenharmony_ci		TEST(i, fscanf(f, "%lf%n %d", &u, &x, &y), 0, "%d != %d");
134570af302Sopenharmony_ci		TEST(u, u, -1.0, "%g != %g");
135570af302Sopenharmony_ci		TEST(i, x, -1, "%d != %d");
136570af302Sopenharmony_ci		TEST(i, y, -1, "%d != %d");
137570af302Sopenharmony_ci		TEST(i, ftell(f), 4, "%d != %d");
138570af302Sopenharmony_ci		TEST(i, feof(f), 0, "%d != %d");
139570af302Sopenharmony_ci		TEST(i, fgetc(f), ' ', "%d != %d");
140570af302Sopenharmony_ci		rewind(f);
141570af302Sopenharmony_ci		TEST(i, fgetc(f), '0', "%d != %d");
142570af302Sopenharmony_ci		TEST(i, fgetc(f), 'x', "%d != %d");
143570af302Sopenharmony_ci		TEST(i, fscanf(f, "%lf%n%c %d", &u, &x, a, &y), 3, "%d != %d");
144570af302Sopenharmony_ci		TEST(u, u, 1.0, "%g != %g");
145570af302Sopenharmony_ci		TEST(i, x, 1, "%d != %d");
146570af302Sopenharmony_ci		TEST(i, a[0], 'p', "%d != %d");
147570af302Sopenharmony_ci		TEST(i, y, 12, "%d != %d");
148570af302Sopenharmony_ci		TEST(i, ftell(f), 7, "%d != %d");
149570af302Sopenharmony_ci		TEST(i, !!feof(f), 1, "%d != %d");
150570af302Sopenharmony_ci		fclose(f);
151570af302Sopenharmony_ci	}
152570af302Sopenharmony_ci	*/
153570af302Sopenharmony_ci
154570af302Sopenharmony_ci	TEST(i, !!(f=writetemp("0x.1p4    012")), 1, "failed to make temp file");
155570af302Sopenharmony_ci	if (f) {
156570af302Sopenharmony_ci		x=y=-1;
157570af302Sopenharmony_ci		u=-1;
158570af302Sopenharmony_ci		TEST(i, fscanf(f, "%lf%n %i", &u, &x, &y), 2, "%d != %d");
159570af302Sopenharmony_ci		TEST(u, u, 1.0, "%g != %g");
160570af302Sopenharmony_ci		TEST(i, x, 6, "%d != %d");
161570af302Sopenharmony_ci		TEST(i, y, 10, "%d != %d");
162570af302Sopenharmony_ci		TEST(i, ftell(f), 13, "%d != %d");
163570af302Sopenharmony_ci		TEST(i, !!feof(f), 1, "%d != %d");
164570af302Sopenharmony_ci		fclose(f);
165570af302Sopenharmony_ci	}
166570af302Sopenharmony_ci
167570af302Sopenharmony_ci	TEST(i, !!(f=writetemp("0xx")), 1, "failed to make temp file");
168570af302Sopenharmony_ci	if (f) {
169570af302Sopenharmony_ci		x=y=-1;
170570af302Sopenharmony_ci		TEST(i, fscanf(f, "%x%n", &x, &y), 0, "%d != %d");
171570af302Sopenharmony_ci		TEST(i, x, -1, "%d != %d");
172570af302Sopenharmony_ci		TEST(i, y, -1, "%d != %d");
173570af302Sopenharmony_ci		TEST(i, ftell(f), 2, "%d != %d");
174570af302Sopenharmony_ci		TEST(i, feof(f), 0, "%d != %d");
175570af302Sopenharmony_ci		fclose(f);
176570af302Sopenharmony_ci	}
177570af302Sopenharmony_ci
178570af302Sopenharmony_ci	test_read_large_data();
179570af302Sopenharmony_ci
180570af302Sopenharmony_ci	return t_status;
181570af302Sopenharmony_ci}
182