-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_test.c
62 lines (49 loc) · 1.57 KB
/
util_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
#include "9cc.h"
static void expect(int line, int expected, int actual) {
if (expected == actual) return;
fprintf(stderr, "%d: %d expected, but got %d\n", line, expected, actual);
exit(1);
}
static void vec_test() {
Vector *vec = new_vec();
expect(__LINE__, 0, vec->len);
for (int i = 0; i < 100; i++) {
vec_push(vec, (void *)(intptr_t)i);
}
expect(__LINE__, 100, vec->len);
expect(__LINE__, 0, (intptr_t)vec->data[0]);
expect(__LINE__, 50, (intptr_t)vec->data[50]);
expect(__LINE__, 99, (intptr_t)vec->data[99]);
}
static void map_test() {
Map *map = new_map();
expect(__LINE__, 0, (intptr_t)map_get(map, "foo"));
map_put(map, "foo", (void *)2);
expect(__LINE__, 2, (intptr_t)map_get(map, "foo"));
map_put(map, "bar", (void *)4);
expect(__LINE__, 4, (intptr_t)map_get(map, "bar"));
map_put(map, "foo", (void *)6);
expect(__LINE__, 6, (intptr_t)map_get(map, "foo"));
}
static void sb_test() {
StringBuilder *sb1 = new_sb();
expect(__LINE__, 0, strlen(sb_get(sb1)));
StringBuilder *sb2 = new_sb();
sb_append(sb2, "foo");
expect(__LINE__, 1, !strcmp(sb_get(sb2), "foo"));
StringBuilder *sb3 = new_sb();
sb_append(sb3, "foo");
sb_append(sb3, "bar");
expect(__LINE__, 1, !strcmp(sb_get(sb3), "foobar"));
StringBuilder *sb4 = new_sb();
sb_append(sb4, "foo");
sb_append(sb4, "bar");
sb_append(sb4, "foo");
sb_append(sb4, "bar");
expect(__LINE__, 1, !strcmp(sb_get(sb4), "foobarfoobar"));
}
void util_test() {
vec_test();
map_test();
sb_test();
}