-
Notifications
You must be signed in to change notification settings - Fork 6
/
util_test.c
70 lines (56 loc) · 1.74 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
63
64
65
66
67
68
69
70
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "kcc.h"
void expect(int line, int expected, int actual) {
if (expected == actual)
return;
error("%d: %d expected, but got %d\n", line, expected, actual);
}
void expect_string(int line, char *expected, char *actual) {
if (strcmp(expected, actual) == 0)
return;
error("%d: \"%s\" expected, but got \"%s\"\n", line, expected, actual);
}
void test_vector() {
Vector *vec = new_vector();
expect(__LINE__, 0, vec->len);
for (int i = 0; i < 100; i++) {
vec_pushi(vec, i);
}
expect(__LINE__, 100, vec->len);
expect(__LINE__, 0, (int)vec->data[0]);
expect(__LINE__, 50, (int)vec->data[50]);
expect(__LINE__, 99, (int)vec->data[99]);
}
void test_map() {
Map *map = new_map();
expect(__LINE__, 0, (int)map_get(map, "foo"));
expect(__LINE__, 0, (int)map_exists(map, "foo"));
map_put(map, "foo", (void *)2);
expect(__LINE__, 2, (int)map_get(map, "foo"));
expect(__LINE__, 1, (int)map_exists(map, "foo"));
map_put(map, "bar", (void *)4);
expect(__LINE__, 2, (int)map_get(map, "foo"));
expect(__LINE__, 4, (int)map_get(map, "bar"));
map_put(map, "foo", (void *)6);
expect(__LINE__, 6, (int)map_get(map, "foo"));
map_puti(map, "baz", 8);
expect(__LINE__, 8, map_geti(map, "baz"));
}
void test_sb() {
StringBuilder *sb = new_sb();
expect_string(__LINE__, "", sb_string(sb));
sb_add(sb, 'a');
expect_string(__LINE__, "a", sb_string(sb));
sb_add(sb, 'A');
expect_string(__LINE__, "aA", sb_string(sb));
sb_add(sb, '\n');
expect_string(__LINE__, "aA\n", sb_string(sb));
}
void run_test() {
test_vector();
test_map();
test_sb();
printf("OK\n");
}