Rev 2421 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2416 | mencl | 1 | /* |
2 | * Copyright (c) 2007 Vojtech Mencl |
||
3 | * All rights reserved. |
||
4 | * |
||
5 | * Redistribution and use in source and binary forms, with or without |
||
6 | * modification, are permitted provided that the following conditions |
||
7 | * are met: |
||
8 | * |
||
9 | * - Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * - Redistributions in binary form must reproduce the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer in the |
||
13 | * documentation and/or other materials provided with the distribution. |
||
14 | * - The name of the author may not be used to endorse or promote products |
||
15 | * derived from this software without specific prior written permission. |
||
16 | * |
||
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
||
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
||
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
27 | */ |
||
28 | |||
29 | #include <test.h> |
||
30 | #include <print.h> |
||
31 | #include <adt/avl.h> |
||
32 | #include <debug.h> |
||
33 | |||
34 | |||
35 | #define NODE_COUNT 100 |
||
36 | |||
37 | /* |
||
38 | * wrapper structure with a pointer to avl tree root |
||
39 | */ |
||
40 | static avltree_t avltree; |
||
41 | |||
42 | /* |
||
43 | * avl tree nodes in array for faster allocating |
||
44 | */ |
||
45 | static avltree_node_t avltree_nodes[NODE_COUNT]; |
||
46 | |||
47 | /* |
||
48 | * head of free nodes' list: |
||
49 | */ |
||
50 | static avltree_node_t *first_free_node = NULL; |
||
51 | |||
52 | |||
53 | |||
54 | static int test_tree_balance(avltree_node_t *node); |
||
2421 | mencl | 55 | static avltree_node_t *test_tree_parents(avltree_node_t *node); |
2416 | mencl | 56 | static void print_tree_structure_flat (avltree_node_t *node, int level); |
57 | static avltree_node_t *alloc_avltree_node(void); |
||
58 | |||
59 | |||
60 | |||
61 | static avltree_node_t *test_tree_parents(avltree_node_t *node) |
||
62 | { |
||
63 | avltree_node_t *tmp; |
||
64 | |||
65 | if (!node) return NULL; |
||
66 | |||
67 | if (node->lft) { |
||
68 | tmp = test_tree_parents(node->lft); |
||
69 | if (tmp != node) { |
||
70 | printf("Bad parent pointer key: %d, address: %p\n",tmp->key,node->lft); |
||
71 | } |
||
72 | } |
||
73 | if (node->rgt) { |
||
74 | tmp = test_tree_parents(node->rgt); |
||
75 | if (tmp != node) { |
||
76 | printf("Bad parent pointer key: %d, address: %p\n",tmp->key,node->rgt); |
||
77 | } |
||
78 | } |
||
79 | return node->par; |
||
80 | } |
||
81 | |||
82 | int test_tree_balance(avltree_node_t *node) |
||
83 | { |
||
84 | int h1, h2, diff; |
||
85 | |||
86 | if (!node) return 0; |
||
87 | h1 = test_tree_balance(node->lft); |
||
88 | h2 = test_tree_balance(node->rgt); |
||
89 | diff = h2 - h1; |
||
90 | if (diff != node->balance || (diff != -1 && diff != 0 && diff != 1)) { |
||
91 | printf("Bad balance\n"); |
||
92 | } |
||
93 | return h1 > h2 ? h1 + 1 : h2 + 1; |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Prints the structure of node, which is level levels from the top of the tree. |
||
98 | */ |
||
2421 | mencl | 99 | static void print_tree_structure_flat (avltree_node_t *node, int level) |
2416 | mencl | 100 | { |
101 | /* You can set the maximum level as high as you like. |
||
102 | Most of the time, you'll want to debug code using small trees, |
||
103 | so that a large level indicates a loop, which is a bug. */ |
||
104 | if (level > 16) |
||
105 | { |
||
106 | printf ("[...]"); |
||
107 | return; |
||
108 | } |
||
109 | |||
110 | if (node == NULL) |
||
111 | return; |
||
112 | |||
113 | printf ("%d[%d]", node->key,node->balance); |
||
114 | if (node->lft != NULL || node->rgt != NULL) |
||
115 | { |
||
2421 | mencl | 116 | printf("("); |
2416 | mencl | 117 | |
118 | print_tree_structure_flat (node->lft, level + 1); |
||
119 | if (node->rgt != NULL) |
||
120 | { |
||
2421 | mencl | 121 | printf(","); |
2416 | mencl | 122 | print_tree_structure_flat (node->rgt, level + 1); |
123 | } |
||
124 | |||
2421 | mencl | 125 | printf(")"); |
2416 | mencl | 126 | } |
127 | } |
||
128 | |||
129 | |||
130 | //**************************************************************** |
||
131 | static void alloc_avltree_node_prepare(void) |
||
132 | { |
||
133 | int i; |
||
134 | |||
135 | for (i = 0; i < NODE_COUNT - 1; i++) { |
||
2421 | mencl | 136 | avltree_nodes[i].par = &(avltree_nodes[i+1]); |
2416 | mencl | 137 | } |
138 | /* |
||
139 | * Node keys which will be used for insertion. Up to NODE_COUNT size of array. |
||
140 | */ |
||
141 | |||
142 | // First tree node and same key |
||
143 | avltree_nodes[0].key = 60; |
||
144 | avltree_nodes[1].key = 60; |
||
145 | avltree_nodes[2].key = 60; |
||
146 | //LL rotation |
||
147 | avltree_nodes[3].key = 50; |
||
148 | avltree_nodes[4].key = 40; |
||
149 | avltree_nodes[5].key = 30; |
||
150 | //LR rotation |
||
151 | avltree_nodes[6].key = 20; |
||
152 | avltree_nodes[7].key = 20; |
||
153 | avltree_nodes[8].key = 25; |
||
154 | avltree_nodes[9].key = 25; |
||
155 | //LL rotation in lower floor |
||
156 | avltree_nodes[10].key = 35; |
||
157 | //RR rotation |
||
158 | avltree_nodes[11].key = 70; |
||
159 | avltree_nodes[12].key = 80; |
||
160 | //RL rotation |
||
161 | avltree_nodes[13].key = 90; |
||
162 | avltree_nodes[14].key = 85; |
||
2431 | mencl | 163 | //Insert 0 key |
164 | avltree_nodes[15].key = 0; |
||
165 | avltree_nodes[16].key = 0; |
||
166 | //Insert reverse |
||
167 | avltree_nodes[17].key = 600; |
||
168 | avltree_nodes[18].key = 500; |
||
169 | avltree_nodes[19].key = 400; |
||
170 | avltree_nodes[20].key = 300; |
||
2416 | mencl | 171 | |
172 | for (i = 21; i < NODE_COUNT; i++) |
||
173 | avltree_nodes[i].key = i * 3; |
||
174 | |||
2421 | mencl | 175 | avltree_nodes[i].par = NULL; |
2416 | mencl | 176 | first_free_node = &(avltree_nodes[0]); |
177 | } |
||
178 | |||
179 | static avltree_node_t *alloc_avltree_node(void) |
||
180 | { |
||
181 | avltree_node_t *node; |
||
182 | |||
183 | node = first_free_node; |
||
2421 | mencl | 184 | first_free_node = first_free_node->par; |
2416 | mencl | 185 | |
186 | return node; |
||
187 | } |
||
188 | //**************************************************************** |
||
189 | |||
190 | static void test_tree_insert(avltree_t *tree, unsigned int node_count, int quiet) |
||
191 | { |
||
192 | unsigned int i; |
||
193 | avltree_node_t *newnode; |
||
194 | |||
195 | /* |
||
196 | * Initialize tree before using. |
||
197 | */ |
||
198 | avltree_create(tree); |
||
199 | |||
2421 | mencl | 200 | if (!quiet) printf("\nInserting %d nodes ...\n", node_count); |
2416 | mencl | 201 | |
202 | for (i = 0; i < node_count; i++) { |
||
203 | newnode = alloc_avltree_node(); |
||
204 | //if (!quiet) printf("[[[%d]]]\n",newnode->key); |
||
205 | |||
206 | avltree_insert(tree, newnode); |
||
207 | if (!quiet) { |
||
208 | test_tree_parents(tree->root); |
||
209 | test_tree_balance(tree->root); |
||
210 | } |
||
211 | } |
||
212 | |||
213 | if (!quiet) printf("Inserting was finished\n"); |
||
214 | } |
||
215 | |||
216 | |||
217 | static void test_tree_delete(avltree_t *tree, int node_count, int node_position, bool quiet) |
||
218 | { |
||
219 | avltree_node_t *delnode; |
||
220 | unsigned int i; |
||
221 | |||
222 | //aktualni pocet tiku: |
||
223 | if (!quiet) printf("Deleting tree...\n"); |
||
224 | |||
225 | switch(node_position) { |
||
226 | case 0: //mazani vzdy korene |
||
2421 | mencl | 227 | if (!quiet) printf("\nDelete root nodes\n"); |
2416 | mencl | 228 | while(tree->root != NULL) { |
229 | delnode = tree->root; |
||
230 | avltree_delete(tree,delnode); |
||
231 | if (!quiet) { |
||
232 | test_tree_parents(tree->root); |
||
233 | test_tree_balance(tree->root); |
||
234 | } |
||
235 | } |
||
236 | |||
237 | break; |
||
238 | case 1: |
||
2421 | mencl | 239 | if (!quiet) printf("\nDelete nodes according to their time of origin\n"); |
2416 | mencl | 240 | for (i = 0; i < node_count; i++) { |
241 | avltree_delete(tree,&(avltree_nodes[i])); |
||
242 | if (!quiet) { |
||
243 | test_tree_parents(tree->root); |
||
244 | test_tree_balance(tree->root); |
||
245 | } |
||
246 | } |
||
247 | |||
248 | break; |
||
249 | } |
||
250 | |||
251 | if (!quiet) printf("Deletion was finished\n"); |
||
252 | } |
||
253 | |||
254 | static void timeout_tree(avltree_t *tree, int node_count, bool quiet) |
||
255 | { |
||
256 | int i = 0; |
||
257 | |||
2421 | mencl | 258 | if (!quiet) printf("\nTimeout tree ...\n"); |
2416 | mencl | 259 | |
2421 | mencl | 260 | while(tree->root != NULL) { |
2416 | mencl | 261 | i++; |
262 | avltree_delete_min(tree); |
||
263 | if (!quiet) { |
||
264 | test_tree_parents(tree->root); |
||
265 | test_tree_balance(tree->root); |
||
266 | } |
||
267 | } |
||
268 | |||
2421 | mencl | 269 | if (!quiet && (i != node_count)) printf("Bad node count. Some nodes have been lost!\n"); |
2416 | mencl | 270 | |
271 | if (!quiet) printf("Timeout tree finished\n"); |
||
272 | } |
||
273 | |||
274 | char * test_avltree1(bool quiet) |
||
275 | { |
||
276 | alloc_avltree_node_prepare(); |
||
2421 | mencl | 277 | test_tree_insert(&avltree, NODE_COUNT, quiet); |
278 | test_tree_delete(&avltree, NODE_COUNT, 0, quiet); |
||
2416 | mencl | 279 | |
280 | alloc_avltree_node_prepare(); |
||
2421 | mencl | 281 | test_tree_insert(&avltree, NODE_COUNT, quiet); |
282 | test_tree_delete(&avltree, NODE_COUNT, 1, quiet); |
||
2416 | mencl | 283 | |
284 | alloc_avltree_node_prepare(); |
||
2421 | mencl | 285 | test_tree_insert(&avltree, NODE_COUNT, quiet); |
286 | timeout_tree(&avltree, NODE_COUNT, quiet); |
||
2416 | mencl | 287 | |
288 | return NULL; |
||
289 | } |