nfs-ganesha 1.4
|
00001 #include <limits.h> 00002 #include <stdio.h> 00003 #include <stdlib.h> 00004 #include <stddef.h> 00005 #include <string.h> 00006 00007 #include "CUnit/Basic.h" 00008 00009 #include "avltree.h" 00010 00011 00012 #define DEBUG 1 00013 00014 /* STATICS we use across multiple tests */ 00015 struct avltree avl_tree_1; 00016 struct avltree avl_tree_2; 00017 struct avltree avl_tree_100; 00018 struct avltree avl_tree_10000; 00019 00020 00021 typedef struct avl_unit_val 00022 { 00023 int refs; 00024 struct avltree_node node_k; 00025 unsigned long key; 00026 unsigned long val; 00027 } avl_unit_val_t; 00028 00029 int 00030 avl_unit_cmpf(const struct avltree_node *lhs, 00031 const struct avltree_node *rhs) 00032 { 00033 avl_unit_val_t *lk, *rk; 00034 00035 lk = avltree_container_of(lhs, avl_unit_val_t, node_k); 00036 rk = avltree_container_of(rhs, avl_unit_val_t, node_k); 00037 00038 if (lk->key < rk->key) 00039 return (-1); 00040 00041 if (lk->key == rk->key) 00042 return (0); 00043 00044 return (1); 00045 } 00046 00047 avl_unit_val_t* 00048 avl_unit_new_val(unsigned long intval) 00049 { 00050 avl_unit_val_t *v = malloc(sizeof(avl_unit_val_t)); 00051 memset(v, 0, sizeof(avl_unit_val_t)); 00052 v->val = (intval + 1); 00053 00054 return v; 00055 } 00056 00057 void 00058 avl_unit_free_val(avl_unit_val_t *v) 00059 { 00060 free(v); 00061 } 00062 00063 void 00064 avl_unit_clear_tree(struct avltree *t) 00065 { 00066 avl_unit_val_t *v; 00067 struct avltree_node *node, *next_node; 00068 00069 00070 if (avltree_size(t) < 1) 00071 return; 00072 00073 node = avltree_first(t); 00074 while (node) { 00075 next_node = avltree_next(node); 00076 v = avltree_container_of(node, avl_unit_val_t, node_k); 00077 avltree_remove(&v->node_k, &avl_tree_1); 00078 avl_unit_free_val(v); 00079 node = next_node; 00080 } 00081 } 00082 00083 /* dne */ 00084 void avltree_destroy(struct avltree *t) 00085 { 00086 return; 00087 } 00088 00089 void 00090 avl_unit_clear_and_destroy_tree(struct avltree *t) 00091 { 00092 avl_unit_clear_tree(t); 00093 avltree_destroy(t); 00094 } 00095 00096 /* 00097 * BEGIN SUITE INITIALIZATION and CLEANUP FUNCTIONS 00098 */ 00099 00100 void avl_unit_PkgInit(void) 00101 { 00102 /* nothing */ 00103 } 00104 00105 /* 00106 * The suite initialization function. 00107 * Initializes resources to be shared across tests. 00108 * Returns zero on success, non-zero otherwise. 00109 * 00110 */ 00111 int init_suite1(void) 00112 { 00113 00114 avltree_init(&avl_tree_1, avl_unit_cmpf, 0 /* flags */); 00115 00116 return 0; 00117 } 00118 00119 /* The suite cleanup function. 00120 * Closes the temporary resources used by the tests. 00121 * Returns zero on success, non-zero otherwise. 00122 */ 00123 int clean_suite1(void) 00124 { 00125 if (avltree_size(&avl_tree_1) > 0) 00126 avl_unit_clear_tree(&avl_tree_1); 00127 00128 avltree_destroy(&avl_tree_1); 00129 00130 return 0; 00131 } 00132 00133 /* 00134 * The suite initialization function. 00135 * Initializes resources to be shared across tests. 00136 * Returns zero on success, non-zero otherwise. 00137 * 00138 */ 00139 int init_suite2(void) 00140 { 00141 avltree_init(&avl_tree_2, avl_unit_cmpf, 0 /* flags */); 00142 00143 return 0; 00144 } 00145 00146 /* The suite cleanup function. 00147 * Closes the temporary resources used by the tests. 00148 * Returns zero on success, non-zero otherwise. 00149 */ 00150 int clean_suite2(void) 00151 { 00152 00153 avltree_destroy(&avl_tree_2); 00154 00155 return 0; 00156 } 00157 00158 /* 00159 * The suite initialization function. 00160 * Initializes resources to be shared across tests. 00161 * Returns zero on success, non-zero otherwise. 00162 * 00163 */ 00164 int init_suite100(void) 00165 { 00166 avltree_init(&avl_tree_100, avl_unit_cmpf, 0 /* flags */); 00167 00168 return 0; 00169 } 00170 00171 /* The suite cleanup function. 00172 * Closes the temporary resources used by the tests. 00173 * Returns zero on success, non-zero otherwise. 00174 */ 00175 int clean_suite100(void) 00176 { 00177 00178 avltree_destroy(&avl_tree_100); 00179 00180 return 0; 00181 } 00182 00183 /* 00184 * The suite initialization function. 00185 * Initializes resources to be shared across tests. 00186 * Returns zero on success, non-zero otherwise. 00187 * 00188 */ 00189 int init_suite10000(void) 00190 { 00191 avltree_init(&avl_tree_10000, avl_unit_cmpf, 0 /* flags */); 00192 00193 return 0; 00194 } 00195 00196 /* The suite cleanup function. 00197 * Closes the temporary resources used by the tests. 00198 * Returns zero on success, non-zero otherwise. 00199 */ 00200 int clean_suite10000(void) 00201 { 00202 avltree_destroy(&avl_tree_10000); 00203 00204 return 0; 00205 } 00206 00207 00208 /* 00209 * The suite initialization function. 00210 * Initializes resources to be shared across tests. 00211 * Returns zero on success, non-zero otherwise. 00212 * 00213 */ 00214 int init_supremum(void) 00215 { 00216 avltree_init(&avl_tree_2, avl_unit_cmpf, 0 /* flags */); 00217 00218 return 0; 00219 } 00220 00221 /* The suite cleanup function. 00222 * Closes the temporary resources used by the tests. 00223 * Returns zero on success, non-zero otherwise. 00224 */ 00225 int clean_supremum(void) 00226 { 00227 00228 avltree_destroy(&avl_tree_2); 00229 00230 return 0; 00231 } 00232 00233 00234 /* 00235 * END SUITE INITIALIZATION and CLEANUP FUNCTIONS 00236 */ 00237 00238 00239 /* 00240 * BEGIN BASIC TESTS 00241 */ 00242 00243 void inserts_tree_1(void) 00244 { 00245 avl_unit_val_t *v; 00246 int ix; 00247 00248 for (ix = 1; ix < 2; ++ix) { 00249 00250 /* new k, v */ 00251 v = avl_unit_new_val(ix); 00252 00253 /* if actual key cannot be marshalled as a pointer */ 00254 v->key = ix; 00255 00256 /* insert mapping */ 00257 avltree_insert(&v->node_k, &avl_tree_1); 00258 } 00259 } 00260 00261 void check_tree_1(void) 00262 { 00263 int code = 0; 00264 CU_ASSERT_EQUAL(code, 0); 00265 } 00266 00267 void lookups_tree_1(void) 00268 { 00269 struct avltree_node *node; 00270 avl_unit_val_t *v2, *v = avl_unit_new_val(0); 00271 int ix; 00272 00273 for (ix = 1; ix < 2; ++ix) { 00274 00275 /* reuse v */ 00276 v->key = ix; 00277 00278 /* lookup mapping */ 00279 node = avltree_lookup(&v->node_k, &avl_tree_1); 00280 v2 = avltree_container_of(node, avl_unit_val_t, node_k); 00281 CU_ASSERT((unsigned long) v2->val == (ix+1)); 00282 } 00283 00284 /* free v */ 00285 avl_unit_free_val(v); 00286 } 00287 00288 void deletes_tree_1(void) 00289 { 00290 struct avltree_node *node; 00291 avl_unit_val_t *v2, *v = avl_unit_new_val(0); 00292 int ix; 00293 00294 for (ix = 1; ix < 2; ++ix) { 00295 00296 /* reuse key */ 00297 v->key = ix; 00298 00299 /* find mapping */ 00300 node = avltree_lookup(&v->node_k, &avl_tree_1); 00301 v2 = avltree_container_of(node, avl_unit_val_t, node_k); 00302 CU_ASSERT(v2->val == (ix+1)); 00303 00304 /* and remove it */ 00305 avltree_remove(&v2->node_k, &avl_tree_1); 00306 avl_unit_free_val(v2); 00307 } 00308 00309 /* free search k */ 00310 avl_unit_free_val(v); 00311 } 00312 00313 void inserts_tree_2(void) 00314 { 00315 avl_unit_val_t *v; 00316 int ix; 00317 00318 for (ix = 1; ix < 4; ++ix) { 00319 00320 /* new k, v */ 00321 v = avl_unit_new_val(ix); 00322 00323 /* if actual key cannot be marshalled as a pointer */ 00324 v->key = ix; 00325 00326 /* insert mapping */ 00327 avltree_insert(&v->node_k, &avl_tree_2); 00328 } 00329 } 00330 00331 void inserts_tree_2r(void) 00332 { 00333 avl_unit_val_t *v; 00334 int ix; 00335 00336 for (ix = 3; ix > 0; --ix) { 00337 00338 /* new k, v */ 00339 v = avl_unit_new_val(ix); 00340 00341 /* if actual key cannot be marshalled as a pointer */ 00342 v->key = ix; 00343 00344 /* insert mapping */ 00345 avltree_insert(&v->node_k, &avl_tree_2); 00346 } 00347 } 00348 00349 00350 void check_tree_2(void) 00351 { 00352 int code = 0; 00353 CU_ASSERT_EQUAL(code, 0); 00354 } 00355 00356 void lookups_tree_2(void) 00357 { 00358 struct avltree_node *node; 00359 avl_unit_val_t *v2, *v = avl_unit_new_val(0); 00360 int ix; 00361 00362 for (ix = 1; ix < 4; ++ix) { 00363 00364 /* reuse v */ 00365 v->key = ix; 00366 00367 /* lookup mapping */ 00368 node = avltree_lookup(&v->node_k, &avl_tree_2); 00369 v2 = avltree_container_of(node, avl_unit_val_t, node_k); 00370 CU_ASSERT((unsigned long) v2->val == (ix+1)); 00371 } 00372 00373 /* free v */ 00374 avl_unit_free_val(v); 00375 } 00376 00377 void deletes_tree_2(void) 00378 { 00379 struct avltree_node *node; 00380 avl_unit_val_t *v2, *v = avl_unit_new_val(0); 00381 int ix; 00382 00383 for (ix = 1; ix < 4; ++ix) { 00384 00385 /* reuse key */ 00386 v->key = ix; 00387 00388 /* find mapping */ 00389 node = avltree_lookup(&v->node_k, &avl_tree_2); 00390 v2 = avltree_container_of(node, avl_unit_val_t, node_k); 00391 CU_ASSERT(v2->val == (ix+1)); 00392 00393 /* and remove it */ 00394 avltree_remove(&v2->node_k, &avl_tree_2); 00395 avl_unit_free_val(v2); 00396 } 00397 00398 /* free search k */ 00399 avl_unit_free_val(v); 00400 } 00401 00402 00403 // xxxx 00404 void inserts_supremum(void) 00405 { 00406 avl_unit_val_t *v; 00407 int ix; 00408 00409 for (ix = 100; ix < 1000; ix += 100) { 00410 00411 /* new k, v */ 00412 v = avl_unit_new_val(ix); 00413 00414 /* if actual key cannot be marshalled as a pointer */ 00415 v->key = ix; 00416 00417 /* insert mapping */ 00418 avltree_insert(&v->node_k, &avl_tree_2); 00419 } 00420 } 00421 00422 void checks_supremum(void) 00423 { 00424 struct avltree_node *node; 00425 avl_unit_val_t *v2, *v = avl_unit_new_val(0); 00426 int ix; 00427 00428 for (ix = 100; ix < 1000; ix += 100) { 00429 00430 /* reuse v */ 00431 v->key = (ix - 2); /* a value -just less than ix- */ 00432 00433 /* lookup mapping */ 00434 node = avltree_sup(&v->node_k, &avl_tree_2); 00435 CU_ASSERT(node != NULL); 00436 if (node) { 00437 v2 = avltree_container_of(node, avl_unit_val_t, node_k); 00438 CU_ASSERT((unsigned long) v2->val == (ix+1)); 00439 } 00440 00441 /* ok, now find the -infimum- */ 00442 v->key = ix + 2; /* a value just above ix */ 00443 00444 /* lookup mapping */ 00445 node = avltree_inf(&v->node_k, &avl_tree_2); 00446 CU_ASSERT(node != NULL); 00447 if (node) { 00448 v2 = avltree_container_of(node, avl_unit_val_t, node_k); 00449 CU_ASSERT((unsigned long) v2->val == (ix+1)); 00450 } 00451 00452 } 00453 00454 /* now check the boundary case for supremum */ 00455 v->key = 500; 00456 00457 node = avltree_sup(&v->node_k, &avl_tree_2); 00458 CU_ASSERT(node != NULL); 00459 if (node) { 00460 v2 = avltree_container_of(node, avl_unit_val_t, node_k); 00461 CU_ASSERT((unsigned long) v2->val == (v->key+1)); 00462 } 00463 00464 /* and infimum */ 00465 node = avltree_inf(&v->node_k, &avl_tree_2); 00466 CU_ASSERT(node != NULL); 00467 if (node) { 00468 v2 = avltree_container_of(node, avl_unit_val_t, node_k); 00469 CU_ASSERT((unsigned long) v2->val == (v->key+1)); 00470 } 00471 00472 /* free v */ 00473 avl_unit_free_val(v); 00474 } 00475 00476 void deletes_supremum(void) 00477 { 00478 struct avltree_node *node; 00479 avl_unit_val_t *v2, *v = avl_unit_new_val(0); 00480 int ix; 00481 00482 for (ix = 100; ix < 1000; ix += 100) { 00483 00484 /* reuse key */ 00485 v->key = ix; 00486 00487 /* find mapping */ 00488 node = avltree_lookup(&v->node_k, &avl_tree_2); 00489 v2 = avltree_container_of(node, avl_unit_val_t, node_k); 00490 CU_ASSERT(v2->val == (ix+1)); 00491 00492 /* and remove it */ 00493 avltree_remove(&v2->node_k, &avl_tree_2); 00494 avl_unit_free_val(v2); 00495 } 00496 00497 /* free search k */ 00498 avl_unit_free_val(v); 00499 } 00500 00501 void inserts_tree_100(void) 00502 { 00503 avl_unit_val_t *v; 00504 int ix; 00505 00506 for (ix = 1; ix < 101; ++ix) { 00507 00508 /* new k, v */ 00509 v = avl_unit_new_val(ix); 00510 00511 /* if actual key cannot be marshalled as a pointer */ 00512 v->key = ix; 00513 00514 /* insert mapping */ 00515 avltree_insert(&v->node_k, &avl_tree_100); 00516 } 00517 } 00518 00519 void check_tree_100(void) 00520 { 00521 int code = 0; 00522 CU_ASSERT_EQUAL(code, 0); 00523 } 00524 00525 void lookups_tree_100(void) 00526 { 00527 struct avltree_node *node; 00528 avl_unit_val_t *v2, *v = avl_unit_new_val(0); 00529 int ix; 00530 00531 for (ix = 1; ix < 2; ++ix) { 00532 00533 /* reuse v */ 00534 v->key = ix; 00535 00536 /* lookup mapping */ 00537 node = avltree_lookup(&v->node_k, &avl_tree_100); 00538 v2 = avltree_container_of(node, avl_unit_val_t, node_k); 00539 CU_ASSERT((unsigned long) v2->val == (ix+1)); 00540 } 00541 00542 /* free v */ 00543 avl_unit_free_val(v); 00544 } 00545 00546 void trav_tree_100(void) 00547 { 00548 int ntrav = 0; 00549 struct avltree_node *node; 00550 avl_unit_val_t *v; 00551 00552 node = avltree_first(&avl_tree_100); 00553 while (node) { 00554 ntrav++; 00555 v = avltree_container_of(node, avl_unit_val_t, node_k); 00556 if ((ntrav % 10) == 0) 00557 printf("Node at %p key: %lu val: %lu (%d)\n", 00558 v, v->key, v->val, ntrav); 00559 node = avltree_next(node); 00560 } 00561 CU_ASSERT_EQUAL(ntrav, 100); 00562 } 00563 00564 void deletes_tree_100(void) 00565 { 00566 struct avltree_node *node; 00567 avl_unit_val_t *v2, *v = avl_unit_new_val(0); 00568 int ix; 00569 00570 for (ix = 1; ix < 101; ++ix) { 00571 00572 /* reuse key */ 00573 v->key = ix; 00574 00575 /* find mapping */ 00576 node = avltree_lookup(&v->node_k, &avl_tree_100); 00577 v2 = avltree_container_of(node, avl_unit_val_t, node_k); 00578 CU_ASSERT(v2->val == (ix+1)); 00579 00580 /* and remove it */ 00581 avltree_remove(&v2->node_k, &avl_tree_100); 00582 avl_unit_free_val(v2); 00583 } 00584 00585 /* free search k */ 00586 avl_unit_free_val(v); 00587 } 00588 00589 void inserts_tree_10000(void) 00590 { 00591 avl_unit_val_t *v; 00592 int ix; 00593 00594 for (ix = 1; ix < 10001; ++ix) { 00595 00596 /* new k, v */ 00597 v = avl_unit_new_val(ix); 00598 00599 /* if actual key cannot be marshalled as a pointer */ 00600 v->key = ix; 00601 00602 /* insert mapping */ 00603 avltree_insert(&v->node_k, &avl_tree_10000); 00604 } 00605 } 00606 00607 void check_tree_10000(void) 00608 { 00609 int code = 0; 00610 CU_ASSERT_EQUAL(code, 0); 00611 } 00612 00613 void lookups_tree_10000(void) 00614 { 00615 struct avltree_node *node; 00616 avl_unit_val_t *v2, *v = avl_unit_new_val(0); 00617 int ix; 00618 00619 for (ix = 1; ix < 2; ++ix) { 00620 00621 /* reuse v */ 00622 v->key = ix; 00623 00624 /* lookup mapping */ 00625 node = avltree_lookup(&v->node_k, &avl_tree_10000); 00626 v2 = avltree_container_of(node, avl_unit_val_t, node_k); 00627 CU_ASSERT((unsigned long) v2->val == (ix+1)); 00628 } 00629 00630 /* free v */ 00631 avl_unit_free_val(v); 00632 } 00633 00634 void trav_tree_10000(void) 00635 { 00636 int ntrav = 0; 00637 struct avltree_node *node; 00638 avl_unit_val_t *v; 00639 00640 node = avltree_first(&avl_tree_10000); 00641 while (node) { 00642 ntrav++; 00643 v = avltree_container_of(node, avl_unit_val_t, node_k); 00644 if ((ntrav % 1000) == 0) 00645 printf("Node at %p key: %lu val: %lu (%d)\n", 00646 v, v->key, v->val, ntrav); 00647 node = avltree_next(node); 00648 } 00649 CU_ASSERT_EQUAL(ntrav, 10000); 00650 } 00651 00652 void deletes_tree_10000(void) 00653 { 00654 struct avltree_node *node; 00655 avl_unit_val_t *v2, *v = avl_unit_new_val(0); 00656 int ix; 00657 00658 for (ix = 1; ix < 10001; ++ix) { 00659 00660 /* reuse key */ 00661 v->key = ix; 00662 00663 /* find mapping */ 00664 node = avltree_lookup(&v->node_k, &avl_tree_10000); 00665 v2 = avltree_container_of(node, avl_unit_val_t, node_k); 00666 CU_ASSERT(v2->val == (ix+1)); 00667 00668 /* and remove it */ 00669 avltree_remove(&v2->node_k, &avl_tree_10000); 00670 avl_unit_free_val(v2); 00671 } 00672 00673 /* free search k */ 00674 avl_unit_free_val(v); 00675 } 00676 00677 void insert_long_val(struct avltree *t, unsigned long l) 00678 { 00679 avl_unit_val_t *v; 00680 00681 /* new k, v */ 00682 v = avl_unit_new_val(l); 00683 00684 /* if actual key cannot be marshalled as a pointer */ 00685 v->key = l; 00686 00687 /* insert mapping */ 00688 avltree_insert(&v->node_k, t); 00689 } 00690 00691 void insert_long_val_safe(struct avltree *t, unsigned long l) 00692 { 00693 struct avltree_node *node; 00694 avl_unit_val_t *v; 00695 00696 /* new k, v */ 00697 v = avl_unit_new_val(l); 00698 v->key = l; 00699 00700 node = avltree_lookup(&v->node_k, t); 00701 if (node == NULL) 00702 avltree_insert(&v->node_k, t); 00703 else 00704 avl_unit_free_val(v); 00705 } 00706 00707 00708 void delete_long_val(struct avltree *t, unsigned long l) 00709 { 00710 struct avltree_node *node; 00711 avl_unit_val_t *v, *v2; 00712 00713 /* new key, v */ 00714 v = avl_unit_new_val(l); 00715 v->key = l; 00716 00717 /* find mapping */ 00718 node = avltree_lookup(&v->node_k, t); 00719 v2 = avltree_container_of(node, avl_unit_val_t, node_k); 00720 CU_ASSERT(v2->key == l); 00721 00722 /* delete mapping */ 00723 avltree_remove(&v2->node_k, t); 00724 00725 /* free original v */ 00726 avl_unit_free_val(v2); 00727 00728 /* free search k, v */ 00729 avl_unit_free_val(v); 00730 } 00731 00732 void check_delete_1(void) 00733 { 00734 struct avltree_node *node; 00735 avl_unit_val_t *v, *v2; 00736 00737 avl_unit_clear_and_destroy_tree(&avl_tree_1); 00738 00739 avltree_init(&avl_tree_1, avl_unit_cmpf, 0 /* flags */); 00740 00741 insert_long_val(&avl_tree_1, 4); 00742 insert_long_val(&avl_tree_1, 1); 00743 insert_long_val(&avl_tree_1, 10010); 00744 insert_long_val(&avl_tree_1, 267); 00745 insert_long_val(&avl_tree_1, 3382); 00746 insert_long_val(&avl_tree_1, 22); 00747 insert_long_val(&avl_tree_1, 82); 00748 insert_long_val(&avl_tree_1, 3); 00749 00750 node = avltree_first(&avl_tree_1); 00751 v2 = avltree_container_of(node, avl_unit_val_t, node_k); 00752 CU_ASSERT(v2->val == (1+1)); 00753 00754 delete_long_val(&avl_tree_1, 1); 00755 00756 /* new key */ 00757 v = avl_unit_new_val(4); 00758 v->key = 4; 00759 node = avltree_lookup(&v->node_k, &avl_tree_1); 00760 v2 = avltree_container_of(node, avl_unit_val_t, node_k); 00761 00762 CU_ASSERT(v2->val == (4+1)); 00763 00764 delete_long_val(&avl_tree_1, 267); 00765 00766 v->key = 3382; 00767 node = avltree_lookup(&v->node_k, &avl_tree_1); 00768 v2 = avltree_container_of(node, avl_unit_val_t, node_k); 00769 00770 CU_ASSERT(v2->val == (3382+1)); 00771 00772 avl_unit_free_val(v); 00773 00774 } 00775 00776 void check_min_1(void) 00777 { 00778 00779 avl_unit_val_t *v; 00780 struct avltree_node *node; 00781 00782 avl_unit_clear_and_destroy_tree(&avl_tree_1); 00783 00784 avltree_init(&avl_tree_1, avl_unit_cmpf, 0 /* flags */); 00785 00786 insert_long_val(&avl_tree_1, 4); 00787 insert_long_val(&avl_tree_1, 10); 00788 insert_long_val(&avl_tree_1, 10010); 00789 insert_long_val(&avl_tree_1, 267); 00790 insert_long_val(&avl_tree_1, 3382); 00791 insert_long_val(&avl_tree_1, 22); 00792 insert_long_val(&avl_tree_1, 82); 00793 00794 node = avltree_first(&avl_tree_1); 00795 v = avltree_container_of(node, avl_unit_val_t, node_k); 00796 CU_ASSERT(v->val == (4+1)); 00797 00798 /* insert new min */ 00799 insert_long_val(&avl_tree_1, 3); 00800 node = avltree_first(&avl_tree_1); 00801 v = avltree_container_of(node, avl_unit_val_t, node_k); 00802 CU_ASSERT(v->val == (3+1)); 00803 00804 /* delete current min */ 00805 delete_long_val(&avl_tree_1, 3); 00806 node = avltree_first(&avl_tree_1); 00807 v = avltree_container_of(node, avl_unit_val_t, node_k); 00808 CU_ASSERT(v->val == (4+1)); 00809 } 00810 00811 void check_min_2(void) 00812 { 00813 avl_unit_val_t *v; 00814 unsigned long mval, rv; 00815 struct avltree_node *node; 00816 int ix; 00817 00818 srand(time(0)); 00819 00820 avl_unit_clear_and_destroy_tree(&avl_tree_1); 00821 00822 avltree_init(&avl_tree_1, avl_unit_cmpf, 0 /* flags */); 00823 00824 mval = ULONG_MAX; 00825 for (ix = 0; ix < 100000; ix++) { 00826 rv = rand(); 00827 /* in solaris avl, inserting an value that compares equal 00828 * to an already inserted value is illegal */ 00829 insert_long_val_safe(&avl_tree_1, rv); 00830 if ((mval < 0) || (rv < mval)) 00831 mval = rv; 00832 } 00833 00834 node = avltree_first(&avl_tree_1); 00835 v = avltree_container_of(node, avl_unit_val_t, node_k); 00836 printf("rv: %lu mval: %lu val: %lu\n", rv, mval, v->val-1); 00837 CU_ASSERT(v->val == (mval+1)); 00838 } 00839 00840 00841 /* The main() function for setting up and running the tests. 00842 * Returns a CUE_SUCCESS on successful running, another 00843 * CUnit error code on failure. 00844 */ 00845 int main() 00846 { 00847 /* initialize the CUnit test registry... get this party started */ 00848 if (CUE_SUCCESS != CU_initialize_registry()) 00849 return CU_get_error(); 00850 00851 /* General avl_tree test. */ 00852 CU_TestInfo avl_tree_unit_1_arr[] = { 00853 { "Tree insertions 1.", inserts_tree_1 }, 00854 { "Tree check 1.", check_tree_1 }, 00855 { "Tree lookups 1.", lookups_tree_1 }, 00856 { "Tree deletes 1.", deletes_tree_1 }, 00857 CU_TEST_INFO_NULL, 00858 }; 00859 00860 CU_TestInfo avl_tree_unit_2_arr[] = { 00861 { "Tree insertions 2.", inserts_tree_2 }, 00862 { "Tree check 2.", check_tree_2 }, 00863 { "Tree lookups 2.", lookups_tree_2 }, 00864 { "Tree deletes 2.", deletes_tree_2 }, 00865 CU_TEST_INFO_NULL, 00866 }; 00867 00868 CU_TestInfo avl_tree_unit_2r_arr[] = { 00869 { "Tree insertions 2.", inserts_tree_2r }, 00870 { "Tree check 2.", check_tree_2 }, 00871 { "Tree lookups 2.", lookups_tree_2 }, 00872 { "Tree deletes 2.", deletes_tree_2 }, 00873 CU_TEST_INFO_NULL, 00874 }; 00875 00876 CU_TestInfo avl_tree_unit_100_arr[] = { 00877 { "Tree insertions 100.", inserts_tree_100 }, 00878 { "Tree check 100.", check_tree_100 }, 00879 { "Tree lookups 100.", lookups_tree_100 }, 00880 { "Tree traverse 100.", trav_tree_100 }, 00881 { "Tree deletes 100.", deletes_tree_100 }, 00882 CU_TEST_INFO_NULL, 00883 }; 00884 00885 CU_TestInfo avl_tree_unit_10000_arr[] = { 00886 { "Tree insertions 10000.", inserts_tree_10000 }, 00887 { "Tree lookups 10000.", lookups_tree_10000 }, 00888 { "Tree check 10000.", check_tree_10000 }, 00889 { "Tree traverse 10000.", trav_tree_10000 }, 00890 { "Tree deletes 10000.", deletes_tree_10000 }, 00891 CU_TEST_INFO_NULL, 00892 }; 00893 00894 CU_TestInfo avl_tree_unit_min_1_arr[] = { 00895 { "Check min after inserts, deletes.", check_min_1 }, 00896 { "Check lookup after delete.", check_delete_1 }, 00897 #if 1 /* skews perf */ 00898 { "Random min check.", check_min_2 }, 00899 #endif 00900 CU_TEST_INFO_NULL, 00901 }; 00902 00903 CU_TestInfo avl_tree_unit_supremum[] = { 00904 { "Inserts supremum.", inserts_supremum }, 00905 { "Checks supremum (and infimum).", checks_supremum }, 00906 { "Deletes supremum.", deletes_supremum }, 00907 CU_TEST_INFO_NULL, 00908 }; 00909 00910 CU_SuiteInfo suites[] = { 00911 { "Avl operations 1", init_suite1, clean_suite1, 00912 avl_tree_unit_1_arr }, 00913 { "Avl operations 2", init_suite2, clean_suite2, 00914 avl_tree_unit_2_arr }, 00915 { "Avl operations 2 R", init_suite2, clean_suite2, 00916 avl_tree_unit_2r_arr }, 00917 { "Avl operations 100", init_suite100, clean_suite100, 00918 avl_tree_unit_100_arr }, 00919 { "Avl operations 10000", init_suite10000, clean_suite10000, 00920 avl_tree_unit_10000_arr }, 00921 { "Check min 1", init_suite1, clean_suite1, 00922 avl_tree_unit_min_1_arr }, 00923 { "Check supremum", init_supremum, clean_supremum, 00924 avl_tree_unit_supremum }, 00925 CU_SUITE_INFO_NULL, 00926 }; 00927 00928 CU_ErrorCode error = CU_register_suites(suites); 00929 00930 /* Initialize the avl_tree package */ 00931 avl_unit_PkgInit(); 00932 00933 /* Run all tests using the CUnit Basic interface */ 00934 CU_basic_set_mode(CU_BRM_VERBOSE); 00935 CU_basic_run_tests(); 00936 CU_cleanup_registry(); 00937 00938 return CU_get_error(); 00939 }