Implemented find/find closest for B+ tree.
Added test suite. Fixed some b+ tree bugs.
This commit is contained in:
13
tests/CMakeLists.txt
Normal file
13
tests/CMakeLists.txt
Normal file
@@ -0,0 +1,13 @@
|
||||
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../src)
|
||||
|
||||
find_package(Check REQUIRED)
|
||||
find_package (Threads REQUIRED)
|
||||
include_directories(${CHECK_INCLUDE_DIRS})
|
||||
link_directories(${CHECK_LIBRARY_DIRS})
|
||||
|
||||
set(TEST_SOURCES
|
||||
check_sdb.c
|
||||
bplus_tree_test.c bplus_tree_test.h)
|
||||
|
||||
add_executable(check_SDB ${TEST_SOURCES})
|
||||
target_link_libraries(check_SDB SDBLib ${CHECK_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
|
||||
76
tests/bplus_tree_test.c
Normal file
76
tests/bplus_tree_test.c
Normal file
@@ -0,0 +1,76 @@
|
||||
//
|
||||
// Created by sam on 23/06/18.
|
||||
//
|
||||
|
||||
#include <stdlib.h>
|
||||
#include "bplus_tree_test.h"
|
||||
#include "../src/bplus_tree.h"
|
||||
|
||||
START_TEST(insert_keys_4)
|
||||
{
|
||||
|
||||
BPlusTree *tree = new_bplus_tree(4);
|
||||
|
||||
char *buffer = NULL;
|
||||
|
||||
ck_assert_str_eq(debug_bplus_tree_str(tree, buffer), "B+<4>[]");
|
||||
|
||||
bplus_tree_insert(tree, 10, NULL);
|
||||
bplus_tree_insert(tree, 20, NULL);
|
||||
|
||||
ck_assert_str_eq(debug_bplus_tree_str(tree, buffer), "B+<4>[{10*}{20*}]");
|
||||
|
||||
bplus_tree_insert(tree, 30, NULL);
|
||||
bplus_tree_insert(tree, 40, NULL);
|
||||
|
||||
ck_assert_str_eq(debug_bplus_tree_str(tree, buffer), "B+<4>[[{10*}{20*}]{30}[{30*}{40*}]]");
|
||||
|
||||
bplus_tree_insert(tree, 35, NULL);
|
||||
bplus_tree_insert(tree, 50, NULL);
|
||||
ck_assert_str_eq(debug_bplus_tree_str(tree, buffer), "B+<4>[[{10*}{20*}]{30}[{30*}{35*}]{40}[{40*}{50*}]]");
|
||||
|
||||
free_bplus_tree(tree);
|
||||
|
||||
if (buffer != NULL) free(buffer);
|
||||
|
||||
}END_TEST
|
||||
|
||||
START_TEST(find_keys_4)
|
||||
{
|
||||
BPlusTree *tree = new_bplus_tree(4);
|
||||
|
||||
bplus_tree_insert(tree, 10, NULL);
|
||||
bplus_tree_insert(tree, 20, NULL);
|
||||
bplus_tree_insert(tree, 30, NULL);
|
||||
bplus_tree_insert(tree, 40, NULL);
|
||||
|
||||
ck_assert(bplus_tree_find(tree, 10) != NULL);
|
||||
ck_assert(bplus_tree_find(tree, 40) != NULL);
|
||||
|
||||
BPlusKV *kv;
|
||||
kv = bplus_tree_find_closest(tree, 15, FIND_LT | FIND_EQ);
|
||||
ck_assert_ptr_ne(kv, NULL);
|
||||
ck_assert_int_eq(kv->key, 10);
|
||||
|
||||
kv = bplus_tree_find_closest(tree, 30, FIND_LT);
|
||||
ck_assert_ptr_ne(kv, NULL);
|
||||
ck_assert_int_eq(kv->key, 20);
|
||||
|
||||
kv = bplus_tree_find_closest(tree, 30, FIND_GT);
|
||||
ck_assert_ptr_ne(kv, NULL);
|
||||
ck_assert_int_eq(kv->key, 40);
|
||||
|
||||
free_bplus_tree(tree);
|
||||
}END_TEST
|
||||
|
||||
Suite *blus_tree_suite(void) {
|
||||
Suite *s = suite_create("B+ Tree");
|
||||
|
||||
TCase *core = tcase_create("Insert");
|
||||
suite_add_tcase(s, core);
|
||||
|
||||
tcase_add_test(core, insert_keys_4);
|
||||
tcase_add_test(core, find_keys_4);
|
||||
|
||||
return s;
|
||||
}
|
||||
12
tests/bplus_tree_test.h
Normal file
12
tests/bplus_tree_test.h
Normal file
@@ -0,0 +1,12 @@
|
||||
//
|
||||
// Created by sam on 23/06/18.
|
||||
//
|
||||
|
||||
#ifndef SDB_BPLUS_TREE_TEST_H
|
||||
#define SDB_BPLUS_TREE_TEST_H
|
||||
|
||||
#include <check.h>
|
||||
|
||||
Suite * blus_tree_suite(void);
|
||||
|
||||
#endif //SDB_BPLUS_TREE_TEST_H
|
||||
21
tests/check_sdb.c
Normal file
21
tests/check_sdb.c
Normal file
@@ -0,0 +1,21 @@
|
||||
//
|
||||
// Created by sam on 22/06/18.
|
||||
//
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <check.h>
|
||||
#include "bplus_tree_test.h"
|
||||
|
||||
int main(void) {
|
||||
int number_failed;
|
||||
Suite *s;
|
||||
SRunner *sr;
|
||||
|
||||
s = blus_tree_suite();
|
||||
sr = srunner_create(s);
|
||||
|
||||
srunner_run_all(sr, CK_NORMAL);
|
||||
number_failed = srunner_ntests_failed(sr);
|
||||
srunner_free(sr);
|
||||
return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
|
||||
}
|
||||
Reference in New Issue
Block a user