test/buffer_tests.c

Mon, 27 Feb 2017 11:45:31 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 27 Feb 2017 11:45:31 +0100
changeset 238
27b31c2c959c
parent 230
4044131874f9
child 250
b7d1317b138e
permissions
-rw-r--r--

fixes major bug in construction of KMP prefix table for strstr()

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2016 Olaf Wintermann. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "buffer_tests.h"
#include <stdint.h>

UCX_TEST(test_ucx_buffer_new) {
    UcxBuffer *b = ucx_buffer_new(NULL, 16, UCX_BUFFER_AUTOEXTEND);
    UcxBuffer *b2 = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT);
    UCX_TEST_BEGIN

    UCX_TEST_ASSERT(b->capacity==16, "wrong capacity");
    UCX_TEST_ASSERT(b2->capacity==32, "wrong capacity");
    
    UCX_TEST_ASSERT(b->size==0, "wrong size");
    UCX_TEST_ASSERT(b2->size==0, "wrong size");
    
    UCX_TEST_ASSERT(b->pos==0, "wrong position");
    UCX_TEST_ASSERT(b2->pos==0, "wrong position");
    
    UCX_TEST_ASSERT(b->flags==(UCX_BUFFER_AUTOEXTEND|UCX_BUFFER_AUTOFREE),
        "wrong flags for autoextending buffer");
    UCX_TEST_ASSERT(b2->flags==UCX_BUFFER_AUTOFREE,
        "wrong flags for default bufer");
    
    UCX_TEST_END
    ucx_buffer_free(b2);
    ucx_buffer_free(b);
}

UCX_TEST(test_ucx_buffer_new_prealloc) {
    char* test = (char*) malloc(16);
    UcxBuffer *b = ucx_buffer_new(test, 16, UCX_BUFFER_DEFAULT);
    UCX_TEST_BEGIN

    UCX_TEST_ASSERT(b->capacity==16, "wrong capacity");
    UCX_TEST_ASSERT(b->size==0, "wrong size");    
    UCX_TEST_ASSERT(b->pos==0, "wrong position");
    
    UCX_TEST_ASSERT(b->flags==0, "wrong flags - all should be cleared");
    
    UCX_TEST_END
    free(test);
    ucx_buffer_free(b);
}

UCX_TEST(test_ucx_buffer_eof) {
    char *test = (char*)"0123456789ABCDEF";
    UcxBuffer *b = ucx_buffer_new(test, 16, UCX_BUFFER_DEFAULT);
    UCX_TEST_BEGIN
    b->pos = 9; b->size = 10;
    UCX_TEST_ASSERT(!ucx_buffer_eof(b), "false positive");
    b->pos = 10; b->size = 10;
    UCX_TEST_ASSERT(ucx_buffer_eof(b), "pos == size should be EOF");
    b->pos = 11; b->size = 10;
    UCX_TEST_ASSERT(ucx_buffer_eof(b), "false negative");
    UCX_TEST_END
    ucx_buffer_free(b);
}

UCX_TEST(test_ucx_buffer_seek_overflow) {
    UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT);
    b->size = 32;
    int r;
    
    UCX_TEST_BEGIN
    const size_t sizemax = (size_t)-1;
    size_t bigpos = sizemax - 5000;
    b->pos = bigpos;
    r = ucx_buffer_seek(b, 5016, SEEK_CUR);
    UCX_TEST_ASSERT(r != 0, "seek cur overflow");
    UCX_TEST_ASSERT(b->pos == bigpos,
            "failed seek shall leave pos unchanged");
    
    b->pos = 0;
    b->size = (sizemax >> 1) + 32;
    
    // we don't want to risk overflows in implicit constant casts
    const size_t bigoff_comp = (sizemax >> 1) - 16;
    off_t bigoff = (off_t)bigoff_comp;
    
    r = ucx_buffer_seek(b, -bigoff, SEEK_CUR);
    UCX_TEST_ASSERT(r != 0, "seek cur underflow");
    UCX_TEST_ASSERT(b->pos == 0,
            "failed seek shall leave pos unchanged");
    
    UCX_TEST_END
    
    ucx_buffer_free(b);
}

