-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_test.c
46 lines (36 loc) · 981 Bytes
/
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
#include "m99cc.h"
#include <stdio.h>
#include <stdlib.h>
int expect(int line, int expected, int actual) {
if (expected == actual) {
return 0;
}
fprintf(stderr, "%d: %d expected, but got %d\n", line, expected, actual);
exit(1);
}
void test_vector() {
Vector *vec = new_vector();
expect(__LINE__, 0, vec->len);
for (int i = 0; i < 100; i++) {
vec_push(vec, (void *)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"));
map_put(map, "foo", (void *)2);
expect(__LINE__, 2, (int)map_get(map, "foo"));
map_put(map, "bar", (void *)4);
expect(__LINE__, 4, (int)map_get(map, "bar"));
map_put(map, "foo", (void *)6);
expect(__LINE__, 6, (int)map_get(map, "foo"));
}
void runtest() {
test_vector();
test_map();
printf("OK\n");
}