test/buffer_tests.c

Fri, 12 Oct 2012 12:09:00 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 12 Oct 2012 12:09:00 +0200
changeset 72
bb3eae81aae8
parent 71
303dabadff1c
child 76
655020a30e77
permissions
-rw-r--r--

Merge with 6721482eaf8e8d35b1cd46f0a21285bfaa520d5a

/*
 *
 */

#include "buffer_tests.h"

UCX_TEST_IMPLEMENT(test_ucx_buffer_seektell) {
    char *buffer = (char*) malloc(16);
    memset(buffer, 32, 7);
    buffer[7] = 0;

    UcxBuffer *b = ucx_buffer_new(buffer, 16, UCX_BUFFER_DEFAULT);
    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 == 2, "seek END-5 set wrong position");

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

    UCX_TEST_END

    ucx_buffer_free(b);
    free(buffer);
}

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

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

    UCX_TEST_BEGIN

    ucx_buffer_putc(b, 48); ucx_buffer_putc(b, 48); ucx_buffer_putc(b, 48);
    UCX_TEST_ASSERT(b->pos == 3, "pos wrong after first 3 puts");
    ucx_buffer_seek(b, 10, SEEK_CUR);
    ucx_buffer_putc(b, 48); ucx_buffer_putc(b, 48); ucx_buffer_putc(b, 48);
    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_TEST_ASSERT(memcmp(buffer, "000          000", 16) == 0,
            "buffer contains incorrect content");

    UCX_TEST_END

    ucx_buffer_free(b);
    free(buffer);
}

UCX_TEST_IMPLEMENT(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);
    int r;

    UCX_TEST_BEGIN

    char rb[16];
    for (int 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_IMPLEMENT(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 == 15, "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_IMPLEMENT(test_ucx_buffer_write_ax) {
    char *buffer = (char*) malloc(4);

    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->size == 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_IMPLEMENT(test_ucx_buffer_read) {
    char *buffer = (char*) malloc(16);
    memset(buffer, 56, 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

    char rb[16];
    memset(rb, 32, 16);

    ucx_buffer_seek(b, 8, SEEK_SET);
    r = ucx_buffer_read(rb, 1, 16, b);
    UCX_TEST_ASSERT(r == 8, "read did not stop at buffer end");
    UCX_TEST_ASSERT(memcmp(rb, "01234567        ", 16) == 0,
            "buffer incorrect after first read");
    UCX_TEST_ASSERT(ucx_buffer_eof(b), "eof shall be set");

    ucx_buffer_seek(b, 0, SEEK_SET);
    r = ucx_buffer_read(rb+8, 1, 8, b);
    UCX_TEST_ASSERT(r == 8, "read did not read the specified amount of bytes");
    UCX_TEST_ASSERT(memcmp(rb, "0123456788888888", 16) == 0,
                "buffer incorrect after second read");

    ucx_buffer_seek(b, 0, SEEK_SET);
    r = ucx_buffer_read(rb, 3, 6, b);
    UCX_TEST_ASSERT(r == 15,
            "three byte read did not read the desired amount of bytes");
    UCX_TEST_ASSERT(memcmp(rb, "8888888801234568", 16) == 0,
                    "buffer incorrect after three byte read");

    UCX_TEST_END

    ucx_buffer_free(b);
    free(buffer);
}

UCX_TEST_IMPLEMENT(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),
            *dst = ucx_buffer_extract(src, 5, 5, UCX_BUFFER_DEFAULT);

    UCX_TEST_BEGIN
    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);
}

mercurial