-
Notifications
You must be signed in to change notification settings - Fork 6
/
check_hash.c
65 lines (49 loc) · 1.38 KB
/
check_hash.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
#include "hash.h"
#include "hash_internal.h"
#include "map_testing.h"
MAP_TESTS(hash, "hash", init_hash());
int chain_len(hash_entry_t *next) {
int len = 0;
while(next) {
len++;
next = next->next;
}
return len;
}
float calc_avg_collisions(hash_t *hash)
// Calculate the average number of collisions per entry in the hash table
// (so ideally, would be 0 for no collisions.
{
int n_collisions = 0;
hash_entry_t *table = hash->table;
int table_len = (1 << hash->size_exponent);
for(int i = 0; i < table_len; i++) {
if(table[i].next != &hash_entry_unused) {
n_collisions += chain_len(&table[i]);
}
}
return ((float) n_collisions)/((float) table_len);
}
START_TEST(test_hash_collisions)
// Check that the number of collisions stays within reasonable bounds.
// This has the side-effect of checking that resizing is occurring.
{
const int n_entries = 100;
hash_t *hash = (hash_t *) init_hash();
for(int i = 0; i < n_entries; i++) {
map_set((map_t*) hash, i, (void *) (long) i);
}
float avg_collisions = calc_avg_collisions(hash);
assert(avg_collisions < 1.0);
}
END_TEST
Suite *hash_suite()
{
Suite *s = suite_create("hash");
TCase *hash_core = hash_tests_core_create();
TCase *hash_extras = tcase_create("hash_extras");
tcase_add_test(hash_extras, test_hash_collisions);
suite_add_tcase(s, hash_extras);
suite_add_tcase(s, hash_core);
return s;
}