test/buffer_tests.c

changeset 390
d345541018fa
parent 389
92e482410453
child 391
f094a53c1178
     1.1 --- a/test/buffer_tests.c	Mon Dec 30 09:54:10 2019 +0100
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,738 +0,0 @@
     1.4 -/*
     1.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6 - *
     1.7 - * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved.
     1.8 - *
     1.9 - * Redistribution and use in source and binary forms, with or without
    1.10 - * modification, are permitted provided that the following conditions are met:
    1.11 - *
    1.12 - *   1. Redistributions of source code must retain the above copyright
    1.13 - *      notice, this list of conditions and the following disclaimer.
    1.14 - *
    1.15 - *   2. Redistributions in binary form must reproduce the above copyright
    1.16 - *      notice, this list of conditions and the following disclaimer in the
    1.17 - *      documentation and/or other materials provided with the distribution.
    1.18 - *
    1.19 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    1.20 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.21 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.22 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    1.23 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    1.24 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    1.25 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    1.26 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    1.27 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    1.28 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    1.29 - * POSSIBILITY OF SUCH DAMAGE.
    1.30 - */
    1.31 -
    1.32 -#include "buffer_tests.h"
    1.33 -#include <stdint.h>
    1.34 -
    1.35 -UCX_TEST(test_ucx_buffer_new) {
    1.36 -    UcxBuffer *b = ucx_buffer_new(NULL, 16, UCX_BUFFER_AUTOEXTEND);
    1.37 -    UcxBuffer *b2 = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT);
    1.38 -    UCX_TEST_BEGIN
    1.39 -
    1.40 -    UCX_TEST_ASSERT(b->capacity==16, "wrong capacity");
    1.41 -    UCX_TEST_ASSERT(b2->capacity==32, "wrong capacity");
    1.42 -    
    1.43 -    UCX_TEST_ASSERT(b->size==0, "wrong size");
    1.44 -    UCX_TEST_ASSERT(b2->size==0, "wrong size");
    1.45 -    
    1.46 -    UCX_TEST_ASSERT(b->pos==0, "wrong position");
    1.47 -    UCX_TEST_ASSERT(b2->pos==0, "wrong position");
    1.48 -    
    1.49 -    UCX_TEST_ASSERT(b->flags==(UCX_BUFFER_AUTOEXTEND|UCX_BUFFER_AUTOFREE),
    1.50 -        "wrong flags for autoextending buffer");
    1.51 -    UCX_TEST_ASSERT(b2->flags==UCX_BUFFER_AUTOFREE,
    1.52 -        "wrong flags for default bufer");
    1.53 -    
    1.54 -    UCX_TEST_END
    1.55 -    ucx_buffer_free(b2);
    1.56 -    ucx_buffer_free(b);
    1.57 -}
    1.58 -
    1.59 -UCX_TEST(test_ucx_buffer_new_prealloc) {
    1.60 -    char* test = (char*) malloc(16);
    1.61 -    UcxBuffer *b = ucx_buffer_new(test, 16, UCX_BUFFER_DEFAULT);
    1.62 -    UCX_TEST_BEGIN
    1.63 -
    1.64 -    UCX_TEST_ASSERT(b->capacity==16, "wrong capacity");
    1.65 -    UCX_TEST_ASSERT(b->size==0, "wrong size");    
    1.66 -    UCX_TEST_ASSERT(b->pos==0, "wrong position");
    1.67 -    
    1.68 -    UCX_TEST_ASSERT(b->flags==0, "wrong flags - all should be cleared");
    1.69 -    
    1.70 -    UCX_TEST_END
    1.71 -    free(test);
    1.72 -    ucx_buffer_free(b);
    1.73 -}
    1.74 -
    1.75 -UCX_TEST(test_ucx_buffer_eof) {
    1.76 -    char *test = (char*)"0123456789ABCDEF";
    1.77 -    UcxBuffer *b = ucx_buffer_new(test, 16, UCX_BUFFER_DEFAULT);
    1.78 -    UCX_TEST_BEGIN
    1.79 -    b->pos = 9; b->size = 10;
    1.80 -    UCX_TEST_ASSERT(!ucx_buffer_eof(b), "false positive");
    1.81 -    b->pos = 10; b->size = 10;
    1.82 -    UCX_TEST_ASSERT(ucx_buffer_eof(b), "pos == size should be EOF");
    1.83 -    b->pos = 11; b->size = 10;
    1.84 -    UCX_TEST_ASSERT(ucx_buffer_eof(b), "false negative");
    1.85 -    UCX_TEST_END
    1.86 -    ucx_buffer_free(b);
    1.87 -}
    1.88 -
    1.89 -UCX_TEST(test_ucx_buffer_seek_overflow) {
    1.90 -    UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT);
    1.91 -    b->size = 32;
    1.92 -    int r;
    1.93 -    
    1.94 -    UCX_TEST_BEGIN
    1.95 -    const size_t sizemax = (size_t)-1;
    1.96 -    size_t bigpos = sizemax - 5000;
    1.97 -    b->pos = bigpos;
    1.98 -    r = ucx_buffer_seek(b, 5016, SEEK_CUR);
    1.99 -    UCX_TEST_ASSERT(r != 0, "seek cur overflow");
   1.100 -    UCX_TEST_ASSERT(b->pos == bigpos,
   1.101 -            "failed seek shall leave pos unchanged");
   1.102 -    
   1.103 -    b->pos = 0;
   1.104 -    b->size = (sizemax >> 1) + 32;
   1.105 -    
   1.106 -    // we don't want to risk overflows in implicit constant casts
   1.107 -    const size_t bigoff_comp = (sizemax >> 1) - 16;
   1.108 -    off_t bigoff = (off_t)bigoff_comp;
   1.109 -    
   1.110 -    r = ucx_buffer_seek(b, -bigoff, SEEK_CUR);
   1.111 -    UCX_TEST_ASSERT(r != 0, "seek cur underflow");
   1.112 -    UCX_TEST_ASSERT(b->pos == 0,
   1.113 -            "failed seek shall leave pos unchanged");
   1.114 -    
   1.115 -    UCX_TEST_END
   1.116 -    
   1.117 -    ucx_buffer_free(b);
   1.118 -}
   1.119 -
   1.120 -UCX_TEST(test_ucx_buffer_seek_invalid) {
   1.121 -    UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT);
   1.122 -    b->pos = 7;
   1.123 -    int r;
   1.124 -    
   1.125 -    UCX_TEST_BEGIN
   1.126 -    r = ucx_buffer_seek(b, 0, ~(SEEK_SET|SEEK_CUR|SEEK_END));
   1.127 -    UCX_TEST_ASSERT(r != 0, "invalid whence shall fail");
   1.128 -    UCX_TEST_ASSERT(b->pos == 7,
   1.129 -            "failed seek shall leave pos unchanged");
   1.130 -    UCX_TEST_END
   1.131 -    
   1.132 -    ucx_buffer_free(b);
   1.133 -}
   1.134 -
   1.135 -UCX_TEST(test_ucx_buffer_seek_oob) {
   1.136 -    UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT);
   1.137 -    b->size = 16; // less than capacity
   1.138 -    b->pos = 7;
   1.139 -    int r;
   1.140 -
   1.141 -    UCX_TEST_BEGIN
   1.142 -    
   1.143 -    r = ucx_buffer_seek(b, -1, SEEK_SET);
   1.144 -    UCX_TEST_ASSERT(r != 0, "seek SET below bounds shall fail");
   1.145 -    UCX_TEST_ASSERT(b->pos == 7,
   1.146 -            "failed seek shall leave pos unchanged");
   1.147 -    
   1.148 -    r = ucx_buffer_seek(b, 16, SEEK_SET);
   1.149 -    UCX_TEST_ASSERT(r != 0, "seek SET above bounds shall fail");
   1.150 -    UCX_TEST_ASSERT(b->pos == 7,
   1.151 -            "failed seek shall leave pos unchanged");
   1.152 -    
   1.153 -    r = ucx_buffer_seek(b, -8, SEEK_CUR);
   1.154 -    UCX_TEST_ASSERT(r != 0, "seek CUR below bounds shall fail");
   1.155 -    UCX_TEST_ASSERT(b->pos == 7,
   1.156 -            "failed seek shall leave pos unchanged");
   1.157 -    
   1.158 -    r = ucx_buffer_seek(b, 9, SEEK_CUR);
   1.159 -    UCX_TEST_ASSERT(r != 0, "seek CUR above bounds shall fail");
   1.160 -    UCX_TEST_ASSERT(b->pos == 7,
   1.161 -            "failed seek shall leave pos unchanged");
   1.162 -    
   1.163 -    r = ucx_buffer_seek(b, -17, SEEK_END);
   1.164 -    UCX_TEST_ASSERT(r != 0, "seek END below bounds shall fail");
   1.165 -    UCX_TEST_ASSERT(b->pos == 7,
   1.166 -            "failed seek shall leave pos unchanged");
   1.167 -    
   1.168 -    r = ucx_buffer_seek(b, 1, SEEK_END);
   1.169 -    UCX_TEST_ASSERT(r != 0, "seek END above bounds shall fail");
   1.170 -    UCX_TEST_ASSERT(b->pos == 7,
   1.171 -            "failed seek shall leave pos unchanged");
   1.172 -
   1.173 -    UCX_TEST_END
   1.174 -
   1.175 -    ucx_buffer_free(b);
   1.176 -}
   1.177 -
   1.178 -UCX_TEST(test_ucx_buffer_seek_set) {
   1.179 -    UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT);
   1.180 -    b->size = 16;
   1.181 -    int r;
   1.182 -
   1.183 -    UCX_TEST_BEGIN
   1.184 -
   1.185 -    r = ucx_buffer_seek(b, 5, SEEK_SET);
   1.186 -    UCX_TEST_ASSERT(r == 0, "seek SET+5 failed");
   1.187 -    UCX_TEST_ASSERT(b->pos == 5, "seek SET+5 set wrong position");
   1.188 -    
   1.189 -    
   1.190 -    r = ucx_buffer_seek(b, 10, SEEK_SET);
   1.191 -    UCX_TEST_ASSERT(r == 0, "seek SET+10 failed");
   1.192 -    UCX_TEST_ASSERT(b->pos == 10, "seek SET+10 set wrong position");
   1.193 -
   1.194 -    UCX_TEST_END
   1.195 -
   1.196 -    ucx_buffer_free(b);
   1.197 -}
   1.198 -
   1.199 -UCX_TEST(test_ucx_buffer_seek_cur) {
   1.200 -    UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT);
   1.201 -    b->size = 16;
   1.202 -    int r;
   1.203 -
   1.204 -    UCX_TEST_BEGIN
   1.205 -    
   1.206 -    b->pos = 7;
   1.207 -    r = ucx_buffer_seek(b, 5, SEEK_CUR);
   1.208 -    UCX_TEST_ASSERT(r == 0, "seek CUR+5 failed");
   1.209 -    UCX_TEST_ASSERT(b->pos == 12, "seek CUR+5 set wrong position");
   1.210 -
   1.211 -    UCX_TEST_END
   1.212 -
   1.213 -    ucx_buffer_free(b);
   1.214 -}
   1.215 -
   1.216 -UCX_TEST(test_ucx_buffer_seek_end) {
   1.217 -    UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT);
   1.218 -    b->size = 16;
   1.219 -    int r;
   1.220 -
   1.221 -    UCX_TEST_BEGIN
   1.222 -    
   1.223 -    r = ucx_buffer_seek(b, -5, SEEK_END);
   1.224 -    UCX_TEST_ASSERT(r == 0, "seek END-5 failed");
   1.225 -    UCX_TEST_ASSERT(b->pos == 11, "seek END-5 set wrong position");
   1.226 -
   1.227 -
   1.228 -    UCX_TEST_END
   1.229 -
   1.230 -    ucx_buffer_free(b);
   1.231 -}
   1.232 -
   1.233 -UCX_TEST(test_ucx_buffer_putc) {
   1.234 -    char *buffer = (char*) malloc(16);
   1.235 -    memset(buffer, 32, 16);
   1.236 -
   1.237 -    UcxBuffer *b = ucx_buffer_new(buffer, 16, UCX_BUFFER_DEFAULT);
   1.238 -    b->size = b->capacity;
   1.239 -
   1.240 -    UCX_TEST_BEGIN
   1.241 -
   1.242 -    ucx_buffer_seek(b, 0, SEEK_SET);
   1.243 -    UCX_TEST_ASSERT(ucx_buffer_putc(b, '0'|~0xFF) == '0',
   1.244 -            "putc shall return (arg & 0xFF)");
   1.245 -    ucx_buffer_putc(b, '0');
   1.246 -    ucx_buffer_putc(b, '0');
   1.247 -    
   1.248 -    UCX_TEST_ASSERT(b->pos == 3, "pos wrong after first 3 puts");
   1.249 -    ucx_buffer_seek(b, 10, SEEK_CUR);
   1.250 -    
   1.251 -    ucx_buffer_putc(b, '0');
   1.252 -    ucx_buffer_putc(b, '0');
   1.253 -    ucx_buffer_putc(b, '0');
   1.254 -    
   1.255 -    UCX_TEST_ASSERT(b->pos == 16, "pos wrong after last 3 puts");
   1.256 -    
   1.257 -    UCX_TEST_ASSERT(!memcmp(b->space, "000          000", 16),
   1.258 -            "buffer content wrong")
   1.259 -    UCX_TEST_END
   1.260 -    ucx_buffer_free(b);
   1.261 -    free(buffer);
   1.262 -}
   1.263 -    
   1.264 -UCX_TEST(test_ucx_buffer_putc_oob) {
   1.265 -    UcxBuffer *b = ucx_buffer_new(NULL, 2, UCX_BUFFER_DEFAULT);
   1.266 -    
   1.267 -    UCX_TEST_BEGIN
   1.268 -    b->pos = b->size = b->capacity = 1;
   1.269 -    b->space[1] = 'X';
   1.270 -    
   1.271 -    UCX_TEST_ASSERT(ucx_buffer_putc(b, 48) == EOF, "put shall return EOF "
   1.272 -            "when buffer is full and auto extend is disabled");
   1.273 -    UCX_TEST_ASSERT(!memcmp(b->space, "\0X", 2),
   1.274 -            "wrong buffer content after failed putc");
   1.275 -
   1.276 -    UCX_TEST_END
   1.277 -
   1.278 -    ucx_buffer_free(b);
   1.279 -}
   1.280 -
   1.281 -
   1.282 -UCX_TEST(test_ucx_buffer_putc_ae) {
   1.283 -    UcxBuffer *b = ucx_buffer_new(NULL, 2, UCX_BUFFER_AUTOEXTEND);
   1.284 -    ucx_buffer_putc(b, '0');
   1.285 -    ucx_buffer_putc(b, '1');
   1.286 -    
   1.287 -    UCX_TEST_BEGIN
   1.288 -    
   1.289 -    UCX_TEST_ASSERT(b->pos == 2, "pos wrong after first 2 puts");
   1.290 -    UCX_TEST_ASSERT(b->size == 2, "size wrong after first 2 puts");
   1.291 -    UCX_TEST_ASSERT(b->capacity == 2, "buffer erroneously extended");
   1.292 -    UCX_TEST_ASSERT(!memcmp(b->space,"01", 2), "wrong content");
   1.293 -    
   1.294 -    UCX_TEST_END
   1.295 -    
   1.296 -    ucx_buffer_free(b);
   1.297 -}
   1.298 -
   1.299 -UCX_TEST(test_ucx_buffer_putc_oobae) {
   1.300 -    UcxBuffer *b = ucx_buffer_new(NULL, 2, UCX_BUFFER_AUTOEXTEND);
   1.301 -    ucx_buffer_putc(b, '0');
   1.302 -    ucx_buffer_putc(b, '1');
   1.303 -    
   1.304 -    UCX_TEST_BEGIN
   1.305 -    
   1.306 -    ucx_buffer_putc(b, 'a');
   1.307 -    
   1.308 -    UCX_TEST_ASSERT(b->pos == 3, "pos wrong after put");
   1.309 -    UCX_TEST_ASSERT(b->capacity == 4, "buffer not properly extended");
   1.310 -    UCX_TEST_ASSERT(b->size == 3, "wrong buffer size");
   1.311 -    
   1.312 -    UCX_TEST_ASSERT(!memcmp(b->space,"01a\0", 4), "wrong content");
   1.313 -    
   1.314 -    UCX_TEST_END
   1.315 -    
   1.316 -    ucx_buffer_free(b);
   1.317 -}
   1.318 -
   1.319 -UCX_TEST(test_ucx_buffer_putc_size) {
   1.320 -    UcxBuffer *b = ucx_buffer_new(NULL, 4, UCX_BUFFER_DEFAULT);
   1.321 -    
   1.322 -    UCX_TEST_BEGIN
   1.323 -    
   1.324 -    UCX_TEST_ASSERT(b->size == 0, "wrong initial size");
   1.325 -    ucx_buffer_putc(b, 'a');
   1.326 -    ucx_buffer_putc(b, 'b');
   1.327 -    ucx_buffer_putc(b, 'c');
   1.328 -    UCX_TEST_ASSERT(b->size == 3, "size does not increase");
   1.329 -    ucx_buffer_seek(b, 1, SEEK_SET);
   1.330 -    ucx_buffer_putc(b, 'd');
   1.331 -    UCX_TEST_ASSERT(b->size == 3, "size shall not decrease");
   1.332 -    UCX_TEST_ASSERT(b->pos == 2, "wrong position after seek and putc");
   1.333 -
   1.334 -    UCX_TEST_END
   1.335 -
   1.336 -    ucx_buffer_free(b);
   1.337 -}
   1.338 -
   1.339 -UCX_TEST(test_ucx_buffer_getc) {
   1.340 -    char *buffer = (char*) malloc(16);
   1.341 -    memset(buffer, 32, 8);
   1.342 -    for (int i = 8; i < 16 ; i++) {
   1.343 -        buffer[i] = 40+i;
   1.344 -    }
   1.345 -
   1.346 -    UcxBuffer *b = ucx_buffer_new(buffer, 16, UCX_BUFFER_DEFAULT);
   1.347 -    b->size = b->capacity;
   1.348 -    
   1.349 -    unsigned char ubuffer[] = {127, 128, 129, 130};
   1.350 -    UcxBuffer *ub = ucx_buffer_new(ubuffer, 4, UCX_BUFFER_DEFAULT);
   1.351 -    ub->size = 4;
   1.352 -
   1.353 -    UCX_TEST_BEGIN
   1.354 -
   1.355 -    char rb[16];
   1.356 -    for (size_t i = 0 ; i < 16 ; i++) {
   1.357 -        UCX_TEST_ASSERT(b->pos == i, "wrong position");
   1.358 -        UCX_TEST_ASSERT(!ucx_buffer_eof(b), "EOF false positive");
   1.359 -        rb[i] = ucx_buffer_getc(b);
   1.360 -    }
   1.361 -    UCX_TEST_ASSERT(memcmp(rb, "        01234567", 16) == 0,
   1.362 -        "read data incorrect");
   1.363 -        
   1.364 -    UCX_TEST_ASSERT(ucx_buffer_eof(b), "EOF not set after last possible read");
   1.365 -    UCX_TEST_ASSERT(b->pos == 16, "wrong position after EOF");
   1.366 -    
   1.367 -    UCX_TEST_ASSERT(ucx_buffer_getc(b) == EOF,
   1.368 -        "out of bounds read does not return EOF");
   1.369 -    UCX_TEST_ASSERT(b->pos == 16, "wrong position after out of bounds read");
   1.370 -    
   1.371 -    int uc[5];
   1.372 -    for(int i=0;i<5;i++) {
   1.373 -        uc[i] = ucx_buffer_getc(ub);
   1.374 -    }
   1.375 -    UCX_TEST_ASSERT(uc[0] == 127, "wrong unsigned value(0)");
   1.376 -    UCX_TEST_ASSERT(uc[1] == 128, "wrong unsigned value(0)");
   1.377 -    UCX_TEST_ASSERT(uc[2] == 129, "wrong unsigned value(0)");
   1.378 -    UCX_TEST_ASSERT(uc[3] == 130, "wrong unsigned value(0)");
   1.379 -    UCX_TEST_ASSERT(uc[4] == EOF, "EOF not set after last ub read");
   1.380 -
   1.381 -    UCX_TEST_END
   1.382 -
   1.383 -    ucx_buffer_free(b);
   1.384 -    ucx_buffer_free(ub);
   1.385 -    free(buffer);
   1.386 -}
   1.387 -
   1.388 -UCX_TEST(test_ucx_buffer_write) {
   1.389 -    char *buffer = (char*) malloc(32);
   1.390 -    memset(buffer, 0, 32);
   1.391 -    
   1.392 -    memset(buffer, 32, 8);
   1.393 -    for (int i = 8; i < 16 ; i++) {
   1.394 -        buffer[i] = 40+i;
   1.395 -    }
   1.396 -
   1.397 -    UcxBuffer *b = ucx_buffer_new(buffer, 32, UCX_BUFFER_DEFAULT);
   1.398 -    int r;
   1.399 -
   1.400 -    UCX_TEST_BEGIN
   1.401 -    b->pos = 4;
   1.402 -    r = ucx_buffer_write("test string", 1, 11, b);
   1.403 -    UCX_TEST_ASSERT(r == 11, "returned incorrect number of written bytes");
   1.404 -    UCX_TEST_ASSERT(b->pos == 15, "incorrect position");
   1.405 -    UCX_TEST_ASSERT(memcmp(buffer, "    test string7\0\0", 18) == 0,
   1.406 -        "incorrect buffer content (test string)");
   1.407 -    
   1.408 -    r = ucx_buffer_write(".", 1, 1, b);
   1.409 -    UCX_TEST_ASSERT(r == 1, "returned incorrect number of written elements");
   1.410 -    UCX_TEST_ASSERT(b->pos == 16, "incorrect position");
   1.411 -    
   1.412 -    int32_t testarr[4] = {0x09abcdef, 0x05fedcba, 0x01abefcd, 0x3cd07ab};
   1.413 -    r = ucx_buffer_write(testarr, 4, 4, b);
   1.414 -    UCX_TEST_ASSERT(r = 4, "returned incorrect number of written elements");
   1.415 -    UCX_TEST_ASSERT(b->pos == 32, "incorrect position");
   1.416 -    
   1.417 -    char cmp[32];
   1.418 -    memset(cmp, 0, 32);
   1.419 -    memcpy(cmp, "    test string.", 16);
   1.420 -    int32_t *ptr = (int32_t*) (cmp+16);
   1.421 -    ptr[0] = testarr[0];
   1.422 -    ptr[1] = testarr[1];
   1.423 -    ptr[2] = testarr[2];
   1.424 -    ptr[3] = testarr[3];
   1.425 -    UCX_TEST_ASSERT(memcmp(buffer, cmp, 32) == 0,
   1.426 -        "incorrect buffer content (int array)");
   1.427 -    
   1.428 -    UCX_TEST_END
   1.429 -
   1.430 -    ucx_buffer_free(b);
   1.431 -    free(buffer);
   1.432 -}
   1.433 -
   1.434 -UCX_TEST(test_ucx_buffer_write_oob) {
   1.435 -    char *buffer = (char*) malloc(32);
   1.436 -    memset(buffer, 0, 32);
   1.437 -
   1.438 -    UcxBuffer *b = ucx_buffer_new(buffer, 15, UCX_BUFFER_DEFAULT);
   1.439 -    int r;
   1.440 -
   1.441 -    UCX_TEST_BEGIN
   1.442 -    r = ucx_buffer_write("a very long string", 1, 18, b);
   1.443 -    UCX_TEST_ASSERT(r == 15, "incorrect number of written bytes");
   1.444 -    UCX_TEST_ASSERT(memcmp(buffer, "a very long str\0\0\0", 18) == 0,
   1.445 -        "invalid buffer content (test string)");
   1.446 -    
   1.447 -    b->size = b->pos = 0;
   1.448 -    int32_t intarr[4] = {0,-1,0,-1};
   1.449 -    memset(buffer, 0, 32);
   1.450 -    
   1.451 -    r = ucx_buffer_write(intarr, 4, 4, b);
   1.452 -    UCX_TEST_ASSERT(r == 3, "incorrect number of written elements");
   1.453 -    UCX_TEST_ASSERT(memcmp(buffer,
   1.454 -        "\0\0\0\0\xff\xff\xff\xff\0\0\0\0\0\0\0\0", 16) == 0,
   1.455 -        "invalid buffer content (test string)");
   1.456 -    
   1.457 -    UCX_TEST_END
   1.458 -
   1.459 -    ucx_buffer_free(b);
   1.460 -    free(buffer);
   1.461 -}
   1.462 -
   1.463 -UCX_TEST(test_ucx_buffer_write_ax) {
   1.464 -    char *buffer = (char*) malloc(16);
   1.465 -    memset(buffer, 0, 16);
   1.466 -
   1.467 -    UcxBuffer *b = ucx_buffer_new(buffer, 16,
   1.468 -            UCX_BUFFER_AUTOEXTEND | UCX_BUFFER_AUTOFREE);
   1.469 -    int r;
   1.470 -
   1.471 -    UCX_TEST_BEGIN
   1.472 -
   1.473 -    const char* teststring = "this is way too much";
   1.474 -    r = ucx_buffer_write((void*)teststring, 1, 20, b);
   1.475 -    buffer = (char*) b->space; /*autoextend enabled, we MUST retrieve pointer*/
   1.476 -    UCX_TEST_ASSERT(r == 20, "not all characters written");
   1.477 -    UCX_TEST_ASSERT(b->capacity == 32, "buffer not properly extended");
   1.478 -    UCX_TEST_ASSERT(b->pos == 20, "position incorrect");
   1.479 -    UCX_TEST_ASSERT(memcmp(buffer,
   1.480 -            "this is way too much\0\0\0\0\0\0\0\0\0\0\0\0", 32) == 0,
   1.481 -            "incorrect buffer content");
   1.482 -
   1.483 -    UCX_TEST_END
   1.484 -
   1.485 -    ucx_buffer_free(b);
   1.486 -}
   1.487 -
   1.488 -UCX_TEST(test_ucx_buffer_read) {
   1.489 -    UcxBuffer *b = ucx_buffer_new(NULL, 8, UCX_BUFFER_AUTOFREE);
   1.490 -    ucx_buffer_write("01234567", 1, 8, b);
   1.491 -    b->pos = 0;
   1.492 -    
   1.493 -    char buf[32];
   1.494 -    memset(buf, 'X', 32);
   1.495 -    int r;
   1.496 -    
   1.497 -    UCX_TEST_BEGIN
   1.498 -    
   1.499 -    ucx_buffer_seek(b, 2, SEEK_SET);
   1.500 -    r = ucx_buffer_read(buf, 1, 2, b);
   1.501 -    UCX_TEST_ASSERT(r == 2, "wrong number of bytes read (2 items)");
   1.502 -    UCX_TEST_ASSERT(buf[0] == '2' && buf[1] == '3' && buf[2] == 'X',
   1.503 -            "buffer incorrect after read");
   1.504 -    UCX_TEST_ASSERT(b->pos == 4, "wrong position after read (2 items)");
   1.505 -    
   1.506 -    UCX_TEST_END
   1.507 -    
   1.508 -    ucx_buffer_free(b);
   1.509 -}
   1.510 -
   1.511 -UCX_TEST(test_ucx_buffer_read_oob) {
   1.512 -    UcxBuffer *b = ucx_buffer_new(NULL, 8, UCX_BUFFER_AUTOFREE);
   1.513 -    ucx_buffer_write("01234567", 1, 8, b);
   1.514 -    
   1.515 -    char buf[32];
   1.516 -    memset(buf, 'X', 32);
   1.517 -    int r;
   1.518 -    
   1.519 -    UCX_TEST_BEGIN
   1.520 -
   1.521 -    b->pos = 2;
   1.522 -    r = ucx_buffer_read(buf + 2, 1, 8, b);
   1.523 -    UCX_TEST_ASSERT(r == 6, "wrong number of bytes read (8 items)");
   1.524 -    UCX_TEST_ASSERT(memcmp(buf, "XX234567XX", 10) == 0,
   1.525 -            "buffer incorrect after read");
   1.526 -    UCX_TEST_ASSERT(b->pos == 8,
   1.527 -        "wrong position after read (8 items out of bound)");
   1.528 -
   1.529 -    b->pos = 0;
   1.530 -    memset(buf, 'X', 32);
   1.531 -    
   1.532 -    r = ucx_buffer_read(buf, 3, 3, b);
   1.533 -    
   1.534 -    UCX_TEST_ASSERT(r == 2, "wrong number of blocks read");
   1.535 -    UCX_TEST_ASSERT(memcmp(buf, "012345XX", 8) == 0,
   1.536 -            "buffer incorrect after block out of bounds read");
   1.537 -    
   1.538 -    r = ucx_buffer_read(buf+6, 1, 5, b);
   1.539 -    
   1.540 -    UCX_TEST_ASSERT(r == 2, "wrong number of remaining bytes read");
   1.541 -    UCX_TEST_ASSERT(memcmp(buf, "01234567XX", 10) == 0,
   1.542 -            "buffer incorrect after remaining byte read");
   1.543 -    
   1.544 -    UCX_TEST_END
   1.545 -    
   1.546 -    ucx_buffer_free(b);
   1.547 -}
   1.548 -
   1.549 -UCX_TEST(test_ucx_buffer_extract) {
   1.550 -    char *buffer = (char*) malloc(16);
   1.551 -    strcpy(buffer, "this is a test!");
   1.552 -
   1.553 -    UcxBuffer *src = ucx_buffer_new(buffer, 16, UCX_BUFFER_AUTOFREE);
   1.554 -    src->size = 16;
   1.555 -    UcxBuffer *dst = ucx_buffer_extract(src, 5, 5, UCX_BUFFER_AUTOEXTEND);
   1.556 -
   1.557 -    UCX_TEST_BEGIN
   1.558 -    UCX_TEST_ASSERT(dst != NULL, "ucx_buffer_extract returned NULL");
   1.559 -    UCX_TEST_ASSERT(dst->flags == (UCX_BUFFER_AUTOEXTEND | UCX_BUFFER_AUTOFREE),
   1.560 -            "autofree flag shall be enforced");
   1.561 -    UCX_TEST_ASSERT(dst->size == 5, "wrong size for new buffer");
   1.562 -    UCX_TEST_ASSERT(dst->capacity == 5, "wrong capacity for new buffer");
   1.563 -    UCX_TEST_ASSERT(dst->pos == 0, "wrong position for new buffer");
   1.564 -    char rb[5];
   1.565 -    ucx_buffer_read(rb, 1, 5, dst);
   1.566 -    UCX_TEST_ASSERT(memcmp(rb, "is a ", 5) == 0,
   1.567 -            "new buffer has incorrect content");
   1.568 -
   1.569 -    UCX_TEST_END
   1.570 -
   1.571 -    ucx_buffer_free(dst);
   1.572 -    ucx_buffer_free(src);
   1.573 -}
   1.574 -
   1.575 -UCX_TEST(test_ucx_buffer_extract_oob) {
   1.576 -    char *buffer = (char*) malloc(16);
   1.577 -    strcpy(buffer, "this is a test!");
   1.578 -
   1.579 -    UcxBuffer *src = ucx_buffer_new(buffer, 16, UCX_BUFFER_AUTOFREE);
   1.580 -    src->size = 16;
   1.581 -    
   1.582 -    UCX_TEST_BEGIN
   1.583 -    
   1.584 -    UCX_TEST_ASSERT(ucx_buffer_extract(src, 5, 0, UCX_BUFFER_DEFAULT) == NULL,
   1.585 -            "extract shall fail on zero length");
   1.586 -    UCX_TEST_ASSERT(ucx_buffer_extract(src, 10, 10, UCX_BUFFER_DEFAULT) == NULL,
   1.587 -            "extract shall fail on invalid bounds (size exceeds limits)");
   1.588 -    UCX_TEST_ASSERT(ucx_buffer_extract(src, 20, -7, UCX_BUFFER_DEFAULT) == NULL,
   1.589 -            "extract shall fail on invalid bounds (start exceeds limits)");
   1.590 -
   1.591 -    UCX_TEST_END
   1.592 -
   1.593 -    ucx_buffer_free(src);
   1.594 -}
   1.595 -
   1.596 -UCX_TEST(test_ucx_buffer_extract_overflow) {
   1.597 -    char *buffer = (char*) malloc(16);
   1.598 -    strcpy(buffer, "this is a test!");
   1.599 -
   1.600 -    UcxBuffer *src = ucx_buffer_new(buffer, 16, UCX_BUFFER_AUTOFREE);
   1.601 -    src->size = 16;
   1.602 -    
   1.603 -    UCX_TEST_BEGIN
   1.604 -    
   1.605 -    UCX_TEST_ASSERT(ucx_buffer_extract(src, 5, (size_t)-4,
   1.606 -        UCX_BUFFER_DEFAULT) == NULL, "extract shall fail on integer overflow");
   1.607 -
   1.608 -    UCX_TEST_END
   1.609 -
   1.610 -    ucx_buffer_free(src);
   1.611 -}
   1.612 -
   1.613 -UCX_TEST(test_ucx_buffer_extend) {
   1.614 -    
   1.615 -    UcxBuffer *b = ucx_buffer_new(NULL, 10, UCX_BUFFER_DEFAULT);
   1.616 -    
   1.617 -    UCX_TEST_BEGIN
   1.618 -    
   1.619 -    UCX_TEST_ASSERT(ucx_buffer_extend(b, 15) == 0, "shall return 0 on success");
   1.620 -    UCX_TEST_ASSERT(b->capacity = 40, "wrong capacity");
   1.621 -    UCX_TEST_ASSERT((b->size == 0 && b->pos == 0),
   1.622 -        "pos and size shall remain unchanged");
   1.623 -    
   1.624 -    UCX_TEST_ASSERT(ucx_buffer_extend(b, (size_t) - 61) != 0,
   1.625 -        "shall fail and return a non-zero value on overflow");
   1.626 -    
   1.627 -    UCX_TEST_END
   1.628 -    
   1.629 -    ucx_buffer_free(b);
   1.630 -}
   1.631 -
   1.632 -UCX_TEST(test_ucx_buffer_shl) {
   1.633 -    
   1.634 -    const char* hw = "Shift the World!";
   1.635 -    
   1.636 -    UcxBuffer *b = ucx_buffer_new(NULL, 20, UCX_BUFFER_DEFAULT);
   1.637 -    ucx_buffer_puts(b, hw);
   1.638 -    b->pos = 5;
   1.639 -    
   1.640 -    UCX_TEST_BEGIN
   1.641 -    char* expected;
   1.642 -    
   1.643 -    ucx_buffer_shift_left(b, 2);
   1.644 -    expected = "ift the World!";
   1.645 -    
   1.646 -    UCX_TEST_ASSERT(b->pos == 3, "position after normal shl wrong");
   1.647 -    UCX_TEST_ASSERT(b->size == strlen(expected), "size after normal shl wrong");
   1.648 -    UCX_TEST_ASSERT(!memcmp(b->space, expected, b->size),
   1.649 -            "contents after normal shl wrong");
   1.650 -    
   1.651 -    
   1.652 -    ucx_buffer_shift_left(b, 5);
   1.653 -    expected = "he World!";
   1.654 -    
   1.655 -    UCX_TEST_ASSERT(b->pos == 0, "position after overshift left wrong");
   1.656 -    UCX_TEST_ASSERT(b->size == strlen(expected),
   1.657 -            "size after overshift left wrong");
   1.658 -    UCX_TEST_ASSERT(!memcmp(b->space, expected, b->size),
   1.659 -            "contents after overshift left wrong");
   1.660 -    
   1.661 -    ucx_buffer_shift_left(b, 10);
   1.662 -    UCX_TEST_ASSERT(b->pos == 0, "position after 'shl everything away' wrong");
   1.663 -    UCX_TEST_ASSERT(b->size == 0, "size after 'shl everything away' wrong");
   1.664 -    
   1.665 -    UCX_TEST_END
   1.666 -    
   1.667 -    ucx_buffer_free(b);    
   1.668 -}
   1.669 -
   1.670 -UCX_TEST(test_ucx_buffer_shr) {
   1.671 -    
   1.672 -    const char* hw = "Shift the World!";
   1.673 -    
   1.674 -    UcxBuffer *b = ucx_buffer_new(NULL, 20, UCX_BUFFER_DEFAULT);
   1.675 -    ucx_buffer_puts(b, hw);
   1.676 -    b->pos = 12;
   1.677 -    
   1.678 -    UCX_TEST_BEGIN
   1.679 -    char* expected;
   1.680 -    
   1.681 -    ucx_buffer_shift_right(b, 2);
   1.682 -    expected = "ShShift the World!";
   1.683 -    
   1.684 -    UCX_TEST_ASSERT(b->pos == 14, "position after normal shr wrong");
   1.685 -    UCX_TEST_ASSERT(b->size == strlen(expected), "size after normal shr wrong");
   1.686 -    UCX_TEST_ASSERT(!memcmp(b->space, expected, b->size),
   1.687 -            "contents after normal shr wrong");
   1.688 -    
   1.689 -    
   1.690 -    ucx_buffer_shift_right(b, 5);
   1.691 -    expected = "ShShiShShift the Wor";
   1.692 -    UCX_TEST_ASSERT(strlen(expected) == b->capacity,
   1.693 -            "Test data is wrong, please fix the test.");
   1.694 -    
   1.695 -    UCX_TEST_ASSERT(b->pos == 19,
   1.696 -            "position after overshift right w/o auto-extend wrong");
   1.697 -    UCX_TEST_ASSERT(b->size == 20,
   1.698 -            "size after overshift right w/o auto-extend wrong");
   1.699 -    UCX_TEST_ASSERT(b->capacity == 20,
   1.700 -            "capacity after overshift right w/o auto-extend wrong");
   1.701 -    UCX_TEST_ASSERT(!memcmp(b->space, expected, b->size),
   1.702 -            "contents after overshift right w/o auto-extend wrong");
   1.703 -    
   1.704 -    ucx_buffer_shift_right(b, 15);
   1.705 -    UCX_TEST_ASSERT(b->pos == b->capacity, "position after 'shr to eof' wrong");
   1.706 -    UCX_TEST_ASSERT(b->size == b->capacity, "size after 'shr to eof' wrong");
   1.707 -    UCX_TEST_ASSERT(ucx_buffer_eof(b), "buffer not eof after 'shr to eof'");
   1.708 -    
   1.709 -    UCX_TEST_END
   1.710 -    
   1.711 -    ucx_buffer_free(b);    
   1.712 -}
   1.713 -
   1.714 -UCX_TEST(test_ucx_buffer_shr_ax) {
   1.715 -    
   1.716 -    const char* hw = "Shift the World!";
   1.717 -    
   1.718 -    UcxBuffer *b = ucx_buffer_new(NULL, 20, UCX_BUFFER_AUTOEXTEND);
   1.719 -    ucx_buffer_puts(b, hw);
   1.720 -    b->pos = 12;
   1.721 -    
   1.722 -    UCX_TEST_BEGIN
   1.723 -    
   1.724 -    const char* expected = "Shift the Shift the World!";
   1.725 -    
   1.726 -    ucx_buffer_shift_right(b, 10);
   1.727 -    UCX_TEST_ASSERT(b->pos == 22, "position after shr w/ auto-extend wrong");
   1.728 -    UCX_TEST_ASSERT(b->size == strlen(expected),
   1.729 -            "size after shr w/ auto-extend wrong");
   1.730 -    UCX_TEST_ASSERT(b->capacity >= b->size,
   1.731 -            "auto-extension of capacity after shr w/ auto-extend failed");
   1.732 -    UCX_TEST_ASSERT(!ucx_buffer_eof(b),
   1.733 -            "buffer should not be eof after shr w/ auto-extend");
   1.734 -    UCX_TEST_ASSERT(!memcmp(b->space, expected, b->size),
   1.735 -            "contents wrong after shr w/ auto-extend");
   1.736 -    
   1.737 -    UCX_TEST_END
   1.738 -    
   1.739 -    ucx_buffer_free(b);    
   1.740 -}
   1.741 -

mercurial