add _DEFAULT_SOURCE wherever _BSD_SOURCE was used
[libc-test] / src / functional / memstream.c
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
23 int main(void)
24 {
25         FILE *f;
26         char *s;
27         size_t l;
28         char buf[100];
29         int i;
30
31         s = 0;
32         TEST_E(f = open_memstream(&s, &l));
33         TEST_E(putc('a', f) == 'a');
34         TEST_E(putc('b', f) == 'b');
35         TEST_E(putc('c', f) == 'c');
36         TEST_E(!fflush(f));
37         fclose(f);
38         if (s) TEST_S(s, "abc", "wrong output");
39         free(s);
40
41         s = 0;
42         TEST_E(f = open_memstream(&s, &l));
43         TEST_E(fseek(f,1,SEEK_CUR)>=0);
44         TEST_E(putc('q', f) == 'q');
45         TEST_E(!fflush(f));
46         if (s) TEST_M(s, "\0q", 3, "wrong output");
47         TEST(i, fseek(f,-3,SEEK_CUR), -1, "invalid seek allowed");
48         TEST(i, errno, EINVAL, "%d != %d");
49         TEST(i, ftell(f), 2, "%d != %d");
50         TEST_E(fseek(f,-2,SEEK_CUR)>=0);
51         TEST_E(putc('e', f) == 'e');
52         TEST_E(!fflush(f));
53         if (s) TEST_S(s, "eq", "wrong output");
54         fclose(f);
55         free(s);
56
57         TEST_E(f = fmemopen(buf, 10, "r+"));
58         TEST_E(fputs("hello", f) >= 0);
59         TEST_E(fputc(0, f)==0);
60         TEST_E(fseek(f, 0, SEEK_SET)>=0);
61         i=0;
62         TEST_E(fscanf(f, "hello%n", &i)==0);
63         TEST(i, i, 5, "%d != %d");
64         TEST(i, ftell(f), 5, "%d != %d");
65         errno = 0;
66         TEST(i, fseek(f, 6, SEEK_CUR)<0, 1, "");
67         TEST(i, errno!=0, 1, "");
68         TEST(i, ftell(f), 5, "%d != %d");
69         TEST_S(buf, "hello", "");
70         fclose(f);
71
72         TEST_E(f = fmemopen(buf, 10, "a+"));
73         TEST(i, ftell(f), 5, "%d != %d");
74         TEST_E(fseek(f, 0, SEEK_SET)>=0);
75         TEST(i, getc(f), 'h', "%d != %d");
76         TEST(i, getc(f), 'e', "%d != %d");
77         TEST(i, getc(f), 'l', "%d != %d");
78         TEST(i, getc(f), 'l', "%d != %d");
79         TEST(i, getc(f), 'o', "%d != %d");
80         TEST(i, getc(f), EOF, "%d != %d");
81         TEST_E(fseek(f, 6, SEEK_SET)>=0);
82         TEST(i, ftell(f), 6, "%d != %d");
83         TEST(i, getc(f), EOF, "%d != %d");
84         TEST(i, ftell(f), 6, "%d != %d");
85         TEST_E(fseek(f, 0, SEEK_SET)>=0);
86         TEST(i, getc(f), 'h', "%d != %d");
87         TEST_E(fseek(f, 0, SEEK_CUR)>=0);
88         buf[7] = 'x';
89         TEST_E(fprintf(f, "%d", i)==3);
90         TEST_E(fflush(f)==0);
91         TEST(i, ftell(f), 8, "%d != %d");
92         TEST_S(buf, "hello104", "");
93         fclose(f);
94         return t_status;
95 }