UCX_TEST(test_ucx_buffer_seek_invalid) {
    UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT);
    b->pos = 7;
    int r;
    
    UCX_TEST_BEGIN
    r = ucx_buffer_seek(b, 0, ~(SEEK_SET|SEEK_CUR|SEEK_END));
    UCX_TEST_ASSERT(r != 0, "invalid whence shall fail");
    UCX_TEST_ASSERT(b->pos == 7,
            "failed seek shall leave pos unchanged");
    UCX_TEST_END
    
    ucx_buffer_free(b);
}

UCX_TEST(test_ucx_buffer_seek_oob) {
    UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT);
    b->size = 16; // less than capacity
    b->pos = 7;
    int r;

    UCX_TEST_BEGIN
    
    r = ucx_buffer_seek(b, -1, SEEK_SET);
    UCX_TEST_ASSERT(r != 0, "seek SET below bounds shall fail");
    UCX_TEST_ASSERT(b->pos == 7,
            "failed seek shall leave pos unchanged");
    
    r = ucx_buffer_seek(b, 16, SEEK_SET);
    UCX_TEST_ASSERT(r != 0, "seek SET above bounds shall fail");
    UCX_TEST_ASSERT(b->pos == 7,
            "failed seek shall leave pos unchanged");
    
    r = ucx_buffer_seek(b, -8, SEEK_CUR);
    UCX_TEST_ASSERT(r != 0, "seek CUR below bounds shall fail");
    UCX_TEST_ASSERT(b->pos == 7,
            "failed seek shall leave pos unchanged");
    
    r = ucx_buffer_seek(b, 9, SEEK_CUR);
    UCX_TEST_ASSERT(r != 0, "seek CUR above bounds shall fail");
    UCX_TEST_ASSERT(b->pos == 7,
            "failed seek shall leave pos unchanged");
    
    r = ucx_buffer_seek(b, -17, SEEK_END);
    UCX_TEST_ASSERT(r != 0, "seek END below bounds shall fail");
    UCX_TEST_ASSERT(b->pos == 7,
            "failed seek shall leave pos unchanged");
    
    r = ucx_buffer_seek(b, 1, SEEK_END);
    UCX_TEST_ASSERT(r != 0, "seek END above bounds shall fail");
    UCX_TEST_ASSERT(b->pos == 7,
            "failed seek shall leave pos unchanged");

    UCX_TEST_END

    ucx_buffer_free(b);
}

UCX_TEST(test_ucx_buffer_seek_set) {
    UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT);
    b->size = 16;
    int r;

    UCX_TEST_BEGIN

    r = ucx_buffer_seek(b, 5, SEEK_SET);
    UCX_TEST_ASSERT(r == 0, "seek SET+5 failed");
    UCX_TEST_ASSERT(b->pos == 5, "seek SET+5 set wrong position");
    
    
    r = ucx_buffer_seek(b, 10, SEEK_SET);
    UCX_TEST_ASSERT(r == 0, "seek SET+10 failed");
    UCX_TEST_ASSERT(b->pos == 10, "seek SET+10 set wrong position");

    UCX_TEST_END

    ucx_buffer_free(b);
}

UCX_TEST(test_ucx_buffer_seek_cur) {
    UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT);
    b->size = 16;
    int r;

    UCX_TEST_BEGIN
    
    b->pos = 7;
    r = ucx_buffer_seek(b, 5, SEEK_CUR);
    UCX_TEST_ASSERT(r == 0, "seek CUR+5 failed");
    UCX_TEST_ASSERT(b->pos == 12, "seek CUR+5 set wrong position");

    UCX_TEST_END

    ucx_buffer_free(b);
}

UCX_TEST(test_ucx_buffer_seek_end) {
    UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT);
    b->size = 16;
    int r;

    UCX_TEST_BEGIN
    
    r = ucx_buffer_seek(b, -5, SEEK_END);
    UCX_TEST_ASSERT(r == 0, "seek END-5 failed");
    UCX_TEST_ASSERT(b->pos == 11, "seek END-5 set wrong position");


    UCX_TEST_END

    ucx_buffer_free(b);
}

