forked from wmorgan/whistlepig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-stringmap.c
88 lines (69 loc) · 2.22 KB
/
test-stringmap.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
#include <string.h>
#include "stringmap.h"
#include "error.h"
#include "test.h"
typedef struct map_and_pool {
stringpool* pool;
stringmap* map;
} map_and_pool;
static map_and_pool* setup() {
map_and_pool* mp = malloc(sizeof(map_and_pool));
mp->pool = malloc(stringpool_initial_size());
stringpool_init(mp->pool);
mp->map = malloc(stringmap_initial_size());
stringmap_init(mp->map);
return mp;
}
TEST(stringmap_initial_state) {
map_and_pool* mp = setup();
ASSERT_EQUALS_UINT(0, mp->map->n_occupied);
ASSERT(!stringmap_needs_bump(mp->map));
free(mp);
return NO_ERROR;
}
TEST(stringmap_lookups_on_empty) {
map_and_pool* mp = setup();
ASSERT(stringmap_string_to_int(mp->map, mp->pool, "hot potato") == (uint32_t)-1);
ASSERT(stringmap_int_to_string(mp->map, mp->pool, 0) == NULL);
ASSERT(stringmap_int_to_string(mp->map, mp->pool, 1234) == NULL);
free(mp);
return NO_ERROR;
}
TEST(stringmap_multiple_adds) {
map_and_pool* mp = setup();
ASSERT(stringmap_string_to_int(mp->map, mp->pool, "hot potato") == (uint32_t)-1);
uint32_t x, y;
RELAY_ERROR(stringmap_add(mp->map, mp->pool, "hot potato", &x));
ASSERT(x != (uint32_t)-1);
RELAY_ERROR(stringmap_add(mp->map, mp->pool, "hot potato", &y));
ASSERT(y != (uint32_t)-1);
ASSERT_EQUALS_UINT(y, x);
free(mp);
return NO_ERROR;
}
TEST(stringmap_hashing_is_preserved) {
map_and_pool* mp = setup();
uint32_t x, y;
RELAY_ERROR(stringmap_add(mp->map, mp->pool, "hello there", &x));
ASSERT(x != (uint32_t)-1);
const char* a = stringmap_int_to_string(mp->map, mp->pool, x);
ASSERT(strcmp(a, "hello there") == 0);
RELAY_ERROR(stringmap_add(mp->map, mp->pool, "how are you?", &y));
const char* b = stringmap_int_to_string(mp->map, mp->pool, y);
ASSERT(strcmp(b, "how are you?") == 0);
ASSERT(x != y);
free(mp);
return NO_ERROR;
}
TEST(stringmap_detects_out_of_room) {
map_and_pool* mp = setup();
uint32_t x, y, z, w;
RELAY_ERROR(stringmap_add(mp->map, mp->pool, "one", &x));
RELAY_ERROR(stringmap_add(mp->map, mp->pool, "two", &y));
RELAY_ERROR(stringmap_add(mp->map, mp->pool, "three", &z));
wp_error* e = stringmap_add(mp->map, mp->pool, "four", &w);
ASSERT(e != NULL);
wp_error_free(e);
free(mp);
return NO_ERROR;
}