#include "test.h" #include "lsm.h" #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); } 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); } TEST_LIST = { { "trie init", test_init }, { "trie insert one", test_insert_one }, { "trie iter", test_iter }, { NULL, NULL } };