-
Notifications
You must be signed in to change notification settings - Fork 6
/
tests.c
170 lines (153 loc) · 5.53 KB
/
tests.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "config.h"
#include "strings.h"
#include "optimize.h"
#include "unsigned.h"
#ifdef INLINE_UNSIGNED
# include <limits.h>
#endif
int main() {
struct strings *strings = strings_new();
assert(strings);
char buffer[12] = {'x'};
const char *string;
unsigned count = 100000;
// test interning strings
for (unsigned i = 1; i <= count; i++) {
unsigned_string(buffer + 1, i);
assert(strings_intern(strings, buffer) == i);
}
assert(strings_count(strings) == count);
// test idempotency
for (unsigned i = 1; i <= count; i++) {
unsigned_string(buffer + 1, i);
assert(strings_intern(strings, buffer) == i);
}
assert(strings_count(strings) == count);
// test looking up ids
for (unsigned i = 1; i <= count; i++) {
unsigned_string(buffer + 1, i);
string = strings_lookup_id(strings, i);
assert(string && !strcmp(buffer, string));
}
// test looking up strings
for (unsigned i = 1; i <= count; i++) {
unsigned_string(buffer + 1, i);
assert(strings_lookup(strings, buffer) == i);
}
// test iterating strings
struct strings_cursor cursor;
strings_cursor_init(&cursor, strings);
assert(!strings_cursor_id(&cursor));
assert(!strings_cursor_string(&cursor));
uint32_t id = 0;
while (strings_cursor_next(&cursor)) {
id = strings_cursor_id(&cursor);
string = strings_cursor_string(&cursor);
assert(id && string);
unsigned_string(buffer + 1, id);
assert(!strcmp(buffer, string));
}
assert(id == count);
assert(!strings_cursor_id(&cursor));
assert(!strings_cursor_string(&cursor));
// test optimizing strings
struct strings *optimized;
struct strings_frequency *freq = strings_frequency_new();
assert(freq);
assert(strings_frequency_add(freq, 3));
assert(strings_frequency_add(freq, 3));
assert(strings_frequency_add(freq, 3));
assert(strings_frequency_add(freq, 2));
assert(strings_frequency_add(freq, 2));
assert(strings_frequency_add(freq, 5));
optimized = strings_optimize(strings, freq);
assert(optimized);
assert(strings_count(optimized) == 3);
assert(strings_lookup(optimized, "x3") == 1);
assert(strings_lookup(optimized, "x2") == 2);
assert(strings_lookup(optimized, "x5") == 3);
strings_frequency_free(freq);
strings_free(optimized);
// test optimizing strings, and making sure all are included
freq = strings_frequency_new();
assert(freq);
assert(strings_frequency_add_all(freq, strings));
assert(strings_frequency_add(freq, 5));
optimized = strings_optimize(strings, freq);
assert(optimized);
assert(strings_count(optimized) == count);
assert(strings_lookup(optimized, "x5") == 1);
strings_free(optimized);
assert(strings_frequency_add(freq, 3));
assert(strings_frequency_add(freq, 3));
optimized = strings_optimize(strings, freq);
assert(optimized);
assert(strings_count(optimized) == count);
assert(strings_lookup(optimized, "x3") == 1);
assert(strings_lookup(optimized, "x5") == 2);
strings_frequency_free(freq);
strings_free(optimized);
// test interning unsigned int strings
#ifdef INLINE_UNSIGNED
for (unsigned i = 1; i <= count; i++) {
unsigned_string(buffer, i);
assert((strings_intern(strings, buffer) & 0x7FFFFFFF) == i);
assert((strings_intern(strings, buffer) & 0x7FFFFFFF) == i);
assert((strings_lookup(strings, buffer) & 0x7FFFFFFF) == i);
string = strings_lookup_id(strings, i | 0x80000000);
assert(string && !strcmp(buffer, string));
}
#endif
assert(strings_intern(strings, "2147483648") == count + 1);
assert(strings_intern(strings, "4294967295") == count + 2);
assert(strings_intern(strings, "4294967296") == count + 3);
#ifdef INLINE_UNSIGNED
uint32_t expected = UINT_MAX;
#else
uint32_t expected = count + 4;
#endif
assert(strings_intern(strings, "2147483647") == expected);
// test strings larger than the page size
char *large_string = malloc(PAGE_SIZE + 1);
assert(large_string);
memset(large_string, 'x', PAGE_SIZE);
large_string[PAGE_SIZE] = '\0';
assert(!strings_intern(strings, large_string));
free(large_string);
strings_free(strings);
// test snapshots
strings = strings_new();
assert(strings);
assert(!strings_count(strings));
struct strings_snapshot middle_snapshot, end_snapshot;
buffer[0] = 'x';
for (unsigned i = 1; i <= count; i++) {
unsigned_string(buffer + 1, i);
assert(strings_intern(strings, buffer) == i);
if (i == count / 2) {
strings_snapshot(strings, &middle_snapshot);
}
}
strings_snapshot(strings, &end_snapshot);
assert(strings_count(strings) == count);
assert(strings_restore(strings, &end_snapshot));
assert(strings_count(strings) == count);
assert(strings_restore(strings, &middle_snapshot));
assert(strings_count(strings) == count / 2);
assert(!strings_restore(strings, &end_snapshot));
for (unsigned i = 1; i <= count / 2; i++) {
unsigned_string(buffer + 1, i);
assert(strings_lookup(strings, buffer) == i);
}
for (unsigned i = count / 2 + 1; i <= count; i++) {
unsigned_string(buffer + 1, i);
assert(!strings_lookup(strings, buffer));
}
assert(strings_intern(strings, "foobar") == count / 2 + 1);
assert(strings_page_size() == PAGE_SIZE);
strings_free(strings);
return 0;
}