UCX_TEST(test_ucx_buffer_putc) {
    char *buffer = (char*) malloc(16);
    memset(buffer, 32, 16);

    UcxBuffer *b = ucx_buffer_new(buffer, 16, UCX_BUFFER_DEFAULT);
    b->size = b->capacity;

    UCX_TEST_BEGIN

    ucx_buffer_seek(b, 0, SEEK_SET);
    UCX_TEST_ASSERT(ucx_buffer_putc(b, '0'|~0xFF) == '0',
            "putc shall return (arg & 0xFF)");
    ucx_buffer_putc(b, '0');
    ucx_buffer_putc(b, '0');
    
    UCX_TEST_ASSERT(b->pos == 3, "pos wrong after first 3 puts");
    ucx_buffer_seek(b, 10, SEEK_CUR);
    
    ucx_buffer_putc(b, '0');
    ucx_buffer_putc(b, '0');
    ucx_buffer_putc(b, '0');
    
    UCX_TEST_ASSERT(b->pos == 16, "pos wrong after last 3 puts");
    
    UCX_TEST_ASSERT(!memcmp(b->space, "000          000", 16),
            "buffer content wrong")
    UCX_TEST_END
    ucx_buffer_free(b);
    free(buffer);
}
    
UCX_TEST(test_ucx_buffer_putc_oob) {
    UcxBuffer *b = ucx_buffer_new(NULL, 2, UCX_BUFFER_DEFAULT);
    
    UCX_TEST_BEGIN
    b->pos = b->size = b->capacity = 1;
    b->space[1] = 'X';
    
    UCX_TEST_ASSERT(ucx_buffer_putc(b, 48) == EOF, "put shall return EOF "
            "when buffer is full and auto extend is disabled");
    UCX_TEST_ASSERT(!memcmp(b->space, "\0X", 2),
            "wrong buffer content after failed putc");

    UCX_TEST_END

    ucx_buffer_free(b);
}


UCX_TEST(test_ucx_buffer_putc_ae) {
    UcxBuffer *b = ucx_buffer_new(NULL, 2, UCX_BUFFER_AUTOEXTEND);
    ucx_buffer_putc(b, '0');
    ucx_buffer_putc(b, '1');
    
    UCX_TEST_BEGIN
    
    UCX_TEST_ASSERT(b->pos == 2, "pos wrong after first 2 puts");
    UCX_TEST_ASSERT(b->size == 2, "size wrong after first 2 puts");
    UCX_TEST_ASSERT(b->capacity == 2, "buffer erroneously extended");
    UCX_TEST_ASSERT(!memcmp(b->space,"01", 2), "wrong content");
    
    UCX_TEST_END
    
    ucx_buffer_free(b);
}

UCX_TEST(test_ucx_buffer_putc_oobae) {
    UcxBuffer *b = ucx_buffer_new(NULL, 2, UCX_BUFFER_AUTOEXTEND);
    ucx_buffer_putc(b, '0');
    ucx_buffer_putc(b, '1');
    
    UCX_TEST_BEGIN
    
    ucx_buffer_putc(b, 'a');
    
    UCX_TEST_ASSERT(b->pos == 3, "pos wrong after put");
    UCX_TEST_ASSERT(b->capacity == 4, "buffer not properly extended");
    UCX_TEST_ASSERT(b->size == 3, "wrong buffer size");
    
    UCX_TEST_ASSERT(!memcmp(b->space,"01a\0", 4), "wrong content");
    
    UCX_TEST_END
    
    ucx_buffer_free(b);
}

UCX_TEST(test_ucx_buffer_putc_size) {
    UcxBuffer *b = ucx_buffer_new(NULL, 4, UCX_BUFFER_DEFAULT);
    
    UCX_TEST_BEGIN
    
    UCX_TEST_ASSERT(b->size == 0, "wrong initial size");
    ucx_buffer_putc(b, 'a');
    ucx_buffer_putc(b, 'b');
    ucx_buffer_putc(b, 'c');
    UCX_TEST_ASSERT(b->size == 3, "size does not increase");
    ucx_buffer_seek(b, 1, SEEK_SET);
    ucx_buffer_putc(b, 'd');
    UCX_TEST_ASSERT(b->size == 3, "size shall not decrease");
    UCX_TEST_ASSERT(b->pos == 2, "wrong position after seek and putc");

    UCX_TEST_END

    ucx_buffer_free(b);
}

