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