test/utils_tests.c

Sun, 21 Jan 2018 14:10:59 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 21 Jan 2018 14:10:59 +0100
changeset 273
9c1591b3c4a4
parent 259
2f5dea574a75
permissions
-rw-r--r--

fixes return value for multiplication with zero in ucx_szmul

olaf@142 1 /*
olaf@142 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
olaf@142 3 *
universe@259 4 * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved.
olaf@142 5 *
olaf@142 6 * Redistribution and use in source and binary forms, with or without
olaf@142 7 * modification, are permitted provided that the following conditions are met:
olaf@142 8 *
olaf@142 9 * 1. Redistributions of source code must retain the above copyright
olaf@142 10 * notice, this list of conditions and the following disclaimer.
olaf@142 11 *
olaf@142 12 * 2. Redistributions in binary form must reproduce the above copyright
olaf@142 13 * notice, this list of conditions and the following disclaimer in the
olaf@142 14 * documentation and/or other materials provided with the distribution.
olaf@142 15 *
olaf@142 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
olaf@142 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
olaf@142 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
olaf@142 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
olaf@142 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
olaf@142 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
olaf@142 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
olaf@142 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
olaf@142 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
olaf@142 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
olaf@142 26 * POSSIBILITY OF SUCH DAMAGE.
olaf@142 27 */
olaf@142 28
olaf@142 29 #include "utils_tests.h"
universe@251 30 #include <ucx/buffer.h>
olaf@142 31
olaf@142 32 UCX_TEST(test_ucx_fprintf) {
olaf@142 33 UcxBuffer *b1 = ucx_buffer_new(NULL, 32, UCX_BUFFER_AUTOEXTEND);
olaf@142 34 UcxBuffer *b2 = ucx_buffer_new(NULL, 4096, UCX_BUFFER_AUTOEXTEND);
olaf@142 35
olaf@142 36 char *teststr1 = (char*)calloc(1, 1024);
olaf@142 37 char *teststr2 = (char*)calloc(1, 1024);
olaf@142 38 memset(teststr1, 'a', 1023);
olaf@142 39 memset(teststr2, 'b', 1023);
olaf@142 40
olaf@142 41 UCX_TEST_BEGIN
olaf@142 42
olaf@142 43 ucx_fprintf(b1, (write_func)ucx_buffer_write, "Hello %s", "World");
olaf@142 44 UCX_TEST_ASSERT(!strcmp(b1->space, "Hello World"), "wrong content in b1");
olaf@142 45 ucx_fprintf(b1, (write_func)ucx_buffer_write, "\nend.\n");
olaf@142 46 UCX_TEST_ASSERT(!strcmp(b1->space, "Hello World\nend.\n"),
olaf@142 47 "wrong content in b1 after second fprintf");
olaf@142 48
olaf@147 49 ucx_bprintf(b2, "%s%s", teststr1, teststr2);
olaf@145 50 UCX_TEST_ASSERT(b2->pos == 2046, "wrong length");
olaf@142 51 UCX_TEST_ASSERT(!memcmp(b2->space, teststr1, 1023),
olaf@142 52 "wrong first half in b2");
olaf@142 53 UCX_TEST_ASSERT(!memcmp(b2->space+1023, teststr2, 1023),
olaf@142 54 "wrong second half in b2");
olaf@142 55
olaf@142 56 UCX_TEST_END
olaf@145 57
olaf@145 58 ucx_buffer_free(b1);
olaf@145 59 ucx_buffer_free(b2);
olaf@147 60 free(teststr1);
olaf@147 61 free(teststr2);
olaf@142 62 }
olaf@142 63
olaf@142 64 UCX_TEST(test_ucx_asprintf) {
olaf@142 65 char *teststr1 = (char*)calloc(1, 1024);
olaf@142 66 char *teststr2 = (char*)calloc(1, 1024);
olaf@142 67 memset(teststr1, 'a', 1023);
olaf@142 68 memset(teststr2, 'b', 1023);
olaf@142 69 UcxAllocator *a = ucx_default_allocator();
olaf@142 70
olaf@142 71 UCX_TEST_BEGIN
olaf@142 72
olaf@142 73 sstr_t s1 = ucx_asprintf(a, "int: %d\nHello %s!", 123, "World");
olaf@142 74 UCX_TEST_ASSERT(s1.ptr, "s1.ptr is NULL");
olaf@142 75 UCX_TEST_ASSERT(s1.length == 21, "wrong length");
olaf@142 76 UCX_TEST_ASSERT(!sstrcmp(s1, S("int: 123\nHello World!")), "wrong content");
olaf@142 77 free(s1.ptr);
olaf@142 78
olaf@142 79 sstr_t s2 = ucx_asprintf(a, "%s%s", teststr1, teststr2);
olaf@145 80 UCX_TEST_ASSERT(s2.ptr, "s2.ptr is NULL");
olaf@145 81 UCX_TEST_ASSERT(s2.length == 2046, "wrong length");
olaf@142 82 UCX_TEST_ASSERT(!memcmp(s2.ptr, teststr1, 1023),
olaf@142 83 "wrong first half in s2");
olaf@142 84 UCX_TEST_ASSERT(!memcmp(s2.ptr+1023, teststr2, 1023),
olaf@142 85 "wrong second half in s2");
olaf@145 86 free(s2.ptr);
olaf@142 87
olaf@142 88 UCX_TEST_END
olaf@147 89
olaf@147 90 free(teststr1);
olaf@147 91 free(teststr2);
olaf@142 92 }
universe@168 93
universe@223 94 UCX_TEST(test_ucx_sprintf) {
universe@223 95 UCX_TEST_BEGIN
universe@223 96
universe@223 97 sstr_t s1 = ucx_sprintf("int: %d\nHello %s!", 123, "World");
universe@223 98 UCX_TEST_ASSERT(s1.ptr, "s1.ptr is NULL");
universe@223 99 UCX_TEST_ASSERT(s1.length == 21, "wrong length");
universe@223 100 UCX_TEST_ASSERT(!sstrcmp(s1, S("int: 123\nHello World!")), "wrong content");
universe@223 101 free(s1.ptr);
universe@223 102
universe@223 103 sstr_t s2 = ucx_sprintf("Nothing to format!");
universe@223 104 UCX_TEST_ASSERT(s2.ptr, "s2.ptr is NULL");
universe@223 105 UCX_TEST_ASSERT(s2.length == 18, "wrong length");
universe@223 106 UCX_TEST_ASSERT(!memcmp(s2.ptr, "Nothing to format!", 18),
universe@223 107 "wrong string without format arguments");
universe@223 108 free(s2.ptr);
universe@223 109
universe@223 110 UCX_TEST_END
universe@223 111 }
universe@223 112
universe@223 113 UCX_TEST(test_ucx_bprintf) {
universe@223 114 UcxBuffer *b = ucx_buffer_new(NULL, 64, UCX_BUFFER_DEFAULT);
universe@223 115
universe@223 116 UCX_TEST_BEGIN
universe@223 117
universe@223 118 ucx_bprintf(b, "int: %d\nHello %s!", 123, "World");
universe@223 119 UCX_TEST_ASSERT(b->size == 21, "wrong length");
universe@223 120 UCX_TEST_ASSERT(!memcmp(b->space, "int: 123\nHello World!", 21),
universe@223 121 "wrong content");
universe@223 122
universe@223 123 ucx_buffer_clear(b);
universe@223 124
universe@223 125 ucx_bprintf(b, "Nothing to format!");
universe@223 126 UCX_TEST_ASSERT(b->size == 18, "wrong length");
universe@223 127 UCX_TEST_ASSERT(!memcmp(b->space, "Nothing to format!", 18),
universe@223 128 "wrong string without format arguments");
universe@223 129
universe@223 130 UCX_TEST_END
universe@223 131
universe@223 132 ucx_buffer_free(b);
universe@223 133 }
universe@223 134
universe@168 135 UCX_TEST(test_ucx_stream_copy) {
universe@168 136 UcxBuffer *b1 = ucx_buffer_new(NULL, 64, UCX_BUFFER_DEFAULT);
universe@168 137 UcxBuffer *b2 = ucx_buffer_new(NULL, 2, UCX_BUFFER_AUTOEXTEND);
universe@168 138
universe@168 139 UCX_TEST_BEGIN
universe@168 140
universe@168 141 ucx_buffer_write("01234567", 1, 8, b1);
universe@168 142 ucx_buffer_write("abcdefgh", 1, 8, b1);
universe@168 143 UCX_TEST_ASSERT(b1->size == 16, "failed to fill buffer b1");
universe@168 144 ucx_buffer_seek(b1, 0, SEEK_SET);
universe@168 145
universe@222 146 char copybuf[256];
universe@222 147 size_t ncp = ucx_stream_bcopy(b1, b2, ucx_buffer_read, ucx_buffer_write,
universe@222 148 copybuf, sizeof(copybuf));
universe@168 149 UCX_TEST_ASSERT(ncp == 16, "wrong number of copied bytes");
universe@168 150 UCX_TEST_ASSERT(b2->size == 16, "b2 has wrong size");
universe@168 151 UCX_TEST_ASSERT(memcmp(b1->space, b2->space, 16) == 0,
universe@168 152 "b1 and b2 have not the same content");
universe@168 153
universe@168 154 memset(b2->space, 0, b2->capacity);
universe@168 155 b2->pos = 0;
universe@168 156 b2->size = 0;
universe@168 157 ucx_buffer_seek(b1, 0, SEEK_SET);
universe@168 158
universe@168 159 FILE *file = tmpfile();
universe@168 160 UCX_TEST_ASSERT(file, "test file cannot be opened, test aborted");
universe@168 161
universe@222 162 ncp = ucx_stream_copy(b1, file, ucx_buffer_read, fwrite);
universe@168 163 UCX_TEST_ASSERT(ncp == 16, "copied wrong number of bytes to file");
universe@168 164
universe@168 165 fseek(file, 0, SEEK_SET);
universe@168 166
universe@222 167 ncp = ucx_stream_copy(file, b2, fread, ucx_buffer_write);
universe@168 168 UCX_TEST_ASSERT(ncp == 16, "copied wrong number of bytes from file");
universe@168 169
universe@168 170 UCX_TEST_ASSERT(memcmp(b1->space, b2->space, 16) == 0,
universe@168 171 "b1 and b2 content mismatch");
universe@168 172
universe@168 173 fclose(file);
universe@168 174
universe@168 175 ucx_buffer_clear(b1);
universe@168 176 ucx_buffer_seek(b2, 0, SEEK_SET);
universe@168 177 ncp = ucx_stream_ncopy(b2, b1, ucx_buffer_read, ucx_buffer_write, 8);
universe@168 178 UCX_TEST_ASSERT(ncp == 8, "copied wrong number of bytes with ncopy");
universe@168 179 UCX_TEST_ASSERT(memcmp(b1->space, "01234567\0\0\0\0\0\0\0\0", 16) == 0,
universe@168 180 "content wrong after ncopy");
universe@168 181
universe@168 182 UCX_TEST_END
universe@168 183
universe@168 184 ucx_buffer_free(b1);
universe@168 185 ucx_buffer_free(b2);
universe@168 186 }
universe@223 187

mercurial