-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashmap_test.c
133 lines (110 loc) · 3.61 KB
/
hashmap_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
122
123
124
125
126
127
128
129
130
131
132
133
#include <alloca.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "testlib.h"
#include "hashmap.h"
void set_get_del(void* void_obj)
{
hashmap_t* obj = (hashmap_t*) void_obj;
char* str = alloca(sizeof("hello world") + 1);
*str = '\0';
strcat(str, "hello world");
long* number = alloca(sizeof(long));
*number = 218448782393093;
double* pi = alloca(sizeof(double));
*pi = 3.14159;
hashmap_set(obj, "a string", str);
hashmap_set(obj, "long number", number);
hashmap_set(obj, "pi", pi);
assert(hashmap_get(obj, "pi") == pi);
assert(*(double*) hashmap_get(obj, "pi") == 3.14159);
assert(hashmap_get(obj, "a string") == str);
assert(strcmp((char*) hashmap_get(obj, "a string"), "hello world") == 0);
assert(hashmap_get(obj, "long number") == number);
assert(*(long*) hashmap_get(obj, "long number") == 218448782393093);
assert(hashmap_get(obj, "non-key") == NULL);
hashmap_del(obj, "a string");
assert(hashmap_get(obj, "a string") == NULL);
hashmap_del(obj, "pi");
assert(hashmap_get(obj, "pi") == NULL);
assert(hashmap_get(obj, "long number") == number);
hashmap_del(obj, "long number");
assert(hashmap_get(obj, "long number") == NULL);
}
void keys_over_buckets(void* void_obj)
{
hashmap_t* obj = (hashmap_t*) void_obj;
const int size = HASHMAP_SIZE_BUCKETS * 10;
long *values = alloca(sizeof(long) * size);
char *keys = alloca(sizeof(char) * 30 * size);
for (int i = 0; i < size; i++) {
values[i] = (long) i * 1024;
sprintf(keys + i * 30, "key-%d", i);
hashmap_set(obj, keys + i * 30, &values[i]);
}
char cur_key[30];
for (int i = 0; i < size; i++) {
sprintf(cur_key, "key-%d", i);
assert(hashmap_get(obj, cur_key) == &values[i]);
assert(*(long*) hashmap_get(obj, cur_key) == values[i]);
}
}
void key_methods(void* void_obj)
{
hashmap_t* obj = (hashmap_t*) void_obj;
char* str = alloca(sizeof("hello world") + 1);
*str = '\0';
strcat(str, "hello world");
long* number = alloca(sizeof(long));
*number = 218448782393093;
double* pi = alloca(sizeof(double));
*pi = 3.14159;
hashmap_set(obj, "a string", str);
hashmap_set(obj, "long number", number);
hashmap_set(obj, "pi", pi);
assert(hashmap_count_keys(obj) == 3);
char* exp_keys[] = {"a string", "long number", "pi"};
char** ret_keys = hashmap_get_keys(obj);
test_sort_strings(ret_keys, hashmap_count_keys(obj));
for (size_t i = 0; i < hashmap_count_keys(obj); i++) {
assert(strcmp(*(ret_keys + i), *(exp_keys + i)) == 0);
}
hashmap_del(obj, "a string");
assert(hashmap_count_keys(obj) == 2);
hashmap_del(obj, "non-key");
hashmap_del(obj, "pi");
hashmap_del(obj, "pi");
assert(hashmap_count_keys(obj) == 1);
ret_keys = hashmap_get_keys(obj);
assert(strcmp(*ret_keys, "long number") == 0);
hashmap_del(obj, "long number");
assert(hashmap_count_keys(obj) == 0);
ret_keys = hashmap_get_keys(obj);
assert(ret_keys == NULL);
}
void pointers(void* void_obj)
{
hashmap_t* obj = (hashmap_t*) void_obj;
long val1 = 29830193;
long val2 = -394810;
assert(hashmap_set(obj, "long value 1", &val1) == NULL);
assert(hashmap_set(obj, "long value 1", &val2) == &val1);
assert(hashmap_del(obj, "long value 1") == &val2);
assert(hashmap_del(obj, "non-key") == NULL);
}
void* b(void)
{
return (void*) hashmap_create();
}
void a(void* obj)
{
hashmap_free((hashmap_t**) &obj);
}
int main(void)
{
test("set, get, del work", set_get_del, b, a);
test("collision resolution (num keys > buckets) works", keys_over_buckets, b, a);
test("key count and retrieval works", key_methods, b, a);
test("set, get, del returned pointers are correct", pointers, b, a);
}