test/buffer_tests.c

Mon, 09 Sep 2013 11:54:32 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 09 Sep 2013 11:54:32 +0200
changeset 156
6a4bd1f4c57e
parent 147
1aa598f36872
child 163
5ec9a2ca6328
permissions
-rw-r--r--

removed unnecessary configurations + added build command for shared lib

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2013 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 "ucx/utils.h"

UCX_TEST(test_ucx_buffer_seektell) {
    UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT);
    b->size = 16; // less than capacity
    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, 20, SEEK_SET);
    UCX_TEST_ASSERT(r != 0, "seek beyond bounds shall fail");
    UCX_TEST_ASSERT(b->pos == 5,
            "failed seek shall leave pos unchanged");

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

    r = ucx_buffer_seek(b, 10, SEEK_CUR);
    UCX_TEST_ASSERT(r != 0, "seek CUR beyond bounds shall fail");
    UCX_TEST_ASSERT(b->pos == 10,
            "failed seek shall leave pos unchanged");

    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");

    r = ucx_buffer_seek(b, -20, SEEK_END);
    UCX_TEST_ASSERT(r != 0, "seek END beyond bounds shall fail");
    UCX_TEST_ASSERT(b->pos == 11,
            "failed seek shall leave pos unchanged");

    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_putc(b, '0');
    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(ucx_buffer_eof(b), "eof not set");
    UCX_TEST_ASSERT(ucx_buffer_putc(b, 48) == EOF,
            "put shall return EOF when buffer is full");
    
    ucx_buffer_seek(b, 3, SEEK_SET);
    ucx_buffer_putc(b, 'a');
    ucx_buffer_putc(b, 'b');
    ucx_buffer_putc(b, 'c');
    
    UCX_TEST_ASSERT(b->size == 16, "wrong size after seek and puts");
    UCX_TEST_ASSERT(memcmp(buffer, "000abc       000", 16) == 0,
            "buffer contains incorrect content");

    UCX_TEST_END

    ucx_buffer_free(b);
    free(buffer);
}

UCX_TEST(test_ucx_buffer_putc_ax) {
    UcxBuffer *b = ucx_buffer_new(NULL, 2, UCX_BUFFER_AUTOEXTEND);
    
    UCX_TEST_BEGIN
    
    ucx_buffer_putc(b, '0');
    ucx_buffer_putc(b, '1');
    
    UCX_TEST_ASSERT(b->pos == 2, "pos wrong after first 2 puts");
    UCX_TEST_ASSERT(b->capacity == 2, "buffer erroneously extended");
    
    ucx_buffer_putc(b, 'a');
    
    UCX_TEST_ASSERT(b->pos == 3, "pos wrong after 1 put");
    UCX_TEST_ASSERT(b->capacity == 4, "buffer not properly extended");
    UCX_TEST_ASSERT(b->size == 3, "wrong buffer size");
    
    UCX_TEST_ASSERT(b->space[2] == 'a', "wrong content");
    
    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, "pos wrong during read loop");
        UCX_TEST_ASSERT(!ucx_buffer_eof(b),
                "EOF shall not be set during read loop");
        rb[i] = ucx_buffer_getc(b);
    }
    UCX_TEST_ASSERT(b->pos == 16, "pos wrong after read loop");
    UCX_TEST_ASSERT(ucx_buffer_eof(b), "EOF not set");
    UCX_TEST_ASSERT(memcmp(rb, "        01234567", 16) == 0,
            "read data incorrect");

    UCX_TEST_END

    ucx_buffer_free(b);
    free(buffer);
}

UCX_TEST(test_ucx_buffer_write) {
    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);
    int r;

    UCX_TEST_BEGIN

    const char* teststring = "this is way too much";
    r = ucx_buffer_write((void*)teststring, 1, 20, b);
    UCX_TEST_ASSERT(r == 16, "string not correctly trimed");
    UCX_TEST_ASSERT(memcmp(buffer, teststring, 16) == 0,
            "buffer data incorrect");
    UCX_TEST_ASSERT(ucx_buffer_eof(b), "eof shall be set");

    ucx_buffer_seek(b, 8, SEEK_SET);
    r = ucx_buffer_write((void*)"not", 1, 3, b);
    UCX_TEST_ASSERT(r == 3, "three bytes should be replace");
    UCX_TEST_ASSERT(memcmp(buffer, "this is not too much", 16) == 0,
            "modified buffer is incorrect");

    const char* threebytestring = "  t  h  r  e  e   ";
    memset(buffer, 49, 16);
    ucx_buffer_seek(b, 0, SEEK_SET);
    r = ucx_buffer_write((void*)threebytestring, 3, 6, b);
    UCX_TEST_ASSERT(r == 5, "three byte string not correctly trimed");
    UCX_TEST_ASSERT(b->pos == 15,
            "position after write of three byte string incorrect");
    UCX_TEST_ASSERT(!ucx_buffer_eof(b), "eof shall not be set");
    UCX_TEST_ASSERT(memcmp(buffer, "  t  h  r  e  e1", 16) == 0,
                "bufer is incorrect after three byte string has been written");

    UCX_TEST_END

    ucx_buffer_free(b);
    free(buffer);
}

