diff options
author | Sander Vrijders <[email protected]> | 2017-03-31 14:31:42 +0000 |
---|---|---|
committer | dimitri staessens <[email protected]> | 2017-03-31 14:31:42 +0000 |
commit | 662c7e7130c6d927ef1eb9999a187163b01e3ccf (patch) | |
tree | d4f096d70a7f2b09a12eedf82f0a96cd34566421 /src/lib/tests/btree_test.c | |
parent | 930c93f7ad6582c1386a6a98259d1acb04d2c64b (diff) | |
parent | 81558308ca6d0707b27fd5b5d4b332bd2eb6e6d3 (diff) | |
download | ouroboros-662c7e7130c6d927ef1eb9999a187163b01e3ccf.tar.gz ouroboros-662c7e7130c6d927ef1eb9999a187163b01e3ccf.zip |
Merged in sandervrijders/ouroboros/be-btree (pull request #444)
lib: Fix bugs in B-tree
Diffstat (limited to 'src/lib/tests/btree_test.c')
-rw-r--r-- | src/lib/tests/btree_test.c | 34 |
1 files changed, 29 insertions, 5 deletions
diff --git a/src/lib/tests/btree_test.c b/src/lib/tests/btree_test.c index 6981f63a..a6344060 100644 --- a/src/lib/tests/btree_test.c +++ b/src/lib/tests/btree_test.c @@ -25,6 +25,7 @@ #include <stdio.h> #include <stdlib.h> +#include <string.h> #define MAX_BTREE_KEY 10000 @@ -33,11 +34,15 @@ int btree_test(int argc, { struct btree * tree; + int vals[MAX_BTREE_KEY]; int i; + int j; (void) argc; (void) argv; + memset(vals, 0, MAX_BTREE_KEY * sizeof(int)); + tree = btree_create(32); if (tree == NULL) return -1; @@ -47,34 +52,53 @@ int btree_test(int argc, return -1; } - for(i = 0; i < MAX_BTREE_KEY; ++i) + for (i = 0; i < MAX_BTREE_KEY; ++i) if (btree_insert(tree, i, &argv)) { printf("Failed to add element.\n"); btree_destroy(tree); return -1; } - for (i = 0; i < MAX_BTREE_KEY / 10; ++i) + for (i = 0; i < MAX_BTREE_KEY; ++i) if (btree_search(tree, rand() % MAX_BTREE_KEY) != &argv) { printf("Added element not in tree.\n"); btree_destroy(tree); return -1; } - for (i = 0; i < MAX_BTREE_KEY / 10; ++i) + for (i = 0; i < MAX_BTREE_KEY; ++i) if (btree_remove(tree, i)) { - printf("Failed to remove element.\n"); + printf("Failed to remove element %d.\n", i); btree_destroy(tree); return -1; } for (i = 0; i < MAX_BTREE_KEY / 10; ++i) - if (btree_search(tree, rand() % MAX_BTREE_KEY / 10) != &argv) { + if (btree_search(tree, rand() % MAX_BTREE_KEY / 10) != NULL) { printf("Removed element found in tree.\n"); btree_destroy(tree); return -1; } + for (i = 0; i < MAX_BTREE_KEY; ++i) + if (btree_insert(tree, i, &argv)) { + printf("Failed to add element.\n"); + btree_destroy(tree); + return -1; + } + + for (i = 0; i < MAX_BTREE_KEY; ++i) { + j = rand() % MAX_BTREE_KEY; + if (vals[j] != -1) { + if (btree_remove(tree, j)) { + printf("Failed to remove element %d.\n", j); + btree_destroy(tree); + return -1; + } + } + vals[j] = -1; + } + btree_destroy(tree); return 0; |