1 /*
2 * Copyright (C) 2005 - 2006, Marco Barisione <marco@barisione.org>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 */
19
20 #undef G_DISABLE_ASSERT
21 #undef G_LOG_DOMAIN
22
23 #include <string.h>
24 #include <locale.h>
25 #include "glib.h"
26
27 #ifdef ENABLE_REGEX
28
29 /* U+20AC EURO SIGN (symbol, currency) */
30 #define EURO "\xe2\x82\xac"
31 /* U+00E0 LATIN SMALL LETTER A WITH GRAVE (letter, lowercase) */
32 #define AGRAVE "\xc3\xa0"
33 /* U+00C0 LATIN CAPITAL LETTER A WITH GRAVE (letter, uppercase) */
34 #define AGRAVE_UPPER "\xc3\x80"
35 /* U+00E8 LATIN SMALL LETTER E WITH GRAVE (letter, lowercase) */
36 #define EGRAVE "\xc3\xa8"
37 /* U+00F2 LATIN SMALL LETTER O WITH GRAVE (letter, lowercase) */
38 #define OGRAVE "\xc3\xb2"
39 /* U+014B LATIN SMALL LETTER ENG (letter, lowercase) */
40 #define ENG "\xc5\x8b"
41 /* U+0127 LATIN SMALL LETTER H WITH STROKE (letter, lowercase) */
42 #define HSTROKE "\xc4\xa7"
43 /* U+0634 ARABIC LETTER SHEEN (letter, other) */
44 #define SHEEN "\xd8\xb4"
45 /* U+1374 ETHIOPIC NUMBER THIRTY (number, other) */
46 #define ETH30 "\xe1\x8d\xb4"
47
48 /* A random value use to mark untouched integer variables. */
49 #define UNTOUCHED -559038737
50
51 static gboolean noisy = FALSE;
52 static gboolean abort_on_fail = FALSE;
53
54 #define PASS passed++
55 #define FAIL \
56 G_STMT_START \
57 { \
58 failed++; \
59 if (abort_on_fail) \
60 goto end; \
61 } \
62 G_STMT_END
63
64 /* A replacement for strcmp that doesn't crash with null pointers. */
65 static gboolean
streq(const gchar * s1,const gchar * s2)66 streq (const gchar *s1, const gchar *s2)
67 {
68 if (s1 == NULL && s2 == NULL)
69 return TRUE;
70 else if (s1 == NULL)
71 return FALSE;
72 else if (s2 == NULL)
73 return FALSE;
74 else
75 return strcmp (s1, s2) == 0;
76 }
77
78 static void
verbose(const gchar * format,...)79 verbose (const gchar *format, ...)
80 {
81 /* Function copied from glib/tests/patterntest.c by Matthias Clasen. */
82 gchar *msg;
83 va_list args;
84
85 va_start (args, format);
86 msg = g_strdup_vprintf (format, args);
87 va_end (args);
88
89 if (noisy)
90 g_print ("%s", msg);
91 g_free (msg);
92 }
93
94 static gboolean
test_new(const gchar * pattern,GRegexCompileFlags compile_opts,GRegexMatchFlags match_opts)95 test_new (const gchar *pattern,
96 GRegexCompileFlags compile_opts,
97 GRegexMatchFlags match_opts)
98 {
99 GRegex *regex;
100
101 verbose ("compiling \"%s\" \t", pattern);
102
103 regex = g_regex_new (pattern, compile_opts, match_opts, NULL);
104 if (regex == NULL)
105 {
106 g_print ("failed \t(pattern: \"%s\", compile: %d, match %d)\n",
107 pattern, compile_opts, match_opts);
108 return FALSE;
109 }
110
111 if (!streq (g_regex_get_pattern (regex), pattern))
112 {
113 g_print ("failed \t(pattern: \"%s\")\n",
114 pattern);
115 g_regex_unref (regex);
116 return FALSE;
117 }
118
119 g_regex_unref (regex);
120
121 verbose ("passed\n");
122 return TRUE;
123 }
124
125 #define TEST_NEW(pattern, compile_opts, match_opts) { \
126 total++; \
127 if (test_new (pattern, compile_opts, match_opts)) \
128 PASS; \
129 else \
130 FAIL; \
131 }
132
133 static gboolean
test_new_fail(const gchar * pattern,GRegexCompileFlags compile_opts,GRegexError expected_error)134 test_new_fail (const gchar *pattern,
135 GRegexCompileFlags compile_opts,
136 GRegexError expected_error)
137 {
138 GRegex *regex;
139 GError *error = NULL;
140
141 verbose ("compiling \"%s\" (expected a failure) \t", pattern);
142
143 regex = g_regex_new (pattern, compile_opts, 0, &error);
144
145 if (regex != NULL)
146 {
147 g_print ("failed \t(pattern: \"%s\", compile: %d)\n",
148 pattern, compile_opts);
149 g_regex_unref (regex);
150 return FALSE;
151 }
152
153 if (error->code != expected_error)
154 {
155 g_print ("failed \t(pattern: \"%s\", compile: %d, got error: %d, "
156 "expected error: %d)\n",
157 pattern, compile_opts, error->code, expected_error);
158 g_error_free (error);
159 return FALSE;
160 }
161
162 verbose ("passed\n");
163 return TRUE;
164 }
165
166 #define TEST_NEW_FAIL(pattern, compile_opts, expected_error) { \
167 total++; \
168 if (test_new_fail (pattern, compile_opts, expected_error)) \
169 PASS; \
170 else \
171 FAIL; \
172 }
173
174 static gboolean
test_match_simple(const gchar * pattern,const gchar * string,GRegexCompileFlags compile_opts,GRegexMatchFlags match_opts,gboolean expected)175 test_match_simple (const gchar *pattern,
176 const gchar *string,
177 GRegexCompileFlags compile_opts,
178 GRegexMatchFlags match_opts,
179 gboolean expected)
180 {
181 gboolean match;
182
183 verbose ("matching \"%s\" against \"%s\" \t", string, pattern);
184
185 match = g_regex_match_simple (pattern, string, compile_opts, match_opts);
186 if (match != expected)
187 {
188 g_print ("failed \t(unexpected %s)\n", match ? "match" : "mismatch");
189 return FALSE;
190 }
191 else
192 {
193 verbose ("passed (%s)\n", match ? "match" : "nomatch");
194 return TRUE;
195 }
196 }
197
198 #define TEST_MATCH_SIMPLE(pattern, string, compile_opts, match_opts, expected) { \
199 total++; \
200 if (test_match_simple (pattern, string, compile_opts, match_opts, expected)) \
201 PASS; \
202 else \
203 FAIL; \
204 }
205
206 static gboolean
test_match(const gchar * pattern,GRegexCompileFlags compile_opts,GRegexMatchFlags match_opts,const gchar * string,gssize string_len,gint start_position,GRegexMatchFlags match_opts2,gboolean expected)207 test_match (const gchar *pattern,
208 GRegexCompileFlags compile_opts,
209 GRegexMatchFlags match_opts,
210 const gchar *string,
211 gssize string_len,
212 gint start_position,
213 GRegexMatchFlags match_opts2,
214 gboolean expected)
215 {
216 GRegex *regex;
217 gboolean match;
218
219 verbose ("matching \"%s\" against \"%s\" (start: %d, len: %d) \t",
220 string, pattern, start_position, string_len);
221
222 regex = g_regex_new (pattern, compile_opts, match_opts, NULL);
223 match = g_regex_match_full (regex, string, string_len,
224 start_position, match_opts2, NULL, NULL);
225 if (match != expected)
226 {
227 gchar *e1 = g_strescape (pattern, NULL);
228 gchar *e2 = g_strescape (string, NULL);
229 g_print ("failed \t(unexpected %s) '%s' against '%s'\n", match ? "match" : "mismatch", e1, e2);
230 g_free (e1);
231 g_free (e2);
232 g_regex_unref (regex);
233 return FALSE;
234 }
235
236 if (string_len == -1 && start_position == 0)
237 {
238 match = g_regex_match (regex, string, match_opts2, NULL);
239 if (match != expected)
240 {
241 g_print ("failed \t(pattern: \"%s\", string: \"%s\")\n",
242 pattern, string);
243 g_regex_unref (regex);
244 return FALSE;
245 }
246 }
247
248 g_regex_unref (regex);
249
250 verbose ("passed (%s)\n", match ? "match" : "nomatch");
251 return TRUE;
252 }
253
254 #define TEST_MATCH(pattern, compile_opts, match_opts, string, \
255 string_len, start_position, match_opts2, expected) { \
256 total++; \
257 if (test_match (pattern, compile_opts, match_opts, string, \
258 string_len, start_position, match_opts2, expected)) \
259 PASS; \
260 else \
261 FAIL; \
262 }
263
264 struct _Match
265 {
266 gchar *string;
267 gint start, end;
268 };
269 typedef struct _Match Match;
270
271 static void
free_match(gpointer data,gpointer user_data)272 free_match (gpointer data, gpointer user_data)
273 {
274 Match *match = data;
275 if (match == NULL)
276 return;
277 g_free (match->string);
278 g_free (match);
279 }
280
281 static gboolean
test_match_next(const gchar * pattern,const gchar * string,gssize string_len,gint start_position,...)282 test_match_next (const gchar *pattern,
283 const gchar *string,
284 gssize string_len,
285 gint start_position,
286 ...)
287 {
288 GRegex *regex;
289 GMatchInfo *match_info;
290 va_list args;
291 GSList *matches = NULL;
292 GSList *expected = NULL;
293 GSList *l_exp, *l_match;
294 gboolean ret = TRUE;
295
296 verbose ("matching \"%s\" against \"%s\" (start: %d, len: %d) \t",
297 string, pattern, start_position, string_len);
298
299 /* The va_list is a NULL-terminated sequence of: extected matched string,
300 * expected start and expected end. */
301 va_start (args, start_position);
302 while (TRUE)
303 {
304 Match *match;
305 const gchar *expected_string = va_arg (args, const gchar *);
306 if (expected_string == NULL)
307 break;
308 match = g_new0 (Match, 1);
309 match->string = g_strdup (expected_string);
310 match->start = va_arg (args, gint);
311 match->end = va_arg (args, gint);
312 expected = g_slist_prepend (expected, match);
313 }
314 expected = g_slist_reverse (expected);
315 va_end (args);
316
317 regex = g_regex_new (pattern, 0, 0, NULL);
318
319 g_regex_match_full (regex, string, string_len,
320 start_position, 0, &match_info, NULL);
321 while (g_match_info_matches (match_info))
322 {
323 Match *match = g_new0 (Match, 1);
324 match->string = g_match_info_fetch (match_info, 0);
325 match->start = UNTOUCHED;
326 match->end = UNTOUCHED;
327 g_match_info_fetch_pos (match_info, 0, &match->start, &match->end);
328 matches = g_slist_prepend (matches, match);
329 g_match_info_next (match_info, NULL);
330 }
331 g_assert (regex == g_match_info_get_regex (match_info));
332 g_assert (string == g_match_info_get_string (match_info));
333 g_match_info_free (match_info);
334 matches = g_slist_reverse (matches);
335
336 if (g_slist_length (matches) != g_slist_length (expected))
337 {
338 gint match_count = g_slist_length (matches);
339 g_print ("failed \t(got %d %s, expected %d)\n", match_count,
340 match_count == 1 ? "match" : "matches",
341 g_slist_length (expected));
342 ret = FALSE;
343 goto exit;
344 }
345
346 l_exp = expected;
347 l_match = matches;
348 while (l_exp != NULL)
349 {
350 Match *exp = l_exp->data;
351 Match *match = l_match->data;
352
353 if (!streq(exp->string, match->string))
354 {
355 g_print ("failed \t(got \"%s\", expected \"%s\")\n",
356 match->string, exp->string);
357 ret = FALSE;
358 goto exit;
359 }
360
361 if (exp->start != match->start || exp->end != match->end)
362 {
363 g_print ("failed \t(got [%d, %d], expected [%d, %d])\n",
364 match->start, match->end, exp->start, exp->end);
365 ret = FALSE;
366 goto exit;
367 }
368
369 l_exp = g_slist_next (l_exp);
370 l_match = g_slist_next (l_match);
371 }
372
373 exit:
374 if (ret)
375 {
376 gint count = g_slist_length (matches);
377 verbose ("passed (%d %s)\n", count, count == 1 ? "match" : "matches");
378 }
379
380 g_regex_unref (regex);
381 g_slist_foreach (expected, free_match, NULL);
382 g_slist_free (expected);
383 g_slist_foreach (matches, free_match, NULL);
384 g_slist_free (matches);
385
386 return ret;
387 }
388
389 #define TEST_MATCH_NEXT0(pattern, string, string_len, start_position) { \
390 total++; \
391 if (test_match_next (pattern, string, string_len, start_position, NULL)) \
392 PASS; \
393 else \
394 FAIL; \
395 }
396
397 #define TEST_MATCH_NEXT1(pattern, string, string_len, start_position, \
398 t1, s1, e1) { \
399 total++; \
400 if (test_match_next (pattern, string, string_len, start_position, \
401 t1, s1, e1, NULL)) \
402 PASS; \
403 else \
404 FAIL; \
405 }
406
407 #define TEST_MATCH_NEXT2(pattern, string, string_len, start_position, \
408 t1, s1, e1, t2, s2, e2) { \
409 total++; \
410 if (test_match_next (pattern, string, string_len, start_position, \
411 t1, s1, e1, t2, s2, e2, NULL)) \
412 PASS; \
413 else \
414 FAIL; \
415 }
416
417 #define TEST_MATCH_NEXT3(pattern, string, string_len, start_position, \
418 t1, s1, e1, t2, s2, e2, t3, s3, e3) { \
419 total++; \
420 if (test_match_next (pattern, string, string_len, start_position, \
421 t1, s1, e1, t2, s2, e2, t3, s3, e3, NULL)) \
422 PASS; \
423 else \
424 FAIL; \
425 }
426
427 #define TEST_MATCH_NEXT4(pattern, string, string_len, start_position, \
428 t1, s1, e1, t2, s2, e2, t3, s3, e3, t4, s4, e4) { \
429 total++; \
430 if (test_match_next (pattern, string, string_len, start_position, \
431 t1, s1, e1, t2, s2, e2, t3, s3, e3, t4, s4, e4, NULL)) \
432 PASS; \
433 else \
434 FAIL; \
435 }
436
437 static gboolean
test_match_count(const gchar * pattern,const gchar * string,gint start_position,GRegexMatchFlags match_opts,gint expected_count)438 test_match_count (const gchar *pattern,
439 const gchar *string,
440 gint start_position,
441 GRegexMatchFlags match_opts,
442 gint expected_count)
443 {
444 GRegex *regex;
445 GMatchInfo *match_info;
446 gint count;
447
448 verbose ("fetching match count (string: \"%s\", pattern: \"%s\", start: %d) \t",
449 string, pattern, start_position);
450
451 regex = g_regex_new (pattern, 0, 0, NULL);
452
453 g_regex_match_full (regex, string, -1, start_position,
454 match_opts, &match_info, NULL);
455 count = g_match_info_get_match_count (match_info);
456
457 if (count != expected_count)
458 {
459 g_print ("failed \t(got %d, expected: %d)\n", count, expected_count);
460 return FALSE;
461 }
462
463 g_match_info_free (match_info);
464 g_regex_unref (regex);
465
466 verbose ("passed\n");
467 return TRUE;
468 }
469
470 #define TEST_MATCH_COUNT(pattern, string, start_position, match_opts, expected_count) { \
471 total++; \
472 if (test_match_count (pattern, string, start_position, match_opts, expected_count)) \
473 PASS; \
474 else \
475 FAIL; \
476 }
477
478 static gboolean
test_partial(const gchar * pattern,const gchar * string,gboolean expected)479 test_partial (const gchar *pattern,
480 const gchar *string,
481 gboolean expected)
482 {
483 GRegex *regex;
484 GMatchInfo *match_info;
485
486 verbose ("partial matching (string: \"%s\", pattern: \"%s\") \t",
487 string, pattern);
488
489 regex = g_regex_new (pattern, 0, 0, NULL);
490
491 g_regex_match (regex, string, G_REGEX_MATCH_PARTIAL, &match_info);
492 if (expected != g_match_info_is_partial_match (match_info))
493 {
494 g_print ("failed \t(got %d, expected: %d)\n", !expected, expected);
495 g_regex_unref (regex);
496 return FALSE;
497 }
498
499 if (expected && g_match_info_fetch_pos (match_info, 0, NULL, NULL))
500 {
501 g_print ("failed \t(got sub-pattern 0)\n");
502 g_regex_unref (regex);
503 return FALSE;
504 }
505
506 if (expected && g_match_info_fetch_pos (match_info, 1, NULL, NULL))
507 {
508 g_print ("failed \t(got sub-pattern 1)\n");
509 g_regex_unref (regex);
510 return FALSE;
511 }
512
513 g_match_info_free (match_info);
514 g_regex_unref (regex);
515
516 verbose ("passed\n");
517 return TRUE;
518 }
519
520 #define TEST_PARTIAL(pattern, string, expected) { \
521 total++; \
522 if (test_partial (pattern, string, expected)) \
523 PASS; \
524 else \
525 FAIL; \
526 }
527
528 static gboolean
test_sub_pattern(const gchar * pattern,const gchar * string,gint start_position,gint sub_n,const gchar * expected_sub,gint expected_start,gint expected_end)529 test_sub_pattern (const gchar *pattern,
530 const gchar *string,
531 gint start_position,
532 gint sub_n,
533 const gchar *expected_sub,
534 gint expected_start,
535 gint expected_end)
536 {
537 GRegex *regex;
538 GMatchInfo *match_info;
539 gchar *sub_expr;
540 gint start = UNTOUCHED, end = UNTOUCHED;
541
542 verbose ("fetching sub-pattern %d from \"%s\" (pattern: \"%s\") \t",
543 sub_n, string, pattern);
544
545 regex = g_regex_new (pattern, 0, 0, NULL);
546 g_regex_match_full (regex, string, -1, start_position, 0, &match_info, NULL);
547
548 sub_expr = g_match_info_fetch (match_info, sub_n);
549 if (!streq(sub_expr, expected_sub))
550 {
551 g_print ("failed \t(got \"%s\", expected \"%s\")\n",
552 sub_expr, expected_sub);
553 g_free (sub_expr);
554 g_regex_unref (regex);
555 return FALSE;
556 }
557 g_free (sub_expr);
558
559 g_match_info_fetch_pos (match_info, sub_n, &start, &end);
560 if (start != expected_start || end != expected_end)
561 {
562 g_print ("failed \t(got [%d, %d], expected [%d, %d])\n",
563 start, end, expected_start, expected_end);
564 g_regex_unref (regex);
565 return FALSE;
566 }
567
568 g_match_info_free (match_info);
569 g_regex_unref (regex);
570
571 verbose ("passed\n");
572 return TRUE;
573 }
574
575 #define TEST_SUB_PATTERN(pattern, string, start_position, sub_n, expected_sub, \
576 expected_start, expected_end) { \
577 total++; \
578 if (test_sub_pattern (pattern, string, start_position, sub_n, expected_sub, \
579 expected_start, expected_end)) \
580 PASS; \
581 else \
582 FAIL; \
583 }
584
585 static gboolean
test_named_sub_pattern(const gchar * pattern,GRegexCompileFlags flags,const gchar * string,gint start_position,const gchar * sub_name,const gchar * expected_sub,gint expected_start,gint expected_end)586 test_named_sub_pattern (const gchar *pattern,
587 GRegexCompileFlags flags,
588 const gchar *string,
589 gint start_position,
590 const gchar *sub_name,
591 const gchar *expected_sub,
592 gint expected_start,
593 gint expected_end)
594 {
595 GRegex *regex;
596 GMatchInfo *match_info;
597 gint start = UNTOUCHED, end = UNTOUCHED;
598 gchar *sub_expr;
599
600 verbose ("fetching sub-pattern \"%s\" from \"%s\" (pattern: \"%s\") \t",
601 sub_name, string, pattern);
602
603 regex = g_regex_new (pattern, flags, 0, NULL);
604
605 g_regex_match_full (regex, string, -1, start_position, 0, &match_info, NULL);
606 sub_expr = g_match_info_fetch_named (match_info, sub_name);
607 if (!streq (sub_expr, expected_sub))
608 {
609 g_print ("failed \t(got \"%s\", expected \"%s\")\n",
610 sub_expr, expected_sub);
611 g_free (sub_expr);
612 g_regex_unref (regex);
613 return FALSE;
614 }
615 g_free (sub_expr);
616
617 g_match_info_fetch_named_pos (match_info, sub_name, &start, &end);
618 if (start != expected_start || end != expected_end)
619 {
620 g_print ("failed \t(got [%d, %d], expected [%d, %d])\n",
621 start, end, expected_start, expected_end);
622 g_regex_unref (regex);
623 return FALSE;
624 }
625
626 g_match_info_free (match_info);
627 g_regex_unref (regex);
628
629 verbose ("passed\n");
630 return TRUE;
631 }
632
633 #define TEST_NAMED_SUB_PATTERN(pattern, string, start_position, sub_name, \
634 expected_sub, expected_start, expected_end) { \
635 total++; \
636 if (test_named_sub_pattern (pattern, 0, string, start_position, sub_name, \
637 expected_sub, expected_start, expected_end)) \
638 PASS; \
639 else \
640 FAIL; \
641 }
642
643 #define TEST_NAMED_SUB_PATTERN_DUPNAMES(pattern, string, start_position, sub_name, \
644 expected_sub, expected_start, expected_end) { \
645 total++; \
646 if (test_named_sub_pattern (pattern, G_REGEX_DUPNAMES, string, start_position, \
647 sub_name, expected_sub, expected_start, expected_end)) \
648 PASS; \
649 else \
650 FAIL; \
651 }
652
653 static gboolean
test_fetch_all(const gchar * pattern,const gchar * string,...)654 test_fetch_all (const gchar *pattern,
655 const gchar *string,
656 ...)
657 {
658 GRegex *regex;
659 GMatchInfo *match_info;
660 va_list args;
661 GSList *expected = NULL;
662 GSList *l_exp;
663 gchar **matches;
664 gint match_count;
665 gboolean ret = TRUE;
666 gint i;
667
668 verbose ("fetching all sub-patterns from \"%s\" (pattern: \"%s\") \t",
669 string, pattern);
670
671 /* The va_list is a NULL-terminated sequence of extected strings. */
672 va_start (args, string);
673 while (TRUE)
674 {
675 gchar *expected_string = va_arg (args, gchar *);
676 if (expected_string == NULL)
677 break;
678 else
679 expected = g_slist_prepend (expected, g_strdup (expected_string));
680 }
681 expected = g_slist_reverse (expected);
682 va_end (args);
683
684 regex = g_regex_new (pattern, 0, 0, NULL);
685 g_regex_match (regex, string, 0, &match_info);
686 matches = g_match_info_fetch_all (match_info);
687 if (matches)
688 match_count = g_strv_length (matches);
689 else
690 match_count = 0;
691
692 if (match_count != g_slist_length (expected))
693 {
694 g_print ("failed \t(got %d %s, expected %d)\n", match_count,
695 match_count == 1 ? "match" : "matches",
696 g_slist_length (expected));
697 ret = FALSE;
698 goto exit;
699 }
700
701 l_exp = expected;
702 for (i = 0; l_exp != NULL; i++, l_exp = g_slist_next (l_exp))
703 {
704 if (!streq(l_exp->data, matches [i]))
705 {
706 g_print ("failed \t(got \"%s\", expected \"%s\")\n",
707 matches [i], (gchar *)l_exp->data);
708 ret = FALSE;
709 goto exit;
710 }
711 }
712
713 verbose ("passed (%d %s)\n", match_count,
714 match_count == 1 ? "match" : "matches");
715
716 exit:
717 g_match_info_free (match_info);
718 g_regex_unref (regex);
719 g_slist_foreach (expected, (GFunc)g_free, NULL);
720 g_slist_free (expected);
721 g_strfreev (matches);
722
723 return ret;
724 }
725
726 #define TEST_FETCH_ALL0(pattern, string) { \
727 total++; \
728 if (test_fetch_all (pattern, string, NULL)) \
729 PASS; \
730 else \
731 FAIL; \
732 }
733
734 #define TEST_FETCH_ALL1(pattern, string, e1) { \
735 total++; \
736 if (test_fetch_all (pattern, string, e1, NULL)) \
737 PASS; \
738 else \
739 FAIL; \
740 }
741
742 #define TEST_FETCH_ALL2(pattern, string, e1, e2) { \
743 total++; \
744 if (test_fetch_all (pattern, string, e1, e2, NULL)) \
745 PASS; \
746 else \
747 FAIL; \
748 }
749
750 #define TEST_FETCH_ALL3(pattern, string, e1, e2, e3) { \
751 total++; \
752 if (test_fetch_all (pattern, string, e1, e2, e3, NULL)) \
753 PASS; \
754 else \
755 FAIL; \
756 }
757
758 static gboolean
test_split_simple(const gchar * pattern,const gchar * string,...)759 test_split_simple (const gchar *pattern,
760 const gchar *string,
761 ...)
762 {
763 va_list args;
764 GSList *expected = NULL;
765 GSList *l_exp;
766 gchar **tokens;
767 gint token_count;
768 gboolean ret = TRUE;
769 gint i;
770
771 verbose ("splitting \"%s\" against \"%s\" \t", string, pattern);
772
773 /* The va_list is a NULL-terminated sequence of extected strings. */
774 va_start (args, string);
775 while (TRUE)
776 {
777 gchar *expected_string = va_arg (args, gchar *);
778 if (expected_string == NULL)
779 break;
780 else
781 expected = g_slist_prepend (expected, g_strdup (expected_string));
782 }
783 expected = g_slist_reverse (expected);
784 va_end (args);
785
786 tokens = g_regex_split_simple (pattern, string, 0, 0);
787 if (tokens)
788 token_count = g_strv_length (tokens);
789 else
790 token_count = 0;
791
792 if (token_count != g_slist_length (expected))
793 {
794 g_print ("failed \t(got %d %s, expected %d)\n", token_count,
795 token_count == 1 ? "match" : "matches",
796 g_slist_length (expected));
797 ret = FALSE;
798 goto exit;
799 }
800
801 l_exp = expected;
802 for (i = 0; l_exp != NULL; i++, l_exp = g_slist_next (l_exp))
803 {
804 if (!streq(l_exp->data, tokens [i]))
805 {
806 g_print ("failed \t(got \"%s\", expected \"%s\")\n",
807 tokens[i], (gchar *)l_exp->data);
808 ret = FALSE;
809 goto exit;
810 }
811 }
812
813 verbose ("passed (%d %s)\n", token_count,
814 token_count == 1 ? "token" : "tokens");
815
816 exit:
817 g_slist_foreach (expected, (GFunc)g_free, NULL);
818 g_slist_free (expected);
819 g_strfreev (tokens);
820
821 return ret;
822 }
823
824 #define TEST_SPLIT_SIMPLE0(pattern, string) { \
825 total++; \
826 if (test_split_simple (pattern, string, NULL)) \
827 PASS; \
828 else \
829 FAIL; \
830 }
831
832 #define TEST_SPLIT_SIMPLE1(pattern, string, e1) { \
833 total++; \
834 if (test_split_simple (pattern, string, e1, NULL)) \
835 PASS; \
836 else \
837 FAIL; \
838 }
839
840 #define TEST_SPLIT_SIMPLE2(pattern, string, e1, e2) { \
841 total++; \
842 if (test_split_simple (pattern, string, e1, e2, NULL)) \
843 PASS; \
844 else \
845 FAIL; \
846 }
847
848 #define TEST_SPLIT_SIMPLE3(pattern, string, e1, e2, e3) { \
849 total++; \
850 if (test_split_simple (pattern, string, e1, e2, e3, NULL)) \
851 PASS; \
852 else \
853 FAIL; \
854 }
855
856 static gboolean
test_split_full(const gchar * pattern,const gchar * string,gint start_position,gint max_tokens,...)857 test_split_full (const gchar *pattern,
858 const gchar *string,
859 gint start_position,
860 gint max_tokens,
861 ...)
862 {
863 GRegex *regex;
864 va_list args;
865 GSList *expected = NULL;
866 GSList *l_exp;
867 gchar **tokens;
868 gint token_count;
869 gboolean ret = TRUE;
870 gint i;
871
872 verbose ("splitting \"%s\" against \"%s\" (start: %d, max: %d) \t",
873 string, pattern, start_position, max_tokens);
874
875 /* The va_list is a NULL-terminated sequence of extected strings. */
876 va_start (args, max_tokens);
877 while (TRUE)
878 {
879 gchar *expected_string = va_arg (args, gchar *);
880 if (expected_string == NULL)
881 break;
882 else
883 expected = g_slist_prepend (expected, g_strdup (expected_string));
884 }
885 expected = g_slist_reverse (expected);
886 va_end (args);
887
888 regex = g_regex_new (pattern, 0, 0, NULL);
889 tokens = g_regex_split_full (regex, string, -1, start_position,
890 0, max_tokens, NULL);
891 if (tokens)
892 token_count = g_strv_length (tokens);
893 else
894 token_count = 0;
895
896 if (token_count != g_slist_length (expected))
897 {
898 g_print ("failed \t(got %d %s, expected %d)\n", token_count,
899 token_count == 1 ? "match" : "matches",
900 g_slist_length (expected));
901 ret = FALSE;
902 goto exit;
903 }
904
905 l_exp = expected;
906 for (i = 0; l_exp != NULL; i++, l_exp = g_slist_next (l_exp))
907 {
908 if (!streq(l_exp->data, tokens [i]))
909 {
910 g_print ("failed \t(got \"%s\", expected \"%s\")\n",
911 tokens[i], (gchar *)l_exp->data);
912 ret = FALSE;
913 goto exit;
914 }
915 }
916
917 verbose ("passed (%d %s)\n", token_count,
918 token_count == 1 ? "token" : "tokens");
919
920 exit:
921 g_regex_unref (regex);
922 g_slist_foreach (expected, (GFunc)g_free, NULL);
923 g_slist_free (expected);
924 g_strfreev (tokens);
925
926 return ret;
927 }
928
929 static gboolean
test_split(const gchar * pattern,const gchar * string,...)930 test_split (const gchar *pattern,
931 const gchar *string,
932 ...)
933 {
934 GRegex *regex;
935 va_list args;
936 GSList *expected = NULL;
937 GSList *l_exp;
938 gchar **tokens;
939 gint token_count;
940 gboolean ret = TRUE;
941 gint i;
942
943 verbose ("splitting \"%s\" against \"%s\" \t", string, pattern);
944
945 /* The va_list is a NULL-terminated sequence of extected strings. */
946 va_start (args, string);
947 while (TRUE)
948 {
949 gchar *expected_string = va_arg (args, gchar *);
950 if (expected_string == NULL)
951 break;
952 else
953 expected = g_slist_prepend (expected, g_strdup (expected_string));
954 }
955 expected = g_slist_reverse (expected);
956 va_end (args);
957
958 regex = g_regex_new (pattern, 0, 0, NULL);
959 tokens = g_regex_split (regex, string, 0);
960 if (tokens)
961 token_count = g_strv_length (tokens);
962 else
963 token_count = 0;
964
965 if (token_count != g_slist_length (expected))
966 {
967 g_print ("failed \t(got %d %s, expected %d)\n", token_count,
968 token_count == 1 ? "match" : "matches",
969 g_slist_length (expected));
970 ret = FALSE;
971 goto exit;
972 }
973
974 l_exp = expected;
975 for (i = 0; l_exp != NULL; i++, l_exp = g_slist_next (l_exp))
976 {
977 if (!streq(l_exp->data, tokens [i]))
978 {
979 g_print ("failed \t(got \"%s\", expected \"%s\")\n",
980 tokens[i], (gchar *)l_exp->data);
981 ret = FALSE;
982 goto exit;
983 }
984 }
985
986 verbose ("passed (%d %s)\n", token_count,
987 token_count == 1 ? "token" : "tokens");
988
989 exit:
990 g_regex_unref (regex);
991 g_slist_foreach (expected, (GFunc)g_free, NULL);
992 g_slist_free (expected);
993 g_strfreev (tokens);
994
995 return ret;
996 }
997
998 #define TEST_SPLIT0(pattern, string, start_position, max_tokens) { \
999 total++; \
1000 if (test_split_full (pattern, string, start_position, max_tokens, NULL)) \
1001 PASS; \
1002 else \
1003 FAIL; \
1004 if (start_position == 0 && max_tokens <= 0) \
1005 { \
1006 total++; \
1007 if (test_split (pattern, string, NULL)) \
1008 PASS; \
1009 else \
1010 FAIL; \
1011 } \
1012 }
1013
1014 #define TEST_SPLIT1(pattern, string, start_position, max_tokens, e1) { \
1015 total++; \
1016 if (test_split_full (pattern, string, start_position, max_tokens, e1, NULL)) \
1017 PASS; \
1018 else \
1019 FAIL; \
1020 if (start_position == 0 && max_tokens <= 0) \
1021 { \
1022 total++; \
1023 if (test_split (pattern, string, e1, NULL)) \
1024 PASS; \
1025 else \
1026 FAIL; \
1027 } \
1028 }
1029
1030 #define TEST_SPLIT2(pattern, string, start_position, max_tokens, e1, e2) { \
1031 total++; \
1032 if (test_split_full (pattern, string, start_position, max_tokens, e1, e2, NULL)) \
1033 PASS; \
1034 else \
1035 FAIL; \
1036 if (start_position == 0 && max_tokens <= 0) \
1037 { \
1038 total++; \
1039 if (test_split (pattern, string, e1, e2, NULL)) \
1040 PASS; \
1041 else \
1042 FAIL; \
1043 } \
1044 }
1045
1046 #define TEST_SPLIT3(pattern, string, start_position, max_tokens, e1, e2, e3) { \
1047 total++; \
1048 if (test_split_full (pattern, string, start_position, max_tokens, e1, e2, e3, NULL)) \
1049 PASS; \
1050 else \
1051 FAIL; \
1052 if (start_position == 0 && max_tokens <= 0) \
1053 { \
1054 total++; \
1055 if (test_split (pattern, string, e1, e2, e3, NULL)) \
1056 PASS; \
1057 else \
1058 FAIL; \
1059 } \
1060 }
1061
1062 static gboolean
test_check_replacement(const gchar * string_to_expand,gboolean expected,gboolean expected_refs)1063 test_check_replacement (const gchar *string_to_expand,
1064 gboolean expected,
1065 gboolean expected_refs)
1066 {
1067 gboolean result;
1068 gboolean has_refs;
1069
1070 verbose ("checking replacement string \"%s\" \t", string_to_expand);
1071
1072 result = g_regex_check_replacement (string_to_expand, &has_refs, NULL);
1073 if (expected != result)
1074 {
1075 g_print ("failed \t(got \"%s\", expected \"%s\")\n",
1076 result ? "TRUE" : "FALSE",
1077 expected ? "TRUE" : "FALSE");
1078 return FALSE;
1079 }
1080
1081 if (expected && expected_refs != has_refs)
1082 {
1083 g_print ("failed \t(got has_references \"%s\", expected \"%s\")\n",
1084 has_refs ? "TRUE" : "FALSE",
1085 expected_refs ? "TRUE" : "FALSE");
1086 return FALSE;
1087 }
1088
1089 verbose ("passed\n");
1090 return TRUE;
1091 }
1092
1093 #define TEST_CHECK_REPLACEMENT(string_to_expand, expected, expected_refs) { \
1094 total++; \
1095 if (test_check_replacement (string_to_expand, expected, expected_refs)) \
1096 PASS; \
1097 else \
1098 FAIL; \
1099 }
1100 static gboolean
test_expand(const gchar * pattern,const gchar * string,const gchar * string_to_expand,gboolean raw,const gchar * expected)1101 test_expand (const gchar *pattern,
1102 const gchar *string,
1103 const gchar *string_to_expand,
1104 gboolean raw,
1105 const gchar *expected)
1106 {
1107 GRegex *regex = NULL;
1108 GMatchInfo *match_info = NULL;
1109 gchar *res;
1110
1111 verbose ("expanding the references in \"%s\" (pattern: \"%s\", string: \"%s\") \t",
1112 string_to_expand,
1113 pattern ? pattern : "(null)",
1114 string ? string : "(null)");
1115
1116 if (pattern)
1117 {
1118 regex = g_regex_new (pattern, raw ? G_REGEX_RAW : 0, 0, NULL);
1119 g_regex_match (regex, string, 0, &match_info);
1120 }
1121
1122 res = g_match_info_expand_references (match_info, string_to_expand, NULL);
1123 if (!streq (res, expected))
1124 {
1125 g_print ("failed \t(got \"%s\", expected \"%s\")\n", res, expected);
1126 g_free (res);
1127 g_match_info_free (match_info);
1128 g_regex_unref (regex);
1129 return FALSE;
1130 }
1131
1132 g_free (res);
1133 g_match_info_free (match_info);
1134 if (regex)
1135 g_regex_unref (regex);
1136
1137 verbose ("passed\n");
1138 return TRUE;
1139 }
1140
1141 #define TEST_EXPAND(pattern, string, string_to_expand, raw, expected) { \
1142 total++; \
1143 if (test_expand (pattern, string, string_to_expand, raw, expected)) \
1144 PASS; \
1145 else \
1146 FAIL; \
1147 }
1148
1149 static gboolean
test_replace(const gchar * pattern,const gchar * string,gint start_position,const gchar * replacement,const gchar * expected)1150 test_replace (const gchar *pattern,
1151 const gchar *string,
1152 gint start_position,
1153 const gchar *replacement,
1154 const gchar *expected)
1155 {
1156 GRegex *regex;
1157 gchar *res;
1158
1159 verbose ("replacing \"%s\" in \"%s\" (pattern: \"%s\", start: %d) \t",
1160 replacement, string, pattern, start_position);
1161
1162 regex = g_regex_new (pattern, 0, 0, NULL);
1163 res = g_regex_replace (regex, string, -1, start_position, replacement, 0, NULL);
1164 if (!streq (res, expected))
1165 {
1166 g_print ("failed \t(got \"%s\", expected \"%s\")\n", res, expected);
1167 g_free (res);
1168 g_regex_unref (regex);
1169 return FALSE;
1170 }
1171
1172 g_free (res);
1173 g_regex_unref (regex);
1174
1175 verbose ("passed\n");
1176 return TRUE;
1177 }
1178
1179 #define TEST_REPLACE(pattern, string, start_position, replacement, expected) { \
1180 total++; \
1181 if (test_replace (pattern, string, start_position, replacement, expected)) \
1182 PASS; \
1183 else \
1184 FAIL; \
1185 }
1186
1187 static gboolean
test_replace_lit(const gchar * pattern,const gchar * string,gint start_position,const gchar * replacement,const gchar * expected)1188 test_replace_lit (const gchar *pattern,
1189 const gchar *string,
1190 gint start_position,
1191 const gchar *replacement,
1192 const gchar *expected)
1193 {
1194 GRegex *regex;
1195 gchar *res;
1196
1197 verbose ("replacing literally \"%s\" in \"%s\" (pattern: \"%s\", start: %d) \t",
1198 replacement, string, pattern, start_position);
1199
1200 regex = g_regex_new (pattern, 0, 0, NULL);
1201 res = g_regex_replace_literal (regex, string, -1, start_position,
1202 replacement, 0, NULL);
1203 if (!streq (res, expected))
1204 {
1205 g_print ("failed \t(got \"%s\", expected \"%s\")\n", res, expected);
1206 g_free (res);
1207 g_regex_unref (regex);
1208 return FALSE;
1209 }
1210
1211 g_free (res);
1212 g_regex_unref (regex);
1213
1214 verbose ("passed\n");
1215 return TRUE;
1216 }
1217
1218 #define TEST_REPLACE_LIT(pattern, string, start_position, replacement, expected) { \
1219 total++; \
1220 if (test_replace_lit (pattern, string, start_position, replacement, expected)) \
1221 PASS; \
1222 else \
1223 FAIL; \
1224 }
1225
1226 static gboolean
test_get_string_number(const gchar * pattern,const gchar * name,gint expected_num)1227 test_get_string_number (const gchar *pattern,
1228 const gchar *name,
1229 gint expected_num)
1230 {
1231 GRegex *regex;
1232 gint num;
1233
1234 verbose ("getting the number of \"%s\" (pattern: \"%s\") \t",
1235 name, pattern);
1236
1237 regex = g_regex_new (pattern, 0, 0, NULL);
1238 num = g_regex_get_string_number (regex, name);
1239 g_regex_unref (regex);
1240
1241 if (num != expected_num)
1242 {
1243 g_print ("failed \t(got %d, expected %d)\n", num, expected_num);
1244 return FALSE;
1245 }
1246 else
1247 {
1248 verbose ("passed\n");
1249 return TRUE;
1250 }
1251 }
1252
1253 #define TEST_GET_STRING_NUMBER(pattern, name, expected_num) { \
1254 total++; \
1255 if (test_get_string_number (pattern, name, expected_num)) \
1256 PASS; \
1257 else \
1258 FAIL; \
1259 }
1260
1261 static gboolean
test_escape(const gchar * string,gint length,const gchar * expected)1262 test_escape (const gchar *string,
1263 gint length,
1264 const gchar *expected)
1265 {
1266 gchar *escaped;
1267
1268 verbose ("escaping \"%s\" (len: %d) \t", string, length);
1269
1270 escaped = g_regex_escape_string (string, length);
1271
1272 if (!streq (escaped, expected))
1273 {
1274 g_print ("failed \t(got \"%s\", expected \"%s\")\n", escaped, expected);
1275 g_free (escaped);
1276 return FALSE;
1277 }
1278
1279 g_free (escaped);
1280
1281 verbose ("passed\n");
1282 return TRUE;
1283 }
1284
1285 #define TEST_ESCAPE(string, length, expected) { \
1286 total++; \
1287 if (test_escape (string, length, expected)) \
1288 PASS; \
1289 else \
1290 FAIL; \
1291 }
1292
1293 static gboolean
test_match_all_full(const gchar * pattern,const gchar * string,gssize string_len,gint start_position,...)1294 test_match_all_full (const gchar *pattern,
1295 const gchar *string,
1296 gssize string_len,
1297 gint start_position,
1298 ...)
1299 {
1300 GRegex *regex;
1301 GMatchInfo *match_info;
1302 va_list args;
1303 GSList *expected = NULL;
1304 GSList *l_exp;
1305 gboolean match_ok;
1306 gboolean ret = TRUE;
1307 gint match_count;
1308 gint i;
1309
1310 verbose ("matching all in \"%s\" against \"%s\" (start: %d, len: %d) \t",
1311 string, pattern, start_position, string_len);
1312
1313 /* The va_list is a NULL-terminated sequence of: extected matched string,
1314 * expected start and expected end. */
1315 va_start (args, start_position);
1316 while (TRUE)
1317 {
1318 Match *match;
1319 const gchar *expected_string = va_arg (args, const gchar *);
1320 if (expected_string == NULL)
1321 break;
1322 match = g_new0 (Match, 1);
1323 match->string = g_strdup (expected_string);
1324 match->start = va_arg (args, gint);
1325 match->end = va_arg (args, gint);
1326 expected = g_slist_prepend (expected, match);
1327 }
1328 expected = g_slist_reverse (expected);
1329 va_end (args);
1330
1331 regex = g_regex_new (pattern, 0, 0, NULL);
1332 match_ok = g_regex_match_all_full (regex, string, string_len, start_position,
1333 0, &match_info, NULL);
1334
1335 if (match_ok && g_slist_length (expected) == 0)
1336 {
1337 g_print ("failed\n");
1338 ret = FALSE;
1339 goto exit;
1340 }
1341 if (!match_ok && g_slist_length (expected) != 0)
1342 {
1343 g_print ("failed\n");
1344 ret = FALSE;
1345 goto exit;
1346 }
1347
1348 match_count = g_match_info_get_match_count (match_info);
1349 if (match_count != g_slist_length (expected))
1350 {
1351 g_print ("failed \t(got %d %s, expected %d)\n", match_count,
1352 match_count == 1 ? "match" : "matches",
1353 g_slist_length (expected));
1354 ret = FALSE;
1355 goto exit;
1356 }
1357
1358 l_exp = expected;
1359 for (i = 0; i < match_count; i++)
1360 {
1361 gint start, end;
1362 gchar *matched_string;
1363 Match *exp = l_exp->data;
1364
1365 matched_string = g_match_info_fetch (match_info, i);
1366 g_match_info_fetch_pos (match_info, i, &start, &end);
1367
1368 if (!streq(exp->string, matched_string))
1369 {
1370 g_print ("failed \t(got \"%s\", expected \"%s\")\n",
1371 matched_string, exp->string);
1372 g_free (matched_string);
1373 ret = FALSE;
1374 goto exit;
1375 }
1376 g_free (matched_string);
1377
1378 if (exp->start != start || exp->end != end)
1379 {
1380 g_print ("failed \t(got [%d, %d], expected [%d, %d])\n",
1381 start, end, exp->start, exp->end);
1382 ret = FALSE;
1383 goto exit;
1384 }
1385
1386 l_exp = g_slist_next (l_exp);
1387 }
1388
1389 exit:
1390 if (ret)
1391 {
1392 verbose ("passed (%d %s)\n", match_count, match_count == 1 ? "match" : "matches");
1393 }
1394
1395 g_match_info_free (match_info);
1396 g_regex_unref (regex);
1397 g_slist_foreach (expected, free_match, NULL);
1398 g_slist_free (expected);
1399
1400 return ret;
1401 }
1402
1403 static gboolean
test_match_all(const gchar * pattern,const gchar * string,...)1404 test_match_all (const gchar *pattern,
1405 const gchar *string,
1406 ...)
1407 {
1408 GRegex *regex;
1409 GMatchInfo *match_info;
1410 va_list args;
1411 GSList *expected = NULL;
1412 GSList *l_exp;
1413 gboolean match_ok;
1414 gboolean ret = TRUE;
1415 gint match_count;
1416 gint i;
1417
1418 verbose ("matching all in \"%s\" against \"%s\" \t", string, pattern);
1419
1420 /* The va_list is a NULL-terminated sequence of: extected matched string,
1421 * expected start and expected end. */
1422 va_start (args, string);
1423 while (TRUE)
1424 {
1425 Match *match;
1426 const gchar *expected_string = va_arg (args, const gchar *);
1427 if (expected_string == NULL)
1428 break;
1429 match = g_new0 (Match, 1);
1430 match->string = g_strdup (expected_string);
1431 match->start = va_arg (args, gint);
1432 match->end = va_arg (args, gint);
1433 expected = g_slist_prepend (expected, match);
1434 }
1435 expected = g_slist_reverse (expected);
1436 va_end (args);
1437
1438 regex = g_regex_new (pattern, 0, 0, NULL);
1439 match_ok = g_regex_match_all (regex, string, 0, &match_info);
1440
1441 if (match_ok && g_slist_length (expected) == 0)
1442 {
1443 g_print ("failed\n");
1444 ret = FALSE;
1445 goto exit;
1446 }
1447 if (!match_ok && g_slist_length (expected) != 0)
1448 {
1449 g_print ("failed\n");
1450 ret = FALSE;
1451 goto exit;
1452 }
1453
1454 match_count = g_match_info_get_match_count (match_info);
1455 if (match_count != g_slist_length (expected))
1456 {
1457 g_print ("failed \t(got %d %s, expected %d)\n", match_count,
1458 match_count == 1 ? "match" : "matches",
1459 g_slist_length (expected));
1460 ret = FALSE;
1461 goto exit;
1462 }
1463
1464 l_exp = expected;
1465 for (i = 0; i < match_count; i++)
1466 {
1467 gint start, end;
1468 gchar *matched_string;
1469 Match *exp = l_exp->data;
1470
1471 matched_string = g_match_info_fetch (match_info, i);
1472 g_match_info_fetch_pos (match_info, i, &start, &end);
1473
1474 if (!streq(exp->string, matched_string))
1475 {
1476 g_print ("failed \t(got \"%s\", expected \"%s\")\n",
1477 matched_string, exp->string);
1478 g_free (matched_string);
1479 ret = FALSE;
1480 goto exit;
1481 }
1482 g_free (matched_string);
1483
1484 if (exp->start != start || exp->end != end)
1485 {
1486 g_print ("failed \t(got [%d, %d], expected [%d, %d])\n",
1487 start, end, exp->start, exp->end);
1488 ret = FALSE;
1489 goto exit;
1490 }
1491
1492 l_exp = g_slist_next (l_exp);
1493 }
1494
1495 exit:
1496 if (ret)
1497 {
1498 verbose ("passed (%d %s)\n", match_count, match_count == 1 ? "match" : "matches");
1499 }
1500
1501 g_match_info_free (match_info);
1502 g_regex_unref (regex);
1503 g_slist_foreach (expected, free_match, NULL);
1504 g_slist_free (expected);
1505
1506 return ret;
1507 }
1508
1509 #define TEST_MATCH_ALL0(pattern, string, string_len, start_position) { \
1510 total++; \
1511 if (test_match_all_full (pattern, string, string_len, start_position, NULL)) \
1512 PASS; \
1513 else \
1514 FAIL; \
1515 if (string_len == -1 && start_position == 0) \
1516 { \
1517 total++; \
1518 if (test_match_all (pattern, string, NULL)) \
1519 PASS; \
1520 else \
1521 FAIL; \
1522 } \
1523 }
1524
1525 #define TEST_MATCH_ALL1(pattern, string, string_len, start_position, \
1526 t1, s1, e1) { \
1527 total++; \
1528 if (test_match_all_full (pattern, string, string_len, start_position, \
1529 t1, s1, e1, NULL)) \
1530 PASS; \
1531 else \
1532 FAIL; \
1533 if (string_len == -1 && start_position == 0) \
1534 { \
1535 total++; \
1536 if (test_match_all (pattern, string, t1, s1, e1, NULL)) \
1537 PASS; \
1538 else \
1539 FAIL; \
1540 } \
1541 }
1542
1543 #define TEST_MATCH_ALL2(pattern, string, string_len, start_position, \
1544 t1, s1, e1, t2, s2, e2) { \
1545 total++; \
1546 if (test_match_all_full (pattern, string, string_len, start_position, \
1547 t1, s1, e1, t2, s2, e2, NULL)) \
1548 PASS; \
1549 else \
1550 FAIL; \
1551 if (string_len == -1 && start_position == 0) \
1552 { \
1553 total++; \
1554 if (test_match_all (pattern, string, t1, s1, e1, t2, s2, e2, NULL)) \
1555 PASS; \
1556 else \
1557 FAIL; \
1558 } \
1559 }
1560
1561 #define TEST_MATCH_ALL3(pattern, string, string_len, start_position, \
1562 t1, s1, e1, t2, s2, e2, t3, s3, e3) { \
1563 total++; \
1564 if (test_match_all_full (pattern, string, string_len, start_position, \
1565 t1, s1, e1, t2, s2, e2, t3, s3, e3, NULL)) \
1566 PASS; \
1567 else \
1568 FAIL; \
1569 if (string_len == -1 && start_position == 0) \
1570 { \
1571 total++; \
1572 if (test_match_all (pattern, string, t1, s1, e1, t2, s2, e2, t3, s3, e3, NULL)) \
1573 PASS; \
1574 else \
1575 FAIL; \
1576 } \
1577 }
1578
1579 int
main(int argc,char * argv[])1580 main (int argc, char *argv[])
1581 {
1582 gint total = 0;
1583 gint passed = 0;
1584 gint failed = 0;
1585 gint i = 0;
1586
1587 setlocale (LC_ALL, "");
1588
1589 for (i = 1; i < argc; i++)
1590 {
1591 if (streq ("--noisy", argv[i]))
1592 noisy = TRUE;
1593 else if (streq ("--abort", argv[i]))
1594 abort_on_fail = TRUE;
1595 }
1596
1597 g_setenv ("G_DEBUG", "fatal_warnings", TRUE);
1598
1599 /* TEST_NEW(pattern, compile_opts, match_opts) */
1600 TEST_NEW("", 0, 0);
1601 TEST_NEW(".*", 0, 0);
1602 TEST_NEW(".*", G_REGEX_OPTIMIZE, 0);
1603 TEST_NEW(".*", G_REGEX_MULTILINE, 0);
1604 TEST_NEW(".*", G_REGEX_DOTALL, 0);
1605 TEST_NEW(".*", G_REGEX_DOTALL, G_REGEX_MATCH_NOTBOL);
1606 TEST_NEW("(123\\d*)[a-zA-Z]+(?P<hello>.*)", 0, 0);
1607 TEST_NEW("(123\\d*)[a-zA-Z]+(?P<hello>.*)", G_REGEX_CASELESS, 0);
1608 TEST_NEW("(123\\d*)[a-zA-Z]+(?P<hello>.*)", G_REGEX_CASELESS | G_REGEX_OPTIMIZE, 0);
1609 TEST_NEW("(?P<A>x)|(?P<A>y)", G_REGEX_DUPNAMES, 0);
1610 TEST_NEW("(?P<A>x)|(?P<A>y)", G_REGEX_DUPNAMES | G_REGEX_OPTIMIZE, 0);
1611 /* This gives "internal error: code overflow" with pcre 6.0 */
1612 TEST_NEW("(?i)(?-i)", 0, 0);
1613
1614 /* TEST_NEW_FAIL(pattern, compile_opts, expected_error) */
1615 TEST_NEW_FAIL("(", 0, G_REGEX_ERROR_UNMATCHED_PARENTHESIS);
1616 TEST_NEW_FAIL(")", 0, G_REGEX_ERROR_UNMATCHED_PARENTHESIS);
1617 TEST_NEW_FAIL("[", 0, G_REGEX_ERROR_UNTERMINATED_CHARACTER_CLASS);
1618 TEST_NEW_FAIL("*", 0, G_REGEX_ERROR_NOTHING_TO_REPEAT);
1619 TEST_NEW_FAIL("?", 0, G_REGEX_ERROR_NOTHING_TO_REPEAT);
1620 TEST_NEW_FAIL("(?P<A>x)|(?P<A>y)", 0, G_REGEX_ERROR_DUPLICATE_SUBPATTERN_NAME);
1621
1622 /* TEST_MATCH_SIMPLE(pattern, string, compile_opts, match_opts, expected) */
1623 TEST_MATCH_SIMPLE("a", "", 0, 0, FALSE);
1624 TEST_MATCH_SIMPLE("a", "a", 0, 0, TRUE);
1625 TEST_MATCH_SIMPLE("a", "ba", 0, 0, TRUE);
1626 TEST_MATCH_SIMPLE("^a", "ba", 0, 0, FALSE);
1627 TEST_MATCH_SIMPLE("a", "ba", G_REGEX_ANCHORED, 0, FALSE);
1628 TEST_MATCH_SIMPLE("a", "ba", 0, G_REGEX_MATCH_ANCHORED, FALSE);
1629 TEST_MATCH_SIMPLE("a", "ab", G_REGEX_ANCHORED, 0, TRUE);
1630 TEST_MATCH_SIMPLE("a", "ab", 0, G_REGEX_MATCH_ANCHORED, TRUE);
1631 TEST_MATCH_SIMPLE("a", "a", G_REGEX_CASELESS, 0, TRUE);
1632 TEST_MATCH_SIMPLE("a", "A", G_REGEX_CASELESS, 0, TRUE);
1633 /* These are needed to test extended properties. */
1634 TEST_MATCH_SIMPLE(AGRAVE, AGRAVE, G_REGEX_CASELESS, 0, TRUE);
1635 TEST_MATCH_SIMPLE(AGRAVE, AGRAVE_UPPER, G_REGEX_CASELESS, 0, TRUE);
1636 TEST_MATCH_SIMPLE("\\p{L}", "a", 0, 0, TRUE);
1637 TEST_MATCH_SIMPLE("\\p{L}", "1", 0, 0, FALSE);
1638 TEST_MATCH_SIMPLE("\\p{L}", AGRAVE, 0, 0, TRUE);
1639 TEST_MATCH_SIMPLE("\\p{L}", AGRAVE_UPPER, 0, 0, TRUE);
1640 TEST_MATCH_SIMPLE("\\p{L}", SHEEN, 0, 0, TRUE);
1641 TEST_MATCH_SIMPLE("\\p{L}", ETH30, 0, 0, FALSE);
1642 TEST_MATCH_SIMPLE("\\p{Ll}", "a", 0, 0, TRUE);
1643 TEST_MATCH_SIMPLE("\\p{Ll}", AGRAVE, 0, 0, TRUE);
1644 TEST_MATCH_SIMPLE("\\p{Ll}", AGRAVE_UPPER, 0, 0, FALSE);
1645 TEST_MATCH_SIMPLE("\\p{Ll}", ETH30, 0, 0, FALSE);
1646 TEST_MATCH_SIMPLE("\\p{Sc}", AGRAVE, 0, 0, FALSE);
1647 TEST_MATCH_SIMPLE("\\p{Sc}", EURO, 0, 0, TRUE);
1648 TEST_MATCH_SIMPLE("\\p{Sc}", ETH30, 0, 0, FALSE);
1649 TEST_MATCH_SIMPLE("\\p{N}", "a", 0, 0, FALSE);
1650 TEST_MATCH_SIMPLE("\\p{N}", "1", 0, 0, TRUE);
1651 TEST_MATCH_SIMPLE("\\p{N}", AGRAVE, 0, 0, FALSE);
1652 TEST_MATCH_SIMPLE("\\p{N}", AGRAVE_UPPER, 0, 0, FALSE);
1653 TEST_MATCH_SIMPLE("\\p{N}", SHEEN, 0, 0, FALSE);
1654 TEST_MATCH_SIMPLE("\\p{N}", ETH30, 0, 0, TRUE);
1655 TEST_MATCH_SIMPLE("\\p{Nd}", "a", 0, 0, FALSE);
1656 TEST_MATCH_SIMPLE("\\p{Nd}", "1", 0, 0, TRUE);
1657 TEST_MATCH_SIMPLE("\\p{Nd}", AGRAVE, 0, 0, FALSE);
1658 TEST_MATCH_SIMPLE("\\p{Nd}", AGRAVE_UPPER, 0, 0, FALSE);
1659 TEST_MATCH_SIMPLE("\\p{Nd}", SHEEN, 0, 0, FALSE);
1660 TEST_MATCH_SIMPLE("\\p{Nd}", ETH30, 0, 0, FALSE);
1661 TEST_MATCH_SIMPLE("\\p{Common}", SHEEN, 0, 0, FALSE);
1662 TEST_MATCH_SIMPLE("\\p{Common}", "a", 0, 0, FALSE);
1663 TEST_MATCH_SIMPLE("\\p{Common}", AGRAVE, 0, 0, FALSE);
1664 TEST_MATCH_SIMPLE("\\p{Common}", AGRAVE_UPPER, 0, 0, FALSE);
1665 TEST_MATCH_SIMPLE("\\p{Common}", ETH30, 0, 0, FALSE);
1666 TEST_MATCH_SIMPLE("\\p{Common}", "%", 0, 0, TRUE);
1667 TEST_MATCH_SIMPLE("\\p{Common}", "1", 0, 0, TRUE);
1668 TEST_MATCH_SIMPLE("\\p{Arabic}", SHEEN, 0, 0, TRUE);
1669 TEST_MATCH_SIMPLE("\\p{Arabic}", "a", 0, 0, FALSE);
1670 TEST_MATCH_SIMPLE("\\p{Arabic}", AGRAVE, 0, 0, FALSE);
1671 TEST_MATCH_SIMPLE("\\p{Arabic}", AGRAVE_UPPER, 0, 0, FALSE);
1672 TEST_MATCH_SIMPLE("\\p{Arabic}", ETH30, 0, 0, FALSE);
1673 TEST_MATCH_SIMPLE("\\p{Arabic}", "%", 0, 0, FALSE);
1674 TEST_MATCH_SIMPLE("\\p{Arabic}", "1", 0, 0, FALSE);
1675 TEST_MATCH_SIMPLE("\\p{Latin}", SHEEN, 0, 0, FALSE);
1676 TEST_MATCH_SIMPLE("\\p{Latin}", "a", 0, 0, TRUE);
1677 TEST_MATCH_SIMPLE("\\p{Latin}", AGRAVE, 0, 0, TRUE);
1678 TEST_MATCH_SIMPLE("\\p{Latin}", AGRAVE_UPPER, 0, 0, TRUE);
1679 TEST_MATCH_SIMPLE("\\p{Latin}", ETH30, 0, 0, FALSE);
1680 TEST_MATCH_SIMPLE("\\p{Latin}", "%", 0, 0, FALSE);
1681 TEST_MATCH_SIMPLE("\\p{Latin}", "1", 0, 0, FALSE);
1682 TEST_MATCH_SIMPLE("\\p{Ethiopic}", SHEEN, 0, 0, FALSE);
1683 TEST_MATCH_SIMPLE("\\p{Ethiopic}", "a", 0, 0, FALSE);
1684 TEST_MATCH_SIMPLE("\\p{Ethiopic}", AGRAVE, 0, 0, FALSE);
1685 TEST_MATCH_SIMPLE("\\p{Ethiopic}", AGRAVE_UPPER, 0, 0, FALSE);
1686 TEST_MATCH_SIMPLE("\\p{Ethiopic}", ETH30, 0, 0, TRUE);
1687 TEST_MATCH_SIMPLE("\\p{Ethiopic}", "%", 0, 0, FALSE);
1688 TEST_MATCH_SIMPLE("\\p{Ethiopic}", "1", 0, 0, FALSE);
1689 TEST_MATCH_SIMPLE("\\p{L}(?<=\\p{Arabic})", SHEEN, 0, 0, TRUE);
1690 TEST_MATCH_SIMPLE("\\p{L}(?<=\\p{Latin})", SHEEN, 0, 0, FALSE);
1691 /* Invalid patterns. */
1692 TEST_MATCH_SIMPLE("\\", "a", 0, 0, FALSE);
1693 TEST_MATCH_SIMPLE("[", "", 0, 0, FALSE);
1694
1695 /* TEST_MATCH(pattern, compile_opts, match_opts, string,
1696 * string_len, start_position, match_opts2, expected) */
1697 TEST_MATCH("a", 0, 0, "a", -1, 0, 0, TRUE);
1698 TEST_MATCH("a", 0, 0, "A", -1, 0, 0, FALSE);
1699 TEST_MATCH("a", G_REGEX_CASELESS, 0, "A", -1, 0, 0, TRUE);
1700 TEST_MATCH("a", 0, 0, "ab", -1, 1, 0, FALSE);
1701 TEST_MATCH("a", 0, 0, "ba", 1, 0, 0, FALSE);
1702 TEST_MATCH("a", 0, 0, "bab", -1, 0, 0, TRUE);
1703 TEST_MATCH("a", 0, 0, "b", -1, 0, 0, FALSE);
1704 TEST_MATCH("a", 0, G_REGEX_ANCHORED, "a", -1, 0, 0, TRUE);
1705 TEST_MATCH("a", 0, G_REGEX_ANCHORED, "ab", -1, 1, 0, FALSE);
1706 TEST_MATCH("a", 0, G_REGEX_ANCHORED, "ba", 1, 0, 0, FALSE);
1707 TEST_MATCH("a", 0, G_REGEX_ANCHORED, "bab", -1, 0, 0, FALSE);
1708 TEST_MATCH("a", 0, G_REGEX_ANCHORED, "b", -1, 0, 0, FALSE);
1709 TEST_MATCH("a", 0, 0, "a", -1, 0, G_REGEX_ANCHORED, TRUE);
1710 TEST_MATCH("a", 0, 0, "ab", -1, 1, G_REGEX_ANCHORED, FALSE);
1711 TEST_MATCH("a", 0, 0, "ba", 1, 0, G_REGEX_ANCHORED, FALSE);
1712 TEST_MATCH("a", 0, 0, "bab", -1, 0, G_REGEX_ANCHORED, FALSE);
1713 TEST_MATCH("a", 0, 0, "b", -1, 0, G_REGEX_ANCHORED, FALSE);
1714 TEST_MATCH("a|b", 0, 0, "a", -1, 0, 0, TRUE);
1715 TEST_MATCH("\\d", 0, 0, EURO, -1, 0, 0, FALSE);
1716 TEST_MATCH("^.$", 0, 0, EURO, -1, 0, 0, TRUE);
1717 TEST_MATCH("^.{3}$", 0, 0, EURO, -1, 0, 0, FALSE);
1718 TEST_MATCH("^.$", G_REGEX_RAW, 0, EURO, -1, 0, 0, FALSE);
1719 TEST_MATCH("^.{3}$", G_REGEX_RAW, 0, EURO, -1, 0, 0, TRUE);
1720 TEST_MATCH(AGRAVE, G_REGEX_CASELESS, 0, AGRAVE_UPPER, -1, 0, 0, TRUE);
1721
1722 /* New lines handling. */
1723 TEST_MATCH("^a\\Rb$", 0, 0, "a\r\nb", -1, 0, 0, TRUE);
1724 TEST_MATCH("^a\\Rb$", 0, 0, "a\nb", -1, 0, 0, TRUE);
1725 TEST_MATCH("^a\\Rb$", 0, 0, "a\rb", -1, 0, 0, TRUE);
1726 TEST_MATCH("^a\\Rb$", 0, 0, "a\n\rb", -1, 0, 0, FALSE);
1727 TEST_MATCH("^a\\R\\Rb$", 0, 0, "a\n\rb", -1, 0, 0, TRUE);
1728 TEST_MATCH("^a\\nb$", 0, 0, "a\r\nb", -1, 0, 0, FALSE);
1729 TEST_MATCH("^a\\r\\nb$", 0, 0, "a\r\nb", -1, 0, 0, TRUE);
1730
1731 TEST_MATCH("^b$", 0, 0, "a\nb\nc", -1, 0, 0, FALSE);
1732 TEST_MATCH("^b$", G_REGEX_MULTILINE, 0, "a\nb\nc", -1, 0, 0, TRUE);
1733 TEST_MATCH("^b$", G_REGEX_MULTILINE, 0, "a\r\nb\r\nc", -1, 0, 0, TRUE);
1734 TEST_MATCH("^b$", G_REGEX_MULTILINE, 0, "a\rb\rc", -1, 0, 0, TRUE);
1735 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, 0, "a\nb\nc", -1, 0, 0, FALSE);
1736 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_LF, 0, "a\nb\nc", -1, 0, 0, TRUE);
1737 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CRLF, 0, "a\nb\nc", -1, 0, 0, FALSE);
1738 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, 0, "a\r\nb\r\nc", -1, 0, 0, FALSE);
1739 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_LF, 0, "a\r\nb\r\nc", -1, 0, 0, FALSE);
1740 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CRLF, 0, "a\r\nb\r\nc", -1, 0, 0, TRUE);
1741 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, 0, "a\rb\rc", -1, 0, 0, TRUE);
1742 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_LF, 0, "a\rb\rc", -1, 0, 0, FALSE);
1743 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CRLF, 0, "a\rb\rc", -1, 0, 0, FALSE);
1744 TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_CR, "a\nb\nc", -1, 0, 0, FALSE);
1745 TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_LF, "a\nb\nc", -1, 0, 0, TRUE);
1746 TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_CRLF, "a\nb\nc", -1, 0, 0, FALSE);
1747 TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_CR, "a\r\nb\r\nc", -1, 0, 0, FALSE);
1748 TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_LF, "a\r\nb\r\nc", -1, 0, 0, FALSE);
1749 TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_CRLF, "a\r\nb\r\nc", -1, 0, 0, TRUE);
1750 TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_CR, "a\rb\rc", -1, 0, 0, TRUE);
1751 TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_LF, "a\rb\rc", -1, 0, 0, FALSE);
1752 TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_CRLF, "a\rb\rc", -1, 0, 0, FALSE);
1753
1754 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_ANY, "a\nb\nc", -1, 0, 0, TRUE);
1755 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_ANY, "a\rb\rc", -1, 0, 0, TRUE);
1756 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_ANY, "a\r\nb\r\nc", -1, 0, 0, TRUE);
1757 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_LF, "a\nb\nc", -1, 0, 0, TRUE);
1758 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_LF, "a\rb\rc", -1, 0, 0, FALSE);
1759 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_CRLF, "a\r\nb\r\nc", -1, 0, 0, TRUE);
1760 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_CRLF, "a\rb\rc", -1, 0, 0, FALSE);
1761
1762 TEST_MATCH("a#\nb", G_REGEX_EXTENDED, 0, "a", -1, 0, 0, FALSE);
1763 TEST_MATCH("a#\r\nb", G_REGEX_EXTENDED, 0, "a", -1, 0, 0, FALSE);
1764 TEST_MATCH("a#\rb", G_REGEX_EXTENDED, 0, "a", -1, 0, 0, FALSE);
1765 TEST_MATCH("a#\nb", G_REGEX_EXTENDED, G_REGEX_MATCH_NEWLINE_CR, "a", -1, 0, 0, FALSE);
1766 TEST_MATCH("a#\nb", G_REGEX_EXTENDED | G_REGEX_NEWLINE_CR, 0, "a", -1, 0, 0, TRUE);
1767
1768 /* TEST_MATCH_NEXT#(pattern, string, string_len, start_position, ...) */
1769 TEST_MATCH_NEXT0("a", "x", -1, 0);
1770 TEST_MATCH_NEXT0("a", "ax", -1, 1);
1771 TEST_MATCH_NEXT0("a", "xa", 1, 0);
1772 TEST_MATCH_NEXT0("a", "axa", 1, 2);
1773 TEST_MATCH_NEXT1("a", "a", -1, 0, "a", 0, 1);
1774 TEST_MATCH_NEXT1("a", "xax", -1, 0, "a", 1, 2);
1775 TEST_MATCH_NEXT1(EURO, ENG EURO, -1, 0, EURO, 2, 5);
1776 TEST_MATCH_NEXT1("a*", "", -1, 0, "", 0, 0);
1777 TEST_MATCH_NEXT2("a*", "aa", -1, 0, "aa", 0, 2, "", 2, 2);
1778 TEST_MATCH_NEXT2(EURO "*", EURO EURO, -1, 0, EURO EURO, 0, 6, "", 6, 6);
1779 TEST_MATCH_NEXT2("a", "axa", -1, 0, "a", 0, 1, "a", 2, 3);
1780 TEST_MATCH_NEXT2("a+", "aaxa", -1, 0, "aa", 0, 2, "a", 3, 4);
1781 TEST_MATCH_NEXT2("a", "aa", -1, 0, "a", 0, 1, "a", 1, 2);
1782 TEST_MATCH_NEXT2("a", "ababa", -1, 2, "a", 2, 3, "a", 4, 5);
1783 TEST_MATCH_NEXT2(EURO "+", EURO "-" EURO, -1, 0, EURO, 0, 3, EURO, 4, 7);
1784 TEST_MATCH_NEXT3("", "ab", -1, 0, "", 0, 0, "", 1, 1, "", 2, 2);
1785 TEST_MATCH_NEXT3("", AGRAVE "b", -1, 0, "", 0, 0, "", 2, 2, "", 3, 3);
1786 TEST_MATCH_NEXT3("a", "aaxa", -1, 0, "a", 0, 1, "a", 1, 2, "a", 3, 4);
1787 TEST_MATCH_NEXT3("a", "aa" OGRAVE "a", -1, 0, "a", 0, 1, "a", 1, 2, "a", 4, 5);
1788 TEST_MATCH_NEXT3("a*", "aax", -1, 0, "aa", 0, 2, "", 2, 2, "", 3, 3);
1789 TEST_MATCH_NEXT3("(?=[A-Z0-9])", "RegExTest", -1, 0, "", 0, 0, "", 3, 3, "", 5, 5);
1790 TEST_MATCH_NEXT4("a*", "aaxa", -1, 0, "aa", 0, 2, "", 2, 2, "a", 3, 4, "", 4, 4);
1791
1792 /* TEST_MATCH_COUNT(pattern, string, start_position, match_opts, expected_count) */
1793 TEST_MATCH_COUNT("a", "", 0, 0, 0);
1794 TEST_MATCH_COUNT("a", "a", 0, 0, 1);
1795 TEST_MATCH_COUNT("a", "a", 1, 0, 0);
1796 TEST_MATCH_COUNT("(.)", "a", 0, 0, 2);
1797 TEST_MATCH_COUNT("(.)", EURO, 0, 0, 2);
1798 TEST_MATCH_COUNT("(?:.)", "a", 0, 0, 1);
1799 TEST_MATCH_COUNT("(?P<A>.)", "a", 0, 0, 2);
1800 TEST_MATCH_COUNT("a$", "a", 0, G_REGEX_MATCH_NOTEOL, 0);
1801 TEST_MATCH_COUNT("(a)?(b)", "b", 0, 0, 3);
1802 TEST_MATCH_COUNT("(a)?(b)", "ab", 0, 0, 3);
1803
1804 /* TEST_PARTIAL(pattern, string, expected) */
1805 TEST_PARTIAL("^ab", "a", TRUE);
1806 TEST_PARTIAL("^ab", "xa", FALSE);
1807 TEST_PARTIAL("ab", "xa", TRUE);
1808 TEST_PARTIAL("ab", "ab", FALSE); /* normal match. */
1809 TEST_PARTIAL("a+b", "aa", FALSE); /* PCRE_ERROR_BAD_PARTIAL */
1810 TEST_PARTIAL("(a)+b", "aa", TRUE);
1811 TEST_PARTIAL("a?b", "a", TRUE);
1812
1813 /* TEST_SUB_PATTERN(pattern, string, start_position, sub_n, expected_sub,
1814 * expected_start, expected_end) */
1815 TEST_SUB_PATTERN("a", "a", 0, 0, "a", 0, 1);
1816 TEST_SUB_PATTERN("a(.)", "ab", 0, 1, "b", 1, 2);
1817 TEST_SUB_PATTERN("a(.)", "a" EURO, 0, 1, EURO, 1, 4);
1818 TEST_SUB_PATTERN("(?:.*)(a)(.)", "xxa" ENG, 0, 2, ENG, 3, 5);
1819 TEST_SUB_PATTERN("(" HSTROKE ")", "a" HSTROKE ENG, 0, 1, HSTROKE, 1, 3);
1820 TEST_SUB_PATTERN("a", "a", 0, 1, NULL, UNTOUCHED, UNTOUCHED);
1821 TEST_SUB_PATTERN("a", "a", 0, 1, NULL, UNTOUCHED, UNTOUCHED);
1822 TEST_SUB_PATTERN("(a)?(b)", "b", 0, 0, "b", 0, 1);
1823 TEST_SUB_PATTERN("(a)?(b)", "b", 0, 1, "", -1, -1);
1824 TEST_SUB_PATTERN("(a)?(b)", "b", 0, 2, "b", 0, 1);
1825
1826 /* TEST_NAMED_SUB_PATTERN(pattern, string, start_position, sub_name,
1827 * expected_sub, expected_start, expected_end) */
1828 TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?", "ab", 0, "A", "b", 1, 2);
1829 TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?", "aab", 1, "A", "b", 2, 3);
1830 TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?", EURO "ab", 0, "A", "b", 4, 5);
1831 TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?", EURO "ab", 0, "B", NULL, UNTOUCHED, UNTOUCHED);
1832 TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?", EURO "ab", 0, "C", NULL, UNTOUCHED, UNTOUCHED);
1833 TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?", "a" EGRAVE "x", 0, "A", EGRAVE, 1, 3);
1834 TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?", "a" EGRAVE "x", 0, "B", "x", 3, 4);
1835 TEST_NAMED_SUB_PATTERN("(?P<A>a)?(?P<B>b)", "b", 0, "A", "", -1, -1);
1836 TEST_NAMED_SUB_PATTERN("(?P<A>a)?(?P<B>b)", "b", 0, "B", "b", 0, 1);
1837
1838 /* TEST_NAMED_SUB_PATTERN_DUPNAMES(pattern, string, start_position, sub_name,
1839 * expected_sub, expected_start, expected_end) */
1840 TEST_NAMED_SUB_PATTERN_DUPNAMES("(?P<N>a)|(?P<N>b)", "ab", 0, "N", "a", 0, 1);
1841 TEST_NAMED_SUB_PATTERN_DUPNAMES("(?P<N>aa)|(?P<N>a)", "aa", 0, "N", "aa", 0, 2);
1842 TEST_NAMED_SUB_PATTERN_DUPNAMES("(?P<N>aa)(?P<N>a)", "aaa", 0, "N", "aa", 0, 2);
1843 TEST_NAMED_SUB_PATTERN_DUPNAMES("(?P<N>x)|(?P<N>a)", "a", 0, "N", "a", 0, 1);
1844 TEST_NAMED_SUB_PATTERN_DUPNAMES("(?P<N>x)y|(?P<N>a)b", "ab", 0, "N", "a", 0, 1);
1845
1846 /* DUPNAMES option inside the pattern */
1847 TEST_NAMED_SUB_PATTERN("(?J)(?P<N>a)|(?P<N>b)", "ab", 0, "N", "a", 0, 1);
1848 TEST_NAMED_SUB_PATTERN("(?J)(?P<N>aa)|(?P<N>a)", "aa", 0, "N", "aa", 0, 2);
1849 TEST_NAMED_SUB_PATTERN("(?J)(?P<N>aa)(?P<N>a)", "aaa", 0, "N", "aa", 0, 2);
1850 TEST_NAMED_SUB_PATTERN("(?J)(?P<N>x)|(?P<N>a)", "a", 0, "N", "a", 0, 1);
1851 TEST_NAMED_SUB_PATTERN("(?J)(?P<N>x)y|(?P<N>a)b", "ab", 0, "N", "a", 0, 1);
1852
1853 /* TEST_FETCH_ALL#(pattern, string, ...) */
1854 TEST_FETCH_ALL0("a", "");
1855 TEST_FETCH_ALL0("a", "b");
1856 TEST_FETCH_ALL1("a", "a", "a");
1857 TEST_FETCH_ALL1("a+", "aa", "aa");
1858 TEST_FETCH_ALL1("(?:a)", "a", "a");
1859 TEST_FETCH_ALL2("(a)", "a", "a", "a");
1860 TEST_FETCH_ALL2("a(.)", "ab", "ab", "b");
1861 TEST_FETCH_ALL2("a(.)", "a" HSTROKE, "a" HSTROKE, HSTROKE);
1862 TEST_FETCH_ALL3("(?:.*)(a)(.)", "xyazk", "xyaz", "a", "z");
1863 TEST_FETCH_ALL3("(?P<A>.)(a)", "xa", "xa", "x", "a");
1864 TEST_FETCH_ALL3("(?P<A>.)(a)", ENG "a", ENG "a", ENG, "a");
1865 TEST_FETCH_ALL3("(a)?(b)", "b", "b", "", "b");
1866 TEST_FETCH_ALL3("(a)?(b)", "ab", "ab", "a", "b");
1867
1868 /* TEST_SPLIT_SIMPLE#(pattern, string, ...) */
1869 TEST_SPLIT_SIMPLE0("", "");
1870 TEST_SPLIT_SIMPLE0("a", "");
1871 TEST_SPLIT_SIMPLE1(",", "a", "a");
1872 TEST_SPLIT_SIMPLE1("(,)\\s*", "a", "a");
1873 TEST_SPLIT_SIMPLE2(",", "a,b", "a", "b");
1874 TEST_SPLIT_SIMPLE3(",", "a,b,c", "a", "b", "c");
1875 TEST_SPLIT_SIMPLE3(",\\s*", "a,b,c", "a", "b", "c");
1876 TEST_SPLIT_SIMPLE3(",\\s*", "a, b, c", "a", "b", "c");
1877 TEST_SPLIT_SIMPLE3("(,)\\s*", "a,b", "a", ",", "b");
1878 TEST_SPLIT_SIMPLE3("(,)\\s*", "a, b", "a", ",", "b");
1879 /* Not matched sub-strings. */
1880 TEST_SPLIT_SIMPLE2("a|(b)", "xay", "x", "y");
1881 TEST_SPLIT_SIMPLE3("a|(b)", "xby", "x", "b", "y");
1882 /* Empty matches. */
1883 TEST_SPLIT_SIMPLE3("", "abc", "a", "b", "c");
1884 TEST_SPLIT_SIMPLE3(" *", "ab c", "a", "b", "c");
1885 /* Invalid patterns. */
1886 TEST_SPLIT_SIMPLE0("\\", "");
1887 TEST_SPLIT_SIMPLE0("[", "");
1888
1889 /* TEST_SPLIT#(pattern, string, start_position, max_tokens, ...) */
1890 TEST_SPLIT0("", "", 0, 0);
1891 TEST_SPLIT0("a", "", 0, 0);
1892 TEST_SPLIT0("a", "", 0, 1);
1893 TEST_SPLIT0("a", "", 0, 2);
1894 TEST_SPLIT0("a", "a", 1, 0);
1895 TEST_SPLIT1(",", "a", 0, 0, "a");
1896 TEST_SPLIT1(",", "a,b", 0, 1, "a,b");
1897 TEST_SPLIT1("(,)\\s*", "a", 0, 0, "a");
1898 TEST_SPLIT1(",", "a,b", 2, 0, "b");
1899 TEST_SPLIT2(",", "a,b", 0, 0, "a", "b");
1900 TEST_SPLIT2(",", "a,b,c", 0, 2, "a", "b,c");
1901 TEST_SPLIT2(",", "a,b", 1, 0, "", "b");
1902 TEST_SPLIT2(",", "a,", 0, 0, "a", "");
1903 TEST_SPLIT3(",", "a,b,c", 0, 0, "a", "b", "c");
1904 TEST_SPLIT3(",\\s*", "a,b,c", 0, 0, "a", "b", "c");
1905 TEST_SPLIT3(",\\s*", "a, b, c", 0, 0, "a", "b", "c");
1906 TEST_SPLIT3("(,)\\s*", "a,b", 0, 0, "a", ",", "b");
1907 TEST_SPLIT3("(,)\\s*", "a, b", 0, 0, "a", ",", "b");
1908 /* Not matched sub-strings. */
1909 TEST_SPLIT2("a|(b)", "xay", 0, 0, "x", "y");
1910 TEST_SPLIT3("a|(b)", "xby", 0, -1, "x", "b", "y");
1911 /* Empty matches. */
1912 TEST_SPLIT2(" *", "ab c", 1, 0, "b", "c");
1913 TEST_SPLIT3("", "abc", 0, 0, "a", "b", "c");
1914 TEST_SPLIT3(" *", "ab c", 0, 0, "a", "b", "c");
1915 TEST_SPLIT1(" *", "ab c", 0, 1, "ab c");
1916 TEST_SPLIT2(" *", "ab c", 0, 2, "a", "b c");
1917 TEST_SPLIT3(" *", "ab c", 0, 3, "a", "b", "c");
1918 TEST_SPLIT3(" *", "ab c", 0, 4, "a", "b", "c");
1919
1920 /* TEST_CHECK_REPLACEMENT(string_to_expand, expected, expected_refs) */
1921 TEST_CHECK_REPLACEMENT("", TRUE, FALSE);
1922 TEST_CHECK_REPLACEMENT("a", TRUE, FALSE);
1923 TEST_CHECK_REPLACEMENT("\\t\\n\\v\\r\\f\\a\\b\\\\\\x{61}", TRUE, FALSE);
1924 TEST_CHECK_REPLACEMENT("\\0", TRUE, TRUE);
1925 TEST_CHECK_REPLACEMENT("\\n\\2", TRUE, TRUE);
1926 TEST_CHECK_REPLACEMENT("\\g<foo>", TRUE, TRUE);
1927 /* Invalid strings */
1928 TEST_CHECK_REPLACEMENT("\\Q", FALSE, FALSE);
1929 TEST_CHECK_REPLACEMENT("x\\Ay", FALSE, FALSE);
1930
1931 /* TEST_EXPAND(pattern, string, string_to_expand, raw, expected) */
1932 TEST_EXPAND("a", "a", "", FALSE, "");
1933 TEST_EXPAND("a", "a", "\\0", FALSE, "a");
1934 TEST_EXPAND("a", "a", "\\1", FALSE, "");
1935 TEST_EXPAND("(a)", "ab", "\\1", FALSE, "a");
1936 TEST_EXPAND("(a)", "a", "\\1", FALSE, "a");
1937 TEST_EXPAND("(a)", "a", "\\g<1>", FALSE, "a");
1938 TEST_EXPAND("a", "a", "\\0130", FALSE, "X");
1939 TEST_EXPAND("a", "a", "\\\\\\0", FALSE, "\\a");
1940 TEST_EXPAND("a(?P<G>.)c", "xabcy", "X\\g<G>X", FALSE, "XbX");
1941 TEST_EXPAND("(.)(?P<1>.)", "ab", "\\1", FALSE, "a");
1942 TEST_EXPAND("(.)(?P<1>.)", "ab", "\\g<1>", FALSE, "a");
1943 TEST_EXPAND(".", EURO, "\\0", FALSE, EURO);
1944 TEST_EXPAND("(.)", EURO, "\\1", FALSE, EURO);
1945 TEST_EXPAND("(?P<G>.)", EURO, "\\g<G>", FALSE, EURO);
1946 TEST_EXPAND(".", "a", EURO, FALSE, EURO);
1947 TEST_EXPAND(".", "a", EURO "\\0", FALSE, EURO "a");
1948 TEST_EXPAND(".", "", "\\Lab\\Ec", FALSE, "abc");
1949 TEST_EXPAND(".", "", "\\LaB\\EC", FALSE, "abC");
1950 TEST_EXPAND(".", "", "\\Uab\\Ec", FALSE, "ABc");
1951 TEST_EXPAND(".", "", "a\\ubc", FALSE, "aBc");
1952 TEST_EXPAND(".", "", "a\\lbc", FALSE, "abc");
1953 TEST_EXPAND(".", "", "A\\uBC", FALSE, "ABC");
1954 TEST_EXPAND(".", "", "A\\lBC", FALSE, "AbC");
1955 TEST_EXPAND(".", "", "A\\l\\\\BC", FALSE, "A\\BC");
1956 TEST_EXPAND(".", "", "\\L" AGRAVE "\\E", FALSE, AGRAVE);
1957 TEST_EXPAND(".", "", "\\U" AGRAVE "\\E", FALSE, AGRAVE_UPPER);
1958 TEST_EXPAND(".", "", "\\u" AGRAVE "a", FALSE, AGRAVE_UPPER "a");
1959 TEST_EXPAND(".", "ab", "x\\U\\0y\\Ez", FALSE, "xAYz");
1960 TEST_EXPAND(".(.)", "AB", "x\\L\\1y\\Ez", FALSE, "xbyz");
1961 TEST_EXPAND(".", "ab", "x\\u\\0y\\Ez", FALSE, "xAyz");
1962 TEST_EXPAND(".(.)", "AB", "x\\l\\1y\\Ez", FALSE, "xbyz");
1963 TEST_EXPAND(".(.)", "a" AGRAVE_UPPER, "x\\l\\1y", FALSE, "x" AGRAVE "y");
1964 TEST_EXPAND("a", "bab", "\\x{61}", FALSE, "a");
1965 TEST_EXPAND("a", "bab", "\\x61", FALSE, "a");
1966 TEST_EXPAND("a", "bab", "\\x5a", FALSE, "Z");
1967 TEST_EXPAND("a", "bab", "\\0\\x5A", FALSE, "aZ");
1968 TEST_EXPAND("a", "bab", "\\1\\x{5A}", FALSE, "Z");
1969 TEST_EXPAND("a", "bab", "\\x{00E0}", FALSE, AGRAVE);
1970 TEST_EXPAND("", "bab", "\\x{0634}", FALSE, SHEEN);
1971 TEST_EXPAND("", "bab", "\\x{634}", FALSE, SHEEN);
1972 TEST_EXPAND("", "", "\\t", FALSE, "\t");
1973 TEST_EXPAND("", "", "\\v", FALSE, "\v");
1974 TEST_EXPAND("", "", "\\r", FALSE, "\r");
1975 TEST_EXPAND("", "", "\\n", FALSE, "\n");
1976 TEST_EXPAND("", "", "\\f", FALSE, "\f");
1977 TEST_EXPAND("", "", "\\a", FALSE, "\a");
1978 TEST_EXPAND("", "", "\\b", FALSE, "\b");
1979 TEST_EXPAND("a(.)", "abc", "\\0\\b\\1", FALSE, "ab\bb");
1980 TEST_EXPAND("a(.)", "abc", "\\0141", FALSE, "a");
1981 TEST_EXPAND("a(.)", "abc", "\\078", FALSE, "\a8");
1982 TEST_EXPAND("a(.)", "abc", "\\077", FALSE, "?");
1983 TEST_EXPAND("a(.)", "abc", "\\0778", FALSE, "?8");
1984 TEST_EXPAND("a(.)", "a" AGRAVE "b", "\\1", FALSE, AGRAVE);
1985 TEST_EXPAND("a(.)", "a" AGRAVE "b", "\\1", TRUE, "\xc3");
1986 TEST_EXPAND("a(.)", "a" AGRAVE "b", "\\0", TRUE, "a\xc3");
1987 /* Invalid strings. */
1988 TEST_EXPAND("", "", "\\Q", FALSE, NULL);
1989 TEST_EXPAND("", "", "x\\Ay", FALSE, NULL);
1990 TEST_EXPAND("", "", "\\g<", FALSE, NULL);
1991 TEST_EXPAND("", "", "\\g<>", FALSE, NULL);
1992 TEST_EXPAND("", "", "\\g<1a>", FALSE, NULL);
1993 TEST_EXPAND("", "", "\\g<a$>", FALSE, NULL);
1994 TEST_EXPAND("", "", "\\", FALSE, NULL);
1995 TEST_EXPAND("a", "a", "\\x{61", FALSE, NULL);
1996 TEST_EXPAND("a", "a", "\\x6X", FALSE, NULL);
1997 /* Pattern-less. */
1998 TEST_EXPAND(NULL, NULL, "", FALSE, "");
1999 TEST_EXPAND(NULL, NULL, "\\n", FALSE, "\n");
2000 /* Invalid strings */
2001 TEST_EXPAND(NULL, NULL, "\\Q", FALSE, NULL);
2002 TEST_EXPAND(NULL, NULL, "x\\Ay", FALSE, NULL);
2003
2004 /* TEST_REPLACE(pattern, string, start_position, replacement, expected) */
2005 TEST_REPLACE("a", "ababa", 0, "A", "AbAbA");
2006 TEST_REPLACE("a", "ababa", 1, "A", "abAbA");
2007 TEST_REPLACE("a", "ababa", 2, "A", "abAbA");
2008 TEST_REPLACE("a", "ababa", 3, "A", "ababA");
2009 TEST_REPLACE("a", "ababa", 4, "A", "ababA");
2010 TEST_REPLACE("a", "ababa", 5, "A", "ababa");
2011 TEST_REPLACE("a", "ababa", 6, "A", "ababa");
2012 TEST_REPLACE("a", "abababa", 2, "A", "abAbAbA");
2013 TEST_REPLACE("a", "abab", 0, "A", "AbAb");
2014 TEST_REPLACE("a", "baba", 0, "A", "bAbA");
2015 TEST_REPLACE("a", "bab", 0, "A", "bAb");
2016 TEST_REPLACE("$^", "abc", 0, "X", "abc");
2017 TEST_REPLACE("(.)a", "ciao", 0, "a\\1", "caio");
2018 TEST_REPLACE("a.", "abc", 0, "\\0\\0", "ababc");
2019 TEST_REPLACE("a", "asd", 0, "\\0101", "Asd");
2020 TEST_REPLACE("(a).\\1", "aba cda", 0, "\\1\\n", "a\n cda");
2021 TEST_REPLACE("a" AGRAVE "a", "a" AGRAVE "a", 0, "x", "x");
2022 TEST_REPLACE("a" AGRAVE "a", "a" AGRAVE "a", 0, OGRAVE, OGRAVE);
2023 TEST_REPLACE("[^-]", "-" EURO "-x-" HSTROKE, 0, "a", "-a-a-a");
2024 TEST_REPLACE("[^-]", "-" EURO "-" HSTROKE, 0, "a\\g<0>a", "-a" EURO "a-a" HSTROKE "a");
2025 TEST_REPLACE("-", "-" EURO "-" HSTROKE, 0, "", EURO HSTROKE);
2026 TEST_REPLACE(".*", "hello", 0, "\\U\\0\\E", "HELLO");
2027 TEST_REPLACE(".*", "hello", 0, "\\u\\0", "Hello");
2028 TEST_REPLACE("\\S+", "hello world", 0, "\\U-\\0-", "-HELLO- -WORLD-");
2029 TEST_REPLACE(".", "a", 0, "\\A", NULL);
2030 TEST_REPLACE(".", "a", 0, "\\g", NULL);
2031
2032 /* TEST_REPLACE_LIT(pattern, string, start_position, replacement, expected) */
2033 TEST_REPLACE_LIT("a", "ababa", 0, "A", "AbAbA");
2034 TEST_REPLACE_LIT("a", "ababa", 1, "A", "abAbA");
2035 TEST_REPLACE_LIT("a", "ababa", 2, "A", "abAbA");
2036 TEST_REPLACE_LIT("a", "ababa", 3, "A", "ababA");
2037 TEST_REPLACE_LIT("a", "ababa", 4, "A", "ababA");
2038 TEST_REPLACE_LIT("a", "ababa", 5, "A", "ababa");
2039 TEST_REPLACE_LIT("a", "ababa", 6, "A", "ababa");
2040 TEST_REPLACE_LIT("a", "abababa", 2, "A", "abAbAbA");
2041 TEST_REPLACE_LIT("a", "abcadaa", 0, "A", "AbcAdAA");
2042 TEST_REPLACE_LIT("$^", "abc", 0, "X", "abc");
2043 TEST_REPLACE_LIT("(.)a", "ciao", 0, "a\\1", "ca\\1o");
2044 TEST_REPLACE_LIT("a.", "abc", 0, "\\0\\0\\n", "\\0\\0\\nc");
2045 TEST_REPLACE_LIT("a" AGRAVE "a", "a" AGRAVE "a", 0, "x", "x");
2046 TEST_REPLACE_LIT("a" AGRAVE "a", "a" AGRAVE "a", 0, OGRAVE, OGRAVE);
2047 TEST_REPLACE_LIT(AGRAVE, "-" AGRAVE "-" HSTROKE, 0, "a" ENG "a", "-a" ENG "a-" HSTROKE);
2048 TEST_REPLACE_LIT("[^-]", "-" EURO "-" AGRAVE "-" HSTROKE, 0, "a", "-a-a-a");
2049 TEST_REPLACE_LIT("[^-]", "-" EURO "-" AGRAVE, 0, "a\\g<0>a", "-a\\g<0>a-a\\g<0>a");
2050 TEST_REPLACE_LIT("-", "-" EURO "-" AGRAVE "-" HSTROKE, 0, "", EURO AGRAVE HSTROKE);
2051 TEST_REPLACE_LIT("(?=[A-Z0-9])", "RegExTest", 0, "_", "_Reg_Ex_Test");
2052 TEST_REPLACE_LIT("(?=[A-Z0-9])", "RegExTest", 1, "_", "Reg_Ex_Test");
2053
2054 /* TEST_GET_STRING_NUMBER(pattern, name, expected_num) */
2055 TEST_GET_STRING_NUMBER("", "A", -1);
2056 TEST_GET_STRING_NUMBER("(?P<A>.)", "A", 1);
2057 TEST_GET_STRING_NUMBER("(?P<A>.)", "B", -1);
2058 TEST_GET_STRING_NUMBER("(?P<A>.)(?P<B>a)", "A", 1);
2059 TEST_GET_STRING_NUMBER("(?P<A>.)(?P<B>a)", "B", 2);
2060 TEST_GET_STRING_NUMBER("(?P<A>.)(?P<B>a)", "C", -1);
2061 TEST_GET_STRING_NUMBER("(?P<A>.)(.)(?P<B>a)", "A", 1);
2062 TEST_GET_STRING_NUMBER("(?P<A>.)(.)(?P<B>a)", "B", 3);
2063 TEST_GET_STRING_NUMBER("(?P<A>.)(.)(?P<B>a)", "C", -1);
2064 TEST_GET_STRING_NUMBER("(?:a)(?P<A>.)", "A", 1);
2065 TEST_GET_STRING_NUMBER("(?:a)(?P<A>.)", "B", -1);
2066
2067 /* TEST_ESCAPE(string, length, expected) */
2068 TEST_ESCAPE("hello world", -1, "hello world");
2069 TEST_ESCAPE("hello world", 5, "hello");
2070 TEST_ESCAPE("hello.world", -1, "hello\\.world");
2071 TEST_ESCAPE("a(b\\b.$", -1, "a\\(b\\\\b\\.\\$");
2072 TEST_ESCAPE("hello\0world", -1, "hello");
2073 TEST_ESCAPE("hello\0world", 11, "hello\\0world");
2074 TEST_ESCAPE(EURO "*" ENG, -1, EURO "\\*" ENG);
2075 TEST_ESCAPE("a$", -1, "a\\$");
2076 TEST_ESCAPE("$a", -1, "\\$a");
2077 TEST_ESCAPE("a$a", -1, "a\\$a");
2078 TEST_ESCAPE("$a$", -1, "\\$a\\$");
2079 TEST_ESCAPE("$a$", 0, "");
2080 TEST_ESCAPE("$a$", 1, "\\$");
2081 TEST_ESCAPE("$a$", 2, "\\$a");
2082 TEST_ESCAPE("$a$", 3, "\\$a\\$");
2083 TEST_ESCAPE("$a$", 4, "\\$a\\$\\0");
2084 TEST_ESCAPE("|()[]{}^$*+?.", -1, "\\|\\(\\)\\[\\]\\{\\}\\^\\$\\*\\+\\?\\.");
2085 TEST_ESCAPE("a|a(a)a[a]a{a}a^a$a*a+a?a.a", -1,
2086 "a\\|a\\(a\\)a\\[a\\]a\\{a\\}a\\^a\\$a\\*a\\+a\\?a\\.a");
2087
2088 /* TEST_MATCH_ALL#(pattern, string, string_len, start_position, ...) */
2089 TEST_MATCH_ALL0("<.*>", "", -1, 0);
2090 TEST_MATCH_ALL0("a+", "", -1, 0);
2091 TEST_MATCH_ALL0("a+", "a", 0, 0);
2092 TEST_MATCH_ALL0("a+", "a", -1, 1);
2093 TEST_MATCH_ALL1("<.*>", "<a>", -1, 0, "<a>", 0, 3);
2094 TEST_MATCH_ALL1("a+", "a", -1, 0, "a", 0, 1);
2095 TEST_MATCH_ALL1("a+", "aa", 1, 0, "a", 0, 1);
2096 TEST_MATCH_ALL1("a+", "aa", -1, 1, "a", 1, 2);
2097 TEST_MATCH_ALL1("a+", "aa", 2, 1, "a", 1, 2);
2098 TEST_MATCH_ALL1(".+", ENG, -1, 0, ENG, 0, 2);
2099 TEST_MATCH_ALL2("<.*>", "<a><b>", -1, 0, "<a><b>", 0, 6, "<a>", 0, 3);
2100 TEST_MATCH_ALL2("a+", "aa", -1, 0, "aa", 0, 2, "a", 0, 1);
2101 TEST_MATCH_ALL2(".+", ENG EURO, -1, 0, ENG EURO, 0, 5, ENG, 0, 2);
2102 TEST_MATCH_ALL3("<.*>", "<a><b><c>", -1, 0, "<a><b><c>", 0, 9,
2103 "<a><b>", 0, 6, "<a>", 0, 3);
2104 TEST_MATCH_ALL3("a+", "aaa", -1, 0, "aaa", 0, 3, "aa", 0, 2, "a", 0, 1);
2105
2106 end: /* if abort_on_fail is TRUE the flow passes to this label. */
2107 verbose ("\n%u tests passed, %u failed\n", passed, failed);
2108 return failed;
2109 }
2110
2111 #else /* ENABLE_REGEX false */
2112
2113 int
main(int argc,char * argv[])2114 main (int argc, char *argv[])
2115 {
2116 g_print ("GRegex is disabled.\n");
2117 return 0;
2118 }
2119
2120 #endif /* ENABLE_REGEX */
2121