UCX_TEST(test_ucx_buffer_write_ax) {
    char *buffer = (char*) malloc(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);
    
    char buf[32];
    memset(buf, 'X', 32);
    int r;
    
    UCX_TEST_BEGIN
    
    ucx_buffer_write("01234567", 1, 8, b);
    UCX_TEST_ASSERT(b->pos == 8, "buffer not correctly filled");
    b->pos = 0;
    
    r = ucx_buffer_read(buf, 1, 2, b);
    UCX_TEST_ASSERT(r == 2, "wrong number of bytes read");
    UCX_TEST_ASSERT(buf[0] == '0' && buf[1] == '1' && buf[2] == 'X',
            "buffer incorrect after first read");
    
    r = ucx_buffer_read(buf + 2, 1, 8, b);
    UCX_TEST_ASSERT(r == 6, "wrong number of bytes read(2)");
    UCX_TEST_ASSERT(memcmp(buf, "01234567XX", 10) == 0,
            "buffer incorrect after second read");
    
    memset(buf, 'X', 32);
    ucx_buffer_seek(b, 0, SEEK_SET);
    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 three 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 = 15;
    UcxBuffer *dst = ucx_buffer_extract(src, 5, 5, UCX_BUFFER_DEFAULT);

    UCX_TEST_BEGIN
    UCX_TEST_ASSERT(dst != NULL, "ucx_buffer_extract returned NULL");
            
    UCX_TEST_ASSERT((dst->flags & UCX_BUFFER_AUTOFREE) == UCX_BUFFER_AUTOFREE,
            "autofree flag shall be enforced");
    UCX_TEST_ASSERT(dst->size == 5, "wrong size 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_ASSERT(ucx_buffer_extract(dst, 3, 3, UCX_BUFFER_DEFAULT) == NULL,
            "extract shall fail on invalid bounds");

    UCX_TEST_END

    ucx_buffer_free(dst);
    ucx_buffer_free(src);
}

UCX_TEST(test_ucx_stream_copy) {
    UcxBuffer *b1 = ucx_buffer_new(NULL, 64, UCX_BUFFER_DEFAULT);
    UcxBuffer *b2 = ucx_buffer_new(NULL, 2, UCX_BUFFER_AUTOEXTEND);
    
    UCX_TEST_BEGIN
    
    ucx_buffer_write("01234567", 1, 8, b1);
    ucx_buffer_write("abcdefgh", 1, 8, b1);
    UCX_TEST_ASSERT(b1->size == 16, "failed to fill buffer b1");
    ucx_buffer_seek(b1, 0, SEEK_SET);
    
    size_t ncp = ucx_stream_hcopy(b1, b2, ucx_buffer_read, ucx_buffer_write);
    UCX_TEST_ASSERT(ncp == 16, "wrong number of copied bytes");
    UCX_TEST_ASSERT(b2->size == 16, "b2 has wrong size");
    UCX_TEST_ASSERT(memcmp(b1->space, b2->space, 16) == 0,
            "b1 and b2 have not the same content");
    
    memset(b2->space, 0, b2->capacity);
    b2->pos = 0;
    b2->size = 0;
    ucx_buffer_seek(b1, 0, SEEK_SET);
    
    FILE *file = tmpfile();
    UCX_TEST_ASSERT(file, "test file cannot be opened, test aborted");
    
    ncp = ucx_stream_hcopy(b1, file, ucx_buffer_read, fwrite);
    UCX_TEST_ASSERT(ncp == 16, "copied wrong number of bytes to file");
    
    fseek(file, 0, SEEK_SET);
    
    ncp = ucx_stream_hcopy(file, b2, fread, ucx_buffer_write);
    UCX_TEST_ASSERT(ncp == 16, "copied wrong number of bytes from file");
    
    UCX_TEST_ASSERT(memcmp(b1->space, b2->space, 16) == 0,
            "b1 and b2 content mismatch");
    
    fclose(file);

    ucx_buffer_clear(b1);
    ucx_buffer_seek(b2, 0, SEEK_SET);
    ncp = ucx_stream_ncopy(b2, b1, ucx_buffer_read, ucx_buffer_write, 8);
    UCX_TEST_ASSERT(ncp == 8, "copied wrong number of bytes with ncopy");
    UCX_TEST_ASSERT(memcmp(b1->space, "01234567\0\0\0\0\0\0\0\0", 16) == 0,
        "content wrong after ncopy");
    
    UCX_TEST_END
    
    ucx_buffer_free(b1);
    ucx_buffer_free(b2);
}

mercurial