test/buffer_tests.c

changeset 164
1fa3f13f774c
parent 163
5ec9a2ca6328
child 166
350a0e3898bd
equal deleted inserted replaced
163:5ec9a2ca6328 164:1fa3f13f774c
27 */ 27 */
28 28
29 #include "buffer_tests.h" 29 #include "buffer_tests.h"
30 #include "ucx/utils.h" 30 #include "ucx/utils.h"
31 31
32 UCX_TEST(test_ucx_buffer_seek) { 32 /*
33 * TODO: refactor tests
34 *
35 * ucx_buffer_extend
36 * ucx_buffer_extract
37 * ucx_buffer_free
38 * ucx_buffer_getc
39 * ucx_buffer_new
40 * ucx_buffer_puts
41 * ucx_buffer_read
42 * ucx_buffer_write
43 *
44 */
45
46 UCX_TEST(test_ucx_buffer_eof) {
47 char *test = "0123456789ABCDEF";
48 UcxBuffer *b = ucx_buffer_new(test, 16, UCX_BUFFER_DEFAULT);
49 UCX_TEST_BEGIN
50 b->pos = 9; b->size = 10;
51 UCX_TEST_ASSERT(!ucx_buffer_eof(b), "false positive");
52 b->pos = 10; b->size = 10;
53 UCX_TEST_ASSERT(ucx_buffer_eof(b), "pos == size should be EOF");
54 b->pos = 11; b->size = 10;
55 UCX_TEST_ASSERT(ucx_buffer_eof(b), "false negative");
56 UCX_TEST_END
57 }
58
59 UCX_TEST(test_ucx_buffer_seek_overflow) {
60 UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT);
61 b->size = 32;
62 int r;
63
64 UCX_TEST_BEGIN
65 size_t bigpos = SIZE_MAX - 5000;
66 b->pos = bigpos;
67 r = ucx_buffer_seek(b, 5016, SEEK_CUR);
68 UCX_TEST_ASSERT(r != 0, "seek cur overflow");
69 UCX_TEST_ASSERT(b->pos == bigpos,
70 "failed seek shall leave pos unchanged");
71
72 b->pos = 0;
73 b->size = SIZE_MAX / 2 + 32;
74 off_t bigoff = SIZE_MAX / 2 - 16;
75 r = ucx_buffer_seek(b, -bigoff, SEEK_CUR);
76 UCX_TEST_ASSERT(r != 0, "seek cur underflow");
77 UCX_TEST_ASSERT(b->pos == 0,
78 "failed seek shall leave pos unchanged");
79
80 UCX_TEST_END
81
82 ucx_buffer_free(b);
83 }
84
85 UCX_TEST(test_ucx_buffer_seek_invalid) {
86 UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT);
87 b->pos = 7;
88 int r;
89
90 UCX_TEST_BEGIN
91 r = ucx_buffer_seek(b, 0, ~(SEEK_SET|SEEK_CUR|SEEK_END));
92 UCX_TEST_ASSERT(r != 0, "invalid whence shall fail");
93 UCX_TEST_ASSERT(b->pos == 7,
94 "failed seek shall leave pos unchanged");
95 UCX_TEST_END
96
97 ucx_buffer_free(b);
98 }
99
100 UCX_TEST(test_ucx_buffer_seek_oob) {
33 UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT); 101 UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT);
34 b->size = 16; // less than capacity 102 b->size = 16; // less than capacity
103 b->pos = 7;
104 int r;
105
106 UCX_TEST_BEGIN
107
108 r = ucx_buffer_seek(b, -1, SEEK_SET);
109 UCX_TEST_ASSERT(r != 0, "seek SET below bounds shall fail");
110 UCX_TEST_ASSERT(b->pos == 7,
111 "failed seek shall leave pos unchanged");
112
113 r = ucx_buffer_seek(b, 16, SEEK_SET);
114 UCX_TEST_ASSERT(r != 0, "seek SET above bounds shall fail");
115 UCX_TEST_ASSERT(b->pos == 7,
116 "failed seek shall leave pos unchanged");
117
118 r = ucx_buffer_seek(b, -8, SEEK_CUR);
119 UCX_TEST_ASSERT(r != 0, "seek CUR below bounds shall fail");
120 UCX_TEST_ASSERT(b->pos == 7,
121 "failed seek shall leave pos unchanged");
122
123 r = ucx_buffer_seek(b, 9, SEEK_CUR);
124 UCX_TEST_ASSERT(r != 0, "seek CUR above bounds shall fail");
125 UCX_TEST_ASSERT(b->pos == 7,
126 "failed seek shall leave pos unchanged");
127
128 r = ucx_buffer_seek(b, -17, SEEK_END);
129 UCX_TEST_ASSERT(r != 0, "seek END below bounds shall fail");
130 UCX_TEST_ASSERT(b->pos == 7,
131 "failed seek shall leave pos unchanged");
132
133 r = ucx_buffer_seek(b, 1, SEEK_END);
134 UCX_TEST_ASSERT(r != 0, "seek END above bounds shall fail");
135 UCX_TEST_ASSERT(b->pos == 7,
136 "failed seek shall leave pos unchanged");
137
138 UCX_TEST_END
139
140 ucx_buffer_free(b);
141 }
142
143 UCX_TEST(test_ucx_buffer_seek_set) {
144 UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT);
145 b->size = 16;
35 int r; 146 int r;
36 147
37 UCX_TEST_BEGIN 148 UCX_TEST_BEGIN
38 149
39 r = ucx_buffer_seek(b, 5, SEEK_SET); 150 r = ucx_buffer_seek(b, 5, SEEK_SET);
40 UCX_TEST_ASSERT(r == 0, "seek SET+5 failed"); 151 UCX_TEST_ASSERT(r == 0, "seek SET+5 failed");
41 UCX_TEST_ASSERT(b->pos == 5, "seek SET+5 set wrong position"); 152 UCX_TEST_ASSERT(b->pos == 5, "seek SET+5 set wrong position");
42 153
43 r = ucx_buffer_seek(b, 20, SEEK_SET); 154
44 UCX_TEST_ASSERT(r != 0, "seek beyond bounds shall fail"); 155 r = ucx_buffer_seek(b, 10, SEEK_SET);
45 UCX_TEST_ASSERT(b->pos == 5, 156 UCX_TEST_ASSERT(r == 0, "seek SET+10 failed");
46 "failed seek shall leave pos unchanged"); 157 UCX_TEST_ASSERT(b->pos == 10, "seek SET+10 set wrong position");
47 158
159 UCX_TEST_END
160
161 ucx_buffer_free(b);
162 }
163
164 UCX_TEST(test_ucx_buffer_seek_cur) {
165 UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT);
166 b->size = 16;
167 int r;
168
169 UCX_TEST_BEGIN
170
171 b->pos = 7;
48 r = ucx_buffer_seek(b, 5, SEEK_CUR); 172 r = ucx_buffer_seek(b, 5, SEEK_CUR);
49 UCX_TEST_ASSERT(r == 0, "seek CUR+5 failed"); 173 UCX_TEST_ASSERT(r == 0, "seek CUR+5 failed");
50 UCX_TEST_ASSERT(b->pos == 10, "seek CUR+5 set wrong position"); 174 UCX_TEST_ASSERT(b->pos == 12, "seek CUR+5 set wrong position");
51 175
52 r = ucx_buffer_seek(b, 10, SEEK_CUR); 176 UCX_TEST_END
53 UCX_TEST_ASSERT(r != 0, "seek CUR beyond bounds shall fail"); 177
54 UCX_TEST_ASSERT(b->pos == 10, 178 ucx_buffer_free(b);
55 "failed seek shall leave pos unchanged"); 179 }
56 180
181 UCX_TEST(test_ucx_buffer_seek_end) {
182 UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT);
183 b->size = 16;
184 int r;
185
186 UCX_TEST_BEGIN
187
57 r = ucx_buffer_seek(b, -5, SEEK_END); 188 r = ucx_buffer_seek(b, -5, SEEK_END);
58 UCX_TEST_ASSERT(r == 0, "seek END-5 failed"); 189 UCX_TEST_ASSERT(r == 0, "seek END-5 failed");
59 UCX_TEST_ASSERT(b->pos == 11, "seek END-5 set wrong position"); 190 UCX_TEST_ASSERT(b->pos == 11, "seek END-5 set wrong position");
60 191
61 r = ucx_buffer_seek(b, -20, SEEK_END);
62 UCX_TEST_ASSERT(r != 0, "seek END beyond bounds shall fail");
63 UCX_TEST_ASSERT(b->pos == 11,
64 "failed seek shall leave pos unchanged");
65 192
66 UCX_TEST_END 193 UCX_TEST_END
67 194
68 ucx_buffer_free(b); 195 ucx_buffer_free(b);
69 } 196 }
75 UcxBuffer *b = ucx_buffer_new(buffer, 16, UCX_BUFFER_DEFAULT); 202 UcxBuffer *b = ucx_buffer_new(buffer, 16, UCX_BUFFER_DEFAULT);
76 b->size = b->capacity; 203 b->size = b->capacity;
77 204
78 UCX_TEST_BEGIN 205 UCX_TEST_BEGIN
79 206
80 ucx_buffer_putc(b, '0'); 207 ucx_buffer_seek(b, 0, SEEK_SET);
208 UCX_TEST_ASSERT(ucx_buffer_putc(b, '0'|~0xFF) == '0',
209 "putc shall return (arg & 0xFF)");
81 ucx_buffer_putc(b, '0'); 210 ucx_buffer_putc(b, '0');
82 ucx_buffer_putc(b, '0'); 211 ucx_buffer_putc(b, '0');
83 212
84 UCX_TEST_ASSERT(b->pos == 3, "pos wrong after first 3 puts"); 213 UCX_TEST_ASSERT(b->pos == 3, "pos wrong after first 3 puts");
85 ucx_buffer_seek(b, 10, SEEK_CUR); 214 ucx_buffer_seek(b, 10, SEEK_CUR);
87 ucx_buffer_putc(b, '0'); 216 ucx_buffer_putc(b, '0');
88 ucx_buffer_putc(b, '0'); 217 ucx_buffer_putc(b, '0');
89 ucx_buffer_putc(b, '0'); 218 ucx_buffer_putc(b, '0');
90 219
91 UCX_TEST_ASSERT(b->pos == 16, "pos wrong after last 3 puts"); 220 UCX_TEST_ASSERT(b->pos == 16, "pos wrong after last 3 puts");
92 UCX_TEST_ASSERT(ucx_buffer_eof(b), "eof not set"); 221
93 UCX_TEST_ASSERT(ucx_buffer_putc(b, 48) == EOF, 222 UCX_TEST_ASSERT(!memcmp(b->space, "000 000", 16),
94 "put shall return EOF when buffer is full"); 223 "buffer content wrong")
95 224 UCX_TEST_END
96 ucx_buffer_seek(b, 3, SEEK_SET); 225 ucx_buffer_free(b);
226 free(buffer);
227 }
228
229 UCX_TEST(test_ucx_buffer_putc_oob) {
230 UcxBuffer *b = ucx_buffer_new(NULL, 2, UCX_BUFFER_DEFAULT);
231
232 UCX_TEST_BEGIN
233 b->pos = b->size = b->capacity = 1;
234 b->space[1] = 'X';
235
236 UCX_TEST_ASSERT(ucx_buffer_putc(b, 48) == EOF, "put shall return EOF "
237 "when buffer is full and auto extend is disabled");
238 UCX_TEST_ASSERT(!memcmp(b->space, "\0X", 2),
239 "wrong buffer content after failed putc");
240
241 UCX_TEST_END
242
243 ucx_buffer_free(b);
244 }
245
246
247 UCX_TEST(test_ucx_buffer_putc_ae) {
248 UcxBuffer *b = ucx_buffer_new(NULL, 2, UCX_BUFFER_AUTOEXTEND);
249 ucx_buffer_putc(b, '0');
250 ucx_buffer_putc(b, '1');
251
252 UCX_TEST_BEGIN
253
254 UCX_TEST_ASSERT(b->pos == 2, "pos wrong after first 2 puts");
255 UCX_TEST_ASSERT(b->size == 2, "size wrong after first 2 puts");
256 UCX_TEST_ASSERT(b->capacity == 2, "buffer erroneously extended");
257 UCX_TEST_ASSERT(!memcmp(b->space,"01", 2), "wrong content");
258
259 UCX_TEST_END
260
261 ucx_buffer_free(b);
262 }
263
264 UCX_TEST(test_ucx_buffer_putc_oobae) {
265 UcxBuffer *b = ucx_buffer_new(NULL, 2, UCX_BUFFER_AUTOEXTEND);
266 ucx_buffer_putc(b, '0');
267 ucx_buffer_putc(b, '1');
268
269 UCX_TEST_BEGIN
270
271 ucx_buffer_putc(b, 'a');
272
273 UCX_TEST_ASSERT(b->pos == 3, "pos wrong after put");
274 UCX_TEST_ASSERT(b->capacity == 4, "buffer not properly extended");
275 UCX_TEST_ASSERT(b->size == 3, "wrong buffer size");
276
277 UCX_TEST_ASSERT(!memcmp(b->space,"01a\0", 4), "wrong content");
278
279 UCX_TEST_END
280
281 ucx_buffer_free(b);
282 }
283
284 UCX_TEST(test_ucx_buffer_putc_size) {
285 UcxBuffer *b = ucx_buffer_new(NULL, 4, UCX_BUFFER_DEFAULT);
286
287 UCX_TEST_BEGIN
288
289 UCX_TEST_ASSERT(b->size == 0, "wrong initial size");
97 ucx_buffer_putc(b, 'a'); 290 ucx_buffer_putc(b, 'a');
98 ucx_buffer_putc(b, 'b'); 291 ucx_buffer_putc(b, 'b');
99 ucx_buffer_putc(b, 'c'); 292 ucx_buffer_putc(b, 'c');
100 293 UCX_TEST_ASSERT(b->size == 3, "size does not increase");
101 UCX_TEST_ASSERT(b->size == 16, "wrong size after seek and puts"); 294 ucx_buffer_seek(b, 1, SEEK_SET);
102 UCX_TEST_ASSERT(memcmp(buffer, "000abc 000", 16) == 0, 295 ucx_buffer_putc(b, 'd');
103 "buffer contains incorrect content"); 296 UCX_TEST_ASSERT(b->size == 3, "size shall not decrease");
104 297 UCX_TEST_ASSERT(b->pos == 2, "wrong position after seek and putc");
105 UCX_TEST_END 298
106 299 UCX_TEST_END
107 ucx_buffer_free(b); 300
108 free(buffer);
109 }
110
111 UCX_TEST(test_ucx_buffer_putc_ax) {
112 UcxBuffer *b = ucx_buffer_new(NULL, 2, UCX_BUFFER_AUTOEXTEND);
113
114 UCX_TEST_BEGIN
115
116 ucx_buffer_putc(b, '0');
117 ucx_buffer_putc(b, '1');
118
119 UCX_TEST_ASSERT(b->pos == 2, "pos wrong after first 2 puts");
120 UCX_TEST_ASSERT(b->capacity == 2, "buffer erroneously extended");
121
122 ucx_buffer_putc(b, 'a');
123
124 UCX_TEST_ASSERT(b->pos == 3, "pos wrong after 1 put");
125 UCX_TEST_ASSERT(b->capacity == 4, "buffer not properly extended");
126 UCX_TEST_ASSERT(b->size == 3, "wrong buffer size");
127
128 UCX_TEST_ASSERT(b->space[2] == 'a', "wrong content");
129
130 UCX_TEST_END
131
132 ucx_buffer_free(b); 301 ucx_buffer_free(b);
133 } 302 }
134 303
135 UCX_TEST(test_ucx_buffer_getc) { 304 UCX_TEST(test_ucx_buffer_getc) {
136 char *buffer = (char*) malloc(16); 305 char *buffer = (char*) malloc(16);

mercurial