test/buffer_tests.c

changeset 63
1d3500806565
parent 62
6814aea8462d
child 64
16590c9c497c
equal deleted inserted replaced
62:6814aea8462d 63:1d3500806565
14 14
15 UCX_TEST_BEGIN 15 UCX_TEST_BEGIN
16 16
17 r = ucx_buffer_seek(b, 5, SEEK_SET); 17 r = ucx_buffer_seek(b, 5, SEEK_SET);
18 UCX_TEST_ASSERT(r == 0, "seek SET+5 failed"); 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"); 19 UCX_TEST_ASSERT(b->pos == 5, "seek SET+5 set wrong position");
20 20
21 r = ucx_buffer_seek(b, 20, SEEK_SET); 21 r = ucx_buffer_seek(b, 20, SEEK_SET);
22 UCX_TEST_ASSERT(r != 0, "seek beyond bounds shall fail"); 22 UCX_TEST_ASSERT(r != 0, "seek beyond bounds shall fail");
23 UCX_TEST_ASSERT(ucx_buffer_tell(b) == 5, 23 UCX_TEST_ASSERT(b->pos == 5,
24 "failed seek shall leave pos unchanged"); 24 "failed seek shall leave pos unchanged");
25 25
26 r = ucx_buffer_seek(b, 5, SEEK_CUR); 26 r = ucx_buffer_seek(b, 5, SEEK_CUR);
27 UCX_TEST_ASSERT(r == 0, "seek CUR+5 failed"); 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"); 28 UCX_TEST_ASSERT(b->pos == 10, "seek CUR+5 set wrong position");
29 29
30 r = ucx_buffer_seek(b, 10, SEEK_CUR); 30 r = ucx_buffer_seek(b, 10, SEEK_CUR);
31 UCX_TEST_ASSERT(r != 0, "seek CUR beyond bounds shall fail"); 31 UCX_TEST_ASSERT(r != 0, "seek CUR beyond bounds shall fail");
32 UCX_TEST_ASSERT(ucx_buffer_tell(b) == 10, 32 UCX_TEST_ASSERT(b->pos == 10,
33 "failed seek shall leave pos unchanged"); 33 "failed seek shall leave pos unchanged");
34 34
35 r = ucx_buffer_seek(b, -5, SEEK_END); 35 r = ucx_buffer_seek(b, -5, SEEK_END);
36 UCX_TEST_ASSERT(r == 0, "seek END-5 failed"); 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"); 37 UCX_TEST_ASSERT(b->pos == 2, "seek END-5 set wrong position");
38 38
39 r = ucx_buffer_seek(b, -10, SEEK_END); 39 r = ucx_buffer_seek(b, -10, SEEK_END);
40 UCX_TEST_ASSERT(r != 0, "seek END beyond bounds shall fail"); 40 UCX_TEST_ASSERT(r != 0, "seek END beyond bounds shall fail");
41 UCX_TEST_ASSERT(ucx_buffer_tell(b) == 2, 41 UCX_TEST_ASSERT(b->pos == 2,
42 "failed seek shall leave pos unchanged"); 42 "failed seek shall leave pos unchanged");
43 43
44 UCX_TEST_END 44 UCX_TEST_END
45 45
46 ucx_buffer_free(b); 46 ucx_buffer_free(b);
55 int r; 55 int r;
56 56
57 UCX_TEST_BEGIN 57 UCX_TEST_BEGIN
58 58
59 ucx_buffer_putc(b, 48); ucx_buffer_putc(b, 48); ucx_buffer_putc(b, 48); 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"); 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 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"); 63 UCX_TEST_ASSERT(b->pos == 16, "pos wrong after last 3 puts");
64 UCX_TEST_ASSERT(ucx_buffer_eof(b), "eof not set"); 64 UCX_TEST_ASSERT(ucx_buffer_eof(b), "eof not set");
65 UCX_TEST_ASSERT(ucx_buffer_putc(b, 48) == EOF, 65 UCX_TEST_ASSERT(ucx_buffer_putc(b, 48) == EOF,
66 "put shall return EOF when buffer is full"); 66 "put shall return EOF when buffer is full");
67 UCX_TEST_ASSERT(memcmp(buffer, "000 000", 16) == 0, 67 UCX_TEST_ASSERT(memcmp(buffer, "000 000", 16) == 0,
68 "buffer contains incorrect content"); 68 "buffer contains incorrect content");
85 85
86 UCX_TEST_BEGIN 86 UCX_TEST_BEGIN
87 87
88 char rb[16]; 88 char rb[16];
89 for (int i = 0 ; i < 16 ; i++) { 89 for (int i = 0 ; i < 16 ; i++) {
90 UCX_TEST_ASSERT(ucx_buffer_tell(b) == i, "pos wrong during read loop"); 90 UCX_TEST_ASSERT(b->pos == i, "pos wrong during read loop");
91 UCX_TEST_ASSERT(!ucx_buffer_eof(b), 91 UCX_TEST_ASSERT(!ucx_buffer_eof(b),
92 "EOF shall not be set during read loop"); 92 "EOF shall not be set during read loop");
93 rb[i] = ucx_buffer_getc(b); 93 rb[i] = ucx_buffer_getc(b);
94 } 94 }
95 UCX_TEST_ASSERT(ucx_buffer_tell(b) == 16, "pos wrong after read loop"); 95 UCX_TEST_ASSERT(b->pos == 16, "pos wrong after read loop");
96 UCX_TEST_ASSERT(ucx_buffer_eof(b), "EOF not set"); 96 UCX_TEST_ASSERT(ucx_buffer_eof(b), "EOF not set");
97 UCX_TEST_ASSERT(memcmp(rb, " 01234567", 16) == 0, 97 UCX_TEST_ASSERT(memcmp(rb, " 01234567", 16) == 0,
98 "read data incorrect"); 98 "read data incorrect");
99 99
100 UCX_TEST_END 100 UCX_TEST_END
131 char* threebytestring = " t h r e e "; 131 char* threebytestring = " t h r e e ";
132 memset(buffer, 49, 16); 132 memset(buffer, 49, 16);
133 ucx_buffer_seek(b, 0, SEEK_SET); 133 ucx_buffer_seek(b, 0, SEEK_SET);
134 r = ucx_buffer_write(threebytestring, 3, 6, b); 134 r = ucx_buffer_write(threebytestring, 3, 6, b);
135 UCX_TEST_ASSERT(r == 15, "three byte string not correctly trimed"); 135 UCX_TEST_ASSERT(r == 15, "three byte string not correctly trimed");
136 UCX_TEST_ASSERT(ucx_buffer_tell(b) == 15, 136 UCX_TEST_ASSERT(b->pos == 15,
137 "position after write of three byte string incorrect"); 137 "position after write of three byte string incorrect");
138 UCX_TEST_ASSERT(!ucx_buffer_eof(b), "eof shall not be set"); 138 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, 139 UCX_TEST_ASSERT(memcmp(buffer, " t h r e e1", 16) == 0,
140 "bufer is incorrect after three byte string has been written"); 140 "bufer is incorrect after three byte string has been written");
141 141
192 192
193 UcxBuffer *src = ucx_buffer_new(buffer, 16, UCX_BUFFER_AUTOFREE), 193 UcxBuffer *src = ucx_buffer_new(buffer, 16, UCX_BUFFER_AUTOFREE),
194 *dst = ucx_buffer_extract(src, 5, 5, UCX_BUFFER_DEFAULT); 194 *dst = ucx_buffer_extract(src, 5, 5, UCX_BUFFER_DEFAULT);
195 195
196 UCX_TEST_BEGIN 196 UCX_TEST_BEGIN
197 UCX_TEST_ASSERT(ucx_buffer_testflags(dst, UCX_BUFFER_AUTOFREE), 197 UCX_TEST_ASSERT((dst->flags & UCX_BUFFER_AUTOFREE) == UCX_BUFFER_AUTOFREE,
198 "autofree flag shall be enforced"); 198 "autofree flag shall be enforced");
199 UCX_TEST_ASSERT(ucx_buffer_size(dst) == 5, "wrong size for new buffer"); 199 UCX_TEST_ASSERT(dst->size == 5, "wrong size for new buffer");
200 char rb[5]; 200 char rb[5];
201 ucx_buffer_read(rb, 1, 5, dst); 201 ucx_buffer_read(rb, 1, 5, dst);
202 UCX_TEST_ASSERT(memcmp(rb, "is a ", 5) == 0, 202 UCX_TEST_ASSERT(memcmp(rb, "is a ", 5) == 0,
203 "new buffer has incorrect content"); 203 "new buffer has incorrect content");
204 204

mercurial