test(tree): add random insert test
ci/woodpecker/pr/lint Pipeline was successful Details
ci/woodpecker/pr/test Pipeline was successful Details
ci/woodpecker/pr/test-mem Pipeline was successful Details

Jef Roosens 2023-02-02 15:30:57 +01:00
parent a6ed89d8cd
commit 5d2ddee71f
2 changed files with 93 additions and 0 deletions

View File

@ -64,6 +64,40 @@ void test_remove() {
vieter_tree_free(tree); vieter_tree_free(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_random() {
const uint64_t n = 500;
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, i) == vieter_tree_ok);
TEST_CHECK(out == (void *)num);
numbers[i] = num;
}
qsort(numbers, n, sizeof(uint64_t), uint64_t_compare);
}
TEST_LIST = { TEST_LIST = {
{"tree init", test_init}, {"tree init", test_init},
{"tree insert", test_insert}, {"tree insert", test_insert},

View File

@ -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}
};