/
main.c
382 lines (293 loc) · 12.9 KB
/
main.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/**
* @file main.c
*
* @section License
* Copyright (C) 2014-2016, Erik Moqvist
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* This file is part of the Simba project.
*/
#include "simba.h"
int test_text(struct harness_t *harness_p)
{
char re[8];
BTASSERT(re_compile(re, "foo", 0, sizeof(re)) != NULL);
BTASSERT(re_match(re, "foo", 3, NULL, NULL) == 3);
BTASSERT(re_match(re, "FoO", 3, NULL, NULL) == -1);
BTASSERT(re_compile(re, "foo", RE_IGNORECASE, sizeof(re)) != NULL);
BTASSERT(re_match(re, "foo", 3, NULL, NULL) == 3);
BTASSERT(re_match(re, "FoO", 3, NULL, NULL) == 3);
BTASSERT(re_match(re, "BAR", 3, NULL, NULL) == -1);
BTASSERT(re_compile(re, "\n", 0, sizeof(re)) != NULL);
BTASSERT(re_match(re, "\n", 1, NULL, NULL) == 1);
return (0);
}
int test_newline(struct harness_t *harness_p)
{
char re[32];
/* RE_DOTALL. */
BTASSERT(re_compile(re, "a.b.c", 0, sizeof(re)) != NULL);
BTASSERT(re_match(re, "a\nb\nc", 5, NULL, NULL) == -1);
BTASSERT(re_compile(re, "a.b.c", RE_DOTALL, sizeof(re)) != NULL);
BTASSERT(re_match(re, "a\nb\nc", 5, NULL, NULL) == 5);
/* RE_MULTILINE. */
/* BTASSERT(re_compile(re, "^a\nb\nc$", 0, sizeof(re)) != NULL); */
/* BTASSERT(re_match(re, "a\nb\nc", 5, NULL, NULL) == 5); */
/* BTASSERT(re_match("^a\nb\nc$", "a\nb\nc", NULL, NULL, RE_MULTILINE) == 3); */
/* BTASSERT(re_match("^a\nb$\n^c$", "a\nb\nc", NULL, NULL, RE_MULTILINE) == 3) */
/* BTASSERT(re_match("^a\nb$\n^$c", "a\nb\nc", NULL, NULL, RE_MULTILINE) == -1) */
return (0);
}
int test_special_escaped(struct harness_t *harness_p)
{
char re[32];
BTASSERT(re_compile(re, "\\s\\s\\s\\s\\s\\s", 0, sizeof(re)) != NULL);
BTASSERT(re_match(re, " \t\r\n\f\v", 6, NULL, NULL) == 6);
BTASSERT(re_match(re, "non-space", 9, NULL, NULL) == -1);
BTASSERT(re_compile(re,
"\\d\\d\\d\\d\\d\\d\\d\\d\\d\\d",
0,
sizeof(re)) != NULL);
BTASSERT(re_match(re, "0123456789", 10, NULL, NULL) == 10);
BTASSERT(re_match(re, "non-decimal-digits", 18, NULL, NULL) == -1);
BTASSERT(re_compile(re,
"\\w\\w\\w\\w\\w\\w\\w\\w\\w\\w",
0,
sizeof(re)) != NULL);
BTASSERT(re_match(re, "amzAMZ059_", 10, NULL, NULL) == 10);
BTASSERT(re_match(re, "-.,\t", 4, NULL, NULL) == -1);
return (0);
}
int test_special(struct harness_t *harness_p)
{
char re[32];
BTASSERT(re_compile(re, ".", 0, sizeof(re)) != NULL);
BTASSERT(re_match(re, "a", 1, NULL, NULL) == 1);
BTASSERT(re_compile(re, "a?b", 0, sizeof(re)) != NULL);
BTASSERT(re_match(re, "b", 1, NULL, NULL) == 1);
BTASSERT(re_match(re, "c", 1, NULL, NULL) == -1);
BTASSERT(re_compile(re, "\\(escape\\)", 0, sizeof(re)) != NULL);
BTASSERT(re_match(re, "(escape)", 8, NULL, NULL) == 8);
BTASSERT(re_match(re, "(escape", 7, NULL, NULL) == -1);
return (0);
}
int test_repetition(struct harness_t *harness_p)
{
char re[32];
BTASSERT(re_compile(re, ".*", 0, sizeof(re)) != NULL);
BTASSERT(re_match(re, "", 0, NULL, NULL) == 0);
BTASSERT(re_match(re, "aaa", 3, NULL, NULL) == 3);
BTASSERT(re_compile(re, "a*", 0, sizeof(re)) != NULL);
BTASSERT(re_match(re, "aa", 2, NULL, NULL) == 2);
BTASSERT(re_compile(re, "a+", 0, sizeof(re)) != NULL);
BTASSERT(re_match(re, "aa", 2, NULL, NULL) == 2);
BTASSERT(re_compile(re, ".+", 0, sizeof(re)) != NULL);
BTASSERT(re_match(re, "", 0, NULL, NULL) == -1);
BTASSERT(re_compile(re, "a*b+", 0, sizeof(re)) != NULL);
BTASSERT(re_match(re, "aabb", 4, NULL, NULL) == 4);
BTASSERT(re_match(re, "b", 1, NULL, NULL) == 1);
BTASSERT(re_compile(re, ".*b+", 0, sizeof(re)) != NULL);
BTASSERT(re_match(re, "aabb", 4, NULL, NULL) == 4);
BTASSERT(re_match(re, "aabbcd", 6, NULL, NULL) == 4);
BTASSERT(re_compile(re, "\\(*\\)+", 0, sizeof(re)) != NULL);
BTASSERT(re_match(re, "((()))", 6, NULL, NULL) == 6);
BTASSERT(re_match(re, "()", 2, NULL, NULL) == 2);
BTASSERT(re_match(re, ")", 1, NULL, NULL) == 1);
BTASSERT(re_match(re, "(", 1, NULL, NULL) == -1);
BTASSERT(re_compile(re, ".{2}", 0, sizeof(re)) != NULL);
BTASSERT(re_match(re, "ab", 2, NULL, NULL) == 2);
BTASSERT(re_match(re, "a", 1, NULL, NULL) == -1);
BTASSERT(re_compile(re, "a{1}b{2}", 0, sizeof(re)) != NULL);
BTASSERT(re_match(re, "abb", 3, NULL, NULL) == 3);
BTASSERT(re_match(re, "abc", 3, NULL, NULL) == -1);
return (0);
}
int test_set(struct harness_t *harness_p)
{
char re[32];
BTASSERT(re_compile(re, "[a]", 0, sizeof(re)) != NULL);
BTASSERT(re_match(re, "a", 1, NULL, NULL) == 1);
BTASSERT(re_compile(re, "[ ]+", 0, sizeof(re)) != NULL);
BTASSERT(re_match(re, " ", 2, NULL, NULL) == 2);
BTASSERT(re_compile(re, "[0-9]+", 0, sizeof(re)) != NULL);
BTASSERT(re_match(re, "0123456789", 10, NULL, NULL) == 10);
BTASSERT(re_compile(re, "[a-zA-Z0-9_]+", 0, sizeof(re)) != NULL);
BTASSERT(re_match(re, "azAZ09_", 7, NULL, NULL) == 7);
BTASSERT(re_match(re, "-", 1, NULL, NULL) == -1);
/* Number of characters must be at least one. */
BTASSERT(re_compile(re, "[]]", 0, sizeof(re)) != NULL);
BTASSERT(re_match(re, "]", 1, NULL, NULL) == 1);
BTASSERT(re_compile(re, "[\\]]", 0, sizeof(re)) != NULL);
BTASSERT(re_match(re, "]", 1, NULL, NULL) == 1);
/* Missing closing bracket. */
BTASSERT(re_compile(re, "[\\]", 0, sizeof(re)) == NULL);
BTASSERT(re_compile(re, "[\\w]", 0, sizeof(re)) != NULL);
BTASSERT(re_match(re, "f", 1, NULL, NULL) == 1);
/* Allow unescaped range operator as first character. */
BTASSERT(re_compile(re, "[-]", 0, sizeof(re)) != NULL);
BTASSERT(re_match(re, "-", 1, NULL, NULL) == 1);
BTASSERT(re_compile(re, "[\\-]", 0, sizeof(re)) != NULL);
BTASSERT(re_match(re, "-", 1, NULL, NULL) == 1);
BTASSERT(re_compile(re, "[a-]", 0, sizeof(re)) != NULL);
BTASSERT(re_match(re, "-", 1, NULL, NULL) == 1);
/* Esacpe range operator '-' to include it in the sequence of
characters. */
BTASSERT(re_compile(re, "[a\\-f]", 0, sizeof(re)) != NULL);
BTASSERT(re_match(re, "-", 1, NULL, NULL) == 1);
BTASSERT(re_compile(re, "[a-z]", RE_IGNORECASE, sizeof(re)) != NULL);
BTASSERT(re_match(re, "A", 1, NULL, NULL) == 1);
/* Missing closing bracket. */
BTASSERT(re_compile(re, "[a-", 0, sizeof(re)) == NULL);
/* Special escape character are not allowed in a range. */
BTASSERT(re_compile(re, "[a-\\d]", 0, sizeof(re)) == NULL);
BTASSERT(re_compile(re, "[a\\w-3]", 0, sizeof(re)) == NULL);
BTASSERT(re_compile(re, "[\\h-y]", 0, sizeof(re)) != NULL);
BTASSERT(re_match(re, "h", 1, NULL, NULL) == 1);
BTASSERT(re_compile(re, "[h-\\y]", 0, sizeof(re)) != NULL);
BTASSERT(re_match(re, "h", 1, NULL, NULL) == 1);
BTASSERT(re_compile(re, "[h--]", 0, sizeof(re)) == NULL);
BTASSERT(re_compile(re, "[\\s]", 0, sizeof(re)) != NULL);
BTASSERT(re_match(re, "\t", 1, NULL, NULL) == 1);
BTASSERT(re_compile(re, "[\\d]", 0, sizeof(re)) != NULL);
BTASSERT(re_match(re, "9", 1, NULL, NULL) == 1);
return (0);
}
int test_groups(struct harness_t *harness_p)
{
/* char re[32]; */
/* struct re_group_t groups[2]; */
/* size_t number_of_groups; */
/* BTASSERT(re_compile(re, "(.)", 0, sizeof(re)) != NULL); */
/* BTASSERT(re_match(re, "a", 1, groups, &number_of_groups) == 1); */
/* BTASSERT(re_compile(re, "(.)", 0, sizeof(re)) != NULL); */
/* number_of_groups = membersof(groups); */
/* BTASSERT(re_match(re, "a", 1, groups, &number_of_groups) == 1); */
/* BTASSERT(number_of_groups == 1); */
/* BTASSERT(groups[0].buf_p != NULL); */
/* BTASSERT(groups[0].size == 1); */
/* BTASSERT(groups[0].buf_p[0] == 'a'); */
/* /\* Two groups. *\/ */
/* BTASSERT(re_compile(re, "(\\d+)(\\w+)", 0, sizeof(re)) == 0); */
/* number_of_groups = 2; */
/* BTASSERT(re_match(re, "123abc", 6, groups, &number_of_groups) == 6); */
/* BTASSERT(number_of_groups == 2); */
/* BTASSERT(groups[0].size == 3); */
/* BTASSERT(strncmp(groups[0].buf_p, "123", 3) == 0); */
/* BTASSERT(groups[1].size == 3); */
/* BTASSERT(strncmp(groups[1].buf_p, "abc", 3) == 0); */
/* /\* Group repetition. *\/ */
/* BTASSERT(re_match("(\\d)+", */
/* "123", */
/* groups, */
/* &number_of_groups, */
/* 0) == 3); */
/* BTASSERT(number_of_groups == 3); */
/* BTASSERT(groups[0].buf_p != NULL); */
/* BTASSERT(groups[0].size == 1); */
/* BTASSERT(strncmp(groups[0].buf_p, "1", 1) == 0); */
/* BTASSERT(groups[1].buf_p != NULL); */
/* BTASSERT(groups[1].size == 1); */
/* BTASSERT(strncmp(groups[1].buf_p, "2", 1) == 0); */
/* BTASSERT(groups[2].buf_p != NULL); */
/* BTASSERT(groups[2].size == 1); */
/* BTASSERT(strncmp(groups[2].buf_p, "3", 1) == 0); */
/* /\* Alternatives in a group. *\/ */
/* BTASSERT(re_match("(\\d+|\\w+)+", */
/* "123abc", */
/* groups, */
/* &number_of_groups, */
/* 0) == 6); */
/* BTASSERT(number_of_groups == 2); */
/* BTASSERT(groups[0].buf_p != NULL); */
/* BTASSERT(groups[0].size == 3); */
/* BTASSERT(strncmp(groups[0].buf_p, "123", 3) == 0); */
/* BTASSERT(groups[1].buf_p != NULL); */
/* BTASSERT(groups[1].size == 3); */
/* BTASSERT(strncmp(groups[1].buf_p, "abc", 3) == 0); */
return (1);
}
int test_alternatives(struct harness_t *harness_p)
{
/* char re[32]; */
/* BTASSERT(re_compile(re, "a|b", 0, sizeof(re)) != NULL); */
/* BTASSERT(re_match(re, "b", 1, NULL, NULL) == 1); */
return (1);
}
int test_greed(struct harness_t *harness_p)
{
char re[32];
/* Greedy, match as much as possible. */
BTASSERT(re_compile(re, "<.\?>", 0, sizeof(re)) != NULL);
BTASSERT(re_match(re, "<p>foo</p>", 10, NULL, NULL) == 3);
/* Greedy, match as much as possible. */
BTASSERT(re_compile(re, "<.*>", 0, sizeof(re)) != NULL);
BTASSERT(re_match(re, "<p>foo</p>", 10, NULL, NULL) == 10);
/* Greedy, match as much as possible, at least one. */
BTASSERT(re_compile(re, "<.+>", 0, sizeof(re)) != NULL);
BTASSERT(re_match(re, "<p>foo</p>", 10, NULL, NULL) == 10);
/* Non-greedy, match as little as possible. */
BTASSERT(re_compile(re, "<.\?\?>", 0, sizeof(re)) != NULL);
BTASSERT(re_match(re, "<p>foo</p>", 10, NULL, NULL) == 3);
BTASSERT(re_match(re, "<>>", 3, NULL, NULL) == 2);
BTASSERT(re_match(re, "<", 1, NULL, NULL) == -1);
/* Non-greedy, match as little as possible. */
BTASSERT(re_compile(re, "<.*?>", 0, sizeof(re)) != NULL);
BTASSERT(re_match(re, "<p>foo</p>", 10, NULL, NULL) == 3);
BTASSERT(re_match(re, "<>>", 3, NULL, NULL) == 2);
/* Non-greedy, match as little as possible, at least one. */
BTASSERT(re_compile(re, "<.+?>", 0, sizeof(re)) != NULL);
BTASSERT(re_match(re, "<p>foo</p>", 10, NULL, NULL) == 3);
return (0);
}
int test_complex(struct harness_t *harness_p)
{
char re[64];
BTASSERT(re_compile(re, "<.*<b{1}.??.?c>*", 0, sizeof(re)) != NULL);
BTASSERT(re_match(re, "<a><b><c>>", 10, NULL, NULL) == 10);
return (0);
}
int test_compile(struct harness_t *harness_p)
{
char re[64];
/* Compiled buffer too small. */
BTASSERT(re_compile(re, ".", 0, 3) != NULL);
BTASSERT(re_compile(re, ".", 0, 2) == NULL);
BTASSERT(re_compile(re, ".?", 0, 6) != NULL);
BTASSERT(re_compile(re, ".?", 0, 4) == NULL);
BTASSERT(re_compile(re, ".{1}", 0, 9) != NULL);
BTASSERT(re_compile(re, ".{1}", 0, 7) == NULL);
BTASSERT(re_compile(re, "a", 0, 4) != NULL);
BTASSERT(re_compile(re, "a", 0, 2) == NULL);
return (0);
}
int main()
{
struct harness_t harness;
struct harness_testcase_t harness_testcases[] = {
{ test_text, "test_text" },
{ test_newline, "test_newline" },
{ test_special_escaped, "test_special_escaped" },
{ test_special, "test_special" },
{ test_repetition, "test_repetition" },
{ test_set, "test_set" },
{ test_groups, "test_groups" },
{ test_alternatives, "test_alternatives" },
{ test_greed, "test_greed" },
{ test_complex, "test_complex" },
{ test_compile, "test_compile" },
{ NULL, NULL }
};
sys_start();
harness_init(&harness);
harness_run(&harness, harness_testcases);
return (0);
}