• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <stdio.h>
2 #include <string.h>
3 #include <errno.h>
4 #include <limits.h>
5 #include <unistd.h>
6 #include "test.h"
7 
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) )
11 
12 #define TEST_S(s, x, m) ( \
13 	!strcmp((s),(x)) || \
14 	(t_error("[%s] != [%s] (%s)\n", s, x, m), 0) )
15 
main(void)16 int main(void)
17 {
18 	int i;
19 	char a[100];
20 	FILE *f;
21 
22 	TEST(i, !(f = tmpfile()), 0, "failed to create temp file %d!=%d (%s)");
23 
24 	if (!f) return t_status;
25 
26 	TEST(i, fprintf(f, "hello, world\n"), 13, "%d != %d (%m)");
27 	TEST(i, fseek(f, 0, SEEK_SET), 0, "%d != %d (%m)");
28 
29 	TEST(i, feof(f), 0, "%d != %d");
30 	TEST(i, fgetc(f), 'h', "'%c' != '%c'");
31 	TEST(i, ftell(f), 1, "%d != %d");
32 	TEST(i, ungetc('x', f), 'x', "%d != %d");
33 	TEST(i, ftell(f), 0, "%d != %d");
34 	TEST(i, fscanf(f, "%[h]", a), 0, "got %d fields, expected %d");
35 	TEST(i, ftell(f), 0, "%d != %d");
36 	TEST(i, fgetc(f), 'x', "'%c' != '%c'");
37 	TEST(i, ftell(f), 1, "%d != %d");
38 
39 	TEST(i, fseek(f, 0, SEEK_SET), 0, "%d != %d");
40 	TEST(i, ungetc('x', f), 'x', "%d != %d");
41 	TEST(i, fread(a, 1, sizeof a, f), 14, "read %d, expected %d");
42 	a[14] = 0;
43 	TEST_S(a, "xhello, world\n", "mismatch reading ungot character");
44 
45 	TEST(i, fseek(f, 0, SEEK_SET), 0, "%d != %d");
46 	TEST(i, fscanf(f, "%[x]", a), 0, "got %d fields, expected %d");
47 	TEST(i, ungetc('x', f), 'x', "unget failed after fscanf: %d != %d");
48 	TEST(i, fgetc(f), 'x', "'%c' != '%c'");
49 	TEST(i, fgetc(f), 'h', "'%c' != '%c'");
50 
51 	fclose(f);
52 	return t_status;
53 }
54