test/utils_tests.c

Sun, 17 May 2015 17:31:32 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 17 May 2015 17:31:32 +0200
changeset 192
1e51558b9d09
parent 177
11ad03783baf
child 222
e0f850709a5c
permissions
-rw-r--r--

updated copyright notice + added files for upcoming AVL tree implementation

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2015 Olaf Wintermann. All rights reserved.
     5  *
     6  * Redistribution and use in source and binary forms, with or without
     7  * modification, are permitted provided that the following conditions are met:
     8  *
     9  *   1. Redistributions of source code must retain the above copyright
    10  *      notice, this list of conditions and the following disclaimer.
    11  *
    12  *   2. Redistributions in binary form must reproduce the above copyright
    13  *      notice, this list of conditions and the following disclaimer in the
    14  *      documentation and/or other materials provided with the distribution.
    15  *
    16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    26  * POSSIBILITY OF SUCH DAMAGE.
    27  */
    29 #include "utils_tests.h"
    30 #include "ucx/buffer.h"
    32 UCX_TEST(test_ucx_fprintf) {
    33     UcxBuffer *b1 = ucx_buffer_new(NULL, 32, UCX_BUFFER_AUTOEXTEND);
    34     UcxBuffer *b2 = ucx_buffer_new(NULL, 4096, UCX_BUFFER_AUTOEXTEND);
    36     char *teststr1 = (char*)calloc(1, 1024);
    37     char *teststr2 = (char*)calloc(1, 1024);
    38     memset(teststr1, 'a', 1023);
    39     memset(teststr2, 'b', 1023);
    41     UCX_TEST_BEGIN
    43     ucx_fprintf(b1, (write_func)ucx_buffer_write, "Hello %s", "World");
    44     UCX_TEST_ASSERT(!strcmp(b1->space, "Hello World"), "wrong content in b1");
    45     ucx_fprintf(b1, (write_func)ucx_buffer_write, "\nend.\n");
    46     UCX_TEST_ASSERT(!strcmp(b1->space, "Hello World\nend.\n"),
    47             "wrong content in b1 after second fprintf");
    49     ucx_bprintf(b2, "%s%s", teststr1, teststr2);
    50     UCX_TEST_ASSERT(b2->pos == 2046, "wrong length");
    51     UCX_TEST_ASSERT(!memcmp(b2->space, teststr1, 1023),
    52             "wrong first half in b2");
    53     UCX_TEST_ASSERT(!memcmp(b2->space+1023, teststr2, 1023),
    54             "wrong second half in b2");
    56     UCX_TEST_END
    58     ucx_buffer_free(b1);
    59     ucx_buffer_free(b2);
    60     free(teststr1);
    61     free(teststr2);
    62 }
    64 UCX_TEST(test_ucx_asprintf) {
    65     char *teststr1 = (char*)calloc(1, 1024);
    66     char *teststr2 = (char*)calloc(1, 1024);
    67     memset(teststr1, 'a', 1023);
    68     memset(teststr2, 'b', 1023);
    69     UcxAllocator *a = ucx_default_allocator();
    71     UCX_TEST_BEGIN
    73     sstr_t s1 = ucx_asprintf(a, "int: %d\nHello %s!", 123, "World");
    74     UCX_TEST_ASSERT(s1.ptr, "s1.ptr is NULL");
    75     UCX_TEST_ASSERT(s1.length == 21, "wrong length");
    76     UCX_TEST_ASSERT(!sstrcmp(s1, S("int: 123\nHello World!")), "wrong content");
    77     free(s1.ptr);
    79     sstr_t s2 = ucx_asprintf(a, "%s%s", teststr1, teststr2);
    80     UCX_TEST_ASSERT(s2.ptr, "s2.ptr is NULL");
    81     UCX_TEST_ASSERT(s2.length == 2046, "wrong length");
    82     UCX_TEST_ASSERT(!memcmp(s2.ptr, teststr1, 1023),
    83             "wrong first half in s2");
    84     UCX_TEST_ASSERT(!memcmp(s2.ptr+1023, teststr2, 1023),
    85             "wrong second half in s2");
    86     free(s2.ptr);
    88     UCX_TEST_END
    90     free(teststr1);
    91     free(teststr2);
    92 }
    94 UCX_TEST(test_ucx_stream_copy) {
    95     UcxBuffer *b1 = ucx_buffer_new(NULL, 64, UCX_BUFFER_DEFAULT);
    96     UcxBuffer *b2 = ucx_buffer_new(NULL, 2, UCX_BUFFER_AUTOEXTEND);
    98     UCX_TEST_BEGIN
   100     ucx_buffer_write("01234567", 1, 8, b1);
   101     ucx_buffer_write("abcdefgh", 1, 8, b1);
   102     UCX_TEST_ASSERT(b1->size == 16, "failed to fill buffer b1");
   103     ucx_buffer_seek(b1, 0, SEEK_SET);
   105     size_t ncp = ucx_stream_hcopy(b1, b2, ucx_buffer_read, ucx_buffer_write);
   106     UCX_TEST_ASSERT(ncp == 16, "wrong number of copied bytes");
   107     UCX_TEST_ASSERT(b2->size == 16, "b2 has wrong size");
   108     UCX_TEST_ASSERT(memcmp(b1->space, b2->space, 16) == 0,
   109             "b1 and b2 have not the same content");
   111     memset(b2->space, 0, b2->capacity);
   112     b2->pos = 0;
   113     b2->size = 0;
   114     ucx_buffer_seek(b1, 0, SEEK_SET);
   116     FILE *file = tmpfile();
   117     UCX_TEST_ASSERT(file, "test file cannot be opened, test aborted");
   119     ncp = ucx_stream_hcopy(b1, file, ucx_buffer_read, fwrite);
   120     UCX_TEST_ASSERT(ncp == 16, "copied wrong number of bytes to file");
   122     fseek(file, 0, SEEK_SET);
   124     ncp = ucx_stream_hcopy(file, b2, fread, ucx_buffer_write);
   125     UCX_TEST_ASSERT(ncp == 16, "copied wrong number of bytes from file");
   127     UCX_TEST_ASSERT(memcmp(b1->space, b2->space, 16) == 0,
   128             "b1 and b2 content mismatch");
   130     fclose(file);
   132     ucx_buffer_clear(b1);
   133     ucx_buffer_seek(b2, 0, SEEK_SET);
   134     ncp = ucx_stream_ncopy(b2, b1, ucx_buffer_read, ucx_buffer_write, 8);
   135     UCX_TEST_ASSERT(ncp == 8, "copied wrong number of bytes with ncopy");
   136     UCX_TEST_ASSERT(memcmp(b1->space, "01234567\0\0\0\0\0\0\0\0", 16) == 0,
   137         "content wrong after ncopy");
   139     UCX_TEST_END
   141     ucx_buffer_free(b1);
   142     ucx_buffer_free(b2);
   143 }

mercurial