master
1#include "hash.h"
2#include <cgreen/cgreen.h>
3#include <string.h>
4
5Describe(HashTable);
6BeforeEach(HashTable) {}
7AfterEach(HashTable) {}
8
9Ensure(HashTable, when_initializing_a_hash) {
10 Hash *hash = hash_init(13);
11
12 assert_that(hash, is_not_equal_to(NULL));
13}
14
15Ensure(HashTable, when_getting_a_value_for_a_key_that_has_not_been_inserted) {
16 Hash *hash = hash_init(13);
17
18 assert_that(hash_get(hash, 1), is_equal_to(NULL));
19}
20
21Ensure(HashTable, when_getting_a_values_for_a_key_that_has_been_inserted) {
22 Hash *hash = hash_init(13);
23 int key = 7;
24
25 hash_set(hash, key, (void *)100);
26 assert_that(hash_get(hash, key), is_equal_to(100));
27}
28
29Ensure(HashTable, when_a_hash_collision_occurs) {
30 Hash *hash = hash_init(13);
31
32 hash_set(hash, 8, (void *)80);
33 hash_set(hash, 21, (void *)210);
34
35 assert_that(hash_get(hash, 8), is_equal_to(80));
36 assert_that(hash_get(hash, 21), is_equal_to(210));
37}
38
39Ensure(HashTable, when_inserting_multiple_items_into_the_hash_table) {
40 Hash *hash = hash_init(13);
41 int items[] = {1, 5, 21, 26, 39, 14, 15, 16, 17, 18, 19, 20, 111, 145, 146};
42 int n = sizeof(items) / sizeof(int);
43
44 for (int i = 0; i < n; i++) {
45 int key = items[i];
46 long value = key * 10;
47 hash_set(hash, key, (void *)value);
48 }
49
50 for (int i = 0; i < n; i++) {
51 int key = items[i];
52 assert_that(hash_get(hash, key), is_equal_to(key * 10));
53 }
54}
55
56TestSuite *hash_table_tests() {
57 TestSuite *suite = create_test_suite();
58
59 add_test_with_context(suite, HashTable, when_initializing_a_hash);
60 add_test_with_context(
61 suite, HashTable,
62 when_getting_a_value_for_a_key_that_has_not_been_inserted);
63 add_test_with_context(suite, HashTable,
64 when_getting_a_values_for_a_key_that_has_been_inserted);
65 add_test_with_context(suite, HashTable, when_a_hash_collision_occurs);
66 add_test_with_context(suite, HashTable,
67 when_inserting_multiple_items_into_the_hash_table);
68 return suite;
69}
70
71extern TestSuite *list_tests();
72extern TestSuite *tuple_tests();
73
74int main(int argc, char **argv) {
75 TestSuite *suite = create_test_suite();
76 add_suite(suite, hash_table_tests());
77 add_suite(suite, list_tests());
78 add_suite(suite, tuple_tests());
79 return run_test_suite(suite, create_text_reporter());
80}