UCX_TEST(test_ucx_buffer_getc) {
    char *buffer = (char*) malloc(16);
    memset(buffer, 32, 8);
    for (int i = 8; i < 16 ; i++) {
        buffer[i] = 40+i;
    }

    UcxBuffer *b = ucx_buffer_new(buffer, 16, UCX_BUFFER_DEFAULT);
    b->size = b->capacity;

    UCX_TEST_BEGIN

    char rb[16];
    for (size_t i = 0 ; i < 16 ; i++) {
        UCX_TEST_ASSERT(b->pos == i, "wrong position");
        UCX_TEST_ASSERT(!ucx_buffer_eof(b), "EOF false positive");
        rb[i] = ucx_buffer_getc(b);
    }
    UCX_TEST_ASSERT(memcmp(rb, "        01234567", 16) == 0,
        "read data incorrect");
        
    UCX_TEST_ASSERT(ucx_buffer_eof(b), "EOF not set after last possible read");
    UCX_TEST_ASSERT(b->pos == 16, "wrong position after EOF");
    
    UCX_TEST_ASSERT(ucx_buffer_getc(b) == EOF,
        "out of bounds read does not return EOF");
    UCX_TEST_ASSERT(b->pos == 16, "wrong position after out of bounds read");

    UCX_TEST_END

    ucx_buffer_free(b);
    free(buffer);
}

UCX_TEST(test_ucx_buffer_write) {
    char *buffer = (char*) malloc(32);
    memset(buffer, 0, 32);
    
    memset(buffer, 32, 8);
    for (int i = 8; i < 16 ; i++) {
        buffer[i] = 40+i;
    }

    UcxBuffer *b = ucx_buffer_new(buffer, 32, UCX_BUFFER_DEFAULT);
    int r;

    UCX_TEST_BEGIN
    b->pos = 4;
    r = ucx_buffer_write("test string", 1, 11, b);
    UCX_TEST_ASSERT(r == 11, "returned incorrect number of written bytes");
    UCX_TEST_ASSERT(b->pos == 15, "incorrect position");
    UCX_TEST_ASSERT(memcmp(buffer, "    test string7\0\0", 18) == 0,
        "incorrect buffer content (test string)");
    
    r = ucx_buffer_write(".", 1, 1, b);
    UCX_TEST_ASSERT(r == 1, "returned incorrect number of written elements");
    UCX_TEST_ASSERT(b->pos == 16, "incorrect position");
    
    int32_t testarr[4] = {0x09abcdef, 0x05fedcba, 0x01abefcd, 0x3cd07ab};
    r = ucx_buffer_write(testarr, 4, 4, b);
    UCX_TEST_ASSERT(r = 4, "returned incorrect number of written elements");
    UCX_TEST_ASSERT(b->pos == 32, "incorrect position");
    
    char cmp[32];
    memset(cmp, 0, 32);
    memcpy(cmp, "    test string.", 16);
    int32_t *ptr = (int32_t*) (cmp+16);
    ptr[0] = testarr[0];
    ptr[1] = testarr[1];
    ptr[2] = testarr[2];
    ptr[3] = testarr[3];
    UCX_TEST_ASSERT(memcmp(buffer, cmp, 32) == 0,
        "incorrect buffer content (int array)");
    
    UCX_TEST_END

    ucx_buffer_free(b);
    free(buffer);
}

