1570af302Sopenharmony_ci#include <stdlib.h>
2570af302Sopenharmony_ci#include <unistd.h>
3570af302Sopenharmony_ci#include <stdio.h>
4570af302Sopenharmony_ci#include <errno.h>
5570af302Sopenharmony_ci#include <string.h>
6570af302Sopenharmony_ci#include "test.h"
7570af302Sopenharmony_ci
8570af302Sopenharmony_ci#define TEST(r, f, x, m) ( \
9570af302Sopenharmony_ci((r) = (f)) == (x) || \
10570af302Sopenharmony_ci(t_error("%s failed (" m ")\n", #f, r, x), 0) )
11570af302Sopenharmony_ci
12570af302Sopenharmony_ci#define TEST_E(f) ( (errno = 0), (f) || \
13570af302Sopenharmony_ci(t_error("%s failed (errno = %d)\n", #f, errno), 0) )
14570af302Sopenharmony_ci
15570af302Sopenharmony_ci#define TEST_S(s, x, m) ( \
16570af302Sopenharmony_ci!strcmp((s),(x)) || \
17570af302Sopenharmony_ci(t_error("[%s] != [%s] (%s)\n", s, x, m), 0) )
18570af302Sopenharmony_ci
19570af302Sopenharmony_ci#define TEST_M(s, x, n, m) ( \
20570af302Sopenharmony_ci!memcmp((s),(x),(n)) || \
21570af302Sopenharmony_ci(t_error("[%s] != [%s] (%s)\n", s, x, m), 0) )
22570af302Sopenharmony_ci
23570af302Sopenharmony_ciint main(void)
24570af302Sopenharmony_ci{
25570af302Sopenharmony_ci	FILE *f;
26570af302Sopenharmony_ci	char *s;
27570af302Sopenharmony_ci	size_t l;
28570af302Sopenharmony_ci	char buf[100];
29570af302Sopenharmony_ci	int i;
30570af302Sopenharmony_ci
31570af302Sopenharmony_ci	s = 0;
32570af302Sopenharmony_ci	TEST_E(f = open_memstream(&s, &l));
33570af302Sopenharmony_ci	TEST_E(putc('a', f) == 'a');
34570af302Sopenharmony_ci	TEST_E(putc('b', f) == 'b');
35570af302Sopenharmony_ci	TEST_E(putc('c', f) == 'c');
36570af302Sopenharmony_ci	TEST_E(!fflush(f));
37570af302Sopenharmony_ci	fclose(f);
38570af302Sopenharmony_ci	if (s) TEST_S(s, "abc", "wrong output");
39570af302Sopenharmony_ci	free(s);
40570af302Sopenharmony_ci
41570af302Sopenharmony_ci	s = 0;
42570af302Sopenharmony_ci	TEST_E(f = open_memstream(&s, &l));
43570af302Sopenharmony_ci	TEST_E(fseek(f,1,SEEK_CUR)>=0);
44570af302Sopenharmony_ci	TEST_E(putc('q', f) == 'q');
45570af302Sopenharmony_ci	TEST_E(!fflush(f));
46570af302Sopenharmony_ci	if (s) TEST_M(s, "\0q", 3, "wrong output");
47570af302Sopenharmony_ci	TEST(i, fseek(f,-3,SEEK_CUR), -1, "invalid seek allowed");
48570af302Sopenharmony_ci	TEST(i, errno, EINVAL, "%d != %d");
49570af302Sopenharmony_ci	TEST(i, ftell(f), 2, "%d != %d");
50570af302Sopenharmony_ci	TEST_E(fseek(f,-2,SEEK_CUR)>=0);
51570af302Sopenharmony_ci	TEST_E(putc('e', f) == 'e');
52570af302Sopenharmony_ci	TEST_E(!fflush(f));
53570af302Sopenharmony_ci	if (s) TEST_S(s, "eq", "wrong output");
54570af302Sopenharmony_ci	fclose(f);
55570af302Sopenharmony_ci	free(s);
56570af302Sopenharmony_ci
57570af302Sopenharmony_ci	TEST_E(f = fmemopen(buf, 10, "r+"));
58570af302Sopenharmony_ci	TEST_E(fputs("hello", f) >= 0);
59570af302Sopenharmony_ci	TEST_E(fputc(0, f)==0);
60570af302Sopenharmony_ci	TEST_E(fseek(f, 0, SEEK_SET)>=0);
61570af302Sopenharmony_ci	i=0;
62570af302Sopenharmony_ci	TEST_E(fscanf(f, "hello%n", &i)==0);
63570af302Sopenharmony_ci	TEST(i, i, 5, "%d != %d");
64570af302Sopenharmony_ci	TEST(i, ftell(f), 5, "%d != %d");
65570af302Sopenharmony_ci	errno = 0;
66570af302Sopenharmony_ci	TEST(i, fseek(f, 6, SEEK_CUR)<0, 1, "");
67570af302Sopenharmony_ci	TEST(i, errno!=0, 1, "");
68570af302Sopenharmony_ci	TEST(i, ftell(f), 5, "%d != %d");
69570af302Sopenharmony_ci	TEST_S(buf, "hello", "");
70570af302Sopenharmony_ci	fclose(f);
71570af302Sopenharmony_ci
72570af302Sopenharmony_ci	TEST_E(f = fmemopen(buf, 10, "a+"));
73570af302Sopenharmony_ci	TEST(i, ftell(f), 5, "%d != %d");
74570af302Sopenharmony_ci	TEST_E(fseek(f, 0, SEEK_SET)>=0);
75570af302Sopenharmony_ci	TEST(i, getc(f), 'h', "%d != %d");
76570af302Sopenharmony_ci	TEST(i, getc(f), 'e', "%d != %d");
77570af302Sopenharmony_ci	TEST(i, getc(f), 'l', "%d != %d");
78570af302Sopenharmony_ci	TEST(i, getc(f), 'l', "%d != %d");
79570af302Sopenharmony_ci	TEST(i, getc(f), 'o', "%d != %d");
80570af302Sopenharmony_ci	TEST(i, getc(f), EOF, "%d != %d");
81570af302Sopenharmony_ci	TEST_E(fseek(f, 6, SEEK_SET)>=0);
82570af302Sopenharmony_ci	TEST(i, ftell(f), 6, "%d != %d");
83570af302Sopenharmony_ci	TEST(i, getc(f), EOF, "%d != %d");
84570af302Sopenharmony_ci	TEST(i, ftell(f), 6, "%d != %d");
85570af302Sopenharmony_ci	TEST_E(fseek(f, 0, SEEK_SET)>=0);
86570af302Sopenharmony_ci	TEST(i, getc(f), 'h', "%d != %d");
87570af302Sopenharmony_ci	TEST_E(fseek(f, 0, SEEK_CUR)>=0);
88570af302Sopenharmony_ci	buf[7] = 'x';
89570af302Sopenharmony_ci	TEST_E(fprintf(f, "%d", i)==3);
90570af302Sopenharmony_ci	TEST_E(fflush(f)==0);
91570af302Sopenharmony_ci	TEST(i, ftell(f), 8, "%d != %d");
92570af302Sopenharmony_ci	TEST_S(buf, "hello104", "");
93570af302Sopenharmony_ci	fclose(f);
94570af302Sopenharmony_ci	return t_status;
95570af302Sopenharmony_ci}
96