2 * Stack-less Just-In-Time compiler
4 * Copyright Zoltan Herczeg (hzmester@freemail.hu). All rights reserved.
6 * Redistribution and use in source and binary forms, with or without modification, are
7 * permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright notice, this list of
10 * conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
13 * of conditions and the following disclaimer in the documentation and/or other materials
14 * provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19 * SHALL THE COPYRIGHT HOLDER(S) OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
22 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 /* Must be the first one. Must not depend on any other include. */
33 #if defined _WIN32 || defined _WIN64
39 #define COLOR_RED "\33[31m"
40 #define COLOR_GREEN "\33[32m"
41 #define COLOR_ARCH "\33[33m"
42 #define COLOR_DEFAULT "\33[0m"
45 #ifdef REGEX_USE_8BIT_CHARS
51 #ifdef REGEX_MATCH_VERBOSE
52 void verbose_test(regex_char_t
*pattern
, regex_char_t
*string
)
56 struct regex_machine
* machine
;
57 struct regex_match
* match
;
64 printf("Start test '%s' matches to '%s'\n", pattern
, string
);
65 machine
= regex_compile(pattern
, (int)(ptr
- pattern
), REGEX_MATCH_VERBOSE
| REGEX_NEWLINE
, &error
);
68 printf("WARNING: Error %d\n", error
);
72 printf("ERROR: machine must be exists. Report this bug, please\n");
76 match
= regex_begin_match(machine
);
78 printf("WARNING: Not enough memory for matching\n");
79 regex_free_machine(machine
);
87 regex_continue_match_debug(match
, string
, (int)(ptr
- string
));
89 begin
= regex_get_result(match
, &end
, &id
);
90 printf("Math returns: %3d->%3d [%3d]\n", begin
, end
, id
);
92 regex_free_match(match
);
93 regex_free_machine(machine
);
98 int begin
; /* Expected begin. */
99 int end
; /* Expected end. */
100 int id
; /* Expected id. */
101 int finished
; /* -1 : don't care, 0 : false, 1 : true. */
102 int flags
; /* REGEX_MATCH_* */
103 const regex_char_t
*pattern
; /* NULL : use the previous pattern. */
104 const regex_char_t
*string
; /* NULL : end of tests. */
107 void run_tests(struct test_case
* test
, int verbose
, int silent
)
110 const regex_char_t
*ptr
;
111 struct regex_machine
* machine
= NULL
;
112 struct regex_match
* match
;
113 int begin
, end
, id
, finished
;
114 int success
= 0, fail
= 0;
116 if (!verbose
&& !silent
)
117 printf("Pass -v to enable verbose, -s to disable this hint.\n\n");
119 for ( ; test
->string
; test
++) {
121 printf("test: '%s' '%s': ", test
->pattern
? test
->pattern
: "[[REUSE]]", test
->string
);
126 regex_free_machine(machine
);
132 machine
= regex_compile(test
->pattern
, (int)(ptr
- test
->pattern
), test
->flags
, &error
);
136 printf("test: '%s' '%s': ", test
->pattern
? test
->pattern
: "[[REUSE]]", test
->string
);
137 printf("ABORT: Error %d\n", error
);
142 printf("test: '%s' '%s': ", test
->pattern
? test
->pattern
: "[[REUSE]]", test
->string
);
143 printf("ABORT: machine must be exists. Report this bug, please\n");
147 else if (test
->flags
!= 0) {
149 printf("test: '%s' '%s': ", test
->pattern
? test
->pattern
: "[[REUSE]]", test
->string
);
150 printf("ABORT: flag must be 0 if no pattern\n");
158 match
= regex_begin_match(machine
);
159 #ifdef REGEX_MATCH_VERBOSE
162 printf("test: '%s' '%s': ", test
->pattern
? test
->pattern
: "[[REUSE]]", test
->string
);
163 printf("ABORT: Not enough memory for matching\n");
164 regex_free_machine(machine
);
167 regex_continue_match_debug(match
, test
->string
, (int)(ptr
- test
->string
));
168 begin
= regex_get_result(match
, &end
, &id
);
169 finished
= regex_is_match_finished(match
);
171 if (begin
!= test
->begin
|| end
!= test
->end
|| id
!= test
->id
) {
173 printf("test: '%s' '%s': ", test
->pattern
? test
->pattern
: "[[REUSE]]", test
->string
);
174 printf("FAIL A: begin: %d != %d || end: %d != %d || id: %d != %d\n", test
->begin
, begin
, test
->end
, end
, test
->id
, id
);
177 if (test
->finished
!= -1 && test
->finished
!= !!finished
) {
179 printf("test: '%s' '%s': ", test
->pattern
? test
->pattern
: "[[REUSE]]", test
->string
);
180 printf("FAIL A: finish check\n");
185 regex_reset_match(match
);
186 regex_continue_match(match
, test
->string
, (int)(ptr
- test
->string
));
187 begin
= regex_get_result(match
, &end
, &id
);
188 finished
= regex_is_match_finished(match
);
189 regex_free_match(match
);
191 if (begin
!= test
->begin
|| end
!= test
->end
|| id
!= test
->id
) {
193 printf("test: '%s' '%s': ", test
->pattern
? test
->pattern
: "[[REUSE]]", test
->string
);
194 printf("FAIL B: begin: %d != %d || end: %d != %d || id: %d != %d\n", test
->begin
, begin
, test
->end
, end
, test
->id
, id
);
197 if (test
->finished
!= -1 && test
->finished
!= !!finished
) {
199 printf("test: '%s' '%s': ", test
->pattern
? test
->pattern
: "[[REUSE]]", test
->string
);
200 printf("FAIL B: finish check\n");
210 regex_free_machine(machine
);
212 printf("REGEX tests: ");
214 printf("all tests are " COLOR_GREEN
"PASSED" COLOR_DEFAULT
" ");
216 printf(COLOR_RED
"%d" COLOR_DEFAULT
" (" COLOR_RED
"%d%%" COLOR_DEFAULT
") tests are failed ", fail
, fail
* 100 / (success
+ fail
));
217 printf("on " COLOR_ARCH
"%s" COLOR_DEFAULT
"\n", regex_get_platform_name());
222 static struct test_case tests
[] = {
224 S("text"), S("is textile") },
226 S("^(ab|c)*?d+(es)?"), S("abccabddeses") },
228 S("^a+"), S("saaaa") },
230 S("(a+|b+)$"), S("saabbb") },
232 S("(a+|b+){,2}$"), S("saabbb") },
234 S("(abcde|bc)(a+*|(b|c){2}+){0}"), S("babcdeaaaaaaaa") },
236 S("(abc(aa)?|(cab+){2})"), S("cabcaa") },
238 S("^(abc(aa)?|(cab+){2})$"), S("cabcaa") },
240 S("^(ab{001!})?c"), S("abcde") },
242 S("(c?(a|bb{2!}){2,3}()+d){2,3}"), S("ccabbadbbadcaadcaad") },
244 NULL
, S("cacaadaadaa") },
245 { -1, 0, 0, -1, REGEX_MATCH_BEGIN
,
246 S("(((ab?c|d{1})))"), S("ad") },
247 { 0, 9, 3, -1, REGEX_MATCH_BEGIN
,
248 S("^((a{1!}|b{2!}|c{3!}){3,6}d)+"), S("cabadbacddaa") },
249 { 1, 6, 0, 0, REGEX_MATCH_END
,
250 S("(a+(bb|cc?)?){4,}"), S("maaaac") },
251 { 3, 12, 1, 0, REGEX_MATCH_END
,
252 S("(x+x+{02,03}(x+|{1!})){03,06}$"), S("aaaxxxxxxxxx") },
254 S("((c{1!})?|x+{2!}|{3!})(a|c)"), S("scs") },
256 NULL
, S("sxxaxxxaccacca") },
258 NULL
, S("ccdcdcdddddcdccccd") },
259 { 0, 3, 0, -1, REGEX_MATCH_NON_GREEDY
,
260 S("^a+a+a+"), S("aaaaaa") },
261 { 2, 5, 0, -1, REGEX_MATCH_NON_GREEDY
,
262 S("a+a+a+"), S("bbaaaaaa") },
264 S("baa|a+"), S("sbaaaaaa") },
266 S("baaa|baa|sbaaaa"), S("sbaaaaa") },
267 { 1, 4, 0, 1, REGEX_MATCH_NON_GREEDY
,
268 S("baaa|baa"), S("xbaaa") },
270 S("{3!}"), S("xx") },
272 S("{1!}(a{2!})*"), S("xx") },
275 { 0, 0, 1, 1, REGEX_MATCH_NON_GREEDY
,
276 S("{1!}(a{2!})*"), S("aaxx") },
278 S("(.[]-]){3}[^]-]{2}"), S("ax-xs-[][]lmn") },
280 S("([ABC]|[abc]{1!}){3,5}"), S("AbSAabbx") },
282 S("^[x\\-y[\\]]+([[\\]]{3!})*$"), S("x-y[-][]") },
284 NULL
, S("x-y[-][]x") },
286 S("<(/{1!})?[^>]+>"), S(" <html></html> ") },
288 NULL
, S(" </html><html> ") },
290 S("[A-Z0-9a-z]+"), S("[(Iden9aA)]") },
292 S("[^x-y]+[a-c_]{2,3}"), S("x_a_y") },
294 NULL
, S("ssaymmaa_ccl") },
295 { 3, 6, 0, 1, REGEX_NEWLINE
,
296 S(".a[^k]"), S("\na\nxa\ns") },
297 { 0, 2, 0, 1, REGEX_NEWLINE
,
298 S("^a+"), S("aa\n") },
299 { 1, 4, 0, 1, 0 /* =REGEX_NEWLINE */,
300 NULL
, S("\naaa\n") },
301 { 2, 3, 0, 1, 0 /* =REGEX_NEWLINE */,
302 NULL
, S("\n\na\n") },
303 { 0, 2, 0, 1, REGEX_NEWLINE
,
304 S("a+$"), S("aa\n") },
305 { 0, 3, 0, 0, 0 /* =REGEX_NEWLINE */,
307 { 2, 4, 1, 1, REGEX_NEWLINE
,
308 S("^a(a{1!})*$"), S("\n\naa\n\n") },
309 { 0, 1, 0, 0, 0 /* REGEX_NEWLINE */,
311 { -1, 0, 0, -1, 0 /* REGEX_NEWLINE */,
317 int main(int argc
, char* argv
[])
319 int has_arg
= (argc
>= 2 && argv
[1][0] == '-' && argv
[1][2] == '\0');
321 /* verbose_test("a((b)((c|d))|)c|"); */
322 /* verbose_test("Xa{009,0010}Xb{,7}Xc{5,}Xd{,}Xe{1,}Xf{,1}X"); */
323 /* verbose_test("{3!}({3})({0!}){,"); */
324 /* verbose_test("(s(ab){2,4}t){2,}*S(a*(b)(c()|)d+){3,4}{0,0}*M"); */
325 /* verbose_test("^a({2!})*b+(a|{1!}b)+d$"); */
326 /* verbose_test("((a|b|c)*(xy)+)+", "asbcxyxy"); */
328 run_tests(tests
, has_arg
&& argv
[1][1] == 'v', has_arg
&& argv
[1][1] == 's');
330 #if !(defined SLJIT_CONFIG_UNSUPPORTED && SLJIT_CONFIG_UNSUPPORTED)
331 sljit_free_unused_memory_exec();
332 #endif /* !SLJIT_CONFIG_UNSUPPORTED */