-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.c
117 lines (79 loc) · 2.9 KB
/
test.c
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include "hash_table.h"
void test_hash_table_creation_successful();
void test_hash_function_returns_correct_value();
void test_add_works_as_expected();
void test_exists_checks_for_key_membership();
void test_get_returns_correct_value();
void test_remove_works_as_expected();
int main() {
printf("==============================================================\n");
printf("HASH TABLE TESTS\n");
printf("==============================================================\n");
test_hash_table_creation_successful();
test_hash_function_returns_correct_value();
test_add_works_as_expected();
test_exists_checks_for_key_membership();
test_get_returns_correct_value();
test_remove_works_as_expected();
return 0;
}
void test_hash_table_creation_successful() {
printf("test hash table is created successfuly\n");
HashTable *ht = construct_ht();
assert(ht->size == 0);
assert(ht->capacity == DEFAULT_CAPACITY);
destroy_ht(ht);
}
void test_hash_function_returns_correct_value() {
printf("test hash table returns correct function for key\n");
HashTable *ht = construct_ht();
assert(hash(ht, "domino") == 6);
assert(hash(ht, "programix") == 9);
assert(hash(ht, "jessie") == 3);
assert(hash(ht, "lipa") == 6);
assert(hash(ht, "garrix") == 13);
assert(hash(ht, "trevor") == 2);
destroy_ht(ht);
}
void test_add_works_as_expected() {
printf("test hash table add function works as expected\n");
HashTable *ht = construct_ht();
add(ht, "domino", "etremis");
assert(strcmp(ht->array[6]->key, "domino") == 0);
assert(strcmp(ht->array[6]->value, "etremis") == 0);
destroy_ht(ht);
}
void test_exists_checks_for_key_membership() {
printf("test exists function checks for key membership\n");
HashTable *ht = construct_ht();
add(ht, "domino", "etremis");
assert(exists_ht(ht, "domino") == 1);
assert(exists_ht(ht, "lipa") == 0);
destroy_ht(ht);
}
void test_get_returns_correct_value() {
printf("test get function returns correct value\n");
HashTable *ht = construct_ht();
add(ht, "lipa", "dua");
add(ht, "domino", "extremis");
HashTableItem *item = get_ht(ht, "domino");
assert(strcmp(item->key, "domino") == 0);
assert(strcmp(item->value, "extremis") == 0);
HashTableItem *another_item = get_ht(ht, "trevor");
assert(another_item == NULL);
destroy_ht(ht);
}
void test_remove_works_as_expected() {
printf("test hash table remove function works as expected\n");
HashTable *ht = construct_ht();
add(ht, "domino", "etremis");
assert(strcmp(ht->array[6]->key, "domino") == 0);
assert(strcmp(ht->array[6]->value, "etremis") == 0);
remove_ht(ht, "domino");
assert(strcmp(ht->array[6]->key, DELETED_KEY) == 0);
assert(strcmp(ht->array[6]->value, DELETED_KEY) == 0);
destroy_ht(ht);
}