-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChainingHashTable_test.c
121 lines (92 loc) · 3.51 KB
/
ChainingHashTable_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
118
119
120
121
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "acutest/acutest.h"
#include "../modules/SeparateChainingHashTable/ChainingHashTable.h"
// Hash functions
long unsigned int DJB2_hash(void *k) {
char *key = (char *)k;
unsigned int hash = 5381;
int c;
while ((c = *key++))
hash = ((hash << 5) + hash) + c; // hash * 33 + c
return hash;
}
long unsigned int SDBM_hash(void *k) {
char *key = (char *)k;
unsigned int hash = 0;
int c;
while ((c = *key++))
hash = c + (hash << 6) + (hash << 16) - hash;
return hash;
}
static int compare_strings(void *a, void *b) {
return strcmp((char *)a, (char *)b);
}
// static void *copy_int(void *data) {
// int *new_data = (int *)malloc(sizeof(int));
// if (new_data) {
// *new_data = *(int *)data;
// }
// return new_data;
// }
static void mock_print(void *data) {
// Implement a mock print function to capture the printed output
// In this example, we won't check the output, only if the function was called.
}
static void test_separate_chaining_hash_table_insert_and_search() {
SCHashtable *hash_table = SChashtable_create(compare_strings, mock_print, free, NULL, DJB2_hash);
char *keys[] = {"key1", "key2", "key3", "key4", "key5"};
int values[] = {100, 200, 300, 400, 500};
int num_elements = sizeof(keys) / sizeof(keys[0]);
// Insert key-value pairs
for (int i = 0; i < num_elements; i++) {
char *key_copy = (char *)malloc(strlen(keys[i]) + 1);
strcpy(key_copy, keys[i]);
SChashtable_insert(hash_table, key_copy, &values[i]);
}
// Search for inserted key-value pairs
for (int i = 0; i < num_elements; i++) {
void *value = SChashtable_search(hash_table, keys[i]);
TEST_CHECK(*(int *)value == values[i]);
}
// Search for non-existing key
char *non_existing_key = "key6";
void *value = SChashtable_search(hash_table, non_existing_key);
TEST_CHECK(value == NULL);
SChashtable_destroy(hash_table);
}
static void test_separate_chaining_hash_table_remove() {
SCHashtable *hash_table = SChashtable_create(compare_strings, mock_print, free, NULL, SDBM_hash);
char *keys[] = {"key1", "key2", "key3", "key4", "key5"};
int values[] = {100, 200, 300, 400, 500};
int num_elements = sizeof(keys) / sizeof(keys[0]);
// Insert key-value pairs
for (int i = 0; i < num_elements; i++) {
char *key_copy = (char *)malloc(strlen(keys[i]) + 1);
strcpy(key_copy, keys[i]);
SChashtable_insert(hash_table, key_copy, &values[i]);
}
// Remove key-value pairs
SChashtable_remove(hash_table, "key1");
SChashtable_remove(hash_table, "key3");
SChashtable_remove(hash_table, "key5");
// Search for removed key-value pairs
void *value = SChashtable_search(hash_table, "key1");
TEST_CHECK(value == NULL);
value = SChashtable_search(hash_table, "key3");
TEST_CHECK(value == NULL);
value = SChashtable_search(hash_table, "key5");
TEST_CHECK(value == NULL);
// Search for existing key-value pairs
value = SChashtable_search(hash_table, "key2");
TEST_CHECK(*(int *)value == 200);
value = SChashtable_search(hash_table, "key4");
TEST_CHECK(*(int *)value == 400);
SChashtable_destroy(hash_table);
}
TEST_LIST = {
{"test_separate_chaining_hash_table_insert_and_search", test_separate_chaining_hash_table_insert_and_search},
{"test_separate_chaining_hash_table_remove", test_separate_chaining_hash_table_remove},
{NULL, NULL}
};