Go to most recent revision | Details | 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/extavlreltree.h> |
||
32 | #include <debug.h> |
||
33 | |||
34 | #include <panic.h> |
||
35 | |||
36 | |||
37 | #define NODE_COUNT 100 |
||
38 | |||
39 | /* |
||
40 | * wrapper structure with a pointer to extended avl tree root |
||
41 | */ |
||
42 | static extavlreltree_t extreltree; |
||
43 | |||
44 | /* |
||
45 | * extavltree nodes in array for faster allocating |
||
46 | */ |
||
47 | static extavlreltree_node_t extavlreltree_nodes[NODE_COUNT]; |
||
48 | |||
49 | /* |
||
50 | * head of free nodes' list: |
||
51 | */ |
||
52 | static extavlreltree_node_t *first_free_node = NULL; |
||
53 | |||
54 | |||
55 | |||
56 | static int extreltree_test_height(extrelavltree_node_t *node,bool timeout); |
||
57 | static extavlreltree_node_t *extreltree_test_parents(extavlireltree_node_t *node); |
||
58 | static void print_extreltree_structure_flat (extavlreltree_node_t *node, int level); |
||
59 | static bool extreltree_test_link(int node_count); |
||
60 | static void print_extreltree_link(int node_count); |
||
61 | static extavlreltree_node_t *alloc_extavlreltree_node(void); |
||
62 | |||
63 | |||
64 | |||
65 | //vraci hloubku stromu |
||
66 | static int extreltree_test_height(extavltree_node_t *node,bool timeout) |
||
67 | { |
||
68 | int h1, h2; |
||
69 | |||
70 | if (!node) return -1; |
||
71 | |||
72 | h1 = extreltree_test_height(node->lft,timeout) + 1; |
||
73 | if (!timeout && node->lft_height != h1) { |
||
74 | printf("Bad height: %d of LEFT subtree in node: %d with address: %p\n",h1,node->key,node); |
||
75 | } |
||
76 | h2 = extreltree_test_height(node->rgt,0) + 1; |
||
77 | if (node->rgt_height != h2) { |
||
78 | printf("Bad height: %d of RIGHT subtree in node: %d with address: %p\n",h2,node->key,node); |
||
79 | } |
||
80 | if (!timeout && (((h1-h2)>0?h1-h2:h2-h1) > 1)) { |
||
81 | printf("Bad height: error in definition of avltree: %d with address: %p\n",node->key,node); |
||
82 | } |
||
83 | |||
84 | return (node->lft_height > node->rgt_height? node->lft_height: node->rgt_height); |
||
85 | } |
||
86 | |||
87 | |||
88 | /** Tests par atribute of every tree node |
||
89 | * |
||
90 | */ |
||
91 | static extavlreltree_node_t *extreltree_test_parents(extavlreltree_node_t *node) |
||
92 | { |
||
93 | extavltree_node_t *tmp; |
||
94 | |||
95 | if (!node) return NULL; |
||
96 | |||
97 | if (node->lft) { |
||
98 | tmp = extreltree_test_parents(node->lft); |
||
99 | if (tmp != node) { |
||
100 | printf("Bad parent pointer at node with key: %d, address: %p\n",tmp->key,node->lft); |
||
101 | } |
||
102 | } |
||
103 | if (node->rgt) { |
||
104 | tmp = extreltree_test_parents(node->rgt); |
||
105 | if (tmp != node) { |
||
106 | printf("Bad parent pointer at node with key: %d, address: %p\n",tmp->key,node->rgt); |
||
107 | } |
||
108 | } |
||
109 | return node->par; |
||
110 | } |
||
111 | |||
112 | /** Checks list of nodes |
||
113 | * |
||
114 | */ |
||
115 | static bool extreltree_test_link(int node_count) |
||
116 | { |
||
117 | extavltree_node_t *node; |
||
118 | int i; |
||
119 | bool test_link = true; |
||
120 | |||
121 | for (i = 0,node = exttree.head.next; node != &(exttree.head); i++,node = node->next) |
||
122 | ; |
||
123 | if (node_count && i != node_count) { |
||
124 | printf("\nBad node count!!! Counted: %d, right number: %d", i, node_count); |
||
125 | test_link = false; |
||
126 | } |
||
127 | for (i = 0,node = exttree.head.prev; node != &(exttree.head); i++,node = node->prev) |
||
128 | ; |
||
129 | if (node_count && i != node_count) { |
||
130 | printf("\nBad node count!!! Counted: %d, right number: %d", i, node_count); |
||
131 | test_link = false; |
||
132 | } |
||
133 | return test_link; |
||
134 | } |
||
135 | |||
136 | static int _sum_extreltree(extavlreltree_node_t *node) |
||
137 | { |
||
138 | int sum = 0; |
||
139 | |||
140 | if (node->rgt) |
||
141 | sum += _sum_extreltree(node->rgt); |
||
142 | if (node->lft) |
||
143 | sum += _sum_extreltree(node->lft); |
||
144 | |||
145 | return sum + node->key; |
||
146 | } |
||
147 | |||
148 | static int extreltree_test_maxsum(extavlreltree_node_t *node) |
||
149 | { |
||
150 | int rs, ls; |
||
151 | |||
152 | if (!node) return 0; |
||
153 | |||
154 | rs = extreltree_test_maxsum(node->rgt); |
||
155 | ls = extreltree_test_maxsum(node->lft); |
||
156 | |||
157 | if (rs != node->rgt_max_key) { |
||
158 | printf("\nBad RGT_SUM: %d, should be: %d node: %d, address: %p\n",node->rgt_max_key,rs,node->key,node); |
||
159 | getchar(); |
||
160 | } |
||
161 | return rs + ls + node->key; |
||
162 | } |
||
163 | |||
164 | /** Prints the structure of node, which is level levels from the top of the tree. |
||
165 | * |
||
166 | */ |
||
167 | static void print_extreltree_structure_flat (extavlreltree_node_t *node, int level) |
||
168 | { |
||
169 | extavlreltree_node_t *tmp; |
||
170 | int i; |
||
171 | |||
172 | if (level > 16) |
||
173 | { |
||
174 | printf ("[...]"); |
||
175 | return; |
||
176 | } |
||
177 | |||
178 | if (node == NULL) |
||
179 | return; |
||
180 | |||
181 | for (tmp = node->next,i = 0; (tmp->key == 0) && (tmp != &(extreltree.head)); tmp = tmp->next,i++) |
||
182 | ; |
||
183 | |||
184 | printf ("%d[%d,%d]", node->key,node->rgt_max_key,i); |
||
185 | if (node->lft != NULL || node->rgt != NULL) |
||
186 | { |
||
187 | putchar ('('); |
||
188 | |||
189 | print_extreltree_structure_flat (node->lft, level + 1); |
||
190 | if (node->rgt != NULL) |
||
191 | { |
||
192 | putchar (','); |
||
193 | print_extreltree_structure_flat (node->rgt, level + 1); |
||
194 | } |
||
195 | |||
196 | putchar (')'); |
||
197 | } |
||
198 | } |
||
199 | |||
200 | /** Prints list of nodes |
||
201 | * |
||
202 | */ |
||
203 | static void print_extreltree_link(int node_count) |
||
204 | { |
||
205 | extavltree_node_t *node; |
||
206 | printf("\n"); |
||
207 | for (node = exttree.head.next; node != &(exttree.head); node = node->next) { |
||
208 | printf(" %d,",node->key); |
||
209 | } |
||
210 | for (node = exttree.head.prev; node != &(exttree.head); node = node->prev) { |
||
211 | printf(" %d,",node->key); |
||
212 | } |
||
213 | } |
||
214 | |||
215 | //**************************************************************** |
||
216 | static void alloc_extavltree_node_prepare(void) |
||
217 | { |
||
218 | int i; |
||
219 | |||
220 | for (i = 0; i < NODE_COUNT - 1; i++) { |
||
221 | extavlreltree_nodes[i].next = &(extavlreltree_nodes[i+1]); |
||
222 | } |
||
223 | /* |
||
224 | * Node keys which will be used for insertion. Up to NODE_COUNT size of array. |
||
225 | */ |
||
226 | |||
227 | // First tree node and same key |
||
228 | extavlreltree_nodes[0].key = 60; |
||
229 | extavlreltree_nodes[1].key = 60; |
||
230 | extavlreltree_nodes[2].key = 60; |
||
231 | //LL rotation |
||
232 | extavlreltree_nodes[3].key = 50; |
||
233 | extavlreltree_nodes[4].key = 40; |
||
234 | extavlreltree_nodes[5].key = 30; |
||
235 | //LR rotation |
||
236 | extavlreltree_nodes[6].key = 20; |
||
237 | extavlreltree_nodes[7].key = 20; |
||
238 | extavlreltree_nodes[8].key = 25; |
||
239 | extavlreltree_nodes[9].key = 25; |
||
240 | //LL rotation in lower floor |
||
241 | extavlreltree_nodes[10].key = 35; |
||
242 | //RR rotation |
||
243 | extavlreltree_nodes[11].key = 70; |
||
244 | extavlreltree_nodes[12].key = 80; |
||
245 | //RL rotation |
||
246 | extavlreltree_nodes[13].key = 90; |
||
247 | extavlreltree_nodes[14].key = 85; |
||
248 | extavlreltree_nodes[15].key = 100; |
||
249 | extavlreltree_nodes[16].key = 200; |
||
250 | extavlreltree_nodes[17].key = 300; |
||
251 | extavlreltree_nodes[18].key = 400; |
||
252 | extavlreltree_nodes[19].key = 500; |
||
253 | extavlreltree_nodes[20].key = 600; |
||
254 | |||
255 | for (i = 21; i < NODE_COUNT; i++) |
||
256 | extavlreltree_nodes[i].key = i * 3; |
||
257 | |||
258 | extavlreltree_nodes[i].next = NULL; |
||
259 | first_free_node = &(extavlreltree_nodes[0]); |
||
260 | } |
||
261 | |||
262 | static extavlreltree_node_t *alloc_extavlreltree_node(void) |
||
263 | { |
||
264 | extavlreltree_node_t *node; |
||
265 | |||
266 | node = first_free_node; |
||
267 | first_free_node = first_free_node->next; |
||
268 | |||
269 | return node; |
||
270 | } |
||
271 | //**************************************************************** |
||
272 | |||
273 | static void test_extreltree_insert(extavlreltree_t *tree, unsigned int node_count, int quiet) |
||
274 | { |
||
275 | unsigned int i; |
||
276 | extavlreltree_node_t *newnode; |
||
277 | |||
278 | /* |
||
279 | * Initialize tree before using. |
||
280 | */ |
||
281 | extavlreltree_create(tree); |
||
282 | |||
283 | if (!quiet) printf("\n\nInserting %d nodes ...\n", node_count); |
||
284 | |||
285 | for (i = 0; i < node_count; i++) { |
||
286 | newnode = alloc_extavlreltree_node(); |
||
287 | //if (!quiet) printf("[[[%d]]]\n",newnode->key); |
||
288 | |||
289 | extavlreltree_insert(tree, newnode); |
||
290 | if (!quiet) { |
||
291 | if (!extreltree_test_link(i+1)) { |
||
292 | print_extreltree_link(i+1); |
||
293 | printf("\n"); |
||
294 | } |
||
295 | extreltree_test_parents(tree->root); |
||
296 | extreltree_test_height(tree->root,1); |
||
297 | extreltree_test_rgtsum(tree->root); |
||
298 | } |
||
299 | } |
||
300 | |||
301 | if (!quiet) printf("Inserting was finished\n"); |
||
302 | } |
||
303 | |||
304 | static void test_extreltree_delete(extavlreltree_t *tree, int node_count, int node_position, bool quiet) |
||
305 | { |
||
306 | extavlreltree_node_t *delnode; |
||
307 | unsigned int i; |
||
308 | |||
309 | //aktualni pocet tiku: |
||
310 | if (!quiet) printf("Deleting tree...\n"); |
||
311 | |||
312 | switch(node_position) { |
||
313 | case 0: //mazani vzdy korene |
||
314 | if (!quiet) printf("\n\nDelete root nodes\n"); |
||
315 | i = node_count; |
||
316 | while(tree->root != NULL) { |
||
317 | delnode = tree->root; |
||
318 | extavlreltree_delete(tree,delnode); |
||
319 | if (!quiet) { |
||
320 | if (!extreltree_test_link(i)) { |
||
321 | print_extreltree_link(i); |
||
322 | printf("\n"); |
||
323 | } |
||
324 | extreltree_test_parents(tree->root); |
||
325 | extreltree_test_height(tree->root,1); |
||
326 | extreltree_test_rgtsum(tree->root); |
||
327 | } |
||
328 | i--; |
||
329 | } |
||
330 | |||
331 | break; |
||
332 | case 1: |
||
333 | if (!quiet) printf("\n\nDelete nodes according to their time of origin\n"); |
||
334 | for (i = 0; i < node_count; i++) { |
||
335 | extavlreltree_delete(tree,&(extavlreltree_nodes[i])); |
||
336 | if (!quiet) { |
||
337 | if (!extreltree_test_link(i+1)) { |
||
338 | print_extreltree_link(i+1); |
||
339 | printf("\n"); |
||
340 | } |
||
341 | extreltree_test_parents(tree->root); |
||
342 | extreltree_test_height(tree->root,1); |
||
343 | extreltree_test_rgtsum(tree->root); |
||
344 | } |
||
345 | } |
||
346 | |||
347 | break; |
||
348 | } |
||
349 | |||
350 | if (!quiet) printf("Deletion was finished\n"); |
||
351 | } |
||
352 | |||
353 | static void timeout_extreltree(extavlreltree_t *tree, int node_count, bool quiet) |
||
354 | { |
||
355 | int i = node_count; |
||
356 | |||
357 | if (!quiet) printf("Timeout tree ...\n"); |
||
358 | |||
359 | while(tree->head.next != &(tree->head)) { |
||
360 | extavlreltree_delete_min(tree); |
||
361 | if (!quiet) { |
||
362 | if (!extreltree_test_link(i)) { |
||
363 | print_extreltree_link(i); |
||
364 | printf("\n"); |
||
365 | } |
||
366 | extreltree_test_parents(tree->root); |
||
367 | extreltree_test_height(tree->root,1); |
||
368 | extreltree_test_rgtsum(tree->root); |
||
369 | } |
||
370 | i--; |
||
371 | } |
||
372 | |||
373 | if (!quiet && (i != node_count)) printf("Bad node count. Some nodes have been lost!"); |
||
374 | |||
375 | if (!quiet) printf("Timeout tree finished\n"); |
||
376 | } |
||
377 | |||
378 | char * test_extavlreltree1(bool quiet) |
||
379 | { |
||
380 | alloc_extavlreltree_node_prepare(); |
||
381 | test_extreltree_insert(&extreltree, NODE_COUNT, quiet); |
||
382 | test_extreltree_delete(&extreltree, NODE_COUNT, 0, quiet); |
||
383 | |||
384 | alloc_extavlreltree_node_prepare(); |
||
385 | test_extreltree_insert(&extreltree, NODE_COUNT, quiet); |
||
386 | test_extreltree_delete(&extreltree, NODE_COUNT, 1, quiet); |
||
387 | |||
388 | alloc_extavlreltree_node_prepare(); |
||
389 | test_extreltree_insert(&extreltree, NODE_COUNT, quiet); |
||
390 | timeout_extreltree(&extreltree, NODE_COUNT, quiet); |
||
391 | |||
392 | return NULL; |
||
393 | } |