-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashmap_increase.c
52 lines (39 loc) · 1013 Bytes
/
hashmap_increase.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
#include "../hashmap.h"
#include <assert.h>
void stress()
{
struct hashmap_t map;
hashmap_init(&map);
#define N 1024
for (int i = 0; i < N; i++)
hashmap_put(&map, &i, 4, &i, 4, true);
assert(map.len == N);
for (int i = 0; i < N; i++) {
int *p = hashmap_get(&map, &i, 4);
assert(*p == i);
}
hashmap_free(&map);
}
int main()
{
struct hashmap_t map;
hashmap_init(&map);
/*
* default hashmap has space for 48 items.
* default hashmap will increase the total amount of buckets
* when the load factor >= 0.75, meaning > 36 entries will
* add another bucket
*/
u32 size_log2_pre = map.size_log2;
for (int i = 0; i < 40; i++) {
hashmap_put(&map, &i, 4, &i, 4, true);
}
assert(map.size_log2 == size_log2_pre + 1);
assert(map.len == 40);
for (int i = 0; i < 40; i++) {
int *p = hashmap_get(&map, &i, 4);
assert(*p == i);
}
hashmap_free(&map);
stress();
}