UCX_TEST(test_ucx_buffer_write_oob) {
    char *buffer = (char*) malloc(32);
    memset(buffer, 0, 32);

    UcxBuffer *b = ucx_buffer_new(buffer, 15, UCX_BUFFER_DEFAULT);
    int r;

    UCX_TEST_BEGIN
    r = ucx_buffer_write("a very long string", 1, 18, b);
    UCX_TEST_ASSERT(r == 15, "incorrect number of written bytes");
    UCX_TEST_ASSERT(memcmp(buffer, "a very long str\0\0\0", 18) == 0,
        "invalid buffer content (test string)");
    
    b->size = b->pos = 0;
    int32_t intarr[4] = {0,-1,0,-1};
    memset(buffer, 0, 32);
    
    r = ucx_buffer_write(intarr, 4, 4, b);
    UCX_TEST_ASSERT(r == 3, "incorrect number of written elements");
    UCX_TEST_ASSERT(memcmp(buffer,
        "\0\0\0\0\xff\xff\xff\xff\0\0\0\0\0\0\0\0", 16) == 0,
        "invalid buffer content (test string)");
    
    UCX_TEST_END

    ucx_buffer_free(b);
    free(buffer);
}

UCX_TEST(test_ucx_buffer_write_ax) {
    char *buffer = (char*) malloc(16);
    memset(buffer, 0, 16);

    UcxBuffer *b = ucx_buffer_new(buffer, 16,
            UCX_BUFFER_AUTOEXTEND | UCX_BUFFER_AUTOFREE);
    int r;

    UCX_TEST_BEGIN

    const char* teststring = "this is way too much";
    r = ucx_buffer_write((void*)teststring, 1, 20, b);
    buffer = (char*) b->space; /*autoextend enabled, we MUST retrieve pointer*/
    UCX_TEST_ASSERT(r == 20, "not all characters written");
    UCX_TEST_ASSERT(b->capacity == 32, "buffer not properly extended");
    UCX_TEST_ASSERT(b->pos == 20, "position incorrect");
    UCX_TEST_ASSERT(memcmp(buffer,
            "this is way too much\0\0\0\0\0\0\0\0\0\0\0\0", 32) == 0,
            "incorrect buffer content");

    UCX_TEST_END

    ucx_buffer_free(b);
}

UCX_TEST(test_ucx_buffer_read) {
    UcxBuffer *b = ucx_buffer_new(NULL, 8, UCX_BUFFER_AUTOFREE);
    ucx_buffer_write("01234567", 1, 8, b);
    b->pos = 0;
    
    char buf[32];
    memset(buf, 'X', 32);
    int r;
    
    UCX_TEST_BEGIN
    
    ucx_buffer_seek(b, 2, SEEK_SET);
    r = ucx_buffer_read(buf, 1, 2, b);
    UCX_TEST_ASSERT(r == 2, "wrong number of bytes read (2 items)");
    UCX_TEST_ASSERT(buf[0] == '2' && buf[1] == '3' && buf[2] == 'X',
            "buffer incorrect after read");
    UCX_TEST_ASSERT(b->pos == 4, "wrong position after read (2 items)");
    
    UCX_TEST_END
    
    ucx_buffer_free(b);
}

UCX_TEST(test_ucx_buffer_read_oob) {
    UcxBuffer *b = ucx_buffer_new(NULL, 8, UCX_BUFFER_AUTOFREE);
    ucx_buffer_write("01234567", 1, 8, b);
    
    char buf[32];
    memset(buf, 'X', 32);
    int r;
    
    UCX_TEST_BEGIN

    b->pos = 2;
    r = ucx_buffer_read(buf + 2, 1, 8, b);
    UCX_TEST_ASSERT(r == 6, "wrong number of bytes read (8 items)");
    UCX_TEST_ASSERT(memcmp(buf, "XX234567XX", 10) == 0,
            "buffer incorrect after read");
    UCX_TEST_ASSERT(b->pos == 8,
        "wrong position after read (8 items out of bound)");

    b->pos = 0;
    memset(buf, 'X', 32);
    
    r = ucx_buffer_read(buf, 3, 3, b);
    
    UCX_TEST_ASSERT(r == 2, "wrong number of blocks read");
    UCX_TEST_ASSERT(memcmp(buf, "012345XX", 8) == 0,
            "buffer incorrect after block out of bounds read");
    
    r = ucx_buffer_read(buf+6, 1, 5, b);
    
    UCX_TEST_ASSERT(r == 2, "wrong number of remaining bytes read");
    UCX_TEST_ASSERT(memcmp(buf, "01234567XX", 10) == 0,
            "buffer incorrect after remaining byte read");
    
    UCX_TEST_END
    
    ucx_buffer_free(b);
}

