lander/lsm/test/trie/trie.c

89 lines
2.4 KiB
C
Raw Normal View History

2023-10-13 22:08:06 +02:00
#include "test.h"
#include "lsm.h"
2023-10-13 22:08:06 +02:00
#include "lsm/trie_internal.h"
#define TRIE_INIT() \
lsm_trie *trie; \
TEST_CHECK(lsm_trie_init(&trie) == lsm_error_ok); \
TEST_CHECK(trie != NULL)
void test_init() {
TRIE_INIT();
/* lsm_trie_free(trie); */
}
void test_insert_one() {
TRIE_INIT();
lsm_str *s;
lsm_str_init_copy(&s, "hello");
TEST_CHECK(lsm_trie_insert(trie, s, (void *)1) == lsm_error_ok);
TEST_CHECK(lsm_trie_insert(trie, s, (void *)1) == lsm_error_already_present);
void *data;
TEST_CHECK(lsm_trie_search(&data, trie, s) == lsm_error_ok);
TEST_CHECK(data == (void *)1);
}
2023-12-23 10:06:02 +01:00
void test_iter() {
TRIE_INIT();
lsm_str *s;
lsm_str_init_copy(&s, "hello");
TEST_CHECK(lsm_trie_insert(trie, s, (void *)1) == lsm_error_ok);
lsm_str_init_copy(&s, "hella");
TEST_CHECK(lsm_trie_insert(trie, s, (void *)2) == lsm_error_ok);
lsm_str_init_copy(&s, "hel");
TEST_CHECK(lsm_trie_insert(trie, s, (void *)3) == lsm_error_ok);
lsm_str_init_copy(&s, "wow");
TEST_CHECK(lsm_trie_insert(trie, s, (void *)4) == lsm_error_ok);
lsm_str_init_copy(&s, "hel");
lsm_trie_iterator *iter;
TEST_CHECK(lsm_trie_iter(&iter, trie, s) == lsm_error_ok);
void *data;
TEST_CHECK(lsm_trie_iter_next(&data, iter) == lsm_error_ok);
TEST_CHECK_(data == (void *)3, "%p == %p", data, (void *)3);
TEST_CHECK(lsm_trie_iter_next(&data, iter) == lsm_error_ok);
TEST_CHECK_(data == (void *)2, "%p == %p", data, (void *)2);
TEST_CHECK(lsm_trie_iter_next(&data, iter) == lsm_error_ok);
TEST_CHECK_(data == (void *)1, "%p == %p", data, (void *)1);
TEST_CHECK(lsm_trie_iter_next(&data, iter) == lsm_error_done);
lsm_trie_iter_free(iter);
// Test full trie iterator
TEST_CHECK(lsm_trie_iter(&iter, trie, NULL) == lsm_error_ok);
TEST_CHECK(lsm_trie_iter_next(&data, iter) == lsm_error_ok);
TEST_CHECK_(data == (void *)3, "%p == %p", data, (void *)3);
TEST_CHECK(lsm_trie_iter_next(&data, iter) == lsm_error_ok);
TEST_CHECK_(data == (void *)2, "%p == %p", data, (void *)2);
TEST_CHECK(lsm_trie_iter_next(&data, iter) == lsm_error_ok);
TEST_CHECK_(data == (void *)1, "%p == %p", data, (void *)1);
TEST_CHECK(lsm_trie_iter_next(&data, iter) == lsm_error_ok);
TEST_CHECK_(data == (void *)4, "%p == %p", data, (void *)4);
TEST_CHECK(lsm_trie_iter_next(&data, iter) == lsm_error_done);
lsm_trie_iter_free(iter);
}
2023-10-13 22:08:06 +02:00
TEST_LIST = {
{ "trie init", test_init },
{ "trie insert one", test_insert_one },
2023-12-23 10:06:02 +01:00
{ "trie iter", test_iter },
2023-10-13 22:08:06 +02:00
{ NULL, NULL }
};