Thu, 28 Feb 2013 08:50:24 +0100
added license and copyright notice to all files
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2013 Olaf Wintermann. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
29 #include "buffer_tests.h"
31 UCX_TEST_IMPLEMENT(test_ucx_buffer_seektell) {
32 UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT);
33 b->size = 16; // less than capacity
34 int r;
36 UCX_TEST_BEGIN
38 r = ucx_buffer_seek(b, 5, SEEK_SET);
39 UCX_TEST_ASSERT(r == 0, "seek SET+5 failed");
40 UCX_TEST_ASSERT(b->pos == 5, "seek SET+5 set wrong position");
42 r = ucx_buffer_seek(b, 20, SEEK_SET);
43 UCX_TEST_ASSERT(r != 0, "seek beyond bounds shall fail");
44 UCX_TEST_ASSERT(b->pos == 5,
45 "failed seek shall leave pos unchanged");
47 r = ucx_buffer_seek(b, 5, SEEK_CUR);
48 UCX_TEST_ASSERT(r == 0, "seek CUR+5 failed");
49 UCX_TEST_ASSERT(b->pos == 10, "seek CUR+5 set wrong position");
51 r = ucx_buffer_seek(b, 10, SEEK_CUR);
52 UCX_TEST_ASSERT(r != 0, "seek CUR beyond bounds shall fail");
53 UCX_TEST_ASSERT(b->pos == 10,
54 "failed seek shall leave pos unchanged");
56 r = ucx_buffer_seek(b, -5, SEEK_END);
57 UCX_TEST_ASSERT(r == 0, "seek END-5 failed");
58 UCX_TEST_ASSERT(b->pos == 11, "seek END-5 set wrong position");
60 r = ucx_buffer_seek(b, -20, SEEK_END);
61 UCX_TEST_ASSERT(r != 0, "seek END beyond bounds shall fail");
62 UCX_TEST_ASSERT(b->pos == 11,
63 "failed seek shall leave pos unchanged");
65 UCX_TEST_END
67 ucx_buffer_free(b);
68 }
70 UCX_TEST_IMPLEMENT(test_ucx_buffer_putc) {
71 char *buffer = (char*) malloc(16);
72 memset(buffer, 32, 16);
74 UcxBuffer *b = ucx_buffer_new(buffer, 16, UCX_BUFFER_DEFAULT);
75 b->size = b->capacity;
77 UCX_TEST_BEGIN
79 ucx_buffer_putc(b, '0');
80 ucx_buffer_putc(b, '0');
81 ucx_buffer_putc(b, '0');
83 UCX_TEST_ASSERT(b->pos == 3, "pos wrong after first 3 puts");
84 ucx_buffer_seek(b, 10, SEEK_CUR);
86 ucx_buffer_putc(b, '0');
87 ucx_buffer_putc(b, '0');
88 ucx_buffer_putc(b, '0');
90 UCX_TEST_ASSERT(b->pos == 16, "pos wrong after last 3 puts");
91 UCX_TEST_ASSERT(ucx_buffer_eof(b), "eof not set");
92 UCX_TEST_ASSERT(ucx_buffer_putc(b, 48) == EOF,
93 "put shall return EOF when buffer is full");
95 ucx_buffer_seek(b, 3, SEEK_SET);
96 ucx_buffer_putc(b, 'a');
97 ucx_buffer_putc(b, 'b');
98 ucx_buffer_putc(b, 'c');
100 UCX_TEST_ASSERT(b->size == 16, "wrong size after seek and puts");
101 UCX_TEST_ASSERT(memcmp(buffer, "000abc 000", 16) == 0,
102 "buffer contains incorrect content");
104 UCX_TEST_END
106 ucx_buffer_free(b);
107 free(buffer);
108 }
110 UCX_TEST_IMPLEMENT(test_ucx_buffer_putc_ax) {
111 UcxBuffer *b = ucx_buffer_new(NULL, 2, UCX_BUFFER_AUTOEXTEND);
113 UCX_TEST_BEGIN
115 ucx_buffer_putc(b, '0');
116 ucx_buffer_putc(b, '1');
118 UCX_TEST_ASSERT(b->pos == 2, "pos wrong after first 2 puts");
119 UCX_TEST_ASSERT(b->capacity == 2, "buffer erroneously extended");
121 ucx_buffer_putc(b, 'a');
123 UCX_TEST_ASSERT(b->pos == 3, "pos wrong after 1 put");
124 UCX_TEST_ASSERT(b->capacity == 4, "buffer not properly extended");
125 UCX_TEST_ASSERT(b->size == 3, "wrong buffer size");
127 UCX_TEST_ASSERT(b->space[2] == 'a', "wrong content");
129 UCX_TEST_END
132 }
134 UCX_TEST_IMPLEMENT(test_ucx_buffer_getc) {
135 char *buffer = (char*) malloc(16);
136 memset(buffer, 32, 8);
137 for (int i = 8; i < 16 ; i++) {
138 buffer[i] = 40+i;
139 }
141 UcxBuffer *b = ucx_buffer_new(buffer, 16, UCX_BUFFER_DEFAULT);
142 b->size = b->capacity;
144 UCX_TEST_BEGIN
146 char rb[16];
147 for (size_t i = 0 ; i < 16 ; i++) {
148 UCX_TEST_ASSERT(b->pos == i, "pos wrong during read loop");
149 UCX_TEST_ASSERT(!ucx_buffer_eof(b),
150 "EOF shall not be set during read loop");
151 rb[i] = ucx_buffer_getc(b);
152 }
153 UCX_TEST_ASSERT(b->pos == 16, "pos wrong after read loop");
154 UCX_TEST_ASSERT(ucx_buffer_eof(b), "EOF not set");
155 UCX_TEST_ASSERT(memcmp(rb, " 01234567", 16) == 0,
156 "read data incorrect");
158 UCX_TEST_END
160 ucx_buffer_free(b);
161 free(buffer);
162 }
164 UCX_TEST_IMPLEMENT(test_ucx_buffer_write) {
165 char *buffer = (char*) malloc(16);
166 memset(buffer, 32, 8);
167 for (int i = 8; i < 16 ; i++) {
168 buffer[i] = 40+i;
169 }
171 UcxBuffer *b = ucx_buffer_new(buffer, 16, UCX_BUFFER_DEFAULT);
172 int r;
174 UCX_TEST_BEGIN
176 const char* teststring = "this is way too much";
177 r = ucx_buffer_write((void*)teststring, 1, 20, b);
178 UCX_TEST_ASSERT(r == 16, "string not correctly trimed");
179 UCX_TEST_ASSERT(memcmp(buffer, teststring, 16) == 0,
180 "buffer data incorrect");
181 UCX_TEST_ASSERT(ucx_buffer_eof(b), "eof shall be set");
183 ucx_buffer_seek(b, 8, SEEK_SET);
184 r = ucx_buffer_write((void*)"not", 1, 3, b);
185 UCX_TEST_ASSERT(r == 3, "three bytes should be replace");
186 UCX_TEST_ASSERT(memcmp(buffer, "this is not too much", 16) == 0,
187 "modified buffer is incorrect");
189 const char* threebytestring = " t h r e e ";
190 memset(buffer, 49, 16);
191 ucx_buffer_seek(b, 0, SEEK_SET);
192 r = ucx_buffer_write((void*)threebytestring, 3, 6, b);
193 UCX_TEST_ASSERT(r == 5, "three byte string not correctly trimed");
194 UCX_TEST_ASSERT(b->pos == 15,
195 "position after write of three byte string incorrect");
196 UCX_TEST_ASSERT(!ucx_buffer_eof(b), "eof shall not be set");
197 UCX_TEST_ASSERT(memcmp(buffer, " t h r e e1", 16) == 0,
198 "bufer is incorrect after three byte string has been written");
200 UCX_TEST_END
202 ucx_buffer_free(b);
203 free(buffer);
204 }
206 UCX_TEST_IMPLEMENT(test_ucx_buffer_write_ax) {
207 char *buffer = (char*) malloc(16);
209 UcxBuffer *b = ucx_buffer_new(buffer, 16,
210 UCX_BUFFER_AUTOEXTEND | UCX_BUFFER_AUTOFREE);
211 int r;
213 UCX_TEST_BEGIN
215 const char* teststring = "this is way too much";
216 r = ucx_buffer_write((void*)teststring, 1, 20, b);
217 buffer = (char*) b->space; /*autoextend enabled, we MUST retrieve pointer*/
218 UCX_TEST_ASSERT(r == 20, "not all characters written");
219 UCX_TEST_ASSERT(b->capacity == 32, "buffer not properly extended");
220 UCX_TEST_ASSERT(b->pos == 20, "position incorrect");
221 UCX_TEST_ASSERT(memcmp(buffer,
222 "this is way too much\0\0\0\0\0\0\0\0\0\0\0\0", 32) == 0,
223 "incorrect buffer content");
225 UCX_TEST_END
227 ucx_buffer_free(b);
228 }
230 UCX_TEST_IMPLEMENT(test_ucx_buffer_read) {
231 UcxBuffer *b = ucx_buffer_new(NULL, 8, UCX_BUFFER_AUTOFREE);
233 char buf[32];
234 memset(buf, 'X', 32);
235 int r;
237 UCX_TEST_BEGIN
239 ucx_buffer_write("01234567", 1, 8, b);
240 UCX_TEST_ASSERT(b->pos == 8, "buffer not correctly filled");
241 b->pos = 0;
243 r = ucx_buffer_read(buf, 1, 2, b);
244 UCX_TEST_ASSERT(r == 2, "wrong number of bytes read");
245 UCX_TEST_ASSERT(buf[0] == '0' && buf[1] == '1' && buf[2] == 'X',
246 "buffer incorrect after first read");
248 r = ucx_buffer_read(buf + 2, 1, 8, b);
249 UCX_TEST_ASSERT(r == 6, "wrong number of bytes read(2)");
250 UCX_TEST_ASSERT(memcmp(buf, "01234567XX", 10) == 0,
251 "buffer incorrect after second read");
253 memset(buf, 'X', 32);
254 ucx_buffer_seek(b, 0, SEEK_SET);
255 r = ucx_buffer_read(buf, 3, 3, b);
257 UCX_TEST_ASSERT(r == 2, "wrong number of blocks read");
258 UCX_TEST_ASSERT(memcmp(buf, "012345XX", 8) == 0,
259 "buffer incorrect after three byte read");
262 UCX_TEST_END
265 }
267 UCX_TEST_IMPLEMENT(test_ucx_buffer_extract) {
268 char *buffer = (char*) malloc(16);
269 strcpy(buffer, "this is a test!");
271 UcxBuffer *src = ucx_buffer_new(buffer, 16, UCX_BUFFER_AUTOFREE);
272 src->size = 15;
273 UcxBuffer *dst = ucx_buffer_extract(src, 5, 5, UCX_BUFFER_DEFAULT);
275 UCX_TEST_BEGIN
276 UCX_TEST_ASSERT(dst != NULL, "ucx_buffer_extract returned NULL");
278 UCX_TEST_ASSERT((dst->flags & UCX_BUFFER_AUTOFREE) == UCX_BUFFER_AUTOFREE,
279 "autofree flag shall be enforced");
280 UCX_TEST_ASSERT(dst->size == 5, "wrong size for new buffer");
281 char rb[5];
282 ucx_buffer_read(rb, 1, 5, dst);
283 UCX_TEST_ASSERT(memcmp(rb, "is a ", 5) == 0,
284 "new buffer has incorrect content");
286 UCX_TEST_ASSERT(ucx_buffer_extract(dst, 3, 3, UCX_BUFFER_DEFAULT) == NULL,
287 "extract shall fail on invalid bounds");
289 UCX_TEST_END
291 ucx_buffer_free(dst);
292 ucx_buffer_free(src);
293 }
295 UCX_TEST_IMPLEMENT(test_ucx_buffer_generic_copy) {
296 UcxBuffer *b1 = ucx_buffer_new(NULL, 64, UCX_BUFFER_DEFAULT);
297 UcxBuffer *b2 = ucx_buffer_new(NULL, 2, UCX_BUFFER_AUTOEXTEND);
299 UCX_TEST_BEGIN
301 ucx_buffer_write("01234567", 1, 8, b1);
302 ucx_buffer_write("abcdefgh", 1, 8, b1);
303 UCX_TEST_ASSERT(b1->size == 16, "failed to fill buffer b1");
304 ucx_buffer_seek(b1, 0, SEEK_SET);
306 size_t ncp = ucx_buffer_copy(b1, b2, ucx_buffer_read, ucx_buffer_write);
307 UCX_TEST_ASSERT(ncp == 16, "wrong number of copied bytes");
308 UCX_TEST_ASSERT(b2->size == 16, "b2 has wrong size");
309 UCX_TEST_ASSERT(memcmp(b1->space, b2->space, 16) == 0,
310 "b1 and b2 have not the same content");
312 memset(b2->space, 0, b2->capacity);
313 b2->pos = 0;
314 b2->size = 0;
315 ucx_buffer_seek(b1, 0, SEEK_SET);
317 FILE *file = tmpfile();
318 UCX_TEST_ASSERT(file, "test file cannot be opened, test aborted");
320 ncp = ucx_buffer_copy(b1, file, ucx_buffer_read, fwrite);
321 UCX_TEST_ASSERT(ncp == 16, "copied wrong number of bytes to file");
323 fseek(file, 0, SEEK_SET);
325 ncp = ucx_buffer_copy(file, b2, fread, ucx_buffer_write);
326 UCX_TEST_ASSERT(ncp == 16, "copied wrong number of bytes from file");
328 UCX_TEST_ASSERT(memcmp(b1->space, b2->space, 16) == 0,
329 "b1 and b2 content mismatch");
331 fclose(file);
334 UCX_TEST_END
336 ucx_buffer_free(b1);
337 ucx_buffer_free(b2);
338 }