-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtabunit.c
211 lines (189 loc) · 5.87 KB
/
htabunit.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
** pem 2021-05-14
**
** A simple unit test for the hashtables.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include "hashtable.h"
static char *Words[] =
{
"first", "second", "third", "fourth", "fifth",
"sixth", "seventh", "eigth", "ninth", "tentn",
NULL
};
static void
print_info(hashtable_t h)
{
size_t size, count, slots, cmax;
hashtable_info(h, &size, &count, &slots, &cmax);
printf(" Size: %2lu Count: %2lu Slots: %2lu Chain max.: %2lu"
" Load: %4.2f\n",
(unsigned long)size, (unsigned long)count,
(unsigned long)slots, (unsigned long)cmax,
((float)count) / size);
}
static void
perrex(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
exit(1);
}
int
main()
{
int i;
char *val;
hashtable_t h;
/*
** Small table, default values, fast, test grow
*/
h = hashtable_create(10, 0.5, 0.8, hash_string_fast, NULL);
if (h == NULL)
perrex("Failed to create hash table\n");
putchar('\n');
printf("### New table, FAST hash function, min load 0.5, max load 0.8\n");
print_info(h);
for (i = 0 ; i < 8 && Words[i] != NULL ; i++)
if (hashtable_put(h, Words[i], Words[i], NULL) != hashtable_ret_ok)
perrex("Failed to put key %s\n", Words[i]);
printf("### %d words in table\n", i);
print_info(h);
if (hashtable_put(h, Words[i], Words[i], NULL) != hashtable_ret_ok)
perrex("Failed to put key %s\n", Words[i]);
i += 1;
printf("### %d words in table\n", i);
print_info(h);
putchar('\n');
/*
** Don't find non-existing key
*/
if (hashtable_get(h, "xyz", (void **)&val) != hashtable_ret_not_found)
perrex("Found xyz:%s in table, shouln't have.\n", val);
printf("### Get not found test ok\n" );
putchar('\n');
if (hashtable_rem(h, "abc", (void **)&val) != hashtable_ret_not_found)
perrex("Removed abc:%s in table, shouln't have.\n", val);
printf("### Remove not found test ok\n");
putchar('\n');
/*
** Remove them one-by-one
*/
for (int j = 0 ; j < i && Words[j] != NULL ; j++)
{
if (hashtable_rem(h, Words[j], (void **)&val) != hashtable_ret_ok)
perrex("Failed to remove key %s\n", Words[j]);
if (strcmp(Words[j], val) != 0)
perrex("Removed value mismatch: %s != %s\n", Words[j], val);
}
printf("### All keys removed\n");
print_info(h);
putchar('\n');
hashtable_destroy(h);
/*
** Small table, custom values, "good", test grow
*/
h = hashtable_create(10, 0.2, 0.9, hash_string_good, NULL);
if (h == NULL)
perrex("Failed to create hash table\n");
printf("### New table, GOOD hash function, min load 0.2, max load 0.9\n");
print_info(h);
for (i = 0 ; i < 9 && Words[i] != NULL ; i++)
if (hashtable_put(h, Words[i], Words[i], NULL) != hashtable_ret_ok)
perrex("Failed to put key %s\n", Words[i]);
printf("### %d words in table\n", i);
print_info(h);
if (hashtable_put(h, Words[i], Words[i], NULL) != hashtable_ret_ok)
perrex("Failed to put key %s\n", Words[i]);
i += 1;
printf("### %d words in table\n", i);
print_info(h);
putchar('\n');
/*
** Find them all
*/
for (int j = 0 ; j < i && Words[j] != NULL ; j++)
{
if (hashtable_get(h, Words[j], (void **)&val) != hashtable_ret_ok)
perrex("Failed to get key %s\n", Words[j]);
}
printf("### Found all keys\n");
putchar('\n');
/*
** Iterate
*/
int count = 0;
const char *key;
hashtable_iter_t iter;
hashtable_iter_init(h, &iter);
while (hashtable_iter_next(h, &iter, &key, (void **)&val))
{
count += 1;
if (strcmp(key, val) != 0)
perrex("Key-val mismatch: %s != %s\n", key, val);
}
if (i != count)
perrex("Iterator found %d keys, expected %d\n", count, i);
printf("### Iterated over all keys\n");
putchar('\n');
/*
** Clear the table
*/
hashtable_clear(h);
printf("### Cleared table\n");
print_info(h);
putchar('\n');
hashtable_destroy(h);
/*
** Default table with deallocator
*/
h = hashtable_create_dest_default(free);
if (h == NULL)
perrex("Failed to create hash table\n");
printf("### New table, default with deallocator\n");
print_info(h);
for (i = 0 ; Words[i] != NULL ; i++)
if (hashtable_put(h, Words[i], strdup(Words[i]), NULL) != hashtable_ret_ok)
perrex("Failed to put key %s\n", Words[i]);
printf("### %d words in table\n", i);
print_info(h);
putchar('\n');
/*
** Replace a value
*/
if (hashtable_put(h, Words[0], strdup("123456"), (void **)&val) != hashtable_ret_replaced)
perrex("Failed to replace value for %s\n", Words[0]);
if (strcmp(val, Words[0]) != 0)
perrex("Old value didn't match: %s != %s\n", val, Words[0]);
free(val);
if (hashtable_get(h, Words[0], (void **)&val) != hashtable_ret_ok)
perrex("Failed to get %s\n", Words[0]);
if (strcmp(val, "123456") != 0)
perrex("New value does not match 123456: %s\n", val);
/* Replace again (old value should be freed) */
if (hashtable_put(h, Words[0], strdup("ABCDEF"), NULL) != hashtable_ret_replaced)
perrex("Failed to replace value for %s\n", Words[0]);
printf("### Replace ok\n");
print_info(h);
putchar('\n');
/*
** Remove them one-by-one without returning the value.
** (They should be freed.)
*/
for (i = 0 ; Words[i] != NULL ; i++)
{
if (hashtable_rem(h, Words[i], NULL) != hashtable_ret_ok)
perrex("Failed to remove key %s\n", Words[i]);
}
printf("### All keys removed\n");
print_info(h);
putchar('\n');
hashtable_destroy(h);
printf("Ok\n");
exit(0);
}