test/utils_tests.c

Tue, 06 Aug 2019 16:26:46 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 06 Aug 2019 16:26:46 +0200
branch
feature/array
changeset 342
8f0a3c00d1d2
parent 259
2f5dea574a75
permissions
-rw-r--r--

removes cumbersome array accessor macros

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2017 Mike Becker, 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 "utils_tests.h"
#include <ucx/buffer.h>

UCX_TEST(test_ucx_fprintf) {
    UcxBuffer *b1 = ucx_buffer_new(NULL, 32, UCX_BUFFER_AUTOEXTEND);
    UcxBuffer *b2 = ucx_buffer_new(NULL, 4096, UCX_BUFFER_AUTOEXTEND);
    
    char *teststr1 = (char*)calloc(1, 1024);
    char *teststr2 = (char*)calloc(1, 1024);
    memset(teststr1, 'a', 1023);
    memset(teststr2, 'b', 1023);
    
    UCX_TEST_BEGIN
    
    ucx_fprintf(b1, (write_func)ucx_buffer_write, "Hello %s", "World");
    UCX_TEST_ASSERT(!strcmp(b1->space, "Hello World"), "wrong content in b1");
    ucx_fprintf(b1, (write_func)ucx_buffer_write, "\nend.\n");
    UCX_TEST_ASSERT(!strcmp(b1->space, "Hello World\nend.\n"),
            "wrong content in b1 after second fprintf");
    
    ucx_bprintf(b2, "%s%s", teststr1, teststr2);
    UCX_TEST_ASSERT(b2->pos == 2046, "wrong length");
    UCX_TEST_ASSERT(!memcmp(b2->space, teststr1, 1023),
            "wrong first half in b2");
    UCX_TEST_ASSERT(!memcmp(b2->space+1023, teststr2, 1023),
            "wrong second half in b2");
    
    UCX_TEST_END
    
    ucx_buffer_free(b1);
    ucx_buffer_free(b2);
    free(teststr1);
    free(teststr2);
}

UCX_TEST(test_ucx_asprintf) {
    char *teststr1 = (char*)calloc(1, 1024);
    char *teststr2 = (char*)calloc(1, 1024);
    memset(teststr1, 'a', 1023);
    memset(teststr2, 'b', 1023);
    UcxAllocator *a = ucx_default_allocator();
    
    UCX_TEST_BEGIN
    
    sstr_t s1 = ucx_asprintf(a, "int: %d\nHello %s!", 123, "World");
    UCX_TEST_ASSERT(s1.ptr, "s1.ptr is NULL");
    UCX_TEST_ASSERT(s1.length == 21, "wrong length");
    UCX_TEST_ASSERT(!sstrcmp(s1, S("int: 123\nHello World!")), "wrong content");
    free(s1.ptr);
    
    sstr_t s2 = ucx_asprintf(a, "%s%s", teststr1, teststr2);
    UCX_TEST_ASSERT(s2.ptr, "s2.ptr is NULL");
    UCX_TEST_ASSERT(s2.length == 2046, "wrong length");
    UCX_TEST_ASSERT(!memcmp(s2.ptr, teststr1, 1023),
            "wrong first half in s2");
    UCX_TEST_ASSERT(!memcmp(s2.ptr+1023, teststr2, 1023),
            "wrong second half in s2");
    free(s2.ptr);
    
    UCX_TEST_END
            
    free(teststr1);
    free(teststr2);
}

UCX_TEST(test_ucx_sprintf) {
    UCX_TEST_BEGIN
    
    sstr_t s1 = ucx_sprintf("int: %d\nHello %s!", 123, "World");
    UCX_TEST_ASSERT(s1.ptr, "s1.ptr is NULL");
    UCX_TEST_ASSERT(s1.length == 21, "wrong length");
    UCX_TEST_ASSERT(!sstrcmp(s1, S("int: 123\nHello World!")), "wrong content");
    free(s1.ptr);
    
    sstr_t s2 = ucx_sprintf("Nothing to format!");
    UCX_TEST_ASSERT(s2.ptr, "s2.ptr is NULL");
    UCX_TEST_ASSERT(s2.length == 18, "wrong length");
    UCX_TEST_ASSERT(!memcmp(s2.ptr, "Nothing to format!", 18),
            "wrong string without format arguments");
    free(s2.ptr);
    
    UCX_TEST_END
}

UCX_TEST(test_ucx_bprintf) {
    UcxBuffer *b = ucx_buffer_new(NULL, 64, UCX_BUFFER_DEFAULT);
    
    UCX_TEST_BEGIN
    
    ucx_bprintf(b, "int: %d\nHello %s!", 123, "World");
    UCX_TEST_ASSERT(b->size == 21, "wrong length");
    UCX_TEST_ASSERT(!memcmp(b->space, "int: 123\nHello World!", 21),
            "wrong content");
            
    ucx_buffer_clear(b);
            
    ucx_bprintf(b, "Nothing to format!");
    UCX_TEST_ASSERT(b->size == 18, "wrong length");
    UCX_TEST_ASSERT(!memcmp(b->space, "Nothing to format!", 18),
            "wrong string without format arguments");
            
    UCX_TEST_END
    
    ucx_buffer_free(b);
}

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);
    
    char copybuf[256];
    size_t ncp = ucx_stream_bcopy(b1, b2, ucx_buffer_read, ucx_buffer_write,
            copybuf, sizeof(copybuf));
    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_copy(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_copy(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