-
Notifications
You must be signed in to change notification settings - Fork 13
/
test-stringpool.c
67 lines (49 loc) · 1.33 KB
/
test-stringpool.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
#include <string.h>
#include "stringpool.h"
#include "error.h"
#include "test.h"
TEST(stringpool_initial_state) {
stringpool* p = malloc(stringpool_initial_size());
stringpool_init(p);
ASSERT(!stringpool_needs_bump(p));
free(p);
return NO_ERROR;
}
TEST(stringpool_add_gives_unique_ids) {
stringpool* p = malloc(stringpool_initial_size());
stringpool_init(p);
uint32_t ret1 = stringpool_add(p, "potato");
ASSERT(ret1 > 0);
uint32_t ret2 = stringpool_add(p, "monkey");
ASSERT(ret2 > 0);
ASSERT(ret1 != ret2);
free(p);
return NO_ERROR;
}
TEST(stringpool_add_gives_ids_that_lookup_returns) {
stringpool* p = malloc(stringpool_initial_size());
stringpool_init(p);
uint32_t ret;
char* s;
ret = stringpool_add(p, "potato");
s = stringpool_lookup(p, ret);
ASSERT(!strcmp(s, "potato"));
ret = stringpool_add(p, "monkey");
s = stringpool_lookup(p, ret);
ASSERT(!strcmp(s, "monkey"));
free(p);
return NO_ERROR;
}
TEST(stringpool_detects_out_of_room) {
stringpool* p = malloc(stringpool_initial_size());
stringpool_init(p);
uint32_t ret;
int times = stringpool_initial_size() / 6;
for(int i = 0; i < times - 1; i++) {
ret = stringpool_add(p, "12345");
ASSERT(ret != (uint32_t)-1);
}
ret = stringpool_add(p, "12345");
ASSERT_EQUALS_UINT((uint32_t)-1, ret);
return NO_ERROR;
}