Commit 6e2d8e1
Changed files (2)
src
02
src/02/04/hash.c
@@ -33,7 +33,7 @@ void node_inspect(Node *node)
int i = 0;
while (node) {
- printf("[%d: %3d]", i, node->value);
+ printf("[%d: %3p]", i, node->value);
node = node->next;
i++;
}
@@ -63,9 +63,7 @@ void *hash_get(Hash *hash, int key)
void hash_set(Hash *hash, int key, void **value)
{
- node_inspect(hash->head);
int bucket = to_hash(key);
Node *node = node_at(hash->head, bucket);
node->value = value;
- node_inspect(hash->head);
}
src/02/04/hash_test.c
@@ -24,7 +24,17 @@ Ensure(HashTable, when_getting_a_values_for_a_key_that_has_been_inserted) {
Hash *hash = hash_init(13);
hash_set(hash, key, value);
- assert_that(*(int *)hash_get(hash, key), is_equal_to(value));
+ assert_that(hash_get(hash, key), is_equal_to(value));
+}
+
+Ensure(HashTable, when_a_hash_collision_occurs) {
+ Hash *hash = hash_init(13);
+
+ hash_set(hash, 8, 80);
+ hash_set(hash, 21, 210);
+
+ assert_that(hash_get(hash, 8), is_equal_to(80));
+ assert_that(hash_get(hash, 21), is_equal_to(210));
}
TestSuite *hash_table_tests() {
@@ -33,6 +43,7 @@ TestSuite *hash_table_tests() {
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_a_hash_collision_occurs);
return suite;
}