Commit 3a79c28
Changed files (10)
src
src/02/02/btree.c
@@ -1,6 +1,6 @@
-#include <stdio.h>
#include "btree.h"
#include <limits.h>
+#include <stdio.h>
static void inspect(BTree *tree, int level) {
if (!tree)
@@ -22,8 +22,7 @@ static bool in_range(BTree *tree, int min, int max) {
if (data < min || data > max)
return false;
- return in_range(tree->left, min, data) &&
- in_range(tree->right, data, max);
+ return in_range(tree->left, min, data) && in_range(tree->right, data, max);
}
BTree *btree_init(int data) {
@@ -34,10 +33,6 @@ BTree *btree_init(int data) {
return tree;
}
-bool btree_is_bst(BTree *tree) {
- return in_range(tree, INT_MIN, INT_MAX);
-}
+bool btree_is_bst(BTree *tree) { return in_range(tree, INT_MIN, INT_MAX); }
-void btree_inspect(BTree *tree) {
- inspect(tree, 0);
-}
+void btree_inspect(BTree *tree) { inspect(tree, 0); }
src/02/02/btree_test.c
@@ -38,7 +38,8 @@ Ensure(BinaryTree, when_a_node_on_the_left_subtree_is_less_than_an_ancestor) {
assert_that(btree_is_bst(tree), is_equal_to(false));
}
-Ensure(BinaryTree, when_a_node_on_the_right_subtree_is_greater_than_an_ancestor) {
+Ensure(BinaryTree,
+ when_a_node_on_the_right_subtree_is_greater_than_an_ancestor) {
BTree *tree = btree_init(300);
tree->right = btree_init(500);
tree->right->left = btree_init(200);
@@ -64,11 +65,18 @@ TestSuite *binary_search_tree_tests() {
add_test_with_context(suite, BinaryTree, when_a_tree_is_NULL);
add_test_with_context(suite, BinaryTree, when_a_tree_has_a_single_node);
- add_test_with_context(suite, BinaryTree, when_the_node_on_the_left_is_greater_than_the_root);
- add_test_with_context(suite, BinaryTree, when_the_node_on_the_right_is_less_than_the_root);
- add_test_with_context(suite, BinaryTree, when_a_node_on_the_left_subtree_is_less_than_an_ancestor);
- add_test_with_context(suite, BinaryTree, when_a_node_on_the_right_subtree_is_greater_than_an_ancestor);
- add_test_with_context(suite, BinaryTree, when_the_tree_is_a_binary_search_tree);
+ add_test_with_context(suite, BinaryTree,
+ when_the_node_on_the_left_is_greater_than_the_root);
+ add_test_with_context(suite, BinaryTree,
+ when_the_node_on_the_right_is_less_than_the_root);
+ add_test_with_context(
+ suite, BinaryTree,
+ when_a_node_on_the_left_subtree_is_less_than_an_ancestor);
+ add_test_with_context(
+ suite, BinaryTree,
+ when_a_node_on_the_right_subtree_is_greater_than_an_ancestor);
+ add_test_with_context(suite, BinaryTree,
+ when_the_tree_is_a_binary_search_tree);
return suite;
}
src/02/03/btree.c
@@ -42,6 +42,4 @@ BTree *btree_insert(BTree *tree, int data) {
return tree;
}
-void btree_inspect(BTree *tree) {
- inspect(tree, 0);
-}
+void btree_inspect(BTree *tree) { inspect(tree, 0); }
src/02/03/btree_test.c
@@ -13,7 +13,9 @@ Ensure(BinaryTree, when_the_tree_is_NULL) {
assert_that(tree->data, is_equal_to(10));
}
-Ensure(BinaryTree, when_inserting_an_item_less_than_the_root_in_a_tree_it_creates_a_node_on_the_left_side) {
+Ensure(
+ BinaryTree,
+ when_inserting_an_item_less_than_the_root_in_a_tree_it_creates_a_node_on_the_left_side) {
BTree *tree = btree_init(10);
btree_insert(tree, -5);
@@ -21,7 +23,9 @@ Ensure(BinaryTree, when_inserting_an_item_less_than_the_root_in_a_tree_it_create
assert_that(tree->left->data, is_equal_to(-5));
}
-Ensure(BinaryTree, when_inserting_an_item_greater_than_the_root_in_a_tree_it_creates_a_node_on_the_right_side) {
+Ensure(
+ BinaryTree,
+ when_inserting_an_item_greater_than_the_root_in_a_tree_it_creates_a_node_on_the_right_side) {
BTree *tree = btree_init(10);
btree_insert(tree, 16);
@@ -30,7 +34,9 @@ Ensure(BinaryTree, when_inserting_an_item_greater_than_the_root_in_a_tree_it_cre
assert_that(tree->right->data, is_equal_to(16));
}
-Ensure(BinaryTree, when_inserting_an_item_equal_to_the_root_in_a_tree_it_creates_a_node_on_the_left_side) {
+Ensure(
+ BinaryTree,
+ when_inserting_an_item_equal_to_the_root_in_a_tree_it_creates_a_node_on_the_left_side) {
BTree *tree = btree_init(10);
btree_insert(tree, 10);
@@ -39,7 +45,9 @@ Ensure(BinaryTree, when_inserting_an_item_equal_to_the_root_in_a_tree_it_creates
assert_that(tree->left->data, is_equal_to(10));
}
-Ensure(BinaryTree, when_inserting_multiple_items_into_a_tree_it_inserts_in_the_correct_position) {
+Ensure(
+ BinaryTree,
+ when_inserting_multiple_items_into_a_tree_it_inserts_in_the_correct_position) {
BTree *tree = btree_insert(NULL, 10);
btree_insert(tree, -5);
@@ -70,7 +78,9 @@ Ensure(BinaryTree, when_inserting_multiple_items_into_a_tree_it_inserts_in_the_c
assert_that(tree->left->right->left->data, is_equal_to(6));
}
-Ensure(BinaryTree, when_inserting_items_described_in_the_assignment_it_inserts_in_the_expected_position_in_the_tree) {
+Ensure(
+ BinaryTree,
+ when_inserting_items_described_in_the_assignment_it_inserts_in_the_expected_position_in_the_tree) {
BTree *tree = btree_insert(NULL, 1);
tree = btree_insert(tree, 5);
tree = btree_insert(tree, 2);
@@ -81,16 +91,25 @@ Ensure(BinaryTree, when_inserting_items_described_in_the_assignment_it_inserts_i
btree_inspect(tree);
}
-
TestSuite *binary_search_tree_tests() {
TestSuite *suite = create_test_suite();
add_test_with_context(suite, BinaryTree, when_the_tree_is_NULL);
- add_test_with_context(suite, BinaryTree, when_inserting_an_item_less_than_the_root_in_a_tree_it_creates_a_node_on_the_left_side);
- add_test_with_context(suite, BinaryTree, when_inserting_an_item_greater_than_the_root_in_a_tree_it_creates_a_node_on_the_right_side);
- add_test_with_context(suite, BinaryTree, when_inserting_an_item_equal_to_the_root_in_a_tree_it_creates_a_node_on_the_left_side);
- add_test_with_context(suite, BinaryTree, when_inserting_multiple_items_into_a_tree_it_inserts_in_the_correct_position);
- add_test_with_context(suite, BinaryTree, when_inserting_items_described_in_the_assignment_it_inserts_in_the_expected_position_in_the_tree);
+ add_test_with_context(
+ suite, BinaryTree,
+ when_inserting_an_item_less_than_the_root_in_a_tree_it_creates_a_node_on_the_left_side);
+ add_test_with_context(
+ suite, BinaryTree,
+ when_inserting_an_item_greater_than_the_root_in_a_tree_it_creates_a_node_on_the_right_side);
+ add_test_with_context(
+ suite, BinaryTree,
+ when_inserting_an_item_equal_to_the_root_in_a_tree_it_creates_a_node_on_the_left_side);
+ add_test_with_context(
+ suite, BinaryTree,
+ when_inserting_multiple_items_into_a_tree_it_inserts_in_the_correct_position);
+ add_test_with_context(
+ suite, BinaryTree,
+ when_inserting_items_described_in_the_assignment_it_inserts_in_the_expected_position_in_the_tree);
return suite;
}
src/02/03/main.c
@@ -1,4 +1,1 @@
-int main(int argc, char *argv[])
-{
- return 0;
-}
+int main(int argc, char *argv[]) { return 0; }
src/02/04/hash.c
@@ -4,21 +4,16 @@
#include <stdlib.h>
#include <string.h>
-Hash *hash_init(int size)
-{
+Hash *hash_init(int size) {
Hash *hash = malloc(sizeof(Hash));
hash->size = size;
hash->buckets = calloc(size, sizeof(Node));
return hash;
}
-int hash_index(Hash *hash, int key)
-{
- return key % hash->size;
-}
+int hash_index(Hash *hash, int key) { return key % hash->size; }
-void *search(Node *list, int key)
-{
+void *search(Node *list, int key) {
Node *current = list;
while (current) {
@@ -31,15 +26,13 @@ void *search(Node *list, int key)
return NULL;
}
-void *hash_get(Hash *hash, int key)
-{
+void *hash_get(Hash *hash, int key) {
int bucket = hash_index(hash, key);
Node *n = hash->buckets + bucket;
return (n->data) ? search(n, key) : NULL;
}
-void hash_set(Hash *hash, int key, void *value)
-{
+void hash_set(Hash *hash, int key, void *value) {
int bucket = hash_index(hash, key);
Tuple *tuple = tuple_initialize(key, value);
Node *n = hash->buckets + bucket;
@@ -50,8 +43,7 @@ void hash_set(Hash *hash, int key, void *value)
hash->buckets[bucket] = *list_initialize(tuple);
}
-void hash_inspect(Hash *hash)
-{
+void hash_inspect(Hash *hash) {
for (int i = 0; i < hash->size; i++) {
printf("%2d: ", i);
list_inspect(hash->buckets + i);
src/02/04/hash_test.c
@@ -40,8 +40,11 @@ TestSuite *hash_table_tests() {
TestSuite *suite = create_test_suite();
add_test_with_context(suite, HashTable, when_initializing_a_hash);
- add_test_with_context(suite, HashTable, when_getting_a_value_for_a_key_that_has_not_been_inserted);
- add_test_with_context(suite, HashTable, when_getting_a_values_for_a_key_that_has_been_inserted);
+ add_test_with_context(
+ suite, HashTable,
+ when_getting_a_value_for_a_key_that_has_not_been_inserted);
+ add_test_with_context(suite, HashTable,
+ when_getting_a_values_for_a_key_that_has_been_inserted);
add_test_with_context(suite, HashTable, when_a_hash_collision_occurs);
return suite;
}
src/02/04/list_test.c
@@ -13,7 +13,7 @@ Ensure(List, when_getting_head) {
}
Ensure(List, when_getting_mid) {
- Node *head = list_initialize((void*)100);
+ Node *head = list_initialize((void *)100);
Node *mid = list_add(head, (void *)200);
list_add(head, (void *)300);
src/02/04/main.c
@@ -1,6 +1,3 @@
#include <stdio.h>
-int main(int argc, char *argv[])
-{
- return 0;
-}
+int main(int argc, char *argv[]) { return 0; }
src/02/04/tuple.c
@@ -1,15 +1,11 @@
-#include "stdlib.h"
#include "tuple.h"
+#include "stdlib.h"
-Tuple *tuple_initialize(int key, void *value)
-{
+Tuple *tuple_initialize(int key, void *value) {
Tuple *tuple = malloc(sizeof(Tuple));
tuple->key = key;
tuple->value = value;
return tuple;
}
-void tuple_destroy(Tuple *tuple)
-{
- return free(tuple);
-}
+void tuple_destroy(Tuple *tuple) { return free(tuple); }