UCX_TEST(test_ucx_buffer_extract) {
    char *buffer = (char*) malloc(16);
    strcpy(buffer, "this is a test!");

    UcxBuffer *src = ucx_buffer_new(buffer, 16, UCX_BUFFER_AUTOFREE);
    src->size = 16;
    UcxBuffer *dst = ucx_buffer_extract(src, 5, 5, UCX_BUFFER_AUTOEXTEND);

    UCX_TEST_BEGIN
    UCX_TEST_ASSERT(dst != NULL, "ucx_buffer_extract returned NULL");
    UCX_TEST_ASSERT(dst->flags == (UCX_BUFFER_AUTOEXTEND | UCX_BUFFER_AUTOFREE),
            "autofree flag shall be enforced");
    UCX_TEST_ASSERT(dst->size == 5, "wrong size for new buffer");
    UCX_TEST_ASSERT(dst->capacity == 5, "wrong capacity for new buffer");
    UCX_TEST_ASSERT(dst->pos == 0, "wrong position for new buffer");
    char rb[5];
    ucx_buffer_read(rb, 1, 5, dst);
    UCX_TEST_ASSERT(memcmp(rb, "is a ", 5) == 0,
            "new buffer has incorrect content");

    UCX_TEST_END

    ucx_buffer_free(dst);
    ucx_buffer_free(src);
}

UCX_TEST(test_ucx_buffer_extract_oob) {
    char *buffer = (char*) malloc(16);
    strcpy(buffer, "this is a test!");

    UcxBuffer *src = ucx_buffer_new(buffer, 16, UCX_BUFFER_AUTOFREE);
    src->size = 16;
    
    UCX_TEST_BEGIN
    
    UCX_TEST_ASSERT(ucx_buffer_extract(src, 5, 0, UCX_BUFFER_DEFAULT) == NULL,
            "extract shall fail on zero length");
    UCX_TEST_ASSERT(ucx_buffer_extract(src, 10, 10, UCX_BUFFER_DEFAULT) == NULL,
            "extract shall fail on invalid bounds (size exceeds limits)");
    UCX_TEST_ASSERT(ucx_buffer_extract(src, 20, -7, UCX_BUFFER_DEFAULT) == NULL,
            "extract shall fail on invalid bounds (start exceeds limits)");

    UCX_TEST_END

    ucx_buffer_free(src);
}

UCX_TEST(test_ucx_buffer_extract_overflow) {
    char *buffer = (char*) malloc(16);
    strcpy(buffer, "this is a test!");

    UcxBuffer *src = ucx_buffer_new(buffer, 16, UCX_BUFFER_AUTOFREE);
    src->size = 16;
    
    UCX_TEST_BEGIN
    
    UCX_TEST_ASSERT(ucx_buffer_extract(src, 5, (size_t)-4,
        UCX_BUFFER_DEFAULT) == NULL, "extract shall fail on integer overflow");

    UCX_TEST_END

    ucx_buffer_free(src);
}

UCX_TEST(test_ucx_buffer_extend) {
    
    UcxBuffer *b = ucx_buffer_new(NULL, 10, UCX_BUFFER_DEFAULT);
    
    UCX_TEST_BEGIN
    
    UCX_TEST_ASSERT(ucx_buffer_extend(b, 15) == 0, "shall return 0 on success");
    UCX_TEST_ASSERT(b->capacity = 40, "wrong capacity");
    UCX_TEST_ASSERT((b->size == 0 && b->pos == 0),
        "pos and size shall remain unchanged");
    
    UCX_TEST_ASSERT(ucx_buffer_extend(b, (size_t) - 61) != 0,
        "shall fail and return a non-zero value on overflow");
    
    UCX_TEST_END
    
    ucx_buffer_free(b);
}

mercurial