test/avl_tests.c

Wed, 21 Oct 2015 16:32:30 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 21 Oct 2015 16:32:30 +0200
changeset 211
07a284486fa1
parent 204
4477987d40cd
child 225
a1a068c2c4ef
permissions
-rw-r--r--

added ucx_list_free_contents()

56
76caac0da4a0 added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 /*
103
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
56
76caac0da4a0 added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff changeset
3 *
192
1e51558b9d09 updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
4 * Copyright 2015 Olaf Wintermann. All rights reserved.
103
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
5 *
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
6 * Redistribution and use in source and binary forms, with or without
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
7 * modification, are permitted provided that the following conditions are met:
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
8 *
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
10 * notice, this list of conditions and the following disclaimer.
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
11 *
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
14 * documentation and/or other materials provided with the distribution.
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
15 *
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
56
76caac0da4a0 added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27 */
76caac0da4a0 added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28
192
1e51558b9d09 updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents: 177
diff changeset
29 #include "avl_tests.h"
56
76caac0da4a0 added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff changeset
30
198
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
31 #include "ucx/utils.h"
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
32
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
33 static int node_height(UcxAVLNode *node) {
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
34 if(!node) {
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
35 return 0;
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
36 }
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
37
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
38 int left = 0;
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
39 int right = 0;
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
40 if(node->left) {
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
41 left = node_height(node->left);
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
42 }
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
43 if(node->right) {
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
44 right = node_height(node->right);
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
45 }
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
46
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
47 return left > right ? left+1 : right+1;
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
48 }
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
49
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
50 static int check_tree(UcxAVLNode *node) {
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
51 if(!node) {
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
52 return 1;
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
53 }
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
54
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
55 int left_height = node_height(node->left);
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
56 int right_height = node_height(node->right);
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
57 int bf = -left_height + right_height;
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
58 if(bf < -1 || bf > 1) {
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
59 return 0;
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
60 }
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
61 int height = left_height > right_height ? left_height : right_height;
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
62 height++;
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
63 if(height != node->height) {
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
64 return 0;
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
65 }
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
66
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
67 if(!check_tree(node->left)) {
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
68 return 0;
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
69 }
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
70 if(!check_tree(node->right)) {
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
71 return 0;
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
72 }
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
73
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
74 return 1;
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
75 }
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
76
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
77 UCX_TEST(test_ucx_avl_put) {
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
78 UcxAVLTree *tree1 = ucx_avl_new(ucx_ptrcmp);
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
79 UcxAVLTree *tree2 = ucx_avl_new(ucx_ptrcmp);
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
80 UcxAVLTree *tree3 = ucx_avl_new(ucx_ptrcmp);
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
81 UcxAVLTree *tree4 = ucx_avl_new(ucx_ptrcmp);
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
82 UcxAVLTree *tree5 = ucx_avl_new(ucx_ptrcmp);
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
83
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
84 char *data1 = (char*)"data1";
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
85 char *data2 = (char*)"data2";
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
86 char *data3 = (char*)"data3";
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
87
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
88 UCX_TEST_BEGIN
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
89
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
90 ucx_avl_put(tree1, 2, (char*)data2);
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
91 ucx_avl_put(tree1, 1, (char*)data1);
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
92 ucx_avl_put(tree1, 3, (char*)data3);
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
93
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
94 UCX_TEST_ASSERT(check_tree(tree1->root), "check_tree failed (tree1)");
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
95 UCX_TEST_ASSERT(ucx_avl_get(tree1, 1) == data1, "wrong data (tree1)");
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
96 UCX_TEST_ASSERT(ucx_avl_get(tree1, 2) == data2, "wrong data (tree1)");
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
97 UCX_TEST_ASSERT(ucx_avl_get(tree1, 3) == data3, "wrong data (tree1)");
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
98
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
99 for(int i=0;i<100000;i++) {
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
100 ucx_avl_put(tree2, i, data1);
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
101 }
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
102 UCX_TEST_ASSERT(check_tree(tree2->root), "check_tree failed (tree2)");
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
103
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
104 for(int i=100000;i>=0;i--) {
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
105 ucx_avl_put(tree3, i, data1);
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
106 }
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
107 UCX_TEST_ASSERT(check_tree(tree3->root), "check_tree failed (tree3)");
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
108
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
109 for(int i=0;i<100;i++) {
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
110 ucx_avl_put(tree4, i, data1);
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
111 }
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
112 for(int i=400;i<500;i++) {
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
113 ucx_avl_put(tree4, i, data2);
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
114 }
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
115 for(int i=399;i>200;i--) {
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
116 ucx_avl_put(tree4, i, data3);
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
117 }
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
118 for(int i=800;i<1000;i++) {
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
119 ucx_avl_put(tree4, i, data1);
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
120 }
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
121 UCX_TEST_ASSERT(check_tree(tree4->root), "check_tree failed (tree4)");
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
122 UCX_TEST_ASSERT(ucx_avl_get(tree4, 0) == data1, "wrong data for key: 0");
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
123 UCX_TEST_ASSERT(ucx_avl_get(tree4, 1) == data1, "wrong data for key: 1");
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
124 UCX_TEST_ASSERT(ucx_avl_get(tree4, 99) == data1, "wrong data for key: 99");
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
125 UCX_TEST_ASSERT(ucx_avl_get(tree4, 400)==data2, "wrong data for key: 400");
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
126 UCX_TEST_ASSERT(ucx_avl_get(tree4, 410)==data2, "wrong data for key: 410");
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
127 UCX_TEST_ASSERT(ucx_avl_get(tree4, 350)==data3, "wrong data for key: 350");
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
128 UCX_TEST_ASSERT(ucx_avl_get(tree4, 900)==data1, "wrong data for key: 900");
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
129
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
130 int values[] = { 3, 6, 12, 9, 23, 1, 0, 20, 34, 5, 8, 7, 6, 14, 15, 2, 4};
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
131 int len = sizeof(values) / sizeof(int);
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
132 for(int i=0;i<len;i++) {
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
133 ucx_avl_put(tree5, values[i], NULL);
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
134 }
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
135 UCX_TEST_ASSERT(check_tree(tree5->root), "check_tree failed (tree5)");
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
136
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
137 UCX_TEST_END
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
138
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
139 ucx_avl_free(tree1);
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
140 ucx_avl_free(tree2);
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
141 ucx_avl_free(tree3);
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
142 ucx_avl_free(tree4);
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
143 ucx_avl_free(tree5);
b0f4fb043b47 added test for ucx_avl_put
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
144 }
200
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
145
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
146 UCX_TEST(test_ucx_avl_remove) {
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
147 UcxAVLTree *tree1 = ucx_avl_new(ucx_ptrcmp);
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
148 UcxAVLTree *tree2 = ucx_avl_new(ucx_ptrcmp);
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
149 UcxAVLTree *tree3 = ucx_avl_new(ucx_ptrcmp);
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
150 UcxAVLTree *tree4 = ucx_avl_new(ucx_ptrcmp);
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
151
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
152 char *data1 = (char*)"data1";
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
153 char *data2 = (char*)"data2";
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
154 char *data3 = (char*)"data3";
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
155
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
156 UCX_TEST_BEGIN
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
157 ucx_avl_put(tree1, 2, (char*)data2);
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
158 ucx_avl_put(tree1, 1, (char*)data1);
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
159 ucx_avl_put(tree1, 3, (char*)data3);
204
4477987d40cd better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents: 203
diff changeset
160 void *val;
4477987d40cd better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents: 203
diff changeset
161 ucx_avl_remove_s(tree1, 3, NULL, &val);
200
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
162
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
163 UCX_TEST_ASSERT(check_tree(tree1->root), "check_tree failed (tree1)");
204
4477987d40cd better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents: 203
diff changeset
164 UCX_TEST_ASSERT(val == data3,
201
45810f5b7b84 extended ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 200
diff changeset
165 "wrong return value for key: 1 (tree1)");
200
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
166 UCX_TEST_ASSERT(ucx_avl_get(tree1, 3) == NULL, "value not removed (tree1)");
204
4477987d40cd better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents: 203
diff changeset
167
4477987d40cd better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents: 203
diff changeset
168 ucx_avl_remove_s(tree1, 2, NULL, &val);
4477987d40cd better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents: 203
diff changeset
169 UCX_TEST_ASSERT(val == data2,
201
45810f5b7b84 extended ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 200
diff changeset
170 "wrong return value for key: 2 (tree1)");
45810f5b7b84 extended ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 200
diff changeset
171 UCX_TEST_ASSERT(check_tree(tree1->root), "check_tree failed (tree1)");
204
4477987d40cd better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents: 203
diff changeset
172
4477987d40cd better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents: 203
diff changeset
173 ucx_avl_remove_s(tree1, 1, NULL, &val);
4477987d40cd better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents: 203
diff changeset
174 UCX_TEST_ASSERT(val == data1,
201
45810f5b7b84 extended ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 200
diff changeset
175 "wrong return value for key: 1 (tree1)");
45810f5b7b84 extended ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 200
diff changeset
176 UCX_TEST_ASSERT(check_tree(tree1->root), "check_tree failed (tree1)");
45810f5b7b84 extended ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 200
diff changeset
177 UCX_TEST_ASSERT(tree1->root == NULL, "root not NULL (tree1)");
45810f5b7b84 extended ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 200
diff changeset
178
200
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
179
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
180 for(int i=0;i<20;i++) {
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
181 if(i==10) {
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
182 ucx_avl_put(tree2, i, data3);
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
183 } else if(i==15) {
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
184 ucx_avl_put(tree2, i, data2);
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
185 } else {
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
186 ucx_avl_put(tree2, i, data1);
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
187 }
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
188 }
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
189
204
4477987d40cd better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents: 203
diff changeset
190 ucx_avl_remove_s(tree2, 10, NULL, &val);
4477987d40cd better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents: 203
diff changeset
191 UCX_TEST_ASSERT(val == data3,
200
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
192 "wrong return value for key: 10 (tree2)");
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
193 UCX_TEST_ASSERT(check_tree(tree2->root), "check_tree failed (tree2)");
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
194
204
4477987d40cd better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents: 203
diff changeset
195 ucx_avl_remove_s(tree2, 15, NULL, &val);
4477987d40cd better and better and better AVL API
Mike Becker <universe@uap-core.de>
parents: 203
diff changeset
196 UCX_TEST_ASSERT(val == data2,
200
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
197 "wrong return value for key: 15 (tree2)");
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
198 UCX_TEST_ASSERT(check_tree(tree2->root), "check_tree failed (tree2)");
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
199
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
200 for(int i=0;i<20;i++) {
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
201 ucx_avl_remove(tree2, i);
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
202 UCX_TEST_ASSERT(check_tree(tree2->root), "check_tree failed (tree2)");
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
203 }
203
3d999ea3f780 added 1 assert in ucx_avl_remove tests and fixed source code formatting
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 201
diff changeset
204 UCX_TEST_ASSERT(tree2->root == NULL, "root not NULL (tree2)");
200
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
205
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
206 for(int i=0;i<100000;i++) {
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
207 ucx_avl_put(tree3, i, data1);
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
208 }
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
209 for(int i=100000-1;i>=0;i--) {
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
210 ucx_avl_remove(tree3, i);
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
211 }
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
212 UCX_TEST_ASSERT(tree3->root == NULL, "root not NULL (tree3)");
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
213 UCX_TEST_ASSERT(check_tree(tree3->root), "check_tree failed (tree3)");
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
214
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
215 ucx_avl_put(tree4, 1, data1);
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
216 ucx_avl_remove(tree4, 1);
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
217 UCX_TEST_ASSERT(check_tree(tree4->root), "check_tree failed (tree4)");
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
218 UCX_TEST_ASSERT(tree4->root == NULL, "root not NULL (tree4)");
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
219 UCX_TEST_END
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
220
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
221 ucx_avl_free(tree1);
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
222 ucx_avl_free(tree2);
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
223 ucx_avl_free(tree3);
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
224 ucx_avl_free(tree4);
e3aad99d2d80 added ucx_avl_remove tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 198
diff changeset
225 }

mercurial