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
|
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
#include <wait.h>
#include "../src/hashmap.h"
#define INSERT_NUM 5000
#define RESET(map) \
{ \
destroy(map); \
init_hashmap(map); \
}
DebugStats_t DebugStats = {0};
void insert_test(HashMap map, bool with_names) {
int num = 100000;
for (int i = 0; i < num; i++) {
int r = rand();
insert(r, 1, map);
size_t idx = 0;
find_index(r, map, &idx);
if (!map->names[idx].data && with_names) {
set_name(r, "yeah what's up", map);
}
}
printf("collisions: %d\nsteps: %d\n", DebugStats.collisions, DebugStats.steps);
printf("used: %ld\n", map->used);
// there will be duplicates so this will fail. should pass on mac which doesn't repeat randoms till MAX_RAND-1
// assert(map->used == num);
}
void delete_test(HashMap map)
{
for (int i = 0; i < map->size; i++) {
if (map->keys[i]) {
remove_pid(map->keys[i], map);
}
}
}
void memory_leak_test()
{
struct HashMap hashmap;
HashMap m = &hashmap;
for (int i = 0; i < 5; i++) {
init_hashmap(m);
insert_test(m, true);
delete_test(m);
insert_test(m, false);
delete_test(m);
HashError err = destroy(m);
if (err) {
printf("could not destroy\n");
exit(1);
}
}
}
int main()
{
srand(time(0));
struct HashMap m;
HashMap map = &m;
init_hashmap(map);
insert_test(map, true);
RESET(map);
// destroy(map);
int pids[INSERT_NUM] = {0};
int counts[INSERT_NUM] = {0};
for (int i = 0; i < INSERT_NUM; i++) {
pids[i] = rand();
counts[i] = rand();
insert(pids[i], counts[i], map);
}
for (int i = 0; i < INSERT_NUM; i++) {
size_t res = 0;
get_status(pids[i], map, &res);
assert(res == counts[i]);
}
destroy(map);
printf("done\n");
memory_leak_test();
}
|