test/buffer_tests.c

changeset 60
abae4669fba7
parent 58
733f22fca61a
child 61
fb07a0ab9a17
equal deleted inserted replaced
59:c80c910fe191 60:abae4669fba7
1 /*
2 *
3 */
4
5 #include "buffer_tests.h"
6
7 UCX_TEST_IMPLEMENT(test_ucx_buffer_seektell) {
8 char *buffer = malloc(16);
9 memset(buffer, 32, 7);
10 buffer[7] = 0;
11
12 UcxBuffer *b = ucx_buffer_new(buffer, 16);
13 int r;
14
15 UCX_TEST_BEGIN
16
17 r = ucx_buffer_seek(b, 5, SEEK_SET);
18 UCX_TEST_ASSERT(r == 0, "seek SET+5 failed");
19 UCX_TEST_ASSERT(ucx_buffer_tell(b) == 5, "seek SET+5 set wrong position");
20
21 r = ucx_buffer_seek(b, 20, SEEK_SET);
22 UCX_TEST_ASSERT(r != 0, "seek beyond bounds shall fail");
23 UCX_TEST_ASSERT(ucx_buffer_tell(b) == 5,
24 "failed seek shall leave pos unchanged");
25
26 r = ucx_buffer_seek(b, 5, SEEK_CUR);
27 UCX_TEST_ASSERT(r == 0, "seek CUR+5 failed");
28 UCX_TEST_ASSERT(ucx_buffer_tell(b) == 10, "seek CUR+5 set wrong position");
29
30 r = ucx_buffer_seek(b, 10, SEEK_CUR);
31 UCX_TEST_ASSERT(r != 0, "seek CUR beyond bounds shall fail");
32 UCX_TEST_ASSERT(ucx_buffer_tell(b) == 10,
33 "failed seek shall leave pos unchanged");
34
35 r = ucx_buffer_seek(b, -5, SEEK_END);
36 UCX_TEST_ASSERT(r == 0, "seek END-5 failed");
37 UCX_TEST_ASSERT(ucx_buffer_tell(b) == 2, "seek END-5 set wrong position");
38
39 r = ucx_buffer_seek(b, -10, SEEK_END);
40 UCX_TEST_ASSERT(r != 0, "seek END beyond bounds shall fail");
41 UCX_TEST_ASSERT(ucx_buffer_tell(b) == 2,
42 "failed seek shall leave pos unchanged");
43
44 UCX_TEST_END
45
46 ucx_buffer_free(b);
47 free(buffer);
48 }
49
50 UCX_TEST_IMPLEMENT(test_ucx_buffer_putc) {
51 char *buffer = malloc(16);
52 memset(buffer, 32, 16);
53
54 UcxBuffer *b = ucx_buffer_new(buffer, 16);
55 int r;
56
57 UCX_TEST_BEGIN
58
59 ucx_buffer_putc(b, 48); ucx_buffer_putc(b, 48); ucx_buffer_putc(b, 48);
60 UCX_TEST_ASSERT(ucx_buffer_tell(b) == 3, "pos wrong after first 3 puts");
61 ucx_buffer_seek(b, 10, SEEK_CUR);
62 ucx_buffer_putc(b, 48); ucx_buffer_putc(b, 48); ucx_buffer_putc(b, 48);
63 UCX_TEST_ASSERT(ucx_buffer_tell(b) == 16, "pos wrong after last 3 puts");
64 UCX_TEST_ASSERT(ucx_buffer_eof(b), "eof not set");
65 UCX_TEST_ASSERT(ucx_buffer_putc(b, 48) == EOF, "put shall return EOF on memof");
66 UCX_TEST_ASSERT(memcmp(buffer, "000 000", 16) == 0,
67 "buffer contains incorrect content");
68
69 UCX_TEST_END
70
71 ucx_buffer_free(b);
72 free(buffer);
73 }
74
75 UCX_TEST_IMPLEMENT(test_ucx_buffer_getc) {
76 char *buffer = malloc(16);
77 memset(buffer, 32, 8);
78 for (int i = 8; i < 16 ; i++) {
79 buffer[i] = 40+i;
80 }
81
82 UcxBuffer *b = ucx_buffer_new(buffer, 16);
83 int r;
84
85 UCX_TEST_BEGIN
86
87 char rb[16];
88 for (int i = 0 ; i < 16 ; i++) {
89 UCX_TEST_ASSERT(ucx_buffer_tell(b) == i, "pos wrong during read loop");
90 UCX_TEST_ASSERT(!ucx_buffer_eof(b),
91 "EOF shall not be set during read loop");
92 rb[i] = ucx_buffer_getc(b);
93 }
94 UCX_TEST_ASSERT(ucx_buffer_tell(b) == 16, "pos wrong after read loop");
95 UCX_TEST_ASSERT(ucx_buffer_eof(b), "EOF not set");
96 UCX_TEST_ASSERT(memcmp(rb, " 01234567", 16) == 0,
97 "read data incorrect");
98
99 UCX_TEST_END
100
101 ucx_buffer_free(b);
102 free(buffer);
103 }
104
105 UCX_TEST_IMPLEMENT(test_ucx_buffer_write) {
106 char *buffer = malloc(16);
107 memset(buffer, 32, 8);
108 for (int i = 8; i < 16 ; i++) {
109 buffer[i] = 40+i;
110 }
111
112 UcxBuffer *b = ucx_buffer_new(buffer, 16);
113 int r;
114
115 UCX_TEST_BEGIN
116
117 char* teststring = "this is way too much";
118 r = ucx_buffer_write(teststring, 1, 20, b);
119 UCX_TEST_ASSERT(r == 16, "string not correctly trimed");
120 UCX_TEST_ASSERT(memcmp(buffer, teststring, 16) == 0,
121 "buffer data incorrect");
122 UCX_TEST_ASSERT(ucx_buffer_eof(b), "eof shall be set");
123
124 ucx_buffer_seek(b, 8, SEEK_SET);
125 r = ucx_buffer_write("not", 1, 3, b);
126 UCX_TEST_ASSERT(r == 3, "three bytes should be replace");
127 UCX_TEST_ASSERT(memcmp(buffer, "this is not too much", 16) == 0,
128 "modified buffer is incorrect");
129
130 char* threebytestring = " t h r e e ";
131 memset(buffer, 49, 16);
132 ucx_buffer_seek(b, 0, SEEK_SET);
133 r = ucx_buffer_write(threebytestring, 3, 6, b);
134 UCX_TEST_ASSERT(r == 15, "three byte string not correctly trimed");
135 UCX_TEST_ASSERT(ucx_buffer_tell(b) == 15,
136 "position after write of three byte string incorrect");
137 UCX_TEST_ASSERT(!ucx_buffer_eof(b), "eof shall not be set");
138 UCX_TEST_ASSERT(memcmp(buffer, " t h r e e1", 16) == 0,
139 "bufer is incorrect after three byte string has been written");
140
141 UCX_TEST_END
142
143 ucx_buffer_free(b);
144 free(buffer);
145 }
146
147 UCX_TEST_IMPLEMENT(test_ucx_buffer_read) {
148 char *buffer = malloc(16);
149 memset(buffer, 56, 8);
150 for (int i = 8; i < 16 ; i++) {
151 buffer[i] = 40+i;
152 }
153
154 UcxBuffer *b = ucx_buffer_new(buffer, 16);
155 int r;
156
157 UCX_TEST_BEGIN
158
159 char rb[16];
160 memset(rb, 32, 16);
161
162 ucx_buffer_seek(b, 8, SEEK_SET);
163 r = ucx_buffer_read(rb, 1, 16, b);
164 UCX_TEST_ASSERT(r == 8, "read did not stop at buffer end");
165 UCX_TEST_ASSERT(memcmp(rb, "01234567 ", 16) == 0,
166 "buffer incorrect after first read");
167 UCX_TEST_ASSERT(ucx_buffer_eof(b), "eof shall be set");
168
169 ucx_buffer_seek(b, 0, SEEK_SET);
170 r = ucx_buffer_read(rb+8, 1, 8, b);
171 UCX_TEST_ASSERT(r == 8, "read did not read the specified amount of bytes");
172 UCX_TEST_ASSERT(memcmp(rb, "0123456788888888", 16) == 0,
173 "buffer incorrect after second read");
174
175 ucx_buffer_seek(b, 0, SEEK_SET);
176 r = ucx_buffer_read(rb, 3, 6, b);
177 UCX_TEST_ASSERT(r == 15,
178 "three byte read did not read the desired amount of bytes");
179 UCX_TEST_ASSERT(memcmp(rb, "8888888801234568", 16) == 0,
180 "buffer incorrect after three byte read");
181
182 UCX_TEST_END
183
184 ucx_buffer_free(b);
185 free(buffer);
186 }

mercurial