32 UCX_TEST_ASSERT(b->pos == 10, |
29 UCX_TEST_ASSERT(b->pos == 10, |
33 "failed seek shall leave pos unchanged"); |
30 "failed seek shall leave pos unchanged"); |
34 |
31 |
35 r = ucx_buffer_seek(b, -5, SEEK_END); |
32 r = ucx_buffer_seek(b, -5, SEEK_END); |
36 UCX_TEST_ASSERT(r == 0, "seek END-5 failed"); |
33 UCX_TEST_ASSERT(r == 0, "seek END-5 failed"); |
37 UCX_TEST_ASSERT(b->pos == 2, "seek END-5 set wrong position"); |
34 UCX_TEST_ASSERT(b->pos == 11, "seek END-5 set wrong position"); |
38 |
35 |
39 r = ucx_buffer_seek(b, -10, SEEK_END); |
36 r = ucx_buffer_seek(b, -20, SEEK_END); |
40 UCX_TEST_ASSERT(r != 0, "seek END beyond bounds shall fail"); |
37 UCX_TEST_ASSERT(r != 0, "seek END beyond bounds shall fail"); |
41 UCX_TEST_ASSERT(b->pos == 2, |
38 UCX_TEST_ASSERT(b->pos == 11, |
42 "failed seek shall leave pos unchanged"); |
39 "failed seek shall leave pos unchanged"); |
43 |
40 |
44 UCX_TEST_END |
41 UCX_TEST_END |
45 |
42 |
46 ucx_buffer_free(b); |
43 ucx_buffer_free(b); |
47 free(buffer); |
|
48 } |
44 } |
49 |
45 |
50 UCX_TEST_IMPLEMENT(test_ucx_buffer_putc) { |
46 UCX_TEST_IMPLEMENT(test_ucx_buffer_putc) { |
51 char *buffer = (char*) malloc(16); |
47 char *buffer = (char*) malloc(16); |
52 memset(buffer, 32, 16); |
48 memset(buffer, 32, 16); |
53 |
49 |
54 UcxBuffer *b = ucx_buffer_new(buffer, 16, UCX_BUFFER_DEFAULT); |
50 UcxBuffer *b = ucx_buffer_new(buffer, 16, UCX_BUFFER_DEFAULT); |
55 int r; |
51 b->size = b->capacity; |
56 |
52 int r; |
57 UCX_TEST_BEGIN |
53 |
58 |
54 UCX_TEST_BEGIN |
59 ucx_buffer_putc(b, 48); ucx_buffer_putc(b, 48); ucx_buffer_putc(b, 48); |
55 |
|
56 ucx_buffer_putc(b, '0'); |
|
57 ucx_buffer_putc(b, '0'); |
|
58 ucx_buffer_putc(b, '0'); |
|
59 |
60 UCX_TEST_ASSERT(b->pos == 3, "pos wrong after first 3 puts"); |
60 UCX_TEST_ASSERT(b->pos == 3, "pos wrong after first 3 puts"); |
61 ucx_buffer_seek(b, 10, SEEK_CUR); |
61 ucx_buffer_seek(b, 10, SEEK_CUR); |
62 ucx_buffer_putc(b, 48); ucx_buffer_putc(b, 48); ucx_buffer_putc(b, 48); |
62 |
|
63 ucx_buffer_putc(b, '0'); |
|
64 ucx_buffer_putc(b, '0'); |
|
65 ucx_buffer_putc(b, '0'); |
|
66 |
63 UCX_TEST_ASSERT(b->pos == 16, "pos wrong after last 3 puts"); |
67 UCX_TEST_ASSERT(b->pos == 16, "pos wrong after last 3 puts"); |
64 UCX_TEST_ASSERT(ucx_buffer_eof(b), "eof not set"); |
68 UCX_TEST_ASSERT(ucx_buffer_eof(b), "eof not set"); |
65 UCX_TEST_ASSERT(ucx_buffer_putc(b, 48) == EOF, |
69 UCX_TEST_ASSERT(ucx_buffer_putc(b, 48) == EOF, |
66 "put shall return EOF when buffer is full"); |
70 "put shall return EOF when buffer is full"); |
67 UCX_TEST_ASSERT(memcmp(buffer, "000 000", 16) == 0, |
71 |
|
72 ucx_buffer_seek(b, 3, SEEK_SET); |
|
73 ucx_buffer_putc(b, 'a'); |
|
74 ucx_buffer_putc(b, 'b'); |
|
75 ucx_buffer_putc(b, 'c'); |
|
76 |
|
77 UCX_TEST_ASSERT(b->size == 16, "wrong size after seek and puts"); |
|
78 UCX_TEST_ASSERT(memcmp(buffer, "000abc 000", 16) == 0, |
68 "buffer contains incorrect content"); |
79 "buffer contains incorrect content"); |
69 |
80 |
70 UCX_TEST_END |
81 UCX_TEST_END |
71 |
82 |
72 ucx_buffer_free(b); |
83 ucx_buffer_free(b); |
73 free(buffer); |
84 free(buffer); |
|
85 } |
|
86 |
|
87 UCX_TEST_IMPLEMENT(test_ucx_buffer_putc_ax) { |
|
88 UcxBuffer *b = ucx_buffer_new(NULL, 2, UCX_BUFFER_AUTOEXTEND); |
|
89 |
|
90 UCX_TEST_BEGIN |
|
91 |
|
92 ucx_buffer_putc(b, '0'); |
|
93 ucx_buffer_putc(b, '1'); |
|
94 |
|
95 UCX_TEST_ASSERT(b->pos == 2, "pos wrong after first 2 puts"); |
|
96 UCX_TEST_ASSERT(b->capacity == 2, "buffer erroneously extended"); |
|
97 |
|
98 ucx_buffer_putc(b, 'a'); |
|
99 |
|
100 UCX_TEST_ASSERT(b->pos == 3, "pos wrong after 1 put"); |
|
101 UCX_TEST_ASSERT(b->capacity == 4, "buffer not properly extended"); |
|
102 UCX_TEST_ASSERT(b->size == 3, "wrong buffer size"); |
|
103 |
|
104 UCX_TEST_ASSERT(b->space[2] == 'a', "wrong content"); |
|
105 |
|
106 UCX_TEST_END |
|
107 |
|
108 |
74 } |
109 } |
75 |
110 |
76 UCX_TEST_IMPLEMENT(test_ucx_buffer_getc) { |
111 UCX_TEST_IMPLEMENT(test_ucx_buffer_getc) { |
77 char *buffer = (char*) malloc(16); |
112 char *buffer = (char*) malloc(16); |
78 memset(buffer, 32, 8); |
113 memset(buffer, 32, 8); |
79 for (int i = 8; i < 16 ; i++) { |
114 for (int i = 8; i < 16 ; i++) { |
80 buffer[i] = 40+i; |
115 buffer[i] = 40+i; |
81 } |
116 } |
82 |
117 |
83 UcxBuffer *b = ucx_buffer_new(buffer, 16, UCX_BUFFER_DEFAULT); |
118 UcxBuffer *b = ucx_buffer_new(buffer, 16, UCX_BUFFER_DEFAULT); |
|
119 b->size = b->capacity; |
84 int r; |
120 int r; |
85 |
121 |
86 UCX_TEST_BEGIN |
122 UCX_TEST_BEGIN |
87 |
123 |
88 char rb[16]; |
124 char rb[16]; |
130 |
166 |
131 const char* threebytestring = " t h r e e "; |
167 const char* threebytestring = " t h r e e "; |
132 memset(buffer, 49, 16); |
168 memset(buffer, 49, 16); |
133 ucx_buffer_seek(b, 0, SEEK_SET); |
169 ucx_buffer_seek(b, 0, SEEK_SET); |
134 r = ucx_buffer_write((void*)threebytestring, 3, 6, b); |
170 r = ucx_buffer_write((void*)threebytestring, 3, 6, b); |
135 UCX_TEST_ASSERT(r == 15, "three byte string not correctly trimed"); |
171 UCX_TEST_ASSERT(r == 5, "three byte string not correctly trimed"); |
136 UCX_TEST_ASSERT(b->pos == 15, |
172 UCX_TEST_ASSERT(b->pos == 15, |
137 "position after write of three byte string incorrect"); |
173 "position after write of three byte string incorrect"); |
138 UCX_TEST_ASSERT(!ucx_buffer_eof(b), "eof shall not be set"); |
174 UCX_TEST_ASSERT(!ucx_buffer_eof(b), "eof shall not be set"); |
139 UCX_TEST_ASSERT(memcmp(buffer, " t h r e e1", 16) == 0, |
175 UCX_TEST_ASSERT(memcmp(buffer, " t h r e e1", 16) == 0, |
140 "bufer is incorrect after three byte string has been written"); |
176 "bufer is incorrect after three byte string has been written"); |
156 |
192 |
157 const char* teststring = "this is way too much"; |
193 const char* teststring = "this is way too much"; |
158 r = ucx_buffer_write((void*)teststring, 1, 20, b); |
194 r = ucx_buffer_write((void*)teststring, 1, 20, b); |
159 buffer = (char*) b->space; /*autoextend enabled, we MUST retrieve pointer*/ |
195 buffer = (char*) b->space; /*autoextend enabled, we MUST retrieve pointer*/ |
160 UCX_TEST_ASSERT(r == 20, "not all characters written"); |
196 UCX_TEST_ASSERT(r == 20, "not all characters written"); |
161 UCX_TEST_ASSERT(b->size == 32, "buffer not properly extended"); |
197 UCX_TEST_ASSERT(b->capacity == 32, "buffer not properly extended"); |
162 UCX_TEST_ASSERT(b->pos == 20, "position incorrect"); |
198 UCX_TEST_ASSERT(b->pos == 20, "position incorrect"); |
163 UCX_TEST_ASSERT(memcmp(buffer, |
199 UCX_TEST_ASSERT(memcmp(buffer, |
164 "this is way too much\0\0\0\0\0\0\0\0\0\0\0\0", 32) == 0, |
200 "this is way too much\0\0\0\0\0\0\0\0\0\0\0\0", 32) == 0, |
165 "incorrect buffer content"); |
201 "incorrect buffer content"); |
166 |
202 |
168 |
204 |
169 ucx_buffer_free(b); |
205 ucx_buffer_free(b); |
170 } |
206 } |
171 |
207 |
172 UCX_TEST_IMPLEMENT(test_ucx_buffer_read) { |
208 UCX_TEST_IMPLEMENT(test_ucx_buffer_read) { |
173 char *buffer = (char*) malloc(16); |
209 UcxBuffer *b = ucx_buffer_new(NULL, 8, UCX_BUFFER_AUTOFREE); |
174 memset(buffer, 56, 8); |
210 |
175 for (int i = 8; i < 16 ; i++) { |
211 char buf[32]; |
176 buffer[i] = 40+i; |
212 memset(buf, 'X', 32); |
177 } |
213 int r; |
178 |
214 |
179 UcxBuffer *b = ucx_buffer_new(buffer, 16, UCX_BUFFER_DEFAULT); |
215 UCX_TEST_BEGIN |
180 int r; |
216 |
181 |
217 ucx_buffer_write("01234567", 1, 8, b); |
182 UCX_TEST_BEGIN |
218 UCX_TEST_ASSERT(b->pos == 8, "buffer not correctly filled"); |
183 |
219 b->pos = 0; |
184 char rb[16]; |
220 |
185 memset(rb, 32, 16); |
221 r = ucx_buffer_read(buf, 1, 2, b); |
186 |
222 UCX_TEST_ASSERT(r == 2, "wrong number of bytes read"); |
187 ucx_buffer_seek(b, 8, SEEK_SET); |
223 UCX_TEST_ASSERT(buf[0] == '0' && buf[1] == '1' && buf[2] == 'X', |
188 r = ucx_buffer_read(rb, 1, 16, b); |
|
189 UCX_TEST_ASSERT(r == 8, "read did not stop at buffer end"); |
|
190 UCX_TEST_ASSERT(memcmp(rb, "01234567 ", 16) == 0, |
|
191 "buffer incorrect after first read"); |
224 "buffer incorrect after first read"); |
192 UCX_TEST_ASSERT(ucx_buffer_eof(b), "eof shall be set"); |
225 |
193 |
226 r = ucx_buffer_read(buf + 2, 1, 8, b); |
|
227 UCX_TEST_ASSERT(r == 6, "wrong number of bytes read(2)"); |
|
228 UCX_TEST_ASSERT(memcmp(buf, "01234567XX", 10) == 0, |
|
229 "buffer incorrect after second read"); |
|
230 |
|
231 memset(buf, 'X', 32); |
194 ucx_buffer_seek(b, 0, SEEK_SET); |
232 ucx_buffer_seek(b, 0, SEEK_SET); |
195 r = ucx_buffer_read(rb+8, 1, 8, b); |
233 r = ucx_buffer_read(buf, 3, 3, b); |
196 UCX_TEST_ASSERT(r == 8, "read did not read the specified amount of bytes"); |
234 |
197 UCX_TEST_ASSERT(memcmp(rb, "0123456788888888", 16) == 0, |
235 UCX_TEST_ASSERT(r == 2, "wrong number of blocks read"); |
198 "buffer incorrect after second read"); |
236 UCX_TEST_ASSERT(memcmp(buf, "012345XX", 8) == 0, |
199 |
237 "buffer incorrect after three byte read"); |
200 ucx_buffer_seek(b, 0, SEEK_SET); |
238 |
201 r = ucx_buffer_read(rb, 3, 6, b); |
239 |
202 UCX_TEST_ASSERT(r == 15, |
240 UCX_TEST_END |
203 "three byte read did not read the desired amount of bytes"); |
241 |
204 UCX_TEST_ASSERT(memcmp(rb, "8888888801234568", 16) == 0, |
242 |
205 "buffer incorrect after three byte read"); |
|
206 |
|
207 UCX_TEST_END |
|
208 |
|
209 ucx_buffer_free(b); |
|
210 free(buffer); |
|
211 } |
243 } |
212 |
244 |
213 UCX_TEST_IMPLEMENT(test_ucx_buffer_extract) { |
245 UCX_TEST_IMPLEMENT(test_ucx_buffer_extract) { |
214 char *buffer = (char*) malloc(16); |
246 char *buffer = (char*) malloc(16); |
215 strcpy(buffer, "this is a test!"); |
247 strcpy(buffer, "this is a test!"); |
216 |
248 |
217 UcxBuffer *src = ucx_buffer_new(buffer, 16, UCX_BUFFER_AUTOFREE), |
249 UcxBuffer *src = ucx_buffer_new(buffer, 16, UCX_BUFFER_AUTOFREE); |
218 *dst = ucx_buffer_extract(src, 5, 5, UCX_BUFFER_DEFAULT); |
250 src->size = 15; |
219 |
251 UcxBuffer *dst = ucx_buffer_extract(src, 5, 5, UCX_BUFFER_DEFAULT); |
220 UCX_TEST_BEGIN |
252 |
|
253 UCX_TEST_BEGIN |
|
254 UCX_TEST_ASSERT(dst != NULL, "ucx_buffer_extract returned NULL"); |
|
255 |
221 UCX_TEST_ASSERT((dst->flags & UCX_BUFFER_AUTOFREE) == UCX_BUFFER_AUTOFREE, |
256 UCX_TEST_ASSERT((dst->flags & UCX_BUFFER_AUTOFREE) == UCX_BUFFER_AUTOFREE, |
222 "autofree flag shall be enforced"); |
257 "autofree flag shall be enforced"); |
223 UCX_TEST_ASSERT(dst->size == 5, "wrong size for new buffer"); |
258 UCX_TEST_ASSERT(dst->size == 5, "wrong size for new buffer"); |
224 char rb[5]; |
259 char rb[5]; |
225 ucx_buffer_read(rb, 1, 5, dst); |
260 ucx_buffer_read(rb, 1, 5, dst); |
232 UCX_TEST_END |
267 UCX_TEST_END |
233 |
268 |
234 ucx_buffer_free(dst); |
269 ucx_buffer_free(dst); |
235 ucx_buffer_free(src); |
270 ucx_buffer_free(src); |
236 } |
271 } |
|
272 |
|
273 UCX_TEST_IMPLEMENT(test_ucx_buffer_generic_copy) { |
|
274 UcxBuffer *b1 = ucx_buffer_new(NULL, 64, UCX_BUFFER_DEFAULT); |
|
275 UcxBuffer *b2 = ucx_buffer_new(NULL, 2, UCX_BUFFER_AUTOEXTEND); |
|
276 |
|
277 UCX_TEST_BEGIN |
|
278 |
|
279 ucx_buffer_write("01234567", 1, 8, b1); |
|
280 ucx_buffer_write("abcdefgh", 1, 8, b1); |
|
281 UCX_TEST_ASSERT(b1->size == 16, "failed to fill buffer b1"); |
|
282 ucx_buffer_seek(b1, 0, SEEK_SET); |
|
283 |
|
284 size_t ncp = ucx_buffer_copy(b1, b2, ucx_buffer_read, ucx_buffer_write); |
|
285 UCX_TEST_ASSERT(ncp == 16, "wrong number of copied bytes"); |
|
286 UCX_TEST_ASSERT(b2->size == 16, "b2 has wrong size"); |
|
287 UCX_TEST_ASSERT(memcmp(b1->space, b2->space, 16) == 0, |
|
288 "b1 and b2 have not the same content"); |
|
289 |
|
290 memset(b2->space, 0, b2->capacity); |
|
291 b2->pos = 0; |
|
292 b2->size = 0; |
|
293 ucx_buffer_seek(b1, 0, SEEK_SET); |
|
294 |
|
295 FILE *file = tmpfile(); |
|
296 UCX_TEST_ASSERT(file, "test file cannot be opened, test aborted"); |
|
297 |
|
298 ncp = ucx_buffer_copy(b1, file, ucx_buffer_read, fwrite); |
|
299 UCX_TEST_ASSERT(ncp == 16, "copied wrong number of bytes to file"); |
|
300 |
|
301 fseek(file, 0, SEEK_SET); |
|
302 |
|
303 ncp = ucx_buffer_copy(file, b2, fread, ucx_buffer_write); |
|
304 UCX_TEST_ASSERT(ncp == 16, "copied wrong number of bytes from file"); |
|
305 |
|
306 UCX_TEST_ASSERT(memcmp(b1->space, b2->space, 16) == 0, |
|
307 "b1 and b2 content mismatch"); |
|
308 |
|
309 fclose(file); |
|
310 |
|
311 |
|
312 UCX_TEST_END |
|
313 |
|
314 ucx_buffer_free(b1); |
|
315 ucx_buffer_free(b2); |
|
316 } |