-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_test.c
139 lines (110 loc) · 3.3 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
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
#include "util.h"
#include <stdio.h> // fopen(3)
#include <stdlib.h> // free(3)
#include <string.h> // strdup(3)
static void test_ArgsIter() {
char *argv_0[] = {"prog-name"};
ArgsIter *iter = new_ArgsIter(1, argv_0);
expect_str(__LINE__, "prog-name", iter->prog_name);
expect(__LINE__, false, ArgsIter_hasNext(iter));
delete_ArgsIter(iter);
char *argv_1[] = {"prog-name", "-h"};
iter = new_ArgsIter(2, argv_1);
expect_str(__LINE__, "prog-name", iter->prog_name);
expect(__LINE__, true, ArgsIter_hasNext(iter));
expect_str(__LINE__, "-h", ArgsIter_next(iter));
expect(__LINE__, false, ArgsIter_hasNext(iter));
delete_ArgsIter(iter);
}
static void test_Vector() {
Vector *vec = new_Vector();
expect(__LINE__, 0, vec->len);
for (int i = 0; i < 100; i++) {
Vector_push(vec, intdup(i));
}
// clang-format off
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]);
expect(__LINE__, 99, *(int *)Vector_last(vec));
expect(__LINE__, 99, *(int *)Vector_pop(vec));
expect(__LINE__, 98, *(int *)Vector_last(vec));
expect(__LINE__, 99, vec->len);
// clang-format on
delete_Vector(vec);
}
static void test_Map() {
Map *map = new_Map();
expect_ptr(__LINE__, NULL, Map_get(map, "foo"));
Map_put(map, strdup("foo"), intdup(2));
expect(__LINE__, 2, *(int *)Map_get(map, "foo"));
Map_put(map, strdup("bar"), intdup(4));
expect(__LINE__, 4, *(int *)Map_get(map, "bar"));
Map_put(map, strdup("foo"), intdup(6));
expect(__LINE__, 6, *(int *)Map_get(map, "foo"));
// put empty string at key
expect_ptr(__LINE__, NULL, Map_get(map, ""));
Map_put(map, strdup(""), strdup("value"));
expect_str(__LINE__, "value", Map_get(map, ""));
delete_Map(map);
}
static void test_StringBuffer() {
char *str;
StringBuffer *sb;
//
// 1.
//
sb = new_StringBuffer();
StringBuffer_append(sb, "One");
StringBuffer_append(sb, "Two");
str = StringBuffer_toString(sb);
expect_str(__LINE__, "OneTwo", str);
free(str);
StringBuffer_appendChar(sb, '3');
str = StringBuffer_toString(sb);
expect_str(__LINE__, "OneTwo3", str);
free(str);
StringBuffer_append(sb, "Four");
str = StringBuffer_toString(sb);
expect_str(__LINE__, "OneTwo3Four", str);
free(str);
delete_StringBuffer(sb);
//
// 2.
//
sb = new_StringBuffer();
FILE *f = fopen("LICENSE", "r");
int c;
while ((c = fgetc(f)) != EOF) {
StringBuffer_appendChar(sb, c);
}
fclose(f);
str = StringBuffer_toString(sb);
expect(__LINE__, 1064, strlen(str));
free(str);
delete_StringBuffer(sb);
}
static void test_strcmp() {
//
// compare empty string
//
// clang-format off
expect(__LINE__, true, strcmp("", "") == 0);
expect(__LINE__, false, strcmp("A", "") == 0);
expect(__LINE__, false, strcmp("", "A") == 0);
// clang-format on
}
static void test_sizeof() {
char *buf = malloc(256);
expect(__LINE__, 8, sizeof(buf));
free(buf);
}
void run_all_test_util() {
test_ArgsIter();
test_Vector();
test_Map();
test_StringBuffer();
test_strcmp();
test_sizeof();
}