test(tree): add random insert test
parent
a6ed89d8cd
commit
d9402ced54
|
@ -0,0 +1,59 @@
|
|||
#include "acutest.h"
|
||||
#include "vieter_tree_internal.h"
|
||||
|
||||
#define TEST_SIZE(tree, size) \
|
||||
TEST_CHECK(vieter_tree_size(tree) == size); \
|
||||
TEST_MSG("Size: %zu", vieter_tree_size(tree))
|
||||
|
||||
int uint64_t_compare(const void *a, const void *b) {
|
||||
if ((*(uint64_t *)a) < (*(uint64_t *)b)) {
|
||||
return -1;
|
||||
} else if ((*(uint64_t *)a) > (*(uint64_t *)b)) {
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
void test_insert_random() {
|
||||
const uint64_t n = 1000;
|
||||
srand(0);
|
||||
|
||||
vieter_tree *tree = vieter_tree_init();
|
||||
|
||||
uint64_t *numbers = malloc(n * sizeof(uint64_t));
|
||||
uint64_t num;
|
||||
void *out = NULL;
|
||||
|
||||
for (uint64_t i = 0; i < n; i++) {
|
||||
num = rand();
|
||||
vieter_tree_insert(tree, num, (void *)num);
|
||||
TEST_SIZE(tree, i + 1);
|
||||
TEST_CHECK(vieter_tree_validate(tree));
|
||||
TEST_CHECK(vieter_tree_search(&out, tree, num) == vieter_tree_ok);
|
||||
TEST_CHECK(out == (void *)num);
|
||||
|
||||
out = NULL;
|
||||
|
||||
numbers[i] = num;
|
||||
}
|
||||
|
||||
qsort(numbers, n, sizeof(uint64_t), uint64_t_compare);
|
||||
|
||||
vieter_tree_iterator *iter = vieter_tree_iterator_from(tree);
|
||||
out = NULL;
|
||||
|
||||
for (uint64_t i = 0; i < n; i++) {
|
||||
TEST_CHECK(vieter_tree_iterator_next(&out, iter) == vieter_tree_ok);
|
||||
TEST_CHECK(out == (void *)numbers[i]);
|
||||
}
|
||||
|
||||
free(numbers);
|
||||
vieter_tree_iterator_free(&iter);
|
||||
vieter_tree_free(tree);
|
||||
}
|
||||
|
||||
TEST_LIST = {
|
||||
{"tree insert random", test_insert_random},
|
||||
{NULL, NULL}
|
||||
};
|
Loading…
Reference in New Issue