Skip to content
Snippets Groups Projects
Commit 483fedb0 authored by Florian Burgener's avatar Florian Burgener
Browse files

Installation of a unit testing framework

parent c6614738
Branches
No related tags found
No related merge requests found
function insert(root, key)
cursor = traverse until found leaf
right_split = null
while True
if cursor is full
if cursor is a leaf
key, right_split = split_leaf(cursor, key)
else
key, right_split = split_internal(cursor, key, right_split)
if cursor is root:
increase_height(root, key, right_split)
exit loop
else
insert key in cursor.keys
if cursor is not a leaf
insert right_split in cursor.children
exit loop
end
function increase_height(root, key, right_split)
left = new Node
left.is_leaf = right_split.is_leaf
left.keys = root.keys
left.children = root.children
if left is a leaf
fix right_split left link to left
root.keys = [key]
root.children = [left, right_split]
function insert(root, key)
leaf_node = find_leaf_node(root, key)
if length(keys of leaf_node) > 2 * order of lead_node then
insert_full(root, lead_node, key, null)
else
insert_non_full(leaf_node, key, null)
function insert_non_full(node, key, right_child_node)
function insert_full(root, node, key, right_child_node)
function find_parent(root, child_node)
def bptree_delete_leaf(root, parents, leaf, key):
if leaf != root:
parent = parents.pop()
if len(leaf.keys) > leaf.order or leaf == root:
sorted_array_delete(leaf.keys, key)
else:
deletion_index = find_value_index_in_array(leaf.keys, key)
leaf.keys.pop(deletion_index)
children_index = None
for i, child in enumerate(parent.children):
if leaf == child:
children_index = i
sibling, sibling_position = bptree_find_sibling(parent, children_index)
if len(sibling.keys) == sibling.order:
print("Merge 1")
pass
# if children_index == 0:
# # Sibling not on the left.
# array_insert_sorted(sibling.keys, leaf.keys[0])
# parent.children.pop(children_index)
# parent.keys.pop(children_index)
# else:
# for key in sibling.keys:
# array_insert_sorted(leaf.keys, key)
# parent.children.pop(children_index - 1)
# parent.keys.pop(children_index - 1)
else:
if sibling_position == "l":
sibling_key = sibling.keys[len(sibling.keys) - 1]
sorted_array_delete(sibling.keys, sibling_key)
array_insert_sorted(leaf.keys, sibling_key)
parent.keys[children_index - 1] = sibling_key
else:
sibling_key = sibling.keys[0]
sorted_array_delete(sibling.keys, sibling_key)
array_insert_sorted(leaf.keys, sibling_key)
parent.keys[children_index] = sibling.keys[0]
bptree_delete_internal(root, parents, parent, key)
def bptree_delete_internal(root, parents, node, key):
if node != root:
parent = parents.pop()
if len(node.keys) < node.order and node != root:
print("Merge 2")
# children_index = None
# for i, child in enumerate(parent.children):
# if node == child:
# children_index = i
# if children_index == 0:
# sibling = parent.children[children_index + 1]
# parent_key = parent.keys[children_index]
# parent.keys.pop(find_value_index_in_array(parent.keys, parent_key))
# array_insert_sorted(sibling.keys, parent_key)
# array_insert_sorted(sibling.keys, node.keys[0])
# sibling.children = node.children + sibling.children
# else:
# sibling = parent.children[children_index - 1]
# parent_key = parent.keys[children_index - 1]
# parent.keys.pop(find_value_index_in_array(parent.keys, parent_key))
# array_insert_sorted(node.keys, parent_key)
# for key in sibling.keys:
# array_insert_sorted(node.keys, key)
# node.children = sibling.children + node.children
# if parent == root and len(parent.keys) == 0:
# bptree_shrink(root)
if is_value_in_array(node.keys, key) and len(node.children) > 0:
index = find_value_index_in_array(node.keys, key)
node.keys[index] = bptree_find_smallest_key(node.children[index + 1])
if node != root:
bptree_delete_internal(root, parents, parent, key)
......@@ -101,6 +101,18 @@ void IntegerArray_print(IntegerArray *array) {
printf("]\n");
}
void IntegerArray_clear(IntegerArray *array) {
array->size = 0;
}
void IntegerArray_copy(IntegerArray *src, IntegerArray *dest) {
for (int i = 0; i < src->size; i++) {
dest->items[i] = src->items[i];
}
dest->size = src->size;
}
BPTreeNodeArray *BPTreeNodeArray_init(int capacity) {
BPTreeNodeArray *array = (BPTreeNodeArray *)malloc(sizeof(BPTreeNodeArray));
array->items = (BPTreeNode **)malloc(sizeof(BPTreeNode *) * capacity);
......
......@@ -19,8 +19,9 @@ void IntegerArray_insert_at_index(IntegerArray *array, int index, uint64_t item)
void IntegerArray_append(IntegerArray *array, uint64_t item);
bool IntegerArray_binary_search(IntegerArray *array, uint64_t item, int *index);
void IntegerArray_delete_sorted(IntegerArray *array, uint64_t item);
void IntegerArray_print(IntegerArray *array);
void IntegerArray_clear(IntegerArray *array);
void IntegerArray_copy(IntegerArray *src, IntegerArray *dest);
typedef struct BPTreeNode BPTreeNode;
......
......@@ -20,5 +20,21 @@ HEADERS = $(wildcard *.h)
$(TARGET): $(OBJECTS)
$(CC) $(OBJECTS) $(CFLAGS) $(LIBS) -o $@
# TESTS_BEGIN
unity.o: tests/unity.c tests/unity.h
$(CC) $(CFLAGS) -c $< -o $@
testtest.o: tests/testtest.c
$(CC) $(CFLAGS) -c $< -o $@
make_run_tests: unity.o testtest.o Array.o
$(CC) $^ $(CFLAGS) $(LIBS) -o tests_exec
./tests_exec || true
run_tests: make_run_tests clean
# TESTS_END
clean:
rm -f *.o ${TARGET}*
rm -f *.o ${TARGET}* tests_exec
#include "../Array.h"
#include "unity.h"
IntegerArray *array;
void setUp(void) {
array = IntegerArray_init(4);
}
void tearDown(void) {
IntegerArray_destroy(&array);
}
void test_function_should_doAlsoDoBlah(void) {
IntegerArray_append(array, 10);
TEST_ASSERT_EQUAL_INT(1, array->size);
TEST_ASSERT_EQUAL_INT(11, array->items[0]);
}
int main(void) {
UNITY_BEGIN();
RUN_TEST(test_function_should_doAlsoDoBlah);
return UNITY_END();
}
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment