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