• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <stdlib.h>
2 #include <unistd.h>
3 #include <stdio.h>
4 #include <errno.h>
5 #include <string.h>
6 #include "test.h"
7 
8 #define TEST(r, f, x, m) ( \
9 ((r) = (f)) == (x) || \
10 (t_error("%s failed (" m ")\n", #f, r, x), 0) )
11 
12 #define TEST_E(f) ( (errno = 0), (f) || \
13 (t_error("%s failed (errno = %d)\n", #f, errno), 0) )
14 
15 #define TEST_S(s, x, m) ( \
16 !strcmp((s),(x)) || \
17 (t_error("[%s] != [%s] (%s)\n", s, x, m), 0) )
18 
19 #define TEST_M(s, x, n, m) ( \
20 !memcmp((s),(x),(n)) || \
21 (t_error("[%s] != [%s] (%s)\n", s, x, m), 0) )
22 
test_memfile(void)23 static void test_memfile(void)
24 {
25 	FILE *f;
26 	char *ss;
27 	size_t l;
28 	char buf[100];
29 	int i;
30 	int index = 8;
31 
32 	TEST_E(f = fmemopen(buf, 10, "r+"));
33 	TEST_E(fputs("world!", f) >= 0);
34 	TEST_E(!fflush(f));
35 	TEST_E(fclose(f) != -1);
36 
37 	ss = 0;
38 	f = NULL;
39 	TEST_E(f = fmemopen(buf, 10, "a+"));
40 	TEST(i, ftell(f), 6, "%d != %d");
41 	TEST_E(fseek(f, 0, SEEK_SET) >= 0);
42 	TEST(i, getc(f), 'w', "%d != %d");
43 	TEST(i, getc(f), 'o', "%d != %d");
44 	TEST(i, getc(f), 'r', "%d != %d");
45 	TEST(i, getc(f), 'l', "%d != %d");
46 	TEST(i, getc(f), 'd', "%d != %d");
47 	TEST(i, getc(f), '!', "%d != %d");
48 	TEST_E(fseek(f, 7, SEEK_SET) >= 0);
49 	TEST(i, ftell(f), 7, "%d != %d");
50 	TEST(i, getc(f), EOF, "%d != %d");
51 	TEST(i, ftell(f), 7, "%d != %d");
52 	TEST_E(fseek(f, 0, SEEK_SET) >= 0);
53 	TEST(i, getc(f), 'w', "%d != %d");
54 	TEST_E(fseek(f, 0, SEEK_CUR) >= 0);
55 	buf[index] = 'x';
56 	TEST_E(fprintf(f, "%d", i) == 3);
57 	TEST_E(fflush(f) == 0);
58 	TEST(i, ftell(f), 9, "%d != %d");
59 	TEST_S(buf, "world!119", "");
60 	TEST_E(fclose(f) != -1);
61 }
62 
main(void)63 int main(void)
64 {
65 	FILE *f;
66 	char *s;
67 	size_t l;
68 	char buf[100];
69 	int i;
70 
71 	s = 0;
72 	TEST_E(f = open_memstream(&s, &l));
73 	TEST_E(putc('a', f) == 'a');
74 	TEST_E(putc('b', f) == 'b');
75 	TEST_E(putc('c', f) == 'c');
76 	TEST_E(!fflush(f));
77 	fclose(f);
78 	if (s) TEST_S(s, "abc", "wrong output");
79 	free(s);
80 
81 	s = 0;
82 	TEST_E(f = open_memstream(&s, &l));
83 	TEST_E(fseek(f,1,SEEK_CUR)>=0);
84 	TEST_E(putc('q', f) == 'q');
85 	TEST_E(!fflush(f));
86 	if (s) TEST_M(s, "\0q", 3, "wrong output");
87 	TEST(i, fseek(f,-3,SEEK_CUR), -1, "invalid seek allowed");
88 	TEST(i, errno, EINVAL, "%d != %d");
89 	TEST(i, ftell(f), 2, "%d != %d");
90 	TEST_E(fseek(f,-2,SEEK_CUR)>=0);
91 	TEST_E(putc('e', f) == 'e');
92 	TEST_E(!fflush(f));
93 	if (s) TEST_S(s, "eq", "wrong output");
94 	fclose(f);
95 	free(s);
96 
97 	TEST_E(f = fmemopen(buf, 10, "r+"));
98 	TEST_E(fputs("hello", f) >= 0);
99 	TEST_E(fputc(0, f)==0);
100 	TEST_E(fseek(f, 0, SEEK_SET)>=0);
101 	i=0;
102 	TEST_E(fscanf(f, "hello%n", &i)==0);
103 	TEST(i, i, 5, "%d != %d");
104 	TEST(i, ftell(f), 5, "%d != %d");
105 	errno = 0;
106 	TEST(i, fseek(f, 6, SEEK_CUR)<0, 1, "");
107 	TEST(i, errno!=0, 1, "");
108 	TEST(i, ftell(f), 5, "%d != %d");
109 	TEST_S(buf, "hello", "");
110 	fclose(f);
111 
112 	test_memfile();
113 
114 	return t_status;
115 }
116