fscanf.c 4.3 KB
Newer Older
M
maweiye 已提交
1 2 3 4 5 6 7
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
#include <unistd.h>
#include "test.h"

Y
yinchuang 已提交
8 9 10 11 12
#define CONTENT "0.1234567\n"
#define EXPECT 0.1234567
#define TMP_FILE "/data/local/tmp/fscanf_test.txt"
#define TOTAL 515

M
maweiye 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
#define TEST(r, f, x, m) ( \
	errno=0, ((r) = (f)) == (x) || \
	(t_error("%s failed (" m ")\n", #f, r, x, strerror(errno)), 0) )

#define TEST_S(s, x, m) ( \
	!strcmp((s),(x)) || \
	(t_error("[%s] != [%s] (%s)\n", s, x, m), 0) )

static FILE *writetemp(const char *data)
{
	FILE *f = tmpfile();
	if (!f) return 0;
	if (!fwrite(data, strlen(data), 1, f)) {
		fclose(f);
		return 0;
	}
	rewind(f);
	return f;
}

Y
yinchuang 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
void write_content()
{
	FILE *fp;
	fp = fopen(TMP_FILE, "w");
	if (fp == NULL) {  
		t_error("FAIL %s create failed!\n", TMP_FILE);
	}
	for (size_t i = 0; i < TOTAL; i++) {
		fwrite(CONTENT, sizeof(char), strlen(CONTENT), fp);
	}
	fclose(fp);
}

void check_content()
{
	FILE *fp;
	int res;
	double value = 0;
	fp = fopen(TMP_FILE, "r");
	if (fp == NULL) {  
		t_error("FAIL %s open failed!\n", TMP_FILE);
	}
	for (size_t i = 0; i < TOTAL; i++) {
		res = fscanf(fp, "%lf", &value);
		if (res != 1 || value != EXPECT) {
			t_error("FAIL: fscanf expect %lf but get %lf, index=%d\n", EXPECT, value, i);
		}
	}
	fclose(fp);
}

void test_read_large_data()
{
	write_content();
	check_content();
	remove(TMP_FILE);
}


M
maweiye 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
int main(void)
{
	int i, x, y;
	double u;
	char a[100], b[100];
	FILE *f;
	int p[2];

	TEST(i, pipe(p), 0, "failed to open pipe %d!=%d (%s)");
	TEST(i, !(f = fdopen(p[0], "rb")), 0, "failed to fdopen pipe %d!=%d (%s)");

	if (!f) {
		close(p[0]);
		close(p[1]);
		return 1;
	}

	TEST(i, write(p[1], "hello, world\n", 13), 13, "write error %d!=%d (%s)");
	TEST(i, fscanf(f, "%s %[own]", a, b), 2, "got %d fields, expected %d");
	TEST_S(a, "hello,", "wrong result for %s");
	TEST_S(b, "wo", "wrong result for %[own]");
	TEST(i, fgetc(f), 'r', "'%c' != '%c') (%s)");

	TEST(i, write(p[1], " 0x12 0x34", 10), 10, "write error %d!=%d (%s)");
	TEST(i, fscanf(f, "ld %5i%2i", &x, &y), 1, "got %d fields, expected %d");
	TEST(i, x, 0x12, "%d != %d");
	TEST(i, fgetc(f), '3', "'%c' != '%c'");

	fclose(f);
	close(p[1]);

	TEST(i, !!(f=writetemp("      42")), 1, "failed to make temp file");
	if (f) {
		x=y=-1;
		TEST(i, fscanf(f, " %n%*d%n", &x, &y), 0, "%d != %d");
		TEST(i, x, 6, "%d != %d");
		TEST(i, y, 8, "%d != %d");
		TEST(i, ftell(f), 8, "%d != %d");
		TEST(i, !!feof(f), 1, "%d != %d");
		fclose(f);
	}

	TEST(i, !!(f=writetemp("[abc123]....x")), 1, "failed to make temp file");
	if (f) {
		x=y=-1;
		TEST(i, fscanf(f, "%10[^]]%n%10[].]%n", a, &x, b, &y), 2, "%d != %d");
		TEST_S(a, "[abc123", "wrong result for %[^]]");
		TEST_S(b, "]....", "wrong result for %[].]");
		TEST(i, x, 7, "%d != %d");
		TEST(i, y, 12, "%d != %d");
		TEST(i, ftell(f), 12, "%d != %d");
		TEST(i, feof(f), 0, "%d != %d");
		TEST(i, fgetc(f), 'x', "%d != %d");
		fclose(f);
	}

Y
yinchuang 已提交
128
	/* Comment it because musl parse "0x1p" to "0x1" now rather than treat it as abnormal format.
M
maweiye 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
	TEST(i, !!(f=writetemp("0x1p 12")), 1, "failed to make temp file");
	if (f) {
		x=y=-1;
		u=-1;
		TEST(i, fscanf(f, "%lf%n %d", &u, &x, &y), 0, "%d != %d");
		TEST(u, u, -1.0, "%g != %g");
		TEST(i, x, -1, "%d != %d");
		TEST(i, y, -1, "%d != %d");
		TEST(i, ftell(f), 4, "%d != %d");
		TEST(i, feof(f), 0, "%d != %d");
		TEST(i, fgetc(f), ' ', "%d != %d");
		rewind(f);
		TEST(i, fgetc(f), '0', "%d != %d");
		TEST(i, fgetc(f), 'x', "%d != %d");
		TEST(i, fscanf(f, "%lf%n%c %d", &u, &x, a, &y), 3, "%d != %d");
		TEST(u, u, 1.0, "%g != %g");
		TEST(i, x, 1, "%d != %d");
		TEST(i, a[0], 'p', "%d != %d");
		TEST(i, y, 12, "%d != %d");
		TEST(i, ftell(f), 7, "%d != %d");
		TEST(i, !!feof(f), 1, "%d != %d");
		fclose(f);
	}
Y
yinchuang 已提交
152
	*/
M
maweiye 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177

	TEST(i, !!(f=writetemp("0x.1p4    012")), 1, "failed to make temp file");
	if (f) {
		x=y=-1;
		u=-1;
		TEST(i, fscanf(f, "%lf%n %i", &u, &x, &y), 2, "%d != %d");
		TEST(u, u, 1.0, "%g != %g");
		TEST(i, x, 6, "%d != %d");
		TEST(i, y, 10, "%d != %d");
		TEST(i, ftell(f), 13, "%d != %d");
		TEST(i, !!feof(f), 1, "%d != %d");
		fclose(f);
	}

	TEST(i, !!(f=writetemp("0xx")), 1, "failed to make temp file");
	if (f) {
		x=y=-1;
		TEST(i, fscanf(f, "%x%n", &x, &y), 0, "%d != %d");
		TEST(i, x, -1, "%d != %d");
		TEST(i, y, -1, "%d != %d");
		TEST(i, ftell(f), 2, "%d != %d");
		TEST(i, feof(f), 0, "%d != %d");
		fclose(f);
	}

Y
yinchuang 已提交
178 179
	test_read_large_data();

M
maweiye 已提交
180 181
	return t_status;
}