forked from petewarden/c_hashmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
81 lines (61 loc) · 2.13 KB
/
main.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
/*
* A unit test and example of how to use the simple C hashmap
*/
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include "hashmap.h"
#define KEY_MAX_LENGTH (256)
#define KEY_PREFIX ("somekey")
#define KEY_COUNT (1024*1024)
typedef struct data_struct_s
{
char key_string[KEY_MAX_LENGTH];
int number;
} data_struct_t;
int main(char* argv, int argc)
{
int index;
int error;
map_t mymap;
char key_string[KEY_MAX_LENGTH];
data_struct_t* value;
mymap = hashmap_new();
/* First, populate the hash map with ascending values */
for (index=0; index<KEY_COUNT; index+=1)
{
/* Store the key string along side the numerical value so we can free it later */
value = malloc(sizeof(data_struct_t));
snprintf(value->key_string, KEY_MAX_LENGTH, "%s%d", KEY_PREFIX, index);
value->number = index;
error = hashmap_put(mymap, value->key_string, value);
assert(error==MAP_OK);
}
/* Now, check all of the expected values are there */
for (index=0; index<KEY_COUNT; index+=1)
{
snprintf(key_string, KEY_MAX_LENGTH, "%s%d", KEY_PREFIX, index);
error = hashmap_get(mymap, key_string, (void**)(&value));
/* Make sure the value was both found and the correct number */
assert(error==MAP_OK);
assert(value->number==index);
}
/* Make sure that a value that wasn't in the map can't be found */
snprintf(key_string, KEY_MAX_LENGTH, "%s%d", KEY_PREFIX, KEY_COUNT);
error = hashmap_get(mymap, key_string, (void**)(&value));
/* Make sure the value was not found */
assert(error==MAP_MISSING);
/* Free all of the values we allocated and remove them from the map */
for (index=0; index<KEY_COUNT; index+=1)
{
snprintf(key_string, KEY_MAX_LENGTH, "%s%d", KEY_PREFIX, index);
error = hashmap_get(mymap, key_string, (void**)(&value));
assert(error==MAP_OK);
error = hashmap_remove(mymap, key_string);
assert(error==MAP_OK);
free(value);
}
/* Now, destroy the map */
hashmap_free(mymap);
return 1;
}