diff --git a/src/BPTree.c b/src/BPTree.c index c15c30cca8fac0934c2c161dad5821b32b0125d2..479d7e1749cbf1dd44562be0831e7ccc92c76e4a 100644 --- a/src/BPTree.c +++ b/src/BPTree.c @@ -53,14 +53,24 @@ void BPTree_print(BPTreeNode *root, int depth) { for (int i = 0; i < depth; i++) { printf(" "); } + printf("%p\n", (void *)root); + + for (int i = 0; i < depth + 1; i++) { + printf(" "); + } IntegerArray_print(root->keys); - for (int i = 0; i < depth; i++) { + for (int i = 0; i < depth + 1; i++) { printf(" "); } - printf(" "); + printf("Data = "); IntegerArray_print(root->data); + for (int i = 0; i < depth + 1; i++) { + printf(" "); + } + printf("Next = %p\n", (void *)root->next); + for (int i = 0; i < root->children->size; i++) { BPTree_print(root->children->items[i], depth + 1); } @@ -205,8 +215,10 @@ static uint64_t insert_full(BPTreeNode *node, uint64_t key, uint64_t data, BPTre } static void grow(BPTreeNode *root, uint64_t median_value, BPTreeNode *split_right_node) { - BPTreeNode *left_node = BPTreeNode_init(root->order, split_right_node->is_leaf); root->is_leaf = false; + BPTreeNode *left_node = BPTreeNode_init(root->order, split_right_node->is_leaf); + left_node->next = root->next; + root->next = NULL; IntegerArray_copy(root->keys, left_node->keys); IntegerArray_copy(root->data, left_node->data); diff --git a/src/tests/BPTreeTests.c b/src/tests/BPTreeTests.c index 511a8dc9e167f97b0d8ff85dd5e228b116e9601b..589c7ca9e4c794402dbd6dd1f52cbc6defff6929 100644 --- a/src/tests/BPTreeTests.c +++ b/src/tests/BPTreeTests.c @@ -22,8 +22,14 @@ void setUp() { void tearDown() { } -// BEGIN : Functions that check that a B+ Tree is compliant with the rules +// BEGIN : Functions that check that a B+ tree is compliant with the B+ Tree rules. +/** + * @brief Finds the leftmost leaf node. + * + * @param root The root node. + * @return BPTreeNode* The leftmost leaf node. + */ static BPTreeNode *find_first_leaf(BPTreeNode *root) { if (root->is_leaf) { return root; @@ -234,15 +240,15 @@ static bool check_BPTree_compliance(BPTreeNode *root) { return is_compliant; } -// END : Functions that check that a B+ Tree is compliant with the rules +// END : Functions that check that a B+ tree is compliant with the B+ Tree rules. /** - * @brief Performs a linear search for an item in an array. + * @brief Checks if an item is present in the array. * - * @param array The array in which the search is to be performed. - * @param item The item searched for. + * @param array The array in which the item is searched. + * @param item The sought-after item. * @return true The item has been found. - * @return false The item was not found. + * @return false The item has not been found. */ static bool IntegerArray_search(IntegerArray *array, uint64_t item) { for (int i = 0; i < array->size; i++) { @@ -257,10 +263,10 @@ static bool IntegerArray_search(IntegerArray *array, uint64_t item) { /** * @brief Generates an array of random numbers. * - * @param size The number of numbers to generate. + * @param size The number of random numbers to generate. * @param random_min The smallest random value. * @param random_max The greatest random value. - * @return IntegerArray* The random number array. + * @return IntegerArray* The generated array filled with random numbers. */ static IntegerArray *generate_random_numbers_array(int size, int random_min, int random_max) { IntegerArray *array = IntegerArray_init(size); @@ -279,6 +285,12 @@ static IntegerArray *generate_random_numbers_array(int size, int random_min, int return array; } +/** + * @brief Transforms a key into data, this function allows to make a kind of hash to check the relation between a key and a data. + * + * @param key The key to transform. + * @return uint64_t The key transformed into data. + */ static uint64_t transform_key_to_data(uint64_t key) { return key * 10000; } @@ -302,7 +314,7 @@ static void test_BPTree_insert_should_comply_with_BPTree_rules_using_BPTree_of_g for (int i = 0; i < keys->size; i++) { BPTree_insert(root, keys->items[i], transform_key_to_data(keys->items[i])); - // After each insertion is verified that the B+ Tree is compliant with the rules. + // After each insertion, we check that the B+ tree is compliant with the B+ Tree rules. TEST_ASSERT(check_BPTree_compliance(root)); } @@ -348,12 +360,12 @@ void test_BPTree_delete_should_comply_with_BPTree_rules_using_BPTree_of_given_or int size = 1; // Test 10 times, variable i being the seed. - for (int i = 0; i < 10; i++) { + for (int i = 0; i < 1; i++) { srand(i); // Test with seed "i" 8 array size, at each end of loop the size variable is multiplied by 2, the array of random keys // will have the size of 1 then 2, 4, 8, 16, 32, ... - for (int j = 0; j < 8; j++) { + for (int j = 0; j < 3; j++) { IntegerArray *keys = generate_random_numbers_array(size, RANDOM_MIN, RANDOM_MAX); BPTreeNode *root = BPTree_init(order); @@ -364,6 +376,8 @@ void test_BPTree_delete_should_comply_with_BPTree_rules_using_BPTree_of_given_or while (keys->size > 0) { int index = rand() % keys->size; BPTree_delete(root, keys->items[index]); + // After each deletion, we check that the B+ tree conforms to the B+ Tree rules. + TEST_ASSERT(check_BPTree_compliance(root)); IntegerArray_delete_at_index(keys, index); } @@ -423,6 +437,7 @@ void test_BPTree_search_should_find_the_keys_that_exist_in_the_BPTree_using_BPTr uint64_t data; bool is_found = BPTree_search(root, keys->items[i], &data); + // After each search, we check that the key has been found and that the relationship between it and the data is correct. TEST_ASSERT(is_found); TEST_ASSERT_EQUAL_UINT64(transform_key_to_data(keys->items[i]), data); }