sandbox: AVL tree deletion bugfixes and testing
This commit is contained in:
@@ -3,13 +3,14 @@
|
||||
#include <stdlib.h>
|
||||
#include <inttypes.h>
|
||||
#include <time.h>
|
||||
#include <assert.h>
|
||||
#include <sys/mman.h>
|
||||
#include <socks/types.h>
|
||||
#include <socks/btree.h>
|
||||
#include <socks/memblock.h>
|
||||
#include <socks/vm.h>
|
||||
|
||||
#define NR_BTREE_NODES 16383
|
||||
#define NR_BTREE_NODES 13
|
||||
|
||||
/* we're working with 512MiB of simulated system RAM */
|
||||
#define MEMORY_SIZE_MB 512
|
||||
@@ -166,10 +167,14 @@ void btree_print(btree_node_t *node, int depth)
|
||||
*/
|
||||
static int btree_avl_validate(btree_node_t *x)
|
||||
{
|
||||
if (!x) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!x->b_left && !x->b_right) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int left = 0, right = 0;
|
||||
|
||||
if (x->b_left) {
|
||||
@@ -200,7 +205,7 @@ static int btree_avl_validate(btree_node_t *x)
|
||||
if (height != x->b_height) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
return height;
|
||||
}
|
||||
|
||||
@@ -210,38 +215,42 @@ static int btree_test(void)
|
||||
btree_node_t *nodes = calloc(NR_BTREE_NODES, sizeof *nodes);
|
||||
|
||||
for (int i = 0; i < NR_BTREE_NODES; i++) {
|
||||
nodes[i].b_key = (rand() % 512000) + 1;
|
||||
nodes[i].b_key = (rand() % 128) + 1;
|
||||
printf(" - node %d: %llu\n", i, nodes[i].b_key);
|
||||
}
|
||||
|
||||
int validation_result = 0;
|
||||
for (int i = 0; i < NR_BTREE_NODES; i++) {
|
||||
printf("#######################\n");
|
||||
printf("inserting node #%d: %llu\n", i, nodes[i].b_key);
|
||||
|
||||
btree_insert(&tree, &nodes[i]);
|
||||
printf("#######################\n");
|
||||
|
||||
validation_result = btree_avl_validate(tree.b_root);
|
||||
assert(validation_result >= 1);
|
||||
}
|
||||
|
||||
btree_print(tree.b_root, 0);
|
||||
|
||||
int result = btree_avl_validate(tree.b_root);
|
||||
printf("AVL validation result: %d (%s)\n", result, result != -1 ? "pass" : "fail");
|
||||
|
||||
#if 0
|
||||
int to_delete[] = { 3, 1, 0 };
|
||||
int nr_to_delete = sizeof to_delete / sizeof to_delete[0];
|
||||
|
||||
for (int i = 0; i < nr_to_delete; i++) {
|
||||
int node_index = to_delete[i];
|
||||
printf("AVL tree height: %d\n", result);
|
||||
|
||||
for (int i = 0; i < NR_BTREE_NODES; i++) {
|
||||
printf("#######################\n");
|
||||
printf("deleting node %llu\n", nodes[node_index].b_key);
|
||||
printf("deleting node #%d: %llu\n", i, nodes[i].b_key);
|
||||
printf("#######################\n");
|
||||
|
||||
btree_delete(&tree, &nodes[node_index]);
|
||||
if (nodes[i].b_key == 89) {
|
||||
printf("brk\n");
|
||||
}
|
||||
|
||||
btree_delete(&tree, &nodes[i]);
|
||||
btree_print(tree.b_root, 0);
|
||||
|
||||
validation_result = btree_avl_validate(tree.b_root);
|
||||
assert(validation_result >= 0);
|
||||
}
|
||||
#endif
|
||||
|
||||
free(nodes);
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user