-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
310 lines (258 loc) · 9.47 KB
/
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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#define OS_DA_IMPLEMENTATION
#include "os_da.h"
#define OS_LOG_IMPLEMENTATION
#include "os_log.h"
#define OS_SB_IMPLEMENTATION
#include "os_sb.h"
#define OS_SV_IMPLEMENTATION
#include "os_sv.h"
// Headers with other headers as dependencies
#define OS_JSON_IMPLEMENTATION
#include "os_json.h"
#define CHECK_START(name) printf("------------Checking %s -----------\n", name)
#define CHECK_END(name) \
printf("------------Done checking %s-----------\n", name)
#define _stringify(x) #x
#define stringify(x) _stringify(x)
#define TEST_STAGE(check_func, name) \
do \
{ \
if (!check_func(#name)) \
{ \
fprintf(stderr, __FILE__ \
":" stringify(__LINE__) ":ERROR:" #name \
" implementation failed\n"); \
return EXIT_FAILURE; \
} \
} while (0)
/* DA tests */
int check_da(const char *stage)
{
CHECK_START(stage);
typedef struct
{
int *items;
size_t count;
size_t capacity;
} os_numbers_t;
os_numbers_t numbers = {0};
assert(numbers.capacity == 0);
assert(numbers.count == 0);
/* Appending works */
os_da_append(&numbers, 1);
assert(numbers.count == 1);
assert(numbers.items[0] == 1);
os_da_append(&numbers, 2);
assert(numbers.count == 2);
assert(numbers.items[1] == 2);
os_da_append(&numbers, 3);
assert(numbers.count == 3);
assert(numbers.items[2] == 3);
/* Pop from the end */
os_da_pop_tail(&numbers);
assert(numbers.count == 2);
assert(numbers.items[0] == 1);
/* Pop from the start */
os_da_pop_head(int, &numbers);
assert(numbers.count == 1);
assert(numbers.items[0] == 2);
os_da_pop_head(int, &numbers);
assert(numbers.count == 0);
/* Can pop empty DAs */
os_da_pop_head(int, &numbers);
assert(numbers.count == 0);
os_da_free(&numbers);
CHECK_END(stage);
return 1;
}
/* JSON tests */
int check_json(const char *stage)
{
CHECK_START(stage);
os_json_object_t *obj = osj_parse(
"{\"hello\": \"world\",\"number\": 1, \"isWorking\": true}");
assert(obj->keys.count == 3);
assert(obj->values.count == 3);
assert(0 == strcmp("hello", obj->keys.items[0]));
assert(0 == strcmp("world", obj->values.items[0]->value.str));
assert(0 == strcmp("number", obj->keys.items[1]));
assert(1.0L == obj->values.items[1]->value.number);
assert(0 == strcmp("isWorking", obj->keys.items[2]));
assert(true == obj->values.items[2]->value.boolean);
osj_free(obj);
obj = osj_parse("{}");
assert(obj->keys.count == 0);
assert(obj->values.count == 0);
osj_free(obj);
obj = osj_parse("{\"a\": \"b\"}");
assert(obj->keys.count == 1);
assert(obj->values.count == 1);
assert(0 == strcmp("a", obj->keys.items[0]));
assert(0 == strcmp("b", obj->values.items[0]->value.str));
osj_free(obj);
obj = osj_parse("{\"a\": {\"b\": \"c\"}}");
assert(obj->keys.count == 1);
assert(obj->values.count == 1);
assert(0 == strcmp("a", obj->keys.items[0]));
assert(1 == obj->values.items[0]->value.object->keys.count);
assert(1 == obj->values.items[0]->value.object->values.count);
assert(0 == strcmp("b", obj->values.items[0]->value.object->keys.items[0]));
assert(0
== strcmp(
"c",
obj->values.items[0]->value.object->values.items[0]->value.str));
osj_free(obj);
obj = osj_parse("{\"a\": [1, 2, 3]}");
assert(obj->keys.count == 1);
assert(obj->values.count == 1);
assert(0 == strcmp("a", obj->keys.items[0]));
assert(3 == obj->values.items[0]->value.array->count);
assert(1.0L == obj->values.items[0]->value.array->items[0]->value.number);
assert(2.0L == obj->values.items[0]->value.array->items[1]->value.number);
assert(3.0L == obj->values.items[0]->value.array->items[2]->value.number);
osj_free(obj);
obj = osj_parse(
"{\"a\": [{\"hello\": \"world\"}, {\"number\": 1}], \"b\": false, "
"\"c\": null}");
assert(obj->keys.count == 3);
assert(obj->values.count == 3);
assert(0 == strcmp("a", obj->keys.items[0]));
assert(2 == obj->values.items[0]->value.array->count);
assert(1
== obj->values.items[0]
->value.array->items[0]
->value.object->keys.count);
assert(1
== obj->values.items[0]
->value.array->items[0]
->value.object->values.count);
assert(0
== strcmp("hello", obj->values.items[0]
->value.array->items[0]
->value.object->keys.items[0]));
assert(0
== strcmp("world", obj->values.items[0]
->value.array->items[0]
->value.object->values.items[0]
->value.str));
assert(1
== obj->values.items[0]
->value.array->items[1]
->value.object->keys.count);
assert(1
== obj->values.items[0]
->value.array->items[1]
->value.object->values.count);
assert(0
== strcmp("number", obj->values.items[0]
->value.array->items[1]
->value.object->keys.items[0]));
assert(1.0L
== obj->values.items[0]
->value.array->items[1]
->value.object->values.items[0]
->value.number);
assert(0 == strcmp("b", obj->keys.items[1]));
assert(false == obj->values.items[1]->value.boolean);
assert(0 == strcmp("c", obj->keys.items[2]));
assert(osj_null == obj->values.items[2]->type);
osj_print(*obj);
osj_free(obj);
CHECK_END(stage);
return 1;
}
/* Log tests */
int check_log(const char *stage)
{
CHECK_START(stage);
osl_log(OSL_DEBUG, "debug log");
osl_log(OSL_INFO, "info log");
osl_log(OSL_WARNING, "warning log");
osl_log(OSL_ERROR, "error log");
osl_logf(OSL_WARNING, "sizeof(size_t) = %zu", sizeof(size_t));
CHECK_END(stage);
return 1;
}
/* SB tests */
int check_sb(const char *stage)
{
CHECK_START(stage);
os_string_builder_t sb = sb_new();
assert(sb.capacity == 0);
sb = sb_append_cstr(sb, "hello");
assert(sb.length == strlen("hello"));
assert(sb.capacity != 0);
sb = sb_append_cstr(sb, " ");
assert(sb.length == strlen("hello "));
sb = sb_append_cstr(sb, "world!!");
assert(sb.length == strlen("hello world!!"));
sb = sb_end(sb);
assert(sb.length == strlen("hello world!!") + 1);
printf("buffer(%zu): %s\n", sb.capacity, sb_as_cstr(sb));
sb = sb_reset(sb);
assert(sb.capacity != 0);
sb = sb_append_cstr(sb, "hello again...");
assert(sb.length == strlen("hello again..."));
sb = sb_end(sb);
assert(sb.length == strlen("hello again...") + 1);
printf("buffer(%zu): %s\n", sb.capacity, sb_as_cstr(sb));
sb = sb_free(sb);
assert(sb.capacity == 0);
sb = sb_from_cstr("hello for the last time");
assert(sb.capacity != 0);
assert(sb.length == strlen("hello for the last time"));
sb = sb_end(sb);
printf("buffer(%zu): %s\n", sb.capacity, sb_as_cstr(sb));
sb = sb_free(sb);
assert(sb.capacity == 0);
CHECK_END(stage);
return 1;
}
/* SV tests */
int check_sv(const char *stage)
{
CHECK_START(stage);
/* Trimming */
os_string_view_t sv = ossv_from_cstr(" hello world from me ");
printf("before: " OSSV_FMT "\n", OSSV_ARG(sv));
sv = ossv_trim(sv);
printf("after: " OSSV_FMT "\n", OSSV_ARG(sv));
/* Starts and ends with */
assert(ossv_starts_with_cstr(sv, "hello"));
assert(ossv_ends_with_cstr(sv, "me"));
/* Split to words by spaces */
os_string_view_t word = ossv_chop_by_delimeter(&sv, ' ');
printf("first: " OSSV_FMT "\n", OSSV_ARG(word));
printf("remaining: " OSSV_FMT "\n", OSSV_ARG(sv));
assert(true == ossv_equal_cstr(word, "hello"));
word = ossv_chop_by_delimeter(&sv, ' ');
printf("second: " OSSV_FMT "\n", OSSV_ARG(word));
printf("remaining: " OSSV_FMT "\n", OSSV_ARG(sv));
word = ossv_chop_by_delimeter(&sv, ' ');
printf("third: " OSSV_FMT "\n", OSSV_ARG(word));
printf("remaining: " OSSV_FMT "\n", OSSV_ARG(sv));
word = ossv_chop_by_delimeter(&sv, ' ');
printf("fourth: " OSSV_FMT "\n", OSSV_ARG(word));
printf("remaining: " OSSV_FMT "\n", OSSV_ARG(sv));
assert(ossv_is_empty(sv));
/* Character class checks */
sv = ossv_from_cstr("hello 123");
word = ossv_chop_by_delimeter(&sv, ' ');
printf("word: " OSSV_FMT "\n", OSSV_ARG(word));
assert(ossv_is_alpha(word));
assert(ossv_is_digit(sv));
CHECK_END(stage);
return 1;
}
int main(void)
{
TEST_STAGE(check_da, DA);
TEST_STAGE(check_json, JSON);
TEST_STAGE(check_log, Log);
TEST_STAGE(check_sb, SB);
TEST_STAGE(check_sv, SV);
return EXIT_SUCCESS;
}