summaryrefslogtreecommitdiff
path: root/memstream.c
blob: cdb4b0c08200cfc57c57f20c09022c38f5f63510 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
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
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>

#define TEST(r, f, x, m) ( \
((r) = (f)) == (x) || \
(printf(__FILE__ ":%d: %s failed (" m ")\n", __LINE__, #f, r, x), err++, 0) )

#define TEST_E(f) ( (errno = 0), (f) || \
(printf(__FILE__ ":%d: %s failed (errno = %d)\n", __LINE__, #f, errno), err++, 0) )

#define TEST_S(s, x, m) ( \
!strcmp((s),(x)) || \
(printf(__FILE__ ":%d: [%s] != [%s] (%s)\n", __LINE__, s, x, m), err++, 0) )

#define TEST_M(s, x, n, m) ( \
!memcmp((s),(x),(n)) || \
(printf(__FILE__ ":%d: [%s] != [%s] (%s)\n", __LINE__, s, x, m), err++, 0) )

int test_memstream(void)
{
	int err = 0;
	FILE *f;
	char *s;
	size_t l;
	char buf[100];
	int i;

	s = 0;
	TEST_E(f = open_memstream(&s, &l));
	TEST_E(putc('a', f) == 'a');
	TEST_E(putc('b', f) == 'b');
	TEST_E(putc('c', f) == 'c');
	TEST_E(!fflush(f));
	fclose(f);
	if (s) TEST_S(s, "abc", "wrong output");
	free(s);

	s = 0;
	TEST_E(f = open_memstream(&s, &l));
	TEST_E(fseek(f,1,SEEK_CUR)>=0);
	TEST_E(putc('q', f) == 'q');
	TEST_E(!fflush(f));
	if (s) TEST_M(s, "\0q", 3, "wrong output");
	TEST(i, fseek(f,-3,SEEK_CUR), -1, "invalid seek allowed");
	TEST(i, errno, EINVAL, "%d != %d");
	TEST(i, ftell(f), 2, "%d != %d");
	TEST_E(fseek(f,-2,SEEK_CUR)>=0);
	TEST_E(putc('e', f) == 'e');
	TEST_E(!fflush(f));
	if (s) TEST_S(s, "eq", "wrong output");
	fclose(f);
	free(s);

	TEST_E(f = fmemopen(buf, 10, "r+"));
	TEST_E(fputs("hello", f) >= 0);
	TEST_E(fputc(0, f)==0);
	TEST_E(fseek(f, 0, SEEK_SET)>=0);
	i=0;
	TEST_E(fscanf(f, "hello%n", &i)==0);
	TEST(i, i, 5, "%d != %d");
	TEST(i, ftell(f), 5, "%d != %d");
	errno = 0;
	TEST(i, fseek(f, 6, SEEK_CUR)<0, 1, "");
	TEST(i, errno!=0, 1, "");
	TEST(i, ftell(f), 5, "%d != %d");
	TEST_S(buf, "hello", "");
	fclose(f);

	TEST_E(f = fmemopen(buf, 10, "a+"));
	TEST(i, ftell(f), 5, "%d != %d");
	TEST_E(fseek(f, 0, SEEK_SET)>=0);
	TEST(i, getc(f), 'h', "%d != %d");
	TEST(i, getc(f), 'e', "%d != %d");
	TEST(i, getc(f), 'l', "%d != %d");
	TEST(i, getc(f), 'l', "%d != %d");
	TEST(i, getc(f), 'o', "%d != %d");
	TEST(i, getc(f), EOF, "%d != %d");
	TEST_E(fseek(f, 6, SEEK_SET)>=0);
	TEST(i, ftell(f), 6, "%d != %d");
	TEST(i, getc(f), EOF, "%d != %d");
	TEST(i, ftell(f), 6, "%d != %d");
	TEST_E(fseek(f, 0, SEEK_SET)>=0);
	TEST(i, getc(f), 'h', "%d != %d");
	TEST_E(fseek(f, 0, SEEK_CUR)>=0);
	buf[7] = 'x';
	TEST_E(fprintf(f, "%d", i)==3);
	TEST_E(fflush(f)==0);
	TEST(i, ftell(f), 8, "%d != %d");
	TEST_S(buf, "hello104", "");
	fclose(f);

	return err;
}