test/avl_tests.c

Mon, 14 May 2018 18:20:56 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 14 May 2018 18:20:56 +0200
changeset 312
e1e3b768ae8b
parent 259
2f5dea574a75
child 330
d2bbf907a189
permissions
-rw-r--r--

renames ucx_ptrcmp() to ucx_cmp_ptr()

universe@56 1 /*
universe@103 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@56 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.
universe@56 27 */
universe@56 28
universe@192 29 #include "avl_tests.h"
universe@56 30
universe@251 31 #include <ucx/utils.h>
olaf@198 32
olaf@198 33 static int node_height(UcxAVLNode *node) {
olaf@198 34 if(!node) {
olaf@198 35 return 0;
olaf@198 36 }
olaf@198 37
olaf@198 38 int left = 0;
olaf@198 39 int right = 0;
olaf@198 40 if(node->left) {
olaf@198 41 left = node_height(node->left);
olaf@198 42 }
olaf@198 43 if(node->right) {
olaf@198 44 right = node_height(node->right);
olaf@198 45 }
olaf@198 46
olaf@198 47 return left > right ? left+1 : right+1;
olaf@198 48 }
olaf@198 49
olaf@198 50 static int check_tree(UcxAVLNode *node) {
olaf@198 51 if(!node) {
olaf@198 52 return 1;
olaf@198 53 }
olaf@198 54
olaf@198 55 int left_height = node_height(node->left);
olaf@198 56 int right_height = node_height(node->right);
olaf@198 57 int bf = -left_height + right_height;
olaf@198 58 if(bf < -1 || bf > 1) {
olaf@198 59 return 0;
olaf@198 60 }
olaf@198 61 int height = left_height > right_height ? left_height : right_height;
olaf@198 62 height++;
olaf@198 63 if(height != node->height) {
olaf@198 64 return 0;
olaf@198 65 }
olaf@198 66
olaf@198 67 if(!check_tree(node->left)) {
olaf@198 68 return 0;
olaf@198 69 }
olaf@198 70 if(!check_tree(node->right)) {
olaf@198 71 return 0;
olaf@198 72 }
olaf@198 73
olaf@198 74 return 1;
olaf@198 75 }
olaf@198 76
olaf@198 77 UCX_TEST(test_ucx_avl_put) {
universe@312 78 UcxAVLTree *tree1 = ucx_avl_new(ucx_cmp_ptr);
universe@312 79 UcxAVLTree *tree2 = ucx_avl_new(ucx_cmp_ptr);
universe@312 80 UcxAVLTree *tree3 = ucx_avl_new(ucx_cmp_ptr);
universe@312 81 UcxAVLTree *tree4 = ucx_avl_new(ucx_cmp_ptr);
universe@312 82 UcxAVLTree *tree5 = ucx_avl_new(ucx_cmp_ptr);
olaf@198 83
olaf@198 84 char *data1 = (char*)"data1";
olaf@198 85 char *data2 = (char*)"data2";
olaf@198 86 char *data3 = (char*)"data3";
olaf@198 87
olaf@198 88 UCX_TEST_BEGIN
olaf@198 89
olaf@198 90 ucx_avl_put(tree1, 2, (char*)data2);
olaf@198 91 ucx_avl_put(tree1, 1, (char*)data1);
olaf@198 92 ucx_avl_put(tree1, 3, (char*)data3);
olaf@198 93
olaf@198 94 UCX_TEST_ASSERT(check_tree(tree1->root), "check_tree failed (tree1)");
olaf@198 95 UCX_TEST_ASSERT(ucx_avl_get(tree1, 1) == data1, "wrong data (tree1)");
olaf@198 96 UCX_TEST_ASSERT(ucx_avl_get(tree1, 2) == data2, "wrong data (tree1)");
olaf@198 97 UCX_TEST_ASSERT(ucx_avl_get(tree1, 3) == data3, "wrong data (tree1)");
olaf@198 98
olaf@198 99 for(int i=0;i<100000;i++) {
olaf@198 100 ucx_avl_put(tree2, i, data1);
olaf@198 101 }
olaf@198 102 UCX_TEST_ASSERT(check_tree(tree2->root), "check_tree failed (tree2)");
olaf@198 103
olaf@198 104 for(int i=100000;i>=0;i--) {
olaf@198 105 ucx_avl_put(tree3, i, data1);
olaf@198 106 }
olaf@198 107 UCX_TEST_ASSERT(check_tree(tree3->root), "check_tree failed (tree3)");
olaf@198 108
olaf@198 109 for(int i=0;i<100;i++) {
olaf@198 110 ucx_avl_put(tree4, i, data1);
olaf@198 111 }
olaf@198 112 for(int i=400;i<500;i++) {
olaf@198 113 ucx_avl_put(tree4, i, data2);
olaf@198 114 }
olaf@198 115 for(int i=399;i>200;i--) {
olaf@198 116 ucx_avl_put(tree4, i, data3);
olaf@198 117 }
olaf@198 118 for(int i=800;i<1000;i++) {
olaf@198 119 ucx_avl_put(tree4, i, data1);
olaf@198 120 }
olaf@198 121 UCX_TEST_ASSERT(check_tree(tree4->root), "check_tree failed (tree4)");
olaf@198 122 UCX_TEST_ASSERT(ucx_avl_get(tree4, 0) == data1, "wrong data for key: 0");
olaf@198 123 UCX_TEST_ASSERT(ucx_avl_get(tree4, 1) == data1, "wrong data for key: 1");
olaf@198 124 UCX_TEST_ASSERT(ucx_avl_get(tree4, 99) == data1, "wrong data for key: 99");
olaf@198 125 UCX_TEST_ASSERT(ucx_avl_get(tree4, 400)==data2, "wrong data for key: 400");
olaf@198 126 UCX_TEST_ASSERT(ucx_avl_get(tree4, 410)==data2, "wrong data for key: 410");
olaf@198 127 UCX_TEST_ASSERT(ucx_avl_get(tree4, 350)==data3, "wrong data for key: 350");
olaf@198 128 UCX_TEST_ASSERT(ucx_avl_get(tree4, 900)==data1, "wrong data for key: 900");
olaf@198 129
olaf@198 130 int values[] = { 3, 6, 12, 9, 23, 1, 0, 20, 34, 5, 8, 7, 6, 14, 15, 2, 4};
olaf@198 131 int len = sizeof(values) / sizeof(int);
olaf@198 132 for(int i=0;i<len;i++) {
olaf@198 133 ucx_avl_put(tree5, values[i], NULL);
olaf@198 134 }
olaf@198 135 UCX_TEST_ASSERT(check_tree(tree5->root), "check_tree failed (tree5)");
olaf@198 136
olaf@198 137 UCX_TEST_END
olaf@198 138
olaf@198 139 ucx_avl_free(tree1);
olaf@198 140 ucx_avl_free(tree2);
olaf@198 141 ucx_avl_free(tree3);
olaf@198 142 ucx_avl_free(tree4);
olaf@198 143 ucx_avl_free(tree5);
olaf@198 144 }
olaf@200 145
olaf@200 146 UCX_TEST(test_ucx_avl_remove) {
universe@312 147 UcxAVLTree *tree1 = ucx_avl_new(ucx_cmp_ptr);
universe@312 148 UcxAVLTree *tree2 = ucx_avl_new(ucx_cmp_ptr);
universe@312 149 UcxAVLTree *tree3 = ucx_avl_new(ucx_cmp_ptr);
universe@312 150 UcxAVLTree *tree4 = ucx_avl_new(ucx_cmp_ptr);
olaf@200 151
olaf@200 152 char *data1 = (char*)"data1";
olaf@200 153 char *data2 = (char*)"data2";
olaf@200 154 char *data3 = (char*)"data3";
olaf@200 155
olaf@200 156 UCX_TEST_BEGIN
olaf@200 157 ucx_avl_put(tree1, 2, (char*)data2);
olaf@200 158 ucx_avl_put(tree1, 1, (char*)data1);
olaf@200 159 ucx_avl_put(tree1, 3, (char*)data3);
universe@204 160 void *val;
universe@204 161 ucx_avl_remove_s(tree1, 3, NULL, &val);
olaf@200 162
olaf@200 163 UCX_TEST_ASSERT(check_tree(tree1->root), "check_tree failed (tree1)");
universe@204 164 UCX_TEST_ASSERT(val == data3,
olaf@201 165 "wrong return value for key: 1 (tree1)");
olaf@200 166 UCX_TEST_ASSERT(ucx_avl_get(tree1, 3) == NULL, "value not removed (tree1)");
universe@204 167
universe@204 168 ucx_avl_remove_s(tree1, 2, NULL, &val);
universe@204 169 UCX_TEST_ASSERT(val == data2,
olaf@201 170 "wrong return value for key: 2 (tree1)");
olaf@201 171 UCX_TEST_ASSERT(check_tree(tree1->root), "check_tree failed (tree1)");
universe@204 172
universe@204 173 ucx_avl_remove_s(tree1, 1, NULL, &val);
universe@204 174 UCX_TEST_ASSERT(val == data1,
olaf@201 175 "wrong return value for key: 1 (tree1)");
olaf@201 176 UCX_TEST_ASSERT(check_tree(tree1->root), "check_tree failed (tree1)");
olaf@201 177 UCX_TEST_ASSERT(tree1->root == NULL, "root not NULL (tree1)");
olaf@201 178
olaf@200 179
olaf@200 180 for(int i=0;i<20;i++) {
olaf@200 181 if(i==10) {
olaf@200 182 ucx_avl_put(tree2, i, data3);
olaf@200 183 } else if(i==15) {
olaf@200 184 ucx_avl_put(tree2, i, data2);
olaf@200 185 } else {
olaf@200 186 ucx_avl_put(tree2, i, data1);
olaf@200 187 }
olaf@200 188 }
olaf@200 189
universe@204 190 ucx_avl_remove_s(tree2, 10, NULL, &val);
universe@204 191 UCX_TEST_ASSERT(val == data3,
olaf@200 192 "wrong return value for key: 10 (tree2)");
olaf@200 193 UCX_TEST_ASSERT(check_tree(tree2->root), "check_tree failed (tree2)");
olaf@200 194
universe@204 195 ucx_avl_remove_s(tree2, 15, NULL, &val);
universe@204 196 UCX_TEST_ASSERT(val == data2,
olaf@200 197 "wrong return value for key: 15 (tree2)");
olaf@200 198 UCX_TEST_ASSERT(check_tree(tree2->root), "check_tree failed (tree2)");
olaf@200 199
olaf@200 200 for(int i=0;i<20;i++) {
olaf@200 201 ucx_avl_remove(tree2, i);
olaf@200 202 UCX_TEST_ASSERT(check_tree(tree2->root), "check_tree failed (tree2)");
olaf@200 203 }
olaf@203 204 UCX_TEST_ASSERT(tree2->root == NULL, "root not NULL (tree2)");
olaf@200 205
olaf@200 206 for(int i=0;i<100000;i++) {
olaf@200 207 ucx_avl_put(tree3, i, data1);
olaf@200 208 }
olaf@200 209 for(int i=100000-1;i>=0;i--) {
olaf@200 210 ucx_avl_remove(tree3, i);
olaf@200 211 }
olaf@200 212 UCX_TEST_ASSERT(tree3->root == NULL, "root not NULL (tree3)");
olaf@200 213 UCX_TEST_ASSERT(check_tree(tree3->root), "check_tree failed (tree3)");
olaf@200 214
olaf@200 215 ucx_avl_put(tree4, 1, data1);
olaf@200 216 ucx_avl_remove(tree4, 1);
olaf@200 217 UCX_TEST_ASSERT(check_tree(tree4->root), "check_tree failed (tree4)");
olaf@200 218 UCX_TEST_ASSERT(tree4->root == NULL, "root not NULL (tree4)");
olaf@200 219 UCX_TEST_END
olaf@200 220
olaf@200 221 ucx_avl_free(tree1);
olaf@200 222 ucx_avl_free(tree2);
olaf@200 223 ucx_avl_free(tree3);
olaf@200 224 ucx_avl_free(tree4);
olaf@200 225 }
universe@243 226
universe@244 227 static intmax_t dist_int(const void* a, const void* b, void* n) {
universe@243 228 return ((intmax_t)a)-((intmax_t)b);
universe@243 229 }
universe@243 230
universe@243 231 UCX_TEST(test_ucx_avl_find) {
universe@312 232 UcxAVLTree *tree = ucx_avl_new(ucx_cmp_ptr);
universe@243 233
universe@243 234 size_t len = 12;
universe@243 235 int val[] = {10, 15, 3, 4, -30, 20, 14, -11, 12, -5, 1, 13};
universe@243 236
universe@243 237 for (size_t i = 0 ; i < len ; i++) {
universe@243 238 ucx_avl_put(tree, val[i], &(val[i]));
universe@243 239 }
universe@243 240
universe@243 241 UCX_TEST_BEGIN
universe@243 242
universe@243 243 void* ret;
universe@243 244
universe@243 245 /* test present values */
universe@243 246 ret = ucx_avl_find(tree,(intptr_t)-5, dist_int, UCX_AVL_FIND_CLOSEST);
universe@243 247 UCX_TEST_ASSERT(ret && *((int*)ret) == -5, "AVL find closest failed");
universe@243 248 ret = ucx_avl_find(tree,(intptr_t)-5, dist_int, UCX_AVL_FIND_EXACT);
universe@243 249 UCX_TEST_ASSERT(ret && *((int*)ret) == -5, "AVL find exact failed");
universe@243 250 ret = ucx_avl_find(tree,(intptr_t)-5, dist_int, UCX_AVL_FIND_LOWER_BOUNDED);
universe@243 251 UCX_TEST_ASSERT(ret && *((int*)ret) == -5, "AVL find LB failed");
universe@243 252 ret = ucx_avl_find(tree,(intptr_t)-5, dist_int, UCX_AVL_FIND_UPPER_BOUNDED);
universe@243 253 UCX_TEST_ASSERT(ret && *((int*)ret) == -5, "AVL find UB failed");
universe@243 254 ret = ucx_avl_find(tree,(intptr_t)12, dist_int, UCX_AVL_FIND_CLOSEST);
universe@243 255 UCX_TEST_ASSERT(ret && *((int*)ret) == 12, "AVL find closest failed");
universe@243 256 ret = ucx_avl_find(tree,(intptr_t)12, dist_int, UCX_AVL_FIND_EXACT);
universe@243 257 UCX_TEST_ASSERT(ret && *((int*)ret) == 12, "AVL find exact failed");
universe@243 258 ret = ucx_avl_find(tree,(intptr_t)12, dist_int, UCX_AVL_FIND_LOWER_BOUNDED);
universe@243 259 UCX_TEST_ASSERT(ret && *((int*)ret) == 12, "AVL find LB failed");
universe@243 260 ret = ucx_avl_find(tree,(intptr_t)12, dist_int, UCX_AVL_FIND_UPPER_BOUNDED);
universe@243 261 UCX_TEST_ASSERT(ret && *((int*)ret) == 12, "AVL find UB failed");
universe@243 262
universe@243 263 /* test missing values */
universe@243 264 ret = ucx_avl_find(tree,(intptr_t)-10, dist_int, UCX_AVL_FIND_CLOSEST);
universe@243 265 UCX_TEST_ASSERT(ret && *((int*)ret) == -11, "AVL find closest failed");
universe@243 266 ret = ucx_avl_find(tree,(intptr_t)-8, dist_int, UCX_AVL_FIND_EXACT);
universe@243 267 UCX_TEST_ASSERT(!ret, "AVL find exact failed");
universe@243 268 ret = ucx_avl_find(tree,(intptr_t)-8, dist_int, UCX_AVL_FIND_LOWER_BOUNDED);
universe@243 269 UCX_TEST_ASSERT(ret && *((int*)ret) == -5, "AVL find LB failed");
universe@243 270 ret = ucx_avl_find(tree,(intptr_t)-8, dist_int, UCX_AVL_FIND_UPPER_BOUNDED);
universe@243 271 UCX_TEST_ASSERT(ret && *((int*)ret) == -11, "AVL find UB failed");
universe@243 272 ret = ucx_avl_find(tree,(intptr_t)18, dist_int, UCX_AVL_FIND_CLOSEST);
universe@243 273 UCX_TEST_ASSERT(ret && *((int*)ret) == 20, "AVL find closest failed");
universe@243 274 ret = ucx_avl_find(tree,(intptr_t)7, dist_int, UCX_AVL_FIND_EXACT);
universe@243 275 UCX_TEST_ASSERT(!ret, "AVL find exact failed");
universe@243 276 ret = ucx_avl_find(tree,(intptr_t)7, dist_int, UCX_AVL_FIND_LOWER_BOUNDED);
universe@243 277 UCX_TEST_ASSERT(ret && *((int*)ret) == 10, "AVL find LB failed");
universe@243 278 ret = ucx_avl_find(tree,(intptr_t)7, dist_int, UCX_AVL_FIND_UPPER_BOUNDED);
universe@243 279 UCX_TEST_ASSERT(ret && *((int*)ret) == 4, "AVL find UB failed");
universe@243 280
universe@243 281 UCX_TEST_END
universe@243 282
universe@243 283 ucx_avl_free(tree);
universe@243 284 }
universe@245 285
universe@245 286 UCX_TEST(test_ucx_avl_traverse) {
universe@312 287 UcxAVLTree *tree = ucx_avl_new(ucx_cmp_ptr);
universe@245 288
universe@245 289 size_t len = 12;
universe@245 290
universe@245 291 int val[] = {10, 15, 3, 4, -30, 20, 14, -11, 12, -5, 1, 13};
universe@245 292 int val_ordered[] = {-30, -11, -5, 1, 3, 4, 10, 12, 13, 14, 15, 20};
universe@245 293
universe@245 294 for (size_t i = 0 ; i < len ; i++) {
universe@245 295 ucx_avl_put(tree, val[i], &(val[i]));
universe@245 296 }
universe@245 297
universe@245 298 UCX_TEST_BEGIN
universe@245 299
universe@245 300 UcxAVLNode* node = ucx_avl_get_node(tree, (intptr_t) val_ordered[0]);
universe@245 301 for (size_t i = 0 ; i < len ; i++) {
universe@245 302 UCX_TEST_ASSERT(node->key == val_ordered[i], "AVL successor failed");
universe@245 303 node = ucx_avl_succ(node);
universe@245 304 }
universe@245 305 UCX_TEST_ASSERT(!node, "AVL maximum incorrectly has a successor");
universe@245 306
universe@245 307 node = ucx_avl_get_node(tree, (intptr_t) val_ordered[len-1]);
universe@245 308 for (size_t i = len ; i > 0 ; i--) {
universe@245 309 UCX_TEST_ASSERT(node->key == val_ordered[i-1],"AVL predecessor failed");
universe@245 310 node = ucx_avl_pred(node);
universe@245 311 }
universe@245 312 UCX_TEST_ASSERT(!node, "AVL minimum incorrectly has a predecessor");
universe@245 313
universe@245 314 UCX_TEST_END
universe@245 315
universe@245 316 ucx_avl_free(tree);
universe@245 317 }

mercurial