test/stack_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@13 1 /*
universe@103 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
olaf@13 3 *
universe@259 4 * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved.
universe@103 5 *
universe@103 6 * Redistribution and use in source and binary forms, with or without
universe@103 7 * modification, are permitted provided that the following conditions are met:
universe@103 8 *
universe@103 9 * 1. Redistributions of source code must retain the above copyright
universe@103 10 * notice, this list of conditions and the following disclaimer.
universe@103 11 *
universe@103 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@103 13 * notice, this list of conditions and the following disclaimer in the
universe@103 14 * documentation and/or other materials provided with the distribution.
universe@103 15 *
universe@103 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@103 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@103 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@103 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@103 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@103 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@103 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@103 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@103 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@103 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@103 26 * POSSIBILITY OF SUCH DAMAGE.
olaf@13 27 */
olaf@13 28
universe@185 29 #include "stack_tests.h"
olaf@30 30
universe@185 31 #define test_ucx_stack_before \
universe@187 32 char space[99]; \
universe@185 33 UcxStack stack; \
universe@187 34 ucx_stack_init(&stack, space, 99) \
olaf@13 35
universe@185 36 UCX_TEST(test_ucx_stack_init) {
universe@28 37
universe@185 38 test_ucx_stack_before;
olaf@113 39
olaf@113 40 UCX_TEST_BEGIN
olaf@113 41
universe@185 42 UCX_TEST_ASSERT(
universe@185 43 stack.allocator.malloc == (ucx_allocator_malloc) ucx_stack_malloc &&
universe@185 44 stack.allocator.calloc == (ucx_allocator_calloc) ucx_stack_calloc &&
universe@185 45 stack.allocator.realloc == (ucx_allocator_realloc) ucx_stack_realloc &&
universe@185 46 stack.allocator.free == (ucx_allocator_free) ucx_stack_free &&
universe@185 47 stack.allocator.pool == &stack,
universe@185 48 "allocator not properly set");
olaf@113 49
universe@185 50 UCX_TEST_ASSERT(!stack.top && stack.space == space
universe@187 51 && stack.size == 99 - 99 % sizeof(void*),
universe@185 52 "struct fields not properly set");
olaf@113 53
olaf@113 54 UCX_TEST_END
universe@28 55 }
olaf@13 56
universe@185 57 UCX_TEST(test_ucx_stack_malloc) {
universe@185 58
universe@185 59 test_ucx_stack_before;
universe@185 60
universe@185 61 const size_t metasize = sizeof(struct ucx_stack_metadata);
universe@185 62
universe@185 63
universe@185 64 char* first = (char*) ucx_stack_malloc(&stack, 30);
universe@185 65 char* second = (char*) ucx_stack_malloc(&stack, 30);
universe@185 66 char* full = (char*) ucx_stack_malloc(&stack, 30);
universe@185 67
universe@185 68 memcpy(first, "012345678901234567890123456789", 30);
universe@185 69 memcpy(second, "abcdefghijklmnopqrstuvwxyzABCD", 30);
universe@185 70
universe@185 71 UCX_TEST_BEGIN
universe@185 72
universe@185 73 UCX_TEST_ASSERT(!memcmp(space + metasize,
universe@185 74 "012345678901234567890123456789", 30), "first element corrupted");
universe@185 75 UCX_TEST_ASSERT(!memcmp(space + 32+2*metasize,
universe@185 76 "abcdefghijklmnopqrstuvwxyzABCD", 30), "first element corrupted");
universe@185 77
universe@185 78 UCX_TEST_ASSERT(!full, "stack can be overflowed");
universe@185 79 UCX_TEST_ASSERT(stack.top == space + 32 + 2*metasize, "wrong top pointer");
universe@185 80
universe@185 81 if (3*metasize < 32) {
universe@185 82 UCX_TEST_ASSERT(ucx_stack_avail(&stack) == 32-3*metasize,
universe@185 83 "wrong remaining available memory");
universe@185 84 } else {
universe@185 85 UCX_TEST_ASSERT(ucx_stack_avail(&stack) == 0,
universe@185 86 "wrong remaining available memory");
universe@185 87 }
universe@185 88
universe@185 89 UCX_TEST_END
universe@28 90 }
olaf@13 91
universe@185 92 UCX_TEST(test_ucx_stack_calloc) {
universe@28 93
universe@185 94 test_ucx_stack_before;
universe@185 95
universe@187 96 char zeros[99];
universe@187 97 memset(zeros, 0, 99);
universe@187 98 memset(space, 32, 99);
universe@185 99 ucx_stack_calloc(&stack, 4, sizeof(int));
universe@185 100
universe@33 101 UCX_TEST_BEGIN
universe@28 102
universe@185 103 UCX_TEST_ASSERT(!memcmp(space+sizeof(struct ucx_stack_metadata),
universe@185 104 zeros, 4*sizeof(int)), "memory not nulled");
universe@185 105 UCX_TEST_ASSERT(!memcmp(space+sizeof(struct ucx_stack_metadata)
universe@185 106 +4*sizeof(int), " ", 10), "too much memory nulled");
universe@185 107
universe@185 108 UCX_TEST_END
universe@185 109 }
universe@185 110
universe@185 111 UCX_TEST(test_ucx_stack_free) {
universe@28 112
universe@185 113 test_ucx_stack_before;
universe@28 114
universe@185 115 void *fst = ucx_stack_malloc(&stack, 10);
universe@185 116 void *snd = ucx_stack_malloc(&stack, 10);
universe@185 117 void *thrd = ucx_stack_malloc(&stack, 10);
universe@28 118
universe@185 119 UCX_TEST_BEGIN
universe@28 120
universe@185 121 UCX_TEST_ASSERT(stack.top == thrd, "wrong stack");
universe@185 122 UCX_TEST_ASSERT(((struct ucx_stack_metadata*) thrd - 1)->prev == snd,
universe@185 123 "wrong thrd prev pointer before free");
universe@28 124
universe@185 125 ucx_stack_free(&stack, snd);
universe@185 126
universe@185 127 UCX_TEST_ASSERT(((struct ucx_stack_metadata*) thrd - 1)->prev == fst,
universe@185 128 "wrong thrd prev pointer after freeing snd");
universe@185 129 UCX_TEST_ASSERT(stack.top == thrd, "wrong top after freeing snd");
universe@185 130
universe@185 131 ucx_stack_free(&stack, thrd);
universe@185 132
universe@185 133 UCX_TEST_ASSERT(stack.top == fst, "wrong top after freeing thrd");
universe@28 134
universe@28 135 UCX_TEST_END
universe@28 136 }
olaf@13 137
universe@185 138 UCX_TEST(test_ucx_stack_realloc) {
olaf@14 139
universe@185 140 test_ucx_stack_before;
universe@185 141
universe@187 142 void *fst = ucx_stack_malloc(&stack, 14);
universe@185 143 void *snd = ucx_stack_malloc(&stack, 10);
universe@185 144
universe@33 145 UCX_TEST_BEGIN
universe@28 146
universe@185 147 void *nfst = ucx_stack_realloc(&stack, fst, 16);
universe@185 148 UCX_TEST_ASSERT(nfst == fst, "unnecessary move on reallocation");
universe@185 149 UCX_TEST_ASSERT(((struct ucx_stack_metadata*)fst - 1)->size == 16,
universe@185 150 "wrong size after reallocation");
universe@28 151
universe@185 152 void *nsnd = ucx_stack_realloc(&stack, snd, 30);
universe@185 153 UCX_TEST_ASSERT(nsnd == snd, "unnecessary move on top reallocation");
universe@185 154 UCX_TEST_ASSERT(ucx_stack_topsize(&stack) == 30,
universe@185 155 "wrong size after top reallocation");
universe@28 156
universe@185 157 nsnd = ucx_stack_realloc(&stack, snd, 5);
universe@185 158 UCX_TEST_ASSERT(nsnd == snd, "unnecessary move on top shrink");
universe@185 159 UCX_TEST_ASSERT(ucx_stack_topsize(&stack) == 5,
universe@185 160 "wrong size after top shrink");
universe@185 161 UCX_TEST_ASSERT(ucx_stack_avail(&stack) ==
universe@185 162 72-3*sizeof(struct ucx_stack_metadata), "wrong size after top shrink");
universe@28 163
universe@185 164 nfst = ucx_stack_realloc(&stack, fst, 24);
universe@185 165 UCX_TEST_ASSERT(nfst != fst, "missing move on huge reallocation");
universe@185 166 UCX_TEST_ASSERT(stack.top == nfst, "wrong top after huge reallocation");
universe@185 167 UCX_TEST_ASSERT(ucx_stack_topsize(&stack) == 24,
universe@185 168 "wrong size after huge reallocation");
universe@185 169 UCX_TEST_ASSERT(!((struct ucx_stack_metadata*)snd - 1)->prev,
universe@185 170 "element not freed after huge reallocation");
universe@28 171
universe@33 172 UCX_TEST_END
universe@28 173 }
olaf@13 174
universe@185 175 UCX_TEST(test_ucx_stack_pop) {
universe@185 176
universe@185 177 test_ucx_stack_before;
universe@187 178 memset(space, 32, 99);
universe@185 179
universe@185 180 void *fst = ucx_stack_malloc(&stack, 10);
universe@185 181 void *snd = ucx_stack_malloc(&stack, 10);
universe@185 182 ucx_stack_malloc(&stack, 10);
universe@185 183
universe@185 184 char buf[16];
universe@185 185
universe@33 186 UCX_TEST_BEGIN
universe@28 187
universe@185 188 memset(buf, '0', 16);
universe@185 189 ucx_stack_pop(&stack, buf);
universe@185 190 UCX_TEST_ASSERT(memcmp(buf, " 000000", 16) == 0,
universe@185 191 "popped wrong content");
universe@185 192 UCX_TEST_ASSERT(stack.top == snd, "wrong top after pop");
universe@28 193
universe@185 194 memset(buf, '0', 16);
universe@185 195 ucx_stack_popn(&stack, buf, 5);
universe@185 196 UCX_TEST_ASSERT(memcmp(buf, " 00000000000", 16) == 0,
universe@185 197 "n-popped wrong content");
universe@185 198 UCX_TEST_ASSERT(stack.top == fst, "wrong top after pop");
universe@28 199
universe@185 200 ucx_stack_pop(&stack, buf);
universe@185 201 UCX_TEST_ASSERT(!stack.top, "top not NULL after last pop");
universe@28 202
universe@185 203 memset(buf, '0', 16);
universe@185 204 ucx_stack_pop(&stack, buf);
universe@185 205 UCX_TEST_ASSERT(memcmp(buf, "0000000000000000", 16) == 0,
universe@185 206 "content not unchanged after empty pop");
universe@28 207
universe@28 208 UCX_TEST_END
olaf@13 209 }

mercurial