Mon, 05 May 2014 13:53:27 +0200
added allocator tests + started refactoring UcxBuffer tests (HINT: don't fix issues yet, complete tests first)
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@140 | 30 | #include "ucx/utils.h" |
universe@56 | 31 | |
universe@164 | 32 | /* |
universe@164 | 33 | * TODO: refactor tests |
universe@164 | 34 | * |
universe@164 | 35 | * ucx_buffer_extend |
universe@164 | 36 | * ucx_buffer_extract |
universe@164 | 37 | * ucx_buffer_free |
universe@164 | 38 | * ucx_buffer_getc |
universe@164 | 39 | * ucx_buffer_new |
universe@164 | 40 | * ucx_buffer_puts |
universe@164 | 41 | * ucx_buffer_read |
universe@164 | 42 | * ucx_buffer_write |
universe@164 | 43 | * |
universe@164 | 44 | */ |
universe@164 | 45 | |
universe@164 | 46 | UCX_TEST(test_ucx_buffer_eof) { |
universe@164 | 47 | char *test = "0123456789ABCDEF"; |
universe@164 | 48 | UcxBuffer *b = ucx_buffer_new(test, 16, UCX_BUFFER_DEFAULT); |
universe@164 | 49 | UCX_TEST_BEGIN |
universe@164 | 50 | b->pos = 9; b->size = 10; |
universe@164 | 51 | UCX_TEST_ASSERT(!ucx_buffer_eof(b), "false positive"); |
universe@164 | 52 | b->pos = 10; b->size = 10; |
universe@164 | 53 | UCX_TEST_ASSERT(ucx_buffer_eof(b), "pos == size should be EOF"); |
universe@164 | 54 | b->pos = 11; b->size = 10; |
universe@164 | 55 | UCX_TEST_ASSERT(ucx_buffer_eof(b), "false negative"); |
universe@164 | 56 | UCX_TEST_END |
universe@164 | 57 | } |
universe@164 | 58 | |
universe@164 | 59 | UCX_TEST(test_ucx_buffer_seek_overflow) { |
universe@164 | 60 | UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT); |
universe@164 | 61 | b->size = 32; |
universe@164 | 62 | int r; |
universe@164 | 63 | |
universe@164 | 64 | UCX_TEST_BEGIN |
universe@164 | 65 | size_t bigpos = SIZE_MAX - 5000; |
universe@164 | 66 | b->pos = bigpos; |
universe@164 | 67 | r = ucx_buffer_seek(b, 5016, SEEK_CUR); |
universe@164 | 68 | UCX_TEST_ASSERT(r != 0, "seek cur overflow"); |
universe@164 | 69 | UCX_TEST_ASSERT(b->pos == bigpos, |
universe@164 | 70 | "failed seek shall leave pos unchanged"); |
universe@164 | 71 | |
universe@164 | 72 | b->pos = 0; |
universe@164 | 73 | b->size = SIZE_MAX / 2 + 32; |
universe@164 | 74 | off_t bigoff = SIZE_MAX / 2 - 16; |
universe@164 | 75 | r = ucx_buffer_seek(b, -bigoff, SEEK_CUR); |
universe@164 | 76 | UCX_TEST_ASSERT(r != 0, "seek cur underflow"); |
universe@164 | 77 | UCX_TEST_ASSERT(b->pos == 0, |
universe@164 | 78 | "failed seek shall leave pos unchanged"); |
universe@164 | 79 | |
universe@164 | 80 | UCX_TEST_END |
universe@164 | 81 | |
universe@164 | 82 | ucx_buffer_free(b); |
universe@164 | 83 | } |
universe@164 | 84 | |
universe@164 | 85 | UCX_TEST(test_ucx_buffer_seek_invalid) { |
universe@164 | 86 | UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT); |
universe@164 | 87 | b->pos = 7; |
universe@164 | 88 | int r; |
universe@164 | 89 | |
universe@164 | 90 | UCX_TEST_BEGIN |
universe@164 | 91 | r = ucx_buffer_seek(b, 0, ~(SEEK_SET|SEEK_CUR|SEEK_END)); |
universe@164 | 92 | UCX_TEST_ASSERT(r != 0, "invalid whence shall fail"); |
universe@164 | 93 | UCX_TEST_ASSERT(b->pos == 7, |
universe@164 | 94 | "failed seek shall leave pos unchanged"); |
universe@164 | 95 | UCX_TEST_END |
universe@164 | 96 | |
universe@164 | 97 | ucx_buffer_free(b); |
universe@164 | 98 | } |
universe@164 | 99 | |
universe@164 | 100 | UCX_TEST(test_ucx_buffer_seek_oob) { |
olaf@76 | 101 | UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT); |
olaf@76 | 102 | b->size = 16; // less than capacity |
universe@164 | 103 | b->pos = 7; |
universe@164 | 104 | int r; |
universe@164 | 105 | |
universe@164 | 106 | UCX_TEST_BEGIN |
universe@164 | 107 | |
universe@164 | 108 | r = ucx_buffer_seek(b, -1, SEEK_SET); |
universe@164 | 109 | UCX_TEST_ASSERT(r != 0, "seek SET below bounds shall fail"); |
universe@164 | 110 | UCX_TEST_ASSERT(b->pos == 7, |
universe@164 | 111 | "failed seek shall leave pos unchanged"); |
universe@164 | 112 | |
universe@164 | 113 | r = ucx_buffer_seek(b, 16, SEEK_SET); |
universe@164 | 114 | UCX_TEST_ASSERT(r != 0, "seek SET above bounds shall fail"); |
universe@164 | 115 | UCX_TEST_ASSERT(b->pos == 7, |
universe@164 | 116 | "failed seek shall leave pos unchanged"); |
universe@164 | 117 | |
universe@164 | 118 | r = ucx_buffer_seek(b, -8, SEEK_CUR); |
universe@164 | 119 | UCX_TEST_ASSERT(r != 0, "seek CUR below bounds shall fail"); |
universe@164 | 120 | UCX_TEST_ASSERT(b->pos == 7, |
universe@164 | 121 | "failed seek shall leave pos unchanged"); |
universe@164 | 122 | |
universe@164 | 123 | r = ucx_buffer_seek(b, 9, SEEK_CUR); |
universe@164 | 124 | UCX_TEST_ASSERT(r != 0, "seek CUR above bounds shall fail"); |
universe@164 | 125 | UCX_TEST_ASSERT(b->pos == 7, |
universe@164 | 126 | "failed seek shall leave pos unchanged"); |
universe@164 | 127 | |
universe@164 | 128 | r = ucx_buffer_seek(b, -17, SEEK_END); |
universe@164 | 129 | UCX_TEST_ASSERT(r != 0, "seek END below bounds shall fail"); |
universe@164 | 130 | UCX_TEST_ASSERT(b->pos == 7, |
universe@164 | 131 | "failed seek shall leave pos unchanged"); |
universe@164 | 132 | |
universe@164 | 133 | r = ucx_buffer_seek(b, 1, SEEK_END); |
universe@164 | 134 | UCX_TEST_ASSERT(r != 0, "seek END above bounds shall fail"); |
universe@164 | 135 | UCX_TEST_ASSERT(b->pos == 7, |
universe@164 | 136 | "failed seek shall leave pos unchanged"); |
universe@164 | 137 | |
universe@164 | 138 | UCX_TEST_END |
universe@164 | 139 | |
universe@164 | 140 | ucx_buffer_free(b); |
universe@164 | 141 | } |
universe@164 | 142 | |
universe@164 | 143 | UCX_TEST(test_ucx_buffer_seek_set) { |
universe@164 | 144 | UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT); |
universe@164 | 145 | b->size = 16; |
universe@56 | 146 | int r; |
universe@56 | 147 | |
universe@56 | 148 | UCX_TEST_BEGIN |
universe@56 | 149 | |
universe@60 | 150 | r = ucx_buffer_seek(b, 5, SEEK_SET); |
universe@56 | 151 | UCX_TEST_ASSERT(r == 0, "seek SET+5 failed"); |
universe@63 | 152 | UCX_TEST_ASSERT(b->pos == 5, "seek SET+5 set wrong position"); |
universe@164 | 153 | |
universe@164 | 154 | |
universe@164 | 155 | r = ucx_buffer_seek(b, 10, SEEK_SET); |
universe@164 | 156 | UCX_TEST_ASSERT(r == 0, "seek SET+10 failed"); |
universe@164 | 157 | UCX_TEST_ASSERT(b->pos == 10, "seek SET+10 set wrong position"); |
universe@56 | 158 | |
universe@164 | 159 | UCX_TEST_END |
universe@56 | 160 | |
universe@164 | 161 | ucx_buffer_free(b); |
universe@164 | 162 | } |
universe@164 | 163 | |
universe@164 | 164 | UCX_TEST(test_ucx_buffer_seek_cur) { |
universe@164 | 165 | UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT); |
universe@164 | 166 | b->size = 16; |
universe@164 | 167 | int r; |
universe@164 | 168 | |
universe@164 | 169 | UCX_TEST_BEGIN |
universe@164 | 170 | |
universe@164 | 171 | b->pos = 7; |
universe@60 | 172 | r = ucx_buffer_seek(b, 5, SEEK_CUR); |
universe@56 | 173 | UCX_TEST_ASSERT(r == 0, "seek CUR+5 failed"); |
universe@164 | 174 | UCX_TEST_ASSERT(b->pos == 12, "seek CUR+5 set wrong position"); |
universe@56 | 175 | |
universe@164 | 176 | UCX_TEST_END |
universe@56 | 177 | |
universe@164 | 178 | ucx_buffer_free(b); |
universe@164 | 179 | } |
universe@164 | 180 | |
universe@164 | 181 | UCX_TEST(test_ucx_buffer_seek_end) { |
universe@164 | 182 | UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT); |
universe@164 | 183 | b->size = 16; |
universe@164 | 184 | int r; |
universe@164 | 185 | |
universe@164 | 186 | UCX_TEST_BEGIN |
universe@164 | 187 | |
universe@60 | 188 | r = ucx_buffer_seek(b, -5, SEEK_END); |
universe@56 | 189 | UCX_TEST_ASSERT(r == 0, "seek END-5 failed"); |
olaf@76 | 190 | UCX_TEST_ASSERT(b->pos == 11, "seek END-5 set wrong position"); |
universe@56 | 191 | |
universe@56 | 192 | |
universe@56 | 193 | UCX_TEST_END |
universe@56 | 194 | |
universe@60 | 195 | ucx_buffer_free(b); |
olaf@76 | 196 | } |
olaf@76 | 197 | |
universe@134 | 198 | UCX_TEST(test_ucx_buffer_putc) { |
olaf@76 | 199 | char *buffer = (char*) malloc(16); |
olaf@76 | 200 | memset(buffer, 32, 16); |
olaf@76 | 201 | |
olaf@76 | 202 | UcxBuffer *b = ucx_buffer_new(buffer, 16, UCX_BUFFER_DEFAULT); |
olaf@76 | 203 | b->size = b->capacity; |
olaf@76 | 204 | |
olaf@76 | 205 | UCX_TEST_BEGIN |
olaf@76 | 206 | |
universe@164 | 207 | ucx_buffer_seek(b, 0, SEEK_SET); |
universe@164 | 208 | UCX_TEST_ASSERT(ucx_buffer_putc(b, '0'|~0xFF) == '0', |
universe@164 | 209 | "putc shall return (arg & 0xFF)"); |
olaf@76 | 210 | ucx_buffer_putc(b, '0'); |
olaf@76 | 211 | ucx_buffer_putc(b, '0'); |
olaf@76 | 212 | |
olaf@76 | 213 | UCX_TEST_ASSERT(b->pos == 3, "pos wrong after first 3 puts"); |
olaf@76 | 214 | ucx_buffer_seek(b, 10, SEEK_CUR); |
olaf@76 | 215 | |
olaf@76 | 216 | ucx_buffer_putc(b, '0'); |
olaf@76 | 217 | ucx_buffer_putc(b, '0'); |
olaf@76 | 218 | ucx_buffer_putc(b, '0'); |
olaf@76 | 219 | |
olaf@76 | 220 | UCX_TEST_ASSERT(b->pos == 16, "pos wrong after last 3 puts"); |
olaf@76 | 221 | |
universe@164 | 222 | UCX_TEST_ASSERT(!memcmp(b->space, "000 000", 16), |
universe@164 | 223 | "buffer content wrong") |
universe@164 | 224 | UCX_TEST_END |
universe@164 | 225 | ucx_buffer_free(b); |
universe@164 | 226 | free(buffer); |
universe@164 | 227 | } |
olaf@76 | 228 | |
universe@164 | 229 | UCX_TEST(test_ucx_buffer_putc_oob) { |
universe@164 | 230 | UcxBuffer *b = ucx_buffer_new(NULL, 2, UCX_BUFFER_DEFAULT); |
universe@164 | 231 | |
universe@164 | 232 | UCX_TEST_BEGIN |
universe@164 | 233 | b->pos = b->size = b->capacity = 1; |
universe@164 | 234 | b->space[1] = 'X'; |
universe@164 | 235 | |
universe@164 | 236 | UCX_TEST_ASSERT(ucx_buffer_putc(b, 48) == EOF, "put shall return EOF " |
universe@164 | 237 | "when buffer is full and auto extend is disabled"); |
universe@164 | 238 | UCX_TEST_ASSERT(!memcmp(b->space, "\0X", 2), |
universe@164 | 239 | "wrong buffer content after failed putc"); |
olaf@76 | 240 | |
olaf@76 | 241 | UCX_TEST_END |
olaf@76 | 242 | |
olaf@76 | 243 | ucx_buffer_free(b); |
universe@56 | 244 | } |
universe@56 | 245 | |
universe@164 | 246 | |
universe@164 | 247 | UCX_TEST(test_ucx_buffer_putc_ae) { |
olaf@76 | 248 | UcxBuffer *b = ucx_buffer_new(NULL, 2, UCX_BUFFER_AUTOEXTEND); |
universe@164 | 249 | ucx_buffer_putc(b, '0'); |
universe@164 | 250 | ucx_buffer_putc(b, '1'); |
olaf@76 | 251 | |
universe@56 | 252 | UCX_TEST_BEGIN |
olaf@76 | 253 | |
universe@164 | 254 | UCX_TEST_ASSERT(b->pos == 2, "pos wrong after first 2 puts"); |
universe@164 | 255 | UCX_TEST_ASSERT(b->size == 2, "size wrong after first 2 puts"); |
universe@164 | 256 | UCX_TEST_ASSERT(b->capacity == 2, "buffer erroneously extended"); |
universe@164 | 257 | UCX_TEST_ASSERT(!memcmp(b->space,"01", 2), "wrong content"); |
universe@164 | 258 | |
universe@164 | 259 | UCX_TEST_END |
universe@164 | 260 | |
universe@164 | 261 | ucx_buffer_free(b); |
universe@164 | 262 | } |
universe@164 | 263 | |
universe@164 | 264 | UCX_TEST(test_ucx_buffer_putc_oobae) { |
universe@164 | 265 | UcxBuffer *b = ucx_buffer_new(NULL, 2, UCX_BUFFER_AUTOEXTEND); |
olaf@76 | 266 | ucx_buffer_putc(b, '0'); |
olaf@76 | 267 | ucx_buffer_putc(b, '1'); |
olaf@76 | 268 | |
universe@164 | 269 | UCX_TEST_BEGIN |
olaf@76 | 270 | |
olaf@76 | 271 | ucx_buffer_putc(b, 'a'); |
olaf@76 | 272 | |
universe@164 | 273 | UCX_TEST_ASSERT(b->pos == 3, "pos wrong after put"); |
olaf@76 | 274 | UCX_TEST_ASSERT(b->capacity == 4, "buffer not properly extended"); |
olaf@76 | 275 | UCX_TEST_ASSERT(b->size == 3, "wrong buffer size"); |
olaf@76 | 276 | |
universe@164 | 277 | UCX_TEST_ASSERT(!memcmp(b->space,"01a\0", 4), "wrong content"); |
olaf@76 | 278 | |
universe@56 | 279 | UCX_TEST_END |
olaf@76 | 280 | |
olaf@147 | 281 | ucx_buffer_free(b); |
universe@56 | 282 | } |
universe@56 | 283 | |
universe@164 | 284 | UCX_TEST(test_ucx_buffer_putc_size) { |
universe@164 | 285 | UcxBuffer *b = ucx_buffer_new(NULL, 4, UCX_BUFFER_DEFAULT); |
universe@164 | 286 | |
universe@164 | 287 | UCX_TEST_BEGIN |
universe@164 | 288 | |
universe@164 | 289 | UCX_TEST_ASSERT(b->size == 0, "wrong initial size"); |
universe@164 | 290 | ucx_buffer_putc(b, 'a'); |
universe@164 | 291 | ucx_buffer_putc(b, 'b'); |
universe@164 | 292 | ucx_buffer_putc(b, 'c'); |
universe@164 | 293 | UCX_TEST_ASSERT(b->size == 3, "size does not increase"); |
universe@164 | 294 | ucx_buffer_seek(b, 1, SEEK_SET); |
universe@164 | 295 | ucx_buffer_putc(b, 'd'); |
universe@164 | 296 | UCX_TEST_ASSERT(b->size == 3, "size shall not decrease"); |
universe@164 | 297 | UCX_TEST_ASSERT(b->pos == 2, "wrong position after seek and putc"); |
universe@164 | 298 | |
universe@164 | 299 | UCX_TEST_END |
universe@164 | 300 | |
universe@164 | 301 | ucx_buffer_free(b); |
universe@164 | 302 | } |
universe@164 | 303 | |
universe@134 | 304 | UCX_TEST(test_ucx_buffer_getc) { |
universe@69 | 305 | char *buffer = (char*) malloc(16); |
universe@56 | 306 | memset(buffer, 32, 8); |
universe@56 | 307 | for (int i = 8; i < 16 ; i++) { |
universe@56 | 308 | buffer[i] = 40+i; |
universe@56 | 309 | } |
universe@56 | 310 | |
universe@61 | 311 | UcxBuffer *b = ucx_buffer_new(buffer, 16, UCX_BUFFER_DEFAULT); |
olaf@76 | 312 | b->size = b->capacity; |
universe@56 | 313 | |
universe@56 | 314 | UCX_TEST_BEGIN |
universe@56 | 315 | |
universe@56 | 316 | char rb[16]; |
universe@95 | 317 | for (size_t i = 0 ; i < 16 ; i++) { |
universe@63 | 318 | UCX_TEST_ASSERT(b->pos == i, "pos wrong during read loop"); |
universe@60 | 319 | UCX_TEST_ASSERT(!ucx_buffer_eof(b), |
universe@56 | 320 | "EOF shall not be set during read loop"); |
universe@60 | 321 | rb[i] = ucx_buffer_getc(b); |
universe@56 | 322 | } |
universe@63 | 323 | UCX_TEST_ASSERT(b->pos == 16, "pos wrong after read loop"); |
universe@60 | 324 | UCX_TEST_ASSERT(ucx_buffer_eof(b), "EOF not set"); |
universe@56 | 325 | UCX_TEST_ASSERT(memcmp(rb, " 01234567", 16) == 0, |
universe@56 | 326 | "read data incorrect"); |
universe@56 | 327 | |
universe@56 | 328 | UCX_TEST_END |
universe@56 | 329 | |
universe@60 | 330 | ucx_buffer_free(b); |
universe@56 | 331 | free(buffer); |
universe@56 | 332 | } |
universe@56 | 333 | |
universe@134 | 334 | UCX_TEST(test_ucx_buffer_write) { |
universe@69 | 335 | char *buffer = (char*) malloc(16); |
universe@56 | 336 | memset(buffer, 32, 8); |
universe@56 | 337 | for (int i = 8; i < 16 ; i++) { |
universe@56 | 338 | buffer[i] = 40+i; |
universe@56 | 339 | } |
universe@56 | 340 | |
universe@61 | 341 | UcxBuffer *b = ucx_buffer_new(buffer, 16, UCX_BUFFER_DEFAULT); |
universe@56 | 342 | int r; |
universe@56 | 343 | |
universe@56 | 344 | UCX_TEST_BEGIN |
universe@56 | 345 | |
universe@69 | 346 | const char* teststring = "this is way too much"; |
universe@69 | 347 | r = ucx_buffer_write((void*)teststring, 1, 20, b); |
universe@56 | 348 | UCX_TEST_ASSERT(r == 16, "string not correctly trimed"); |
universe@56 | 349 | UCX_TEST_ASSERT(memcmp(buffer, teststring, 16) == 0, |
universe@56 | 350 | "buffer data incorrect"); |
universe@60 | 351 | UCX_TEST_ASSERT(ucx_buffer_eof(b), "eof shall be set"); |
universe@56 | 352 | |
universe@60 | 353 | ucx_buffer_seek(b, 8, SEEK_SET); |
universe@71 | 354 | r = ucx_buffer_write((void*)"not", 1, 3, b); |
universe@56 | 355 | UCX_TEST_ASSERT(r == 3, "three bytes should be replace"); |
universe@56 | 356 | UCX_TEST_ASSERT(memcmp(buffer, "this is not too much", 16) == 0, |
universe@56 | 357 | "modified buffer is incorrect"); |
universe@56 | 358 | |
universe@69 | 359 | const char* threebytestring = " t h r e e "; |
universe@56 | 360 | memset(buffer, 49, 16); |
universe@60 | 361 | ucx_buffer_seek(b, 0, SEEK_SET); |
universe@69 | 362 | r = ucx_buffer_write((void*)threebytestring, 3, 6, b); |
olaf@76 | 363 | UCX_TEST_ASSERT(r == 5, "three byte string not correctly trimed"); |
universe@63 | 364 | UCX_TEST_ASSERT(b->pos == 15, |
universe@56 | 365 | "position after write of three byte string incorrect"); |
universe@60 | 366 | UCX_TEST_ASSERT(!ucx_buffer_eof(b), "eof shall not be set"); |
universe@56 | 367 | UCX_TEST_ASSERT(memcmp(buffer, " t h r e e1", 16) == 0, |
universe@56 | 368 | "bufer is incorrect after three byte string has been written"); |
universe@56 | 369 | |
universe@56 | 370 | UCX_TEST_END |
universe@56 | 371 | |
universe@60 | 372 | ucx_buffer_free(b); |
universe@56 | 373 | free(buffer); |
universe@56 | 374 | } |
universe@56 | 375 | |
universe@134 | 376 | UCX_TEST(test_ucx_buffer_write_ax) { |
olaf@76 | 377 | char *buffer = (char*) malloc(16); |
universe@64 | 378 | |
universe@64 | 379 | UcxBuffer *b = ucx_buffer_new(buffer, 16, |
universe@64 | 380 | UCX_BUFFER_AUTOEXTEND | UCX_BUFFER_AUTOFREE); |
universe@64 | 381 | int r; |
universe@64 | 382 | |
universe@64 | 383 | UCX_TEST_BEGIN |
universe@64 | 384 | |
universe@69 | 385 | const char* teststring = "this is way too much"; |
universe@69 | 386 | r = ucx_buffer_write((void*)teststring, 1, 20, b); |
universe@69 | 387 | buffer = (char*) b->space; /*autoextend enabled, we MUST retrieve pointer*/ |
universe@64 | 388 | UCX_TEST_ASSERT(r == 20, "not all characters written"); |
olaf@76 | 389 | UCX_TEST_ASSERT(b->capacity == 32, "buffer not properly extended"); |
universe@64 | 390 | UCX_TEST_ASSERT(b->pos == 20, "position incorrect"); |
universe@64 | 391 | UCX_TEST_ASSERT(memcmp(buffer, |
universe@64 | 392 | "this is way too much\0\0\0\0\0\0\0\0\0\0\0\0", 32) == 0, |
universe@64 | 393 | "incorrect buffer content"); |
universe@64 | 394 | |
universe@64 | 395 | UCX_TEST_END |
universe@64 | 396 | |
universe@64 | 397 | ucx_buffer_free(b); |
universe@64 | 398 | } |
universe@64 | 399 | |
universe@134 | 400 | UCX_TEST(test_ucx_buffer_read) { |
olaf@76 | 401 | UcxBuffer *b = ucx_buffer_new(NULL, 8, UCX_BUFFER_AUTOFREE); |
olaf@76 | 402 | |
olaf@76 | 403 | char buf[32]; |
olaf@76 | 404 | memset(buf, 'X', 32); |
universe@56 | 405 | int r; |
olaf@76 | 406 | |
universe@56 | 407 | UCX_TEST_BEGIN |
olaf@76 | 408 | |
olaf@76 | 409 | ucx_buffer_write("01234567", 1, 8, b); |
olaf@76 | 410 | UCX_TEST_ASSERT(b->pos == 8, "buffer not correctly filled"); |
olaf@76 | 411 | b->pos = 0; |
olaf@76 | 412 | |
olaf@76 | 413 | r = ucx_buffer_read(buf, 1, 2, b); |
olaf@76 | 414 | UCX_TEST_ASSERT(r == 2, "wrong number of bytes read"); |
olaf@76 | 415 | UCX_TEST_ASSERT(buf[0] == '0' && buf[1] == '1' && buf[2] == 'X', |
universe@56 | 416 | "buffer incorrect after first read"); |
olaf@76 | 417 | |
olaf@76 | 418 | r = ucx_buffer_read(buf + 2, 1, 8, b); |
olaf@76 | 419 | UCX_TEST_ASSERT(r == 6, "wrong number of bytes read(2)"); |
olaf@76 | 420 | UCX_TEST_ASSERT(memcmp(buf, "01234567XX", 10) == 0, |
olaf@76 | 421 | "buffer incorrect after second read"); |
olaf@76 | 422 | |
olaf@76 | 423 | memset(buf, 'X', 32); |
universe@60 | 424 | ucx_buffer_seek(b, 0, SEEK_SET); |
olaf@76 | 425 | r = ucx_buffer_read(buf, 3, 3, b); |
olaf@76 | 426 | |
olaf@76 | 427 | UCX_TEST_ASSERT(r == 2, "wrong number of blocks read"); |
olaf@76 | 428 | UCX_TEST_ASSERT(memcmp(buf, "012345XX", 8) == 0, |
olaf@76 | 429 | "buffer incorrect after three byte read"); |
olaf@76 | 430 | |
olaf@76 | 431 | |
universe@56 | 432 | UCX_TEST_END |
olaf@76 | 433 | |
olaf@147 | 434 | ucx_buffer_free(b); |
universe@56 | 435 | } |
universe@62 | 436 | |
universe@134 | 437 | UCX_TEST(test_ucx_buffer_extract) { |
universe@69 | 438 | char *buffer = (char*) malloc(16); |
universe@62 | 439 | strcpy(buffer, "this is a test!"); |
universe@62 | 440 | |
olaf@76 | 441 | UcxBuffer *src = ucx_buffer_new(buffer, 16, UCX_BUFFER_AUTOFREE); |
olaf@76 | 442 | src->size = 15; |
olaf@76 | 443 | UcxBuffer *dst = ucx_buffer_extract(src, 5, 5, UCX_BUFFER_DEFAULT); |
universe@62 | 444 | |
universe@62 | 445 | UCX_TEST_BEGIN |
olaf@76 | 446 | UCX_TEST_ASSERT(dst != NULL, "ucx_buffer_extract returned NULL"); |
olaf@76 | 447 | |
universe@63 | 448 | UCX_TEST_ASSERT((dst->flags & UCX_BUFFER_AUTOFREE) == UCX_BUFFER_AUTOFREE, |
universe@62 | 449 | "autofree flag shall be enforced"); |
universe@63 | 450 | UCX_TEST_ASSERT(dst->size == 5, "wrong size for new buffer"); |
universe@62 | 451 | char rb[5]; |
universe@62 | 452 | ucx_buffer_read(rb, 1, 5, dst); |
universe@62 | 453 | UCX_TEST_ASSERT(memcmp(rb, "is a ", 5) == 0, |
universe@62 | 454 | "new buffer has incorrect content"); |
universe@62 | 455 | |
universe@62 | 456 | UCX_TEST_ASSERT(ucx_buffer_extract(dst, 3, 3, UCX_BUFFER_DEFAULT) == NULL, |
universe@62 | 457 | "extract shall fail on invalid bounds"); |
universe@62 | 458 | |
universe@62 | 459 | UCX_TEST_END |
universe@62 | 460 | |
universe@62 | 461 | ucx_buffer_free(dst); |
universe@62 | 462 | ucx_buffer_free(src); |
universe@62 | 463 | } |
olaf@76 | 464 | |
universe@140 | 465 | UCX_TEST(test_ucx_stream_copy) { |
olaf@76 | 466 | UcxBuffer *b1 = ucx_buffer_new(NULL, 64, UCX_BUFFER_DEFAULT); |
olaf@76 | 467 | UcxBuffer *b2 = ucx_buffer_new(NULL, 2, UCX_BUFFER_AUTOEXTEND); |
olaf@76 | 468 | |
olaf@76 | 469 | UCX_TEST_BEGIN |
olaf@76 | 470 | |
olaf@76 | 471 | ucx_buffer_write("01234567", 1, 8, b1); |
olaf@76 | 472 | ucx_buffer_write("abcdefgh", 1, 8, b1); |
olaf@76 | 473 | UCX_TEST_ASSERT(b1->size == 16, "failed to fill buffer b1"); |
olaf@76 | 474 | ucx_buffer_seek(b1, 0, SEEK_SET); |
olaf@76 | 475 | |
universe@140 | 476 | size_t ncp = ucx_stream_hcopy(b1, b2, ucx_buffer_read, ucx_buffer_write); |
olaf@76 | 477 | UCX_TEST_ASSERT(ncp == 16, "wrong number of copied bytes"); |
olaf@76 | 478 | UCX_TEST_ASSERT(b2->size == 16, "b2 has wrong size"); |
olaf@76 | 479 | UCX_TEST_ASSERT(memcmp(b1->space, b2->space, 16) == 0, |
olaf@76 | 480 | "b1 and b2 have not the same content"); |
olaf@76 | 481 | |
olaf@76 | 482 | memset(b2->space, 0, b2->capacity); |
olaf@76 | 483 | b2->pos = 0; |
olaf@76 | 484 | b2->size = 0; |
olaf@76 | 485 | ucx_buffer_seek(b1, 0, SEEK_SET); |
olaf@76 | 486 | |
olaf@76 | 487 | FILE *file = tmpfile(); |
olaf@76 | 488 | UCX_TEST_ASSERT(file, "test file cannot be opened, test aborted"); |
olaf@76 | 489 | |
universe@140 | 490 | ncp = ucx_stream_hcopy(b1, file, ucx_buffer_read, fwrite); |
olaf@76 | 491 | UCX_TEST_ASSERT(ncp == 16, "copied wrong number of bytes to file"); |
olaf@76 | 492 | |
olaf@76 | 493 | fseek(file, 0, SEEK_SET); |
olaf@76 | 494 | |
universe@140 | 495 | ncp = ucx_stream_hcopy(file, b2, fread, ucx_buffer_write); |
olaf@76 | 496 | UCX_TEST_ASSERT(ncp == 16, "copied wrong number of bytes from file"); |
olaf@76 | 497 | |
olaf@76 | 498 | UCX_TEST_ASSERT(memcmp(b1->space, b2->space, 16) == 0, |
olaf@76 | 499 | "b1 and b2 content mismatch"); |
olaf@76 | 500 | |
olaf@76 | 501 | fclose(file); |
universe@140 | 502 | |
universe@140 | 503 | ucx_buffer_clear(b1); |
universe@140 | 504 | ucx_buffer_seek(b2, 0, SEEK_SET); |
universe@140 | 505 | ncp = ucx_stream_ncopy(b2, b1, ucx_buffer_read, ucx_buffer_write, 8); |
universe@140 | 506 | UCX_TEST_ASSERT(ncp == 8, "copied wrong number of bytes with ncopy"); |
universe@140 | 507 | UCX_TEST_ASSERT(memcmp(b1->space, "01234567\0\0\0\0\0\0\0\0", 16) == 0, |
universe@140 | 508 | "content wrong after ncopy"); |
olaf@76 | 509 | |
olaf@76 | 510 | UCX_TEST_END |
olaf@76 | 511 | |
olaf@76 | 512 | ucx_buffer_free(b1); |
olaf@76 | 513 | ucx_buffer_free(b2); |
olaf@76 | 514 | } |