2 * Copyright (C) 2005 - 2006, Marco Barisione <marco@barisione.org>
3 * Copyright (C) 2010 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
21 #undef G_DISABLE_ASSERT
32 /* U+20AC EURO SIGN (symbol, currency) */
33 #define EURO "\xe2\x82\xac"
34 /* U+00E0 LATIN SMALL LETTER A WITH GRAVE (letter, lowercase) */
35 #define AGRAVE "\xc3\xa0"
36 /* U+00C0 LATIN CAPITAL LETTER A WITH GRAVE (letter, uppercase) */
37 #define AGRAVE_UPPER "\xc3\x80"
38 /* U+00E8 LATIN SMALL LETTER E WITH GRAVE (letter, lowercase) */
39 #define EGRAVE "\xc3\xa8"
40 /* U+00F2 LATIN SMALL LETTER O WITH GRAVE (letter, lowercase) */
41 #define OGRAVE "\xc3\xb2"
42 /* U+014B LATIN SMALL LETTER ENG (letter, lowercase) */
43 #define ENG "\xc5\x8b"
44 /* U+0127 LATIN SMALL LETTER H WITH STROKE (letter, lowercase) */
45 #define HSTROKE "\xc4\xa7"
46 /* U+0634 ARABIC LETTER SHEEN (letter, other) */
47 #define SHEEN "\xd8\xb4"
48 /* U+1374 ETHIOPIC NUMBER THIRTY (number, other) */
49 #define ETH30 "\xe1\x8d\xb4"
51 /* A random value use to mark untouched integer variables. */
52 #define UNTOUCHED -559038737
58 GRegexCompileFlags compile_opts;
59 GRegexMatchFlags match_opts;
64 test_new (gconstpointer d)
66 const TestNewData *data = d;
70 regex = g_regex_new (data->pattern, data->compile_opts, data->match_opts, &error);
71 g_assert (regex != NULL);
72 g_assert_no_error (error);
73 g_assert_cmpstr (data->pattern, ==, g_regex_get_pattern (regex));
75 g_regex_unref (regex);
78 #define TEST_NEW(_pattern, _compile_opts, _match_opts) { \
81 data = g_new0 (TestNewData, 1); \
82 data->pattern = _pattern; \
83 data->compile_opts = _compile_opts; \
84 data->match_opts = _match_opts; \
85 data->expected_error = 0; \
86 path = g_strdup_printf ("/regex/new/%d", ++total); \
87 g_test_add_data_func (path, data, test_new); \
92 test_new_fail (gconstpointer d)
94 const TestNewData *data = d;
98 regex = g_regex_new (data->pattern, data->compile_opts, data->match_opts, &error);
100 g_assert (regex == NULL);
101 g_assert_error (error, G_REGEX_ERROR, data->expected_error);
102 g_error_free (error);
105 #define TEST_NEW_FAIL(_pattern, _compile_opts, _expected_error) { \
108 data = g_new0 (TestNewData, 1); \
109 data->pattern = _pattern; \
110 data->compile_opts = _compile_opts; \
111 data->match_opts = 0; \
112 data->expected_error = _expected_error; \
113 path = g_strdup_printf ("/regex/new-fail/%d", ++total); \
114 g_test_add_data_func (path, data, test_new_fail); \
119 const gchar *pattern;
121 GRegexCompileFlags compile_opts;
122 GRegexMatchFlags match_opts;
126 GRegexMatchFlags match_opts2;
130 test_match_simple (gconstpointer d)
132 const TestMatchData *data = d;
135 match = g_regex_match_simple (data->pattern, data->string, data->compile_opts, data->match_opts);
136 g_assert_cmpint (match, ==, data->expected);
139 #define TEST_MATCH_SIMPLE(_pattern, _string, _compile_opts, _match_opts, _expected) { \
140 TestMatchData *data; \
142 data = g_new0 (TestMatchData, 1); \
143 data->pattern = _pattern; \
144 data->string = _string; \
145 data->compile_opts = _compile_opts; \
146 data->match_opts = _match_opts; \
147 data->expected = _expected; \
148 path = g_strdup_printf ("/regex/match-simple/%d", ++total); \
149 g_test_add_data_func (path, data, test_match_simple); \
154 test_match (gconstpointer d)
156 const TestMatchData *data = d;
159 GError *error = NULL;
161 regex = g_regex_new (data->pattern, data->compile_opts, data->match_opts, &error);
162 g_assert (regex != NULL);
163 g_assert_no_error (error);
165 match = g_regex_match_full (regex, data->string, data->string_len,
166 data->start_position, data->match_opts2, NULL, NULL);
168 g_assert_cmpint (match, ==, data->expected);
170 if (data->string_len == -1 && data->start_position == 0)
172 match = g_regex_match (regex, data->string, data->match_opts2, NULL);
173 g_assert_cmpint (match, ==, data->expected);
176 g_regex_unref (regex);
179 #define TEST_MATCH(_pattern, _compile_opts, _match_opts, _string, \
180 _string_len, _start_position, _match_opts2, _expected) { \
181 TestMatchData *data; \
183 data = g_new0 (TestMatchData, 1); \
184 data->pattern = _pattern; \
185 data->compile_opts = _compile_opts; \
186 data->match_opts = _match_opts; \
187 data->string = _string; \
188 data->string_len = _string_len; \
189 data->start_position = _start_position; \
190 data->match_opts2 = _match_opts2; \
191 data->expected = _expected; \
192 path = g_strdup_printf ("/regex/match/%d", ++total); \
193 g_test_add_data_func (path, data, test_match); \
202 typedef struct _Match Match;
205 free_match (gpointer data, gpointer user_data)
210 g_free (match->string);
215 const gchar *pattern;
223 test_match_next (gconstpointer d)
225 const TestMatchNextData *data = d;
227 GMatchInfo *match_info;
229 GSList *l_exp, *l_match;
231 regex = g_regex_new (data->pattern, 0, 0, NULL);
233 g_assert (regex != NULL);
235 g_regex_match_full (regex, data->string, data->string_len,
236 data->start_position, 0, &match_info, NULL);
238 while (g_match_info_matches (match_info))
240 Match *match = g_new0 (Match, 1);
241 match->string = g_match_info_fetch (match_info, 0);
242 match->start = UNTOUCHED;
243 match->end = UNTOUCHED;
244 g_match_info_fetch_pos (match_info, 0, &match->start, &match->end);
245 matches = g_slist_prepend (matches, match);
246 g_match_info_next (match_info, NULL);
248 g_assert (regex == g_match_info_get_regex (match_info));
249 g_assert_cmpstr (data->string, ==, g_match_info_get_string (match_info));
250 g_match_info_free (match_info);
251 matches = g_slist_reverse (matches);
253 g_assert_cmpint (g_slist_length (matches), ==, g_slist_length (data->expected));
255 l_exp = data->expected;
257 while (l_exp != NULL)
259 Match *exp = l_exp->data;
260 Match *match = l_match->data;
262 g_assert_cmpstr (exp->string, ==, match->string);
263 g_assert_cmpint (exp->start, ==, match->start);
264 g_assert_cmpint (exp->end, ==, match->end);
266 l_exp = g_slist_next (l_exp);
267 l_match = g_slist_next (l_match);
270 g_regex_unref (regex);
271 g_slist_foreach (matches, free_match, NULL);
272 g_slist_free (matches);
275 #define TEST_MATCH_NEXT0(_pattern, _string, _string_len, _start_position) { \
276 TestMatchNextData *data; \
278 data = g_new0 (TestMatchNextData, 1); \
279 data->pattern = _pattern; \
280 data->string = _string; \
281 data->string_len = _string_len; \
282 data->start_position = _start_position; \
283 data->expected = NULL; \
284 path = g_strdup_printf ("/regex/match/next0/%d", ++total); \
285 g_test_add_data_func (path, data, test_match_next); \
289 #define TEST_MATCH_NEXT1(_pattern, _string, _string_len, _start_position, \
291 TestMatchNextData *data; \
294 data = g_new0 (TestMatchNextData, 1); \
295 data->pattern = _pattern; \
296 data->string = _string; \
297 data->string_len = _string_len; \
298 data->start_position = _start_position; \
299 match = g_new0 (Match, 1); \
300 match->string = t1; \
303 data->expected = g_slist_append (NULL, match); \
304 path = g_strdup_printf ("/regex/match/next1/%d", ++total); \
305 g_test_add_data_func (path, data, test_match_next); \
309 #define TEST_MATCH_NEXT2(_pattern, _string, _string_len, _start_position, \
310 t1, s1, e1, t2, s2, e2) { \
311 TestMatchNextData *data; \
314 data = g_new0 (TestMatchNextData, 1); \
315 data->pattern = _pattern; \
316 data->string = _string; \
317 data->string_len = _string_len; \
318 data->start_position = _start_position; \
319 match = g_new0 (Match, 1); \
320 match->string = t1; \
323 data->expected = g_slist_append (NULL, match); \
324 match = g_new0 (Match, 1); \
325 match->string = t2; \
328 data->expected = g_slist_append (data->expected, match); \
329 path = g_strdup_printf ("/regex/match/next2/%d", ++total); \
330 g_test_add_data_func (path, data, test_match_next); \
334 #define TEST_MATCH_NEXT3(_pattern, _string, _string_len, _start_position, \
335 t1, s1, e1, t2, s2, e2, t3, s3, e3) { \
336 TestMatchNextData *data; \
339 data = g_new0 (TestMatchNextData, 1); \
340 data->pattern = _pattern; \
341 data->string = _string; \
342 data->string_len = _string_len; \
343 data->start_position = _start_position; \
344 match = g_new0 (Match, 1); \
345 match->string = t1; \
348 data->expected = g_slist_append (NULL, match); \
349 match = g_new0 (Match, 1); \
350 match->string = t2; \
353 data->expected = g_slist_append (data->expected, match); \
354 match = g_new0 (Match, 1); \
355 match->string = t3; \
358 data->expected = g_slist_append (data->expected, match); \
359 path = g_strdup_printf ("/regex/match/next3/%d", ++total); \
360 g_test_add_data_func (path, data, test_match_next); \
364 #define TEST_MATCH_NEXT4(_pattern, _string, _string_len, _start_position, \
365 t1, s1, e1, t2, s2, e2, t3, s3, e3, t4, s4, e4) { \
366 TestMatchNextData *data; \
369 data = g_new0 (TestMatchNextData, 1); \
370 data->pattern = _pattern; \
371 data->string = _string; \
372 data->string_len = _string_len; \
373 data->start_position = _start_position; \
374 match = g_new0 (Match, 1); \
375 match->string = t1; \
378 data->expected = g_slist_append (NULL, match); \
379 match = g_new0 (Match, 1); \
380 match->string = t2; \
383 data->expected = g_slist_append (data->expected, match); \
384 match = g_new0 (Match, 1); \
385 match->string = t3; \
388 data->expected = g_slist_append (data->expected, match); \
389 match = g_new0 (Match, 1); \
390 match->string = t4; \
393 data->expected = g_slist_append (data->expected, match); \
394 path = g_strdup_printf ("/regex/match/next4/%d", ++total); \
395 g_test_add_data_func (path, data, test_match_next); \
399 const gchar *pattern;
402 GRegexMatchFlags match_opts;
404 } TestMatchCountData;
407 test_match_count (gconstpointer d)
409 const TestMatchCountData *data = d;
411 GMatchInfo *match_info;
414 regex = g_regex_new (data->pattern, 0, 0, NULL);
416 g_assert (regex != NULL);
418 g_regex_match_full (regex, data->string, -1, data->start_position,
419 data->match_opts, &match_info, NULL);
420 count = g_match_info_get_match_count (match_info);
422 g_assert_cmpint (count, ==, data->expected_count);
424 g_match_info_free (match_info);
425 g_regex_unref (regex);
428 #define TEST_MATCH_COUNT(_pattern, _string, _start_position, _match_opts, _expected_count) { \
429 TestMatchCountData *data; \
431 data = g_new0 (TestMatchCountData, 1); \
432 data->pattern = _pattern; \
433 data->string = _string; \
434 data->start_position = _start_position; \
435 data->match_opts = _match_opts; \
436 data->expected_count = _expected_count; \
437 path = g_strdup_printf ("/regex/match/count/%d", ++total); \
438 g_test_add_data_func (path, data, test_match_count); \
443 test_partial (gconstpointer d)
445 const TestMatchData *data = d;
447 GMatchInfo *match_info;
449 regex = g_regex_new (data->pattern, 0, 0, NULL);
451 g_assert (regex != NULL);
453 g_regex_match (regex, data->string, G_REGEX_MATCH_PARTIAL, &match_info);
455 g_assert_cmpint (data->expected, ==, g_match_info_is_partial_match (match_info));
459 g_assert (!g_match_info_fetch_pos (match_info, 0, NULL, NULL));
460 g_assert (!g_match_info_fetch_pos (match_info, 1, NULL, NULL));
463 g_match_info_free (match_info);
464 g_regex_unref (regex);
467 #define TEST_PARTIAL(_pattern, _string, _expected) { \
468 TestMatchData *data; \
470 data = g_new0 (TestMatchData, 1); \
471 data->pattern = _pattern; \
472 data->string = _string; \
473 data->expected = _expected; \
474 path = g_strdup_printf ("/regex/match/partial/%d", ++total); \
475 g_test_add_data_func (path, data, test_partial); \
480 const gchar *pattern;
484 const gchar *expected_sub;
490 test_sub_pattern (gconstpointer d)
492 const TestSubData *data = d;
494 GMatchInfo *match_info;
496 gint start = UNTOUCHED, end = UNTOUCHED;
498 regex = g_regex_new (data->pattern, 0, 0, NULL);
500 g_assert (regex != NULL);
502 g_regex_match_full (regex, data->string, -1, data->start_position, 0, &match_info, NULL);
504 sub_expr = g_match_info_fetch (match_info, data->sub_n);
505 g_assert_cmpstr (sub_expr, ==, data->expected_sub);
508 g_match_info_fetch_pos (match_info, data->sub_n, &start, &end);
509 g_assert_cmpint (start, ==, data->expected_start);
510 g_assert_cmpint (end, ==, data->expected_end);
512 g_match_info_free (match_info);
513 g_regex_unref (regex);
516 #define TEST_SUB_PATTERN(_pattern, _string, _start_position, _sub_n, _expected_sub, \
517 _expected_start, _expected_end) { \
520 data = g_new0 (TestSubData, 1); \
521 data->pattern = _pattern; \
522 data->string = _string; \
523 data->start_position = _start_position; \
524 data->sub_n = _sub_n; \
525 data->expected_sub = _expected_sub; \
526 data->expected_start = _expected_start; \
527 data->expected_end = _expected_end; \
528 path = g_strdup_printf ("/regex/match/subpattern/%d", ++total); \
529 g_test_add_data_func (path, data, test_sub_pattern); \
534 const gchar *pattern;
535 GRegexCompileFlags flags;
538 const gchar *sub_name;
539 const gchar *expected_sub;
545 test_named_sub_pattern (gconstpointer d)
547 const TestNamedSubData *data = d;
549 GMatchInfo *match_info;
550 gint start = UNTOUCHED, end = UNTOUCHED;
553 regex = g_regex_new (data->pattern, data->flags, 0, NULL);
555 g_assert (regex != NULL);
557 g_regex_match_full (regex, data->string, -1, data->start_position, 0, &match_info, NULL);
558 sub_expr = g_match_info_fetch_named (match_info, data->sub_name);
559 g_assert_cmpstr (sub_expr, ==, data->expected_sub);
562 g_match_info_fetch_named_pos (match_info, data->sub_name, &start, &end);
563 g_assert_cmpint (start, ==, data->expected_start);
564 g_assert_cmpint (end, ==, data->expected_end);
566 g_match_info_free (match_info);
567 g_regex_unref (regex);
570 #define TEST_NAMED_SUB_PATTERN(_pattern, _string, _start_position, _sub_name, \
571 _expected_sub, _expected_start, _expected_end) { \
572 TestNamedSubData *data; \
574 data = g_new0 (TestNamedSubData, 1); \
575 data->pattern = _pattern; \
576 data->string = _string; \
578 data->start_position = _start_position; \
579 data->sub_name = _sub_name; \
580 data->expected_sub = _expected_sub; \
581 data->expected_start = _expected_start; \
582 data->expected_end = _expected_end; \
583 path = g_strdup_printf ("/regex/match/named/subpattern/%d", ++total); \
584 g_test_add_data_func (path, data, test_named_sub_pattern); \
588 #define TEST_NAMED_SUB_PATTERN_DUPNAMES(_pattern, _string, _start_position, _sub_name, \
589 _expected_sub, _expected_start, _expected_end) { \
590 TestNamedSubData *data; \
592 data = g_new0 (TestNamedSubData, 1); \
593 data->pattern = _pattern; \
594 data->string = _string; \
595 data->flags = G_REGEX_DUPNAMES; \
596 data->start_position = _start_position; \
597 data->sub_name = _sub_name; \
598 data->expected_sub = _expected_sub; \
599 data->expected_start = _expected_start; \
600 data->expected_end = _expected_end; \
601 path = g_strdup_printf ("/regex/match/subpattern/named/dupnames/%d", ++total); \
602 g_test_add_data_func (path, data, test_named_sub_pattern); \
607 const gchar *pattern;
615 test_fetch_all (gconstpointer d)
617 const TestFetchAllData *data = d;
619 GMatchInfo *match_info;
625 regex = g_regex_new (data->pattern, 0, 0, NULL);
627 g_assert (regex != NULL);
629 g_regex_match (regex, data->string, 0, &match_info);
630 matches = g_match_info_fetch_all (match_info);
632 match_count = g_strv_length (matches);
636 g_assert_cmpint (match_count, ==, g_slist_length (data->expected));
638 l_exp = data->expected;
639 for (i = 0; l_exp != NULL; i++, l_exp = g_slist_next (l_exp))
641 g_assert_cmpstr (l_exp->data, ==, matches[i]);
644 g_match_info_free (match_info);
645 g_regex_unref (regex);
646 g_strfreev (matches);
649 #define TEST_FETCH_ALL0(_pattern, _string) { \
650 TestFetchAllData *data; \
652 data = g_new0 (TestFetchAllData, 1); \
653 data->pattern = _pattern; \
654 data->string = _string; \
655 data->expected = NULL; \
656 path = g_strdup_printf ("/regex/fetch-all0/%d", ++total); \
657 g_test_add_data_func (path, data, test_fetch_all); \
661 #define TEST_FETCH_ALL1(_pattern, _string, e1) { \
662 TestFetchAllData *data; \
664 data = g_new0 (TestFetchAllData, 1); \
665 data->pattern = _pattern; \
666 data->string = _string; \
667 data->expected = g_slist_append (NULL, e1); \
668 path = g_strdup_printf ("/regex/fetch-all1/%d", ++total); \
669 g_test_add_data_func (path, data, test_fetch_all); \
673 #define TEST_FETCH_ALL2(_pattern, _string, e1, e2) { \
674 TestFetchAllData *data; \
676 data = g_new0 (TestFetchAllData, 1); \
677 data->pattern = _pattern; \
678 data->string = _string; \
679 data->expected = g_slist_append (NULL, e1); \
680 data->expected = g_slist_append (data->expected, e2); \
681 path = g_strdup_printf ("/regex/fetch-all2/%d", ++total); \
682 g_test_add_data_func (path, data, test_fetch_all); \
686 #define TEST_FETCH_ALL3(_pattern, _string, e1, e2, e3) { \
687 TestFetchAllData *data; \
689 data = g_new0 (TestFetchAllData, 1); \
690 data->pattern = _pattern; \
691 data->string = _string; \
692 data->expected = g_slist_append (NULL, e1); \
693 data->expected = g_slist_append (data->expected, e2); \
694 data->expected = g_slist_append (data->expected, e3); \
695 path = g_strdup_printf ("/regex/fetch-all3/%d", ++total); \
696 g_test_add_data_func (path, data, test_fetch_all); \
701 test_split_simple (gconstpointer d)
703 const TestFetchAllData *data = d;
709 tokens = g_regex_split_simple (data->pattern, data->string, 0, 0);
711 token_count = g_strv_length (tokens);
715 g_assert_cmpint (token_count, ==, g_slist_length (data->expected));
717 l_exp = data->expected;
718 for (i = 0; l_exp != NULL; i++, l_exp = g_slist_next (l_exp))
720 g_assert_cmpstr (l_exp->data, ==, tokens[i]);
726 #define TEST_SPLIT_SIMPLE0(_pattern, _string) { \
727 TestFetchAllData *data; \
729 data = g_new0 (TestFetchAllData, 1); \
730 data->pattern = _pattern; \
731 data->string = _string; \
732 data->expected = NULL; \
733 path = g_strdup_printf ("/regex/split/simple0/%d", ++total); \
734 g_test_add_data_func (path, data, test_split_simple); \
738 #define TEST_SPLIT_SIMPLE1(_pattern, _string, e1) { \
739 TestFetchAllData *data; \
741 data = g_new0 (TestFetchAllData, 1); \
742 data->pattern = _pattern; \
743 data->string = _string; \
744 data->expected = g_slist_append (NULL, e1); \
745 path = g_strdup_printf ("/regex/split/simple1/%d", ++total); \
746 g_test_add_data_func (path, data, test_split_simple); \
750 #define TEST_SPLIT_SIMPLE2(_pattern, _string, e1, e2) { \
751 TestFetchAllData *data; \
753 data = g_new0 (TestFetchAllData, 1); \
754 data->pattern = _pattern; \
755 data->string = _string; \
756 data->expected = g_slist_append (NULL, e1); \
757 data->expected = g_slist_append (data->expected, e2); \
758 path = g_strdup_printf ("/regex/split/simple2/%d", ++total); \
759 g_test_add_data_func (path, data, test_split_simple); \
763 #define TEST_SPLIT_SIMPLE3(_pattern, _string, e1, e2, e3) { \
764 TestFetchAllData *data; \
766 data = g_new0 (TestFetchAllData, 1); \
767 data->pattern = _pattern; \
768 data->string = _string; \
769 data->expected = g_slist_append (NULL, e1); \
770 data->expected = g_slist_append (data->expected, e2); \
771 data->expected = g_slist_append (data->expected, e3); \
772 path = g_strdup_printf ("/regex/split/simple3/%d", ++total); \
773 g_test_add_data_func (path, data, test_split_simple); \
778 test_split_full (gconstpointer d)
780 const TestFetchAllData *data = d;
787 regex = g_regex_new (data->pattern, 0, 0, NULL);
789 g_assert (regex != NULL);
791 tokens = g_regex_split_full (regex, data->string, -1, data->start_position,
792 0, data->max_tokens, NULL);
794 token_count = g_strv_length (tokens);
798 g_assert_cmpint (token_count, ==, g_slist_length (data->expected));
800 l_exp = data->expected;
801 for (i = 0; l_exp != NULL; i++, l_exp = g_slist_next (l_exp))
803 g_assert_cmpstr (l_exp->data, ==, tokens[i]);
806 g_regex_unref (regex);
811 test_split (gconstpointer d)
813 const TestFetchAllData *data = d;
820 regex = g_regex_new (data->pattern, 0, 0, NULL);
822 g_assert (regex != NULL);
824 tokens = g_regex_split (regex, data->string, 0);
826 token_count = g_strv_length (tokens);
830 g_assert_cmpint (token_count, ==, g_slist_length (data->expected));
832 l_exp = data->expected;
833 for (i = 0; l_exp != NULL; i++, l_exp = g_slist_next (l_exp))
835 g_assert_cmpstr (l_exp->data, ==, tokens[i]);
838 g_regex_unref (regex);
842 #define TEST_SPLIT0(_pattern, _string, _start_position, _max_tokens) { \
843 TestFetchAllData *data; \
845 data = g_new0 (TestFetchAllData, 1); \
846 data->pattern = _pattern; \
847 data->string = _string; \
848 data->start_position = _start_position; \
849 data->max_tokens = _max_tokens; \
850 data->expected = NULL; \
851 path = g_strdup_printf ("/regex/full-split0/%d", ++total); \
852 g_test_add_data_func (path, data, test_split_full); \
854 if (_start_position == 0 && _max_tokens <= 0) { \
855 path = g_strdup_printf ("/regex/split0/%d", ++total); \
856 g_test_add_data_func (path, data, test_split); \
861 #define TEST_SPLIT1(_pattern, _string, _start_position, _max_tokens, e1) { \
862 TestFetchAllData *data; \
864 data = g_new0 (TestFetchAllData, 1); \
865 data->pattern = _pattern; \
866 data->string = _string; \
867 data->start_position = _start_position; \
868 data->max_tokens = _max_tokens; \
869 data->expected = NULL; \
870 data->expected = g_slist_append (data->expected, e1); \
871 path = g_strdup_printf ("/regex/full-split1/%d", ++total); \
872 g_test_add_data_func (path, data, test_split_full); \
874 if (_start_position == 0 && _max_tokens <= 0) { \
875 path = g_strdup_printf ("/regex/split1/%d", ++total); \
876 g_test_add_data_func (path, data, test_split); \
881 #define TEST_SPLIT2(_pattern, _string, _start_position, _max_tokens, e1, e2) { \
882 TestFetchAllData *data; \
884 data = g_new0 (TestFetchAllData, 1); \
885 data->pattern = _pattern; \
886 data->string = _string; \
887 data->start_position = _start_position; \
888 data->max_tokens = _max_tokens; \
889 data->expected = NULL; \
890 data->expected = g_slist_append (data->expected, e1); \
891 data->expected = g_slist_append (data->expected, e2); \
892 path = g_strdup_printf ("/regex/full-split2/%d", ++total); \
893 g_test_add_data_func (path, data, test_split_full); \
895 if (_start_position == 0 && _max_tokens <= 0) { \
896 path = g_strdup_printf ("/regex/split2/%d", ++total); \
897 g_test_add_data_func (path, data, test_split); \
902 #define TEST_SPLIT3(_pattern, _string, _start_position, _max_tokens, e1, e2, e3) { \
903 TestFetchAllData *data; \
905 data = g_new0 (TestFetchAllData, 1); \
906 data->pattern = _pattern; \
907 data->string = _string; \
908 data->start_position = _start_position; \
909 data->max_tokens = _max_tokens; \
910 data->expected = NULL; \
911 data->expected = g_slist_append (data->expected, e1); \
912 data->expected = g_slist_append (data->expected, e2); \
913 data->expected = g_slist_append (data->expected, e3); \
914 path = g_strdup_printf ("/regex/full-split3/%d", ++total); \
915 g_test_add_data_func (path, data, test_split_full); \
917 if (_start_position == 0 && _max_tokens <= 0) { \
918 path = g_strdup_printf ("/regex/split3/%d", ++total); \
919 g_test_add_data_func (path, data, test_split); \
925 const gchar *string_to_expand;
927 gboolean expected_refs;
928 } TestCheckReplacementData;
931 test_check_replacement (gconstpointer d)
933 const TestCheckReplacementData *data = d;
937 result = g_regex_check_replacement (data->string_to_expand, &has_refs, NULL);
938 g_assert_cmpint (data->expected, ==, result);
941 g_assert_cmpint (data->expected_refs, ==, has_refs);
944 #define TEST_CHECK_REPLACEMENT(_string_to_expand, _expected, _expected_refs) { \
945 TestCheckReplacementData *data; \
947 data = g_new0 (TestCheckReplacementData, 1); \
948 data->string_to_expand = _string_to_expand; \
949 data->expected = _expected; \
950 data->expected_refs = _expected_refs; \
951 path = g_strdup_printf ("/regex/check-repacement/%d", ++total); \
952 g_test_add_data_func (path, data, test_check_replacement); \
957 const gchar *pattern;
959 const gchar *string_to_expand;
961 const gchar *expected;
965 test_expand (gconstpointer d)
967 const TestExpandData *data = d;
968 GRegex *regex = NULL;
969 GMatchInfo *match_info = NULL;
974 regex = g_regex_new (data->pattern, data->raw ? G_REGEX_RAW : 0, 0, NULL);
975 g_regex_match (regex, data->string, 0, &match_info);
978 res = g_match_info_expand_references (match_info, data->string_to_expand, NULL);
979 g_assert_cmpstr (res, ==, data->expected);
981 g_match_info_free (match_info);
983 g_regex_unref (regex);
986 #define TEST_EXPAND(_pattern, _string, _string_to_expand, _raw, _expected) { \
987 TestExpandData *data; \
989 data = g_new0 (TestExpandData, 1); \
990 data->pattern = _pattern; \
991 data->string = _string; \
992 data->string_to_expand = _string_to_expand; \
994 data->expected = _expected; \
995 path = g_strdup_printf ("/regex/expand/%d", ++total); \
996 g_test_add_data_func (path, data, test_expand); \
1001 const gchar *pattern;
1002 const gchar *string;
1003 gint start_position;
1004 const gchar *replacement;
1005 const gchar *expected;
1009 test_replace (gconstpointer d)
1011 const TestReplaceData *data = d;
1015 regex = g_regex_new (data->pattern, 0, 0, NULL);
1016 res = g_regex_replace (regex, data->string, -1, data->start_position, data->replacement, 0, NULL);
1018 g_assert_cmpstr (res, ==, data->expected);
1021 g_regex_unref (regex);
1024 #define TEST_REPLACE(_pattern, _string, _start_position, _replacement, _expected) { \
1025 TestReplaceData *data; \
1027 data = g_new0 (TestReplaceData, 1); \
1028 data->pattern = _pattern; \
1029 data->string = _string; \
1030 data->start_position = _start_position; \
1031 data->replacement = _replacement; \
1032 data->expected = _expected; \
1033 path = g_strdup_printf ("/regex/replace/%d", ++total); \
1034 g_test_add_data_func (path, data, test_replace); \
1039 test_replace_lit (gconstpointer d)
1041 const TestReplaceData *data = d;
1045 regex = g_regex_new (data->pattern, 0, 0, NULL);
1046 res = g_regex_replace_literal (regex, data->string, -1, data->start_position,
1047 data->replacement, 0, NULL);
1048 g_assert_cmpstr (res, ==, data->expected);
1051 g_regex_unref (regex);
1054 #define TEST_REPLACE_LIT(_pattern, _string, _start_position, _replacement, _expected) { \
1055 TestReplaceData *data; \
1057 data = g_new0 (TestReplaceData, 1); \
1058 data->pattern = _pattern; \
1059 data->string = _string; \
1060 data->start_position = _start_position; \
1061 data->replacement = _replacement; \
1062 data->expected = _expected; \
1063 path = g_strdup_printf ("/regex/replace-literally/%d", ++total); \
1064 g_test_add_data_func (path, data, test_replace_lit); \
1069 const gchar *pattern;
1072 } TestStringNumData;
1075 test_get_string_number (gconstpointer d)
1077 const TestStringNumData *data = d;
1081 regex = g_regex_new (data->pattern, 0, 0, NULL);
1082 num = g_regex_get_string_number (regex, data->name);
1084 g_assert_cmpint (num, ==, data->expected_num);
1085 g_regex_unref (regex);
1088 #define TEST_GET_STRING_NUMBER(_pattern, _name, _expected_num) { \
1089 TestStringNumData *data; \
1091 data = g_new0 (TestStringNumData, 1); \
1092 data->pattern = _pattern; \
1093 data->name = _name; \
1094 data->expected_num = _expected_num; \
1095 path = g_strdup_printf ("/regex/string-number/%d", ++total); \
1096 g_test_add_data_func (path, data, test_get_string_number); \
1101 const gchar *string;
1103 const gchar *expected;
1107 test_escape (gconstpointer d)
1109 const TestEscapeData *data = d;
1112 escaped = g_regex_escape_string (data->string, data->length);
1114 g_assert_cmpstr (escaped, ==, data->expected);
1119 #define TEST_ESCAPE(_string, _length, _expected) { \
1120 TestEscapeData *data; \
1122 data = g_new0 (TestEscapeData, 1); \
1123 data->string = _string; \
1124 data->length = _length; \
1125 data->expected = _expected; \
1126 path = g_strdup_printf ("/regex/escape/%d", ++total); \
1127 g_test_add_data_func (path, data, test_escape); \
1132 const gchar *pattern;
1133 const gchar *string;
1135 gint start_position;
1140 test_match_all_full (gconstpointer d)
1142 const TestMatchAllData *data = d;
1144 GMatchInfo *match_info;
1150 regex = g_regex_new (data->pattern, 0, 0, NULL);
1151 match_ok = g_regex_match_all_full (regex, data->string, data->string_len, data->start_position,
1152 0, &match_info, NULL);
1154 if (g_slist_length (data->expected) == 0)
1155 g_assert (!match_ok);
1157 g_assert (match_ok);
1159 match_count = g_match_info_get_match_count (match_info);
1160 g_assert_cmpint (match_count, ==, g_slist_length (data->expected));
1162 l_exp = data->expected;
1163 for (i = 0; i < match_count; i++)
1166 gchar *matched_string;
1167 Match *exp = l_exp->data;
1169 matched_string = g_match_info_fetch (match_info, i);
1170 g_match_info_fetch_pos (match_info, i, &start, &end);
1172 g_assert_cmpstr (exp->string, ==, matched_string);
1173 g_assert_cmpint (exp->start, ==, start);
1174 g_assert_cmpint (exp->end, ==, end);
1176 g_free (matched_string);
1178 l_exp = g_slist_next (l_exp);
1181 g_match_info_free (match_info);
1182 g_regex_unref (regex);
1186 test_match_all (gconstpointer d)
1188 const TestMatchAllData *data = d;
1190 GMatchInfo *match_info;
1196 regex = g_regex_new (data->pattern, 0, 0, NULL);
1197 match_ok = g_regex_match_all (regex, data->string, 0, &match_info);
1199 if (g_slist_length (data->expected) == 0)
1200 g_assert (!match_ok);
1202 g_assert (match_ok);
1204 match_count = g_match_info_get_match_count (match_info);
1205 g_assert_cmpint (match_count, ==, g_slist_length (data->expected));
1207 l_exp = data->expected;
1208 for (i = 0; i < match_count; i++)
1211 gchar *matched_string;
1212 Match *exp = l_exp->data;
1214 matched_string = g_match_info_fetch (match_info, i);
1215 g_match_info_fetch_pos (match_info, i, &start, &end);
1217 g_assert_cmpstr (exp->string, ==, matched_string);
1218 g_assert_cmpint (exp->start, ==, start);
1219 g_assert_cmpint (exp->end, ==, end);
1221 g_free (matched_string);
1223 l_exp = g_slist_next (l_exp);
1226 g_match_info_free (match_info);
1227 g_regex_unref (regex);
1230 #define TEST_MATCH_ALL0(_pattern, _string, _string_len, _start_position) { \
1231 TestMatchAllData *data; \
1233 data = g_new0 (TestMatchAllData, 1); \
1234 data->pattern = _pattern; \
1235 data->string = _string; \
1236 data->string_len = _string_len; \
1237 data->start_position = _start_position; \
1238 data->expected = NULL; \
1239 path = g_strdup_printf ("/regex/match-all-full0/%d", ++total); \
1240 g_test_add_data_func (path, data, test_match_all_full); \
1242 if (_string_len == -1 && _start_position == 0) { \
1243 path = g_strdup_printf ("/regex/match-all0/%d", ++total); \
1244 g_test_add_data_func (path, data, test_match_all); \
1249 #define TEST_MATCH_ALL1(_pattern, _string, _string_len, _start_position, \
1251 TestMatchAllData *data; \
1254 data = g_new0 (TestMatchAllData, 1); \
1255 data->pattern = _pattern; \
1256 data->string = _string; \
1257 data->string_len = _string_len; \
1258 data->start_position = _start_position; \
1259 data->expected = NULL; \
1260 match = g_new0 (Match, 1); \
1261 match->string = t1; \
1262 match->start = s1; \
1264 data->expected = g_slist_append (data->expected, match); \
1265 path = g_strdup_printf ("/regex/match-all-full1/%d", ++total); \
1266 g_test_add_data_func (path, data, test_match_all_full); \
1268 if (_string_len == -1 && _start_position == 0) { \
1269 path = g_strdup_printf ("/regex/match-all1/%d", ++total); \
1270 g_test_add_data_func (path, data, test_match_all); \
1275 #define TEST_MATCH_ALL2(_pattern, _string, _string_len, _start_position, \
1276 t1, s1, e1, t2, s2, e2) { \
1277 TestMatchAllData *data; \
1280 data = g_new0 (TestMatchAllData, 1); \
1281 data->pattern = _pattern; \
1282 data->string = _string; \
1283 data->string_len = _string_len; \
1284 data->start_position = _start_position; \
1285 data->expected = NULL; \
1286 match = g_new0 (Match, 1); \
1287 match->string = t1; \
1288 match->start = s1; \
1290 data->expected = g_slist_append (data->expected, match); \
1291 match = g_new0 (Match, 1); \
1292 match->string = t2; \
1293 match->start = s2; \
1295 data->expected = g_slist_append (data->expected, match); \
1296 path = g_strdup_printf ("/regex/match-all-full2/%d", ++total); \
1297 g_test_add_data_func (path, data, test_match_all_full); \
1299 if (_string_len == -1 && _start_position == 0) { \
1300 path = g_strdup_printf ("/regex/match-all2/%d", ++total); \
1301 g_test_add_data_func (path, data, test_match_all); \
1306 #define TEST_MATCH_ALL3(_pattern, _string, _string_len, _start_position, \
1307 t1, s1, e1, t2, s2, e2, t3, s3, e3) { \
1308 TestMatchAllData *data; \
1311 data = g_new0 (TestMatchAllData, 1); \
1312 data->pattern = _pattern; \
1313 data->string = _string; \
1314 data->string_len = _string_len; \
1315 data->start_position = _start_position; \
1316 data->expected = NULL; \
1317 match = g_new0 (Match, 1); \
1318 match->string = t1; \
1319 match->start = s1; \
1321 data->expected = g_slist_append (data->expected, match); \
1322 match = g_new0 (Match, 1); \
1323 match->string = t2; \
1324 match->start = s2; \
1326 data->expected = g_slist_append (data->expected, match); \
1327 match = g_new0 (Match, 1); \
1328 match->string = t3; \
1329 match->start = s3; \
1331 data->expected = g_slist_append (data->expected, match); \
1332 path = g_strdup_printf ("/regex/match-all-full3/%d", ++total); \
1333 g_test_add_data_func (path, data, test_match_all_full); \
1335 if (_string_len == -1 && _start_position == 0) { \
1336 path = g_strdup_printf ("/regex/match-all3/%d", ++total); \
1337 g_test_add_data_func (path, data, test_match_all); \
1342 #define PCRE_UTF8 0x00000800
1343 #define PCRE_NO_UTF8_CHECK 0x00002000
1344 #define PCRE_NEWLINE_ANY 0x00400000
1345 #define PCRE_UCP 0x20000000
1350 GRegexCompileFlags cflags = G_REGEX_CASELESS | G_REGEX_EXTENDED | G_REGEX_OPTIMIZE;
1351 GRegexMatchFlags mflags = G_REGEX_MATCH_NOTBOL | G_REGEX_MATCH_PARTIAL;
1354 regex = g_regex_new ("[A-Z]+", cflags, mflags, NULL);
1356 g_assert (regex != NULL);
1357 g_assert_cmpint (g_regex_get_compile_flags (regex), ==, cflags|PCRE_UTF8|PCRE_NO_UTF8_CHECK|PCRE_NEWLINE_ANY|PCRE_UCP );
1358 g_assert_cmpint (g_regex_get_match_flags (regex), ==, mflags|PCRE_NO_UTF8_CHECK);
1360 g_regex_unref (regex);
1370 regex = g_regex_new ("a\\", 0, 0, &error);
1371 g_assert (regex == NULL);
1372 g_assert_error (error, G_REGEX_ERROR, G_REGEX_ERROR_STRAY_BACKSLASH);
1373 g_clear_error (&error);
1374 regex = g_regex_new ("a\\c", 0, 0, &error);
1375 g_assert (regex == NULL);
1376 g_assert_error (error, G_REGEX_ERROR, G_REGEX_ERROR_MISSING_CONTROL_CHAR);
1377 g_clear_error (&error);
1378 regex = g_regex_new ("a\\l", 0, 0, &error);
1379 g_assert (regex == NULL);
1380 g_assert_error (error, G_REGEX_ERROR, G_REGEX_ERROR_UNRECOGNIZED_ESCAPE);
1381 g_clear_error (&error);
1382 regex = g_regex_new ("a{4,2}", 0, 0, &error);
1383 g_assert (regex == NULL);
1384 g_assert_error (error, G_REGEX_ERROR, G_REGEX_ERROR_QUANTIFIERS_OUT_OF_ORDER);
1385 g_clear_error (&error);
1386 regex = g_regex_new ("a{999999,}", 0, 0, &error);
1387 g_assert (regex == NULL);
1388 g_assert_error (error, G_REGEX_ERROR, G_REGEX_ERROR_QUANTIFIER_TOO_BIG);
1389 g_clear_error (&error);
1390 regex = g_regex_new ("[a-z", 0, 0, &error);
1391 g_assert (regex == NULL);
1392 g_assert_error (error, G_REGEX_ERROR, G_REGEX_ERROR_UNTERMINATED_CHARACTER_CLASS);
1393 g_clear_error (&error);
1395 regex = g_regex_new ("[\\b]", 0, 0, &error);
1396 g_assert (regex == NULL);
1397 g_assert_error (error, G_REGEX_ERROR, G_REGEX_ERROR_INVALID_ESCAPE_IN_CHARACTER_CLASS);
1398 g_clear_error (&error);
1400 regex = g_regex_new ("[z-a]", 0, 0, &error);
1401 g_assert (regex == NULL);
1402 g_assert_error (error, G_REGEX_ERROR, G_REGEX_ERROR_RANGE_OUT_OF_ORDER);
1403 g_clear_error (&error);
1404 regex = g_regex_new ("{2,4}", 0, 0, &error);
1405 g_assert (regex == NULL);
1406 g_assert_error (error, G_REGEX_ERROR, G_REGEX_ERROR_NOTHING_TO_REPEAT);
1407 g_clear_error (&error);
1408 regex = g_regex_new ("a(?u)", 0, 0, &error);
1409 g_assert (regex == NULL);
1410 g_assert_error (error, G_REGEX_ERROR, G_REGEX_ERROR_UNRECOGNIZED_CHARACTER);
1411 g_clear_error (&error);
1412 regex = g_regex_new ("a(?<$foo)bar", 0, 0, &error);
1413 g_assert (regex == NULL);
1414 g_assert_error (error, G_REGEX_ERROR, G_REGEX_ERROR_UNRECOGNIZED_CHARACTER);
1415 g_clear_error (&error);
1416 regex = g_regex_new ("a[:alpha:]b", 0, 0, &error);
1417 g_assert (regex == NULL);
1418 g_assert_error (error, G_REGEX_ERROR, G_REGEX_ERROR_POSIX_NAMED_CLASS_OUTSIDE_CLASS);
1419 g_clear_error (&error);
1420 regex = g_regex_new ("a(b", 0, 0, &error);
1421 g_assert (regex == NULL);
1422 g_assert_error (error, G_REGEX_ERROR, G_REGEX_ERROR_UNMATCHED_PARENTHESIS);
1423 g_clear_error (&error);
1424 regex = g_regex_new ("a)b", 0, 0, &error);
1425 g_assert (regex == NULL);
1426 g_assert_error (error, G_REGEX_ERROR, G_REGEX_ERROR_UNMATCHED_PARENTHESIS);
1427 g_clear_error (&error);
1428 regex = g_regex_new ("a(?R", 0, 0, &error);
1429 g_assert (regex == NULL);
1430 g_assert_error (error, G_REGEX_ERROR, G_REGEX_ERROR_UNMATCHED_PARENTHESIS);
1431 g_clear_error (&error);
1432 regex = g_regex_new ("a(?-54", 0, 0, &error);
1433 g_assert (regex == NULL);
1434 g_assert_error (error, G_REGEX_ERROR, G_REGEX_ERROR_UNMATCHED_PARENTHESIS);
1435 g_clear_error (&error);
1436 regex = g_regex_new ("a(?#abc", 0, 0, &error);
1437 g_assert (regex == NULL);
1438 g_assert_error (error, G_REGEX_ERROR, G_REGEX_ERROR_UNTERMINATED_COMMENT);
1439 g_clear_error (&error);
1440 regex = g_regex_new ("a[[:fubar:]]b", 0, 0, &error);
1441 g_assert (regex == NULL);
1442 g_assert_error (error, G_REGEX_ERROR, G_REGEX_ERROR_UNKNOWN_POSIX_CLASS_NAME);
1443 g_clear_error (&error);
1447 test_properties (void)
1456 regex = g_regex_new ("\\p{L}\\p{Ll}\\p{Lu}\\p{L&}\\p{N}\\p{Nd}", G_REGEX_OPTIMIZE, 0, &error);
1457 res = g_regex_match (regex, "ppPP01", 0, &match);
1459 str = g_match_info_fetch (match, 0);
1460 g_assert_cmpstr (str, ==, "ppPP01");
1463 g_match_info_free (match);
1464 g_regex_unref (regex);
1477 regex = g_regex_new ("[abc\\x{0B1E}\\p{Mn}\\x{0391}-\\x{03A9}]", G_REGEX_OPTIMIZE, 0, &error);
1478 res = g_regex_match (regex, "a:b:\340\254\236:\333\253:\316\240", 0, &match);
1480 str = g_match_info_fetch (match, 0);
1481 g_assert_cmpstr (str, ==, "a");
1483 res = g_match_info_next (match, NULL);
1485 str = g_match_info_fetch (match, 0);
1486 g_assert_cmpstr (str, ==, "b");
1488 res = g_match_info_next (match, NULL);
1490 str = g_match_info_fetch (match, 0);
1491 g_assert_cmpstr (str, ==, "\340\254\236");
1493 res = g_match_info_next (match, NULL);
1495 str = g_match_info_fetch (match, 0);
1496 g_assert_cmpstr (str, ==, "\333\253");
1498 res = g_match_info_next (match, NULL);
1500 str = g_match_info_fetch (match, 0);
1501 g_assert_cmpstr (str, ==, "\316\240");
1504 res = g_match_info_next (match, NULL);
1507 g_match_info_free (match);
1508 g_regex_unref (regex);
1511 /* examples for lookahead assertions taken from pcrepattern(3) */
1513 test_lookahead (void)
1523 regex = g_regex_new ("\\w+(?=;)", G_REGEX_OPTIMIZE, 0, &error);
1525 g_assert_no_error (error);
1526 res = g_regex_match (regex, "word1 word2: word3;", 0, &match);
1528 g_assert (g_match_info_matches (match));
1529 g_assert_cmpint (g_match_info_get_match_count (match), ==, 1);
1530 str = g_match_info_fetch (match, 0);
1531 g_assert_cmpstr (str, ==, "word3");
1533 g_match_info_free (match);
1534 g_regex_unref (regex);
1537 regex = g_regex_new ("foo(?!bar)", G_REGEX_OPTIMIZE, 0, &error);
1539 g_assert_no_error (error);
1540 res = g_regex_match (regex, "foobar foobaz", 0, &match);
1542 g_assert (g_match_info_matches (match));
1543 g_assert_cmpint (g_match_info_get_match_count (match), ==, 1);
1544 res = g_match_info_fetch_pos (match, 0, &start, &end);
1546 g_assert_cmpint (start, ==, 7);
1547 g_assert_cmpint (end, ==, 10);
1548 g_match_info_free (match);
1549 g_regex_unref (regex);
1552 regex = g_regex_new ("(?!bar)foo", G_REGEX_OPTIMIZE, 0, &error);
1554 g_assert_no_error (error);
1555 res = g_regex_match (regex, "foobar foobaz", 0, &match);
1557 g_assert (g_match_info_matches (match));
1558 g_assert_cmpint (g_match_info_get_match_count (match), ==, 1);
1559 res = g_match_info_fetch_pos (match, 0, &start, &end);
1561 g_assert_cmpint (start, ==, 0);
1562 g_assert_cmpint (end, ==, 3);
1563 res = g_match_info_next (match, &error);
1565 g_assert_no_error (error);
1566 res = g_match_info_fetch_pos (match, 0, &start, &end);
1568 g_assert_cmpint (start, ==, 7);
1569 g_assert_cmpint (end, ==, 10);
1570 g_match_info_free (match);
1571 g_regex_unref (regex);
1574 /* examples for lookbehind assertions taken from pcrepattern(3) */
1576 test_lookbehind (void)
1585 regex = g_regex_new ("(?<!foo)bar", G_REGEX_OPTIMIZE, 0, &error);
1587 g_assert_no_error (error);
1588 res = g_regex_match (regex, "foobar boobar", 0, &match);
1590 g_assert (g_match_info_matches (match));
1591 g_assert_cmpint (g_match_info_get_match_count (match), ==, 1);
1592 res = g_match_info_fetch_pos (match, 0, &start, &end);
1594 g_assert_cmpint (start, ==, 10);
1595 g_assert_cmpint (end, ==, 13);
1596 g_match_info_free (match);
1597 g_regex_unref (regex);
1600 regex = g_regex_new ("(?<=bullock|donkey) poo", G_REGEX_OPTIMIZE, 0, &error);
1602 g_assert_no_error (error);
1603 res = g_regex_match (regex, "don poo, and bullock poo", 0, &match);
1605 g_assert (g_match_info_matches (match));
1606 g_assert_cmpint (g_match_info_get_match_count (match), ==, 1);
1607 res = g_match_info_fetch_pos (match, 0, &start, NULL);
1609 g_assert_cmpint (start, ==, 20);
1610 g_match_info_free (match);
1611 g_regex_unref (regex);
1613 regex = g_regex_new ("(?<!dogs?|cats?) x", G_REGEX_OPTIMIZE, 0, &error);
1614 g_assert (regex == NULL);
1615 g_assert_error (error, G_REGEX_ERROR, G_REGEX_ERROR_VARIABLE_LENGTH_LOOKBEHIND);
1616 g_clear_error (&error);
1618 regex = g_regex_new ("(?<=ab(c|de)) foo", G_REGEX_OPTIMIZE, 0, &error);
1619 g_assert (regex == NULL);
1620 g_assert_error (error, G_REGEX_ERROR, G_REGEX_ERROR_VARIABLE_LENGTH_LOOKBEHIND);
1621 g_clear_error (&error);
1623 regex = g_regex_new ("(?<=abc|abde)foo", G_REGEX_OPTIMIZE, 0, &error);
1625 g_assert_no_error (error);
1626 res = g_regex_match (regex, "abfoo, abdfoo, abcfoo", 0, &match);
1628 g_assert (g_match_info_matches (match));
1629 res = g_match_info_fetch_pos (match, 0, &start, NULL);
1631 g_assert_cmpint (start, ==, 18);
1632 g_match_info_free (match);
1633 g_regex_unref (regex);
1635 regex = g_regex_new ("^.*+(?<=abcd)", G_REGEX_OPTIMIZE, 0, &error);
1637 g_assert_no_error (error);
1638 res = g_regex_match (regex, "abcabcabcabcabcabcabcabcabcd", 0, &match);
1640 g_assert (g_match_info_matches (match));
1641 g_match_info_free (match);
1642 g_regex_unref (regex);
1644 regex = g_regex_new ("(?<=\\d{3})(?<!999)foo", G_REGEX_OPTIMIZE, 0, &error);
1646 g_assert_no_error (error);
1647 res = g_regex_match (regex, "999foo 123abcfoo 123foo", 0, &match);
1649 g_assert (g_match_info_matches (match));
1650 res = g_match_info_fetch_pos (match, 0, &start, NULL);
1652 g_assert_cmpint (start, ==, 20);
1653 g_match_info_free (match);
1654 g_regex_unref (regex);
1656 regex = g_regex_new ("(?<=\\d{3}...)(?<!999)foo", G_REGEX_OPTIMIZE, 0, &error);
1658 g_assert_no_error (error);
1659 res = g_regex_match (regex, "999foo 123abcfoo 123foo", 0, &match);
1661 g_assert (g_match_info_matches (match));
1662 res = g_match_info_fetch_pos (match, 0, &start, NULL);
1664 g_assert_cmpint (start, ==, 13);
1665 g_match_info_free (match);
1666 g_regex_unref (regex);
1668 regex = g_regex_new ("(?<=\\d{3}(?!999)...)foo", G_REGEX_OPTIMIZE, 0, &error);
1670 g_assert_no_error (error);
1671 res = g_regex_match (regex, "999foo 123abcfoo 123foo", 0, &match);
1673 g_assert (g_match_info_matches (match));
1674 res = g_match_info_fetch_pos (match, 0, &start, NULL);
1676 g_assert_cmpint (start, ==, 13);
1677 g_match_info_free (match);
1678 g_regex_unref (regex);
1680 regex = g_regex_new ("(?<=(?<!foo)bar)baz", G_REGEX_OPTIMIZE, 0, &error);
1682 g_assert_no_error (error);
1683 res = g_regex_match (regex, "foobarbaz barfoobaz barbarbaz", 0, &match);
1685 g_assert (g_match_info_matches (match));
1686 res = g_match_info_fetch_pos (match, 0, &start, NULL);
1688 g_assert_cmpint (start, ==, 26);
1689 g_match_info_free (match);
1690 g_regex_unref (regex);
1693 /* examples for subpatterns taken from pcrepattern(3) */
1695 test_subpattern (void)
1705 regex = g_regex_new ("cat(aract|erpillar|)", G_REGEX_OPTIMIZE, 0, &error);
1707 g_assert_no_error (error);
1708 res = g_regex_match_all (regex, "caterpillar", 0, &match);
1710 g_assert (g_match_info_matches (match));
1711 g_assert_cmpint (g_match_info_get_match_count (match), ==, 2);
1712 str = g_match_info_fetch (match, 0);
1713 g_assert_cmpstr (str, ==, "caterpillar");
1715 str = g_match_info_fetch (match, 1);
1716 g_assert_cmpstr (str, ==, "cat");
1718 g_match_info_free (match);
1719 g_regex_unref (regex);
1721 regex = g_regex_new ("the ((red|white) (king|queen))", G_REGEX_OPTIMIZE, 0, &error);
1723 g_assert_no_error (error);
1724 res = g_regex_match (regex, "the red king", 0, &match);
1726 g_assert (g_match_info_matches (match));
1727 g_assert_cmpint (g_match_info_get_match_count (match), ==, 4);
1728 str = g_match_info_fetch (match, 0);
1729 g_assert_cmpstr (str, ==, "the red king");
1731 str = g_match_info_fetch (match, 1);
1732 g_assert_cmpstr (str, ==, "red king");
1734 str = g_match_info_fetch (match, 2);
1735 g_assert_cmpstr (str, ==, "red");
1737 str = g_match_info_fetch (match, 3);
1738 g_assert_cmpstr (str, ==, "king");
1740 g_match_info_free (match);
1741 g_regex_unref (regex);
1743 regex = g_regex_new ("the ((?:red|white) (king|queen))", G_REGEX_OPTIMIZE, 0, &error);
1745 g_assert_no_error (error);
1746 res = g_regex_match (regex, "the white queen", 0, &match);
1748 g_assert (g_match_info_matches (match));
1749 g_assert_cmpint (g_match_info_get_match_count (match), ==, 3);
1750 str = g_match_info_fetch (match, 0);
1751 g_assert_cmpstr (str, ==, "the white queen");
1753 str = g_match_info_fetch (match, 1);
1754 g_assert_cmpstr (str, ==, "white queen");
1756 str = g_match_info_fetch (match, 2);
1757 g_assert_cmpstr (str, ==, "queen");
1759 g_match_info_free (match);
1760 g_regex_unref (regex);
1762 regex = g_regex_new ("(?|(Sat)(ur)|(Sun))day (morning|afternoon)", G_REGEX_OPTIMIZE, 0, &error);
1764 g_assert_no_error (error);
1765 res = g_regex_match (regex, "Saturday morning", 0, &match);
1767 g_assert (g_match_info_matches (match));
1768 g_assert_cmpint (g_match_info_get_match_count (match), ==, 4);
1769 str = g_match_info_fetch (match, 1);
1770 g_assert_cmpstr (str, ==, "Sat");
1772 str = g_match_info_fetch (match, 2);
1773 g_assert_cmpstr (str, ==, "ur");
1775 str = g_match_info_fetch (match, 3);
1776 g_assert_cmpstr (str, ==, "morning");
1778 g_match_info_free (match);
1779 g_regex_unref (regex);
1781 regex = g_regex_new ("(?|(abc)|(def))\\1", G_REGEX_OPTIMIZE, 0, &error);
1783 g_assert_no_error (error);
1784 res = g_regex_match (regex, "abcabc abcdef defabc defdef", 0, &match);
1786 g_assert (g_match_info_matches (match));
1787 res = g_match_info_fetch_pos (match, 0, &start, NULL);
1789 g_assert_cmpint (start, ==, 0);
1790 res = g_match_info_next (match, &error);
1792 res = g_match_info_fetch_pos (match, 0, &start, NULL);
1794 g_assert_cmpint (start, ==, 21);
1795 g_match_info_free (match);
1796 g_regex_unref (regex);
1798 regex = g_regex_new ("(?|(abc)|(def))(?1)", G_REGEX_OPTIMIZE, 0, &error);
1800 g_assert_no_error (error);
1801 res = g_regex_match (regex, "abcabc abcdef defabc defdef", 0, &match);
1803 g_assert (g_match_info_matches (match));
1804 res = g_match_info_fetch_pos (match, 0, &start, NULL);
1806 g_assert_cmpint (start, ==, 0);
1807 res = g_match_info_next (match, &error);
1809 res = g_match_info_fetch_pos (match, 0, &start, NULL);
1811 g_assert_cmpint (start, ==, 14);
1812 g_match_info_free (match);
1813 g_regex_unref (regex);
1815 regex = g_regex_new ("(?<DN>Mon|Fri|Sun)(?:day)?|(?<DN>Tue)(?:sday)?|(?<DN>Wed)(?:nesday)?|(?<DN>Thu)(?:rsday)?|(?<DN>Sat)(?:urday)?", G_REGEX_OPTIMIZE|G_REGEX_DUPNAMES, 0, &error);
1817 g_assert_no_error (error);
1818 res = g_regex_match (regex, "Mon Tuesday Wed Saturday", 0, &match);
1820 g_assert (g_match_info_matches (match));
1821 str = g_match_info_fetch_named (match, "DN");
1822 g_assert_cmpstr (str, ==, "Mon");
1824 res = g_match_info_next (match, &error);
1826 str = g_match_info_fetch_named (match, "DN");
1827 g_assert_cmpstr (str, ==, "Tue");
1829 res = g_match_info_next (match, &error);
1831 str = g_match_info_fetch_named (match, "DN");
1832 g_assert_cmpstr (str, ==, "Wed");
1834 res = g_match_info_next (match, &error);
1836 str = g_match_info_fetch_named (match, "DN");
1837 g_assert_cmpstr (str, ==, "Sat");
1839 g_match_info_free (match);
1840 g_regex_unref (regex);
1842 regex = g_regex_new ("^(a|b\\1)+$", G_REGEX_OPTIMIZE|G_REGEX_DUPNAMES, 0, &error);
1844 g_assert_no_error (error);
1845 res = g_regex_match (regex, "aaaaaaaaaaaaaaaa", 0, &match);
1847 g_assert (g_match_info_matches (match));
1848 g_match_info_free (match);
1849 res = g_regex_match (regex, "ababbaa", 0, &match);
1851 g_assert (g_match_info_matches (match));
1852 g_match_info_free (match);
1853 g_regex_unref (regex);
1856 /* examples for conditions taken from pcrepattern(3) */
1858 test_condition (void)
1866 regex = g_regex_new ("^(a+)(\\()?[^()]+(?(-1)\\))(b+)$", G_REGEX_OPTIMIZE, 0, &error);
1868 g_assert_no_error (error);
1869 res = g_regex_match (regex, "a(zzzzzz)b", 0, &match);
1871 g_assert (g_match_info_matches (match));
1872 g_match_info_free (match);
1873 res = g_regex_match (regex, "aaazzzzzzbbb", 0, &match);
1875 g_assert (g_match_info_matches (match));
1876 g_match_info_free (match);
1877 g_regex_unref (regex);
1880 regex = g_regex_new ("^(a+)(?<OPEN>\\()?[^()]+(?(<OPEN>)\\))(b+)$", G_REGEX_OPTIMIZE, 0, &error);
1882 g_assert_no_error (error);
1883 res = g_regex_match (regex, "a(zzzzzz)b", 0, &match);
1885 g_assert (g_match_info_matches (match));
1886 g_match_info_free (match);
1887 res = g_regex_match (regex, "aaazzzzzzbbb", 0, &match);
1889 g_assert (g_match_info_matches (match));
1890 g_match_info_free (match);
1891 g_regex_unref (regex);
1893 regex = g_regex_new ("^(a+)(?(+1)\\[|\\<)?[^()]+(\\])?(b+)$", G_REGEX_OPTIMIZE, 0, &error);
1895 g_assert_no_error (error);
1896 res = g_regex_match (regex, "a[zzzzzz]b", 0, &match);
1898 g_assert (g_match_info_matches (match));
1899 g_match_info_free (match);
1900 res = g_regex_match (regex, "aaa<zzzzzzbbb", 0, &match);
1902 g_assert (g_match_info_matches (match));
1903 g_match_info_free (match);
1904 g_regex_unref (regex);
1906 regex = g_regex_new ("(?(DEFINE) (?<byte> 2[0-4]\\d | 25[0-5] | 1\\d\\d | [1-9]?\\d) )"
1907 "\\b (?&byte) (\\.(?&byte)){3} \\b",
1908 G_REGEX_OPTIMIZE|G_REGEX_EXTENDED, 0, &error);
1910 g_assert_no_error (error);
1911 res = g_regex_match (regex, "128.0.0.1", 0, &match);
1913 g_assert (g_match_info_matches (match));
1914 g_match_info_free (match);
1915 res = g_regex_match (regex, "192.168.1.1", 0, &match);
1917 g_assert (g_match_info_matches (match));
1918 g_match_info_free (match);
1919 res = g_regex_match (regex, "209.132.180.167", 0, &match);
1921 g_assert (g_match_info_matches (match));
1922 g_match_info_free (match);
1923 g_regex_unref (regex);
1925 regex = g_regex_new ("^(?(?=[^a-z]*[a-z])"
1926 "\\d{2}-[a-z]{3}-\\d{2} | \\d{2}-\\d{2}-\\d{2} )$",
1927 G_REGEX_OPTIMIZE|G_REGEX_EXTENDED, 0, &error);
1929 g_assert_no_error (error);
1930 res = g_regex_match (regex, "01-abc-24", 0, &match);
1932 g_assert (g_match_info_matches (match));
1933 g_match_info_free (match);
1934 res = g_regex_match (regex, "01-23-45", 0, &match);
1936 g_assert (g_match_info_matches (match));
1937 g_match_info_free (match);
1938 res = g_regex_match (regex, "01-uv-45", 0, &match);
1940 g_assert (!g_match_info_matches (match));
1941 g_match_info_free (match);
1942 res = g_regex_match (regex, "01-234-45", 0, &match);
1944 g_assert (!g_match_info_matches (match));
1945 g_match_info_free (match);
1946 g_regex_unref (regex);
1949 /* examples for recursion taken from pcrepattern(3) */
1951 test_recursion (void)
1960 regex = g_regex_new ("\\( ( [^()]++ | (?R) )* \\)", G_REGEX_OPTIMIZE|G_REGEX_EXTENDED, 0, &error);
1962 g_assert_no_error (error);
1963 res = g_regex_match (regex, "(middle)", 0, &match);
1965 g_assert (g_match_info_matches (match));
1966 g_match_info_free (match);
1967 res = g_regex_match (regex, "((((((((((((((((middle))))))))))))))))", 0, &match);
1969 g_assert (g_match_info_matches (match));
1970 g_match_info_free (match);
1971 res = g_regex_match (regex, "(((xxx(((", 0, &match);
1973 g_assert (!g_match_info_matches (match));
1974 g_match_info_free (match);
1975 g_regex_unref (regex);
1977 regex = g_regex_new ("^( \\( ( [^()]++ | (?1) )* \\) )$", G_REGEX_OPTIMIZE|G_REGEX_EXTENDED, 0, &error);
1979 g_assert_no_error (error);
1980 res = g_regex_match (regex, "((((((((((((((((middle))))))))))))))))", 0, &match);
1982 g_assert (g_match_info_matches (match));
1983 g_match_info_free (match);
1984 res = g_regex_match (regex, "(((xxx((()", 0, &match);
1986 g_assert (!g_match_info_matches (match));
1987 g_match_info_free (match);
1988 g_regex_unref (regex);
1990 regex = g_regex_new ("^(?<pn> \\( ( [^()]++ | (?&pn) )* \\) )$", G_REGEX_OPTIMIZE|G_REGEX_EXTENDED, 0, &error);
1992 g_assert_no_error (error);
1993 g_regex_match (regex, "(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()", 0, &match);
1995 g_assert (!g_match_info_matches (match));
1996 g_match_info_free (match);
1997 g_regex_unref (regex);
1999 regex = g_regex_new ("< (?: (?(R) \\d++ | [^<>]*+) | (?R)) * >", G_REGEX_OPTIMIZE|G_REGEX_EXTENDED, 0, &error);
2001 g_assert_no_error (error);
2002 res = g_regex_match (regex, "<ab<01<23<4>>>>", 0, &match);
2004 g_assert (g_match_info_matches (match));
2005 res = g_match_info_fetch_pos (match, 0, &start, NULL);
2007 g_assert_cmpint (start, ==, 0);
2008 g_match_info_free (match);
2009 res = g_regex_match (regex, "<ab<01<xx<x>>>>", 0, &match);
2011 g_assert (g_match_info_matches (match));
2012 res = g_match_info_fetch_pos (match, 0, &start, NULL);
2014 g_assert_cmpint (start, >, 0);
2015 g_match_info_free (match);
2016 g_regex_unref (regex);
2018 regex = g_regex_new ("^((.)(?1)\\2|.)$", G_REGEX_OPTIMIZE, 0, &error);
2020 g_assert_no_error (error);
2021 res = g_regex_match (regex, "abcdcba", 0, &match);
2023 g_assert (g_match_info_matches (match));
2024 g_match_info_free (match);
2025 res = g_regex_match (regex, "abcddcba", 0, &match);
2027 g_assert (!g_match_info_matches (match));
2028 g_match_info_free (match);
2029 g_regex_unref (regex);
2031 regex = g_regex_new ("^(?:((.)(?1)\\2|)|((.)(?3)\\4|.))$", G_REGEX_OPTIMIZE, 0, &error);
2033 g_assert_no_error (error);
2034 res = g_regex_match (regex, "abcdcba", 0, &match);
2036 g_assert (g_match_info_matches (match));
2037 g_match_info_free (match);
2038 res = g_regex_match (regex, "abcddcba", 0, &match);
2040 g_assert (g_match_info_matches (match));
2041 g_match_info_free (match);
2042 g_regex_unref (regex);
2044 regex = g_regex_new ("^\\W*+(?:((.)\\W*+(?1)\\W*+\\2|)|((.)\\W*+(?3)\\W*+\\4|\\W*+.\\W*+))\\W*+$", G_REGEX_OPTIMIZE|G_REGEX_CASELESS, 0, &error);
2046 g_assert_no_error (error);
2047 res = g_regex_match (regex, "abcdcba", 0, &match);
2049 g_assert (g_match_info_matches (match));
2050 g_match_info_free (match);
2051 res = g_regex_match (regex, "A man, a plan, a canal: Panama!", 0, &match);
2053 g_assert (g_match_info_matches (match));
2054 g_match_info_free (match);
2055 res = g_regex_match (regex, "Oozy rat in a sanitary zoo", 0, &match);
2057 g_assert (g_match_info_matches (match));
2058 g_match_info_free (match);
2059 g_regex_unref (regex);
2063 test_multiline (void)
2069 g_test_bug ("640489");
2071 regex = g_regex_new ("^a$", G_REGEX_MULTILINE|G_REGEX_DOTALL, 0, NULL);
2074 g_regex_match (regex, "a\nb\na", 0, &info);
2075 while (g_match_info_matches (info))
2078 g_match_info_next (info, NULL);
2080 g_match_info_free (info);
2081 g_regex_unref (regex);
2083 g_assert_cmpint (count, ==, 2);
2087 main (int argc, char *argv[])
2089 setlocale (LC_ALL, "");
2091 g_test_init (&argc, &argv, NULL);
2093 g_test_bug_base ("http://bugzilla.gnome.org/");
2095 g_test_add_func ("/regex/basic", test_basic);
2096 g_test_add_func ("/regex/compile", test_compile);
2097 g_test_add_func ("/regex/properties", test_properties);
2098 g_test_add_func ("/regex/class", test_class);
2099 g_test_add_func ("/regex/lookahead", test_lookahead);
2100 g_test_add_func ("/regex/lookbehind", test_lookbehind);
2101 g_test_add_func ("/regex/subpattern", test_subpattern);
2102 g_test_add_func ("/regex/condition", test_condition);
2103 g_test_add_func ("/regex/recursion", test_recursion);
2104 g_test_add_func ("/regex/multiline", test_multiline);
2106 /* TEST_NEW(pattern, compile_opts, match_opts) */
2108 TEST_NEW(".*", 0, 0);
2109 TEST_NEW(".*", G_REGEX_OPTIMIZE, 0);
2110 TEST_NEW(".*", G_REGEX_MULTILINE, 0);
2111 TEST_NEW(".*", G_REGEX_DOTALL, 0);
2112 TEST_NEW(".*", G_REGEX_DOTALL, G_REGEX_MATCH_NOTBOL);
2113 TEST_NEW("(123\\d*)[a-zA-Z]+(?P<hello>.*)", 0, 0);
2114 TEST_NEW("(123\\d*)[a-zA-Z]+(?P<hello>.*)", G_REGEX_CASELESS, 0);
2115 TEST_NEW("(123\\d*)[a-zA-Z]+(?P<hello>.*)", G_REGEX_CASELESS | G_REGEX_OPTIMIZE, 0);
2116 TEST_NEW("(?P<A>x)|(?P<A>y)", G_REGEX_DUPNAMES, 0);
2117 TEST_NEW("(?P<A>x)|(?P<A>y)", G_REGEX_DUPNAMES | G_REGEX_OPTIMIZE, 0);
2118 /* This gives "internal error: code overflow" with pcre 6.0 */
2119 TEST_NEW("(?i)(?-i)", 0, 0);
2121 /* TEST_NEW_FAIL(pattern, compile_opts, expected_error) */
2122 TEST_NEW_FAIL("(", 0, G_REGEX_ERROR_UNMATCHED_PARENTHESIS);
2123 TEST_NEW_FAIL(")", 0, G_REGEX_ERROR_UNMATCHED_PARENTHESIS);
2124 TEST_NEW_FAIL("[", 0, G_REGEX_ERROR_UNTERMINATED_CHARACTER_CLASS);
2125 TEST_NEW_FAIL("*", 0, G_REGEX_ERROR_NOTHING_TO_REPEAT);
2126 TEST_NEW_FAIL("?", 0, G_REGEX_ERROR_NOTHING_TO_REPEAT);
2127 TEST_NEW_FAIL("(?P<A>x)|(?P<A>y)", 0, G_REGEX_ERROR_DUPLICATE_SUBPATTERN_NAME);
2129 /* TEST_MATCH_SIMPLE(pattern, string, compile_opts, match_opts, expected) */
2130 TEST_MATCH_SIMPLE("a", "", 0, 0, FALSE);
2131 TEST_MATCH_SIMPLE("a", "a", 0, 0, TRUE);
2132 TEST_MATCH_SIMPLE("a", "ba", 0, 0, TRUE);
2133 TEST_MATCH_SIMPLE("^a", "ba", 0, 0, FALSE);
2134 TEST_MATCH_SIMPLE("a", "ba", G_REGEX_ANCHORED, 0, FALSE);
2135 TEST_MATCH_SIMPLE("a", "ba", 0, G_REGEX_MATCH_ANCHORED, FALSE);
2136 TEST_MATCH_SIMPLE("a", "ab", G_REGEX_ANCHORED, 0, TRUE);
2137 TEST_MATCH_SIMPLE("a", "ab", 0, G_REGEX_MATCH_ANCHORED, TRUE);
2138 TEST_MATCH_SIMPLE("a", "a", G_REGEX_CASELESS, 0, TRUE);
2139 TEST_MATCH_SIMPLE("a", "A", G_REGEX_CASELESS, 0, TRUE);
2140 /* These are needed to test extended properties. */
2141 TEST_MATCH_SIMPLE(AGRAVE, AGRAVE, G_REGEX_CASELESS, 0, TRUE);
2142 TEST_MATCH_SIMPLE(AGRAVE, AGRAVE_UPPER, G_REGEX_CASELESS, 0, TRUE);
2143 TEST_MATCH_SIMPLE("\\p{L}", "a", 0, 0, TRUE);
2144 TEST_MATCH_SIMPLE("\\p{L}", "1", 0, 0, FALSE);
2145 TEST_MATCH_SIMPLE("\\p{L}", AGRAVE, 0, 0, TRUE);
2146 TEST_MATCH_SIMPLE("\\p{L}", AGRAVE_UPPER, 0, 0, TRUE);
2147 TEST_MATCH_SIMPLE("\\p{L}", SHEEN, 0, 0, TRUE);
2148 TEST_MATCH_SIMPLE("\\p{L}", ETH30, 0, 0, FALSE);
2149 TEST_MATCH_SIMPLE("\\p{Ll}", "a", 0, 0, TRUE);
2150 TEST_MATCH_SIMPLE("\\p{Ll}", AGRAVE, 0, 0, TRUE);
2151 TEST_MATCH_SIMPLE("\\p{Ll}", AGRAVE_UPPER, 0, 0, FALSE);
2152 TEST_MATCH_SIMPLE("\\p{Ll}", ETH30, 0, 0, FALSE);
2153 TEST_MATCH_SIMPLE("\\p{Sc}", AGRAVE, 0, 0, FALSE);
2154 TEST_MATCH_SIMPLE("\\p{Sc}", EURO, 0, 0, TRUE);
2155 TEST_MATCH_SIMPLE("\\p{Sc}", ETH30, 0, 0, FALSE);
2156 TEST_MATCH_SIMPLE("\\p{N}", "a", 0, 0, FALSE);
2157 TEST_MATCH_SIMPLE("\\p{N}", "1", 0, 0, TRUE);
2158 TEST_MATCH_SIMPLE("\\p{N}", AGRAVE, 0, 0, FALSE);
2159 TEST_MATCH_SIMPLE("\\p{N}", AGRAVE_UPPER, 0, 0, FALSE);
2160 TEST_MATCH_SIMPLE("\\p{N}", SHEEN, 0, 0, FALSE);
2161 TEST_MATCH_SIMPLE("\\p{N}", ETH30, 0, 0, TRUE);
2162 TEST_MATCH_SIMPLE("\\p{Nd}", "a", 0, 0, FALSE);
2163 TEST_MATCH_SIMPLE("\\p{Nd}", "1", 0, 0, TRUE);
2164 TEST_MATCH_SIMPLE("\\p{Nd}", AGRAVE, 0, 0, FALSE);
2165 TEST_MATCH_SIMPLE("\\p{Nd}", AGRAVE_UPPER, 0, 0, FALSE);
2166 TEST_MATCH_SIMPLE("\\p{Nd}", SHEEN, 0, 0, FALSE);
2167 TEST_MATCH_SIMPLE("\\p{Nd}", ETH30, 0, 0, FALSE);
2168 TEST_MATCH_SIMPLE("\\p{Common}", SHEEN, 0, 0, FALSE);
2169 TEST_MATCH_SIMPLE("\\p{Common}", "a", 0, 0, FALSE);
2170 TEST_MATCH_SIMPLE("\\p{Common}", AGRAVE, 0, 0, FALSE);
2171 TEST_MATCH_SIMPLE("\\p{Common}", AGRAVE_UPPER, 0, 0, FALSE);
2172 TEST_MATCH_SIMPLE("\\p{Common}", ETH30, 0, 0, FALSE);
2173 TEST_MATCH_SIMPLE("\\p{Common}", "%", 0, 0, TRUE);
2174 TEST_MATCH_SIMPLE("\\p{Common}", "1", 0, 0, TRUE);
2175 TEST_MATCH_SIMPLE("\\p{Arabic}", SHEEN, 0, 0, TRUE);
2176 TEST_MATCH_SIMPLE("\\p{Arabic}", "a", 0, 0, FALSE);
2177 TEST_MATCH_SIMPLE("\\p{Arabic}", AGRAVE, 0, 0, FALSE);
2178 TEST_MATCH_SIMPLE("\\p{Arabic}", AGRAVE_UPPER, 0, 0, FALSE);
2179 TEST_MATCH_SIMPLE("\\p{Arabic}", ETH30, 0, 0, FALSE);
2180 TEST_MATCH_SIMPLE("\\p{Arabic}", "%", 0, 0, FALSE);
2181 TEST_MATCH_SIMPLE("\\p{Arabic}", "1", 0, 0, FALSE);
2182 TEST_MATCH_SIMPLE("\\p{Latin}", SHEEN, 0, 0, FALSE);
2183 TEST_MATCH_SIMPLE("\\p{Latin}", "a", 0, 0, TRUE);
2184 TEST_MATCH_SIMPLE("\\p{Latin}", AGRAVE, 0, 0, TRUE);
2185 TEST_MATCH_SIMPLE("\\p{Latin}", AGRAVE_UPPER, 0, 0, TRUE);
2186 TEST_MATCH_SIMPLE("\\p{Latin}", ETH30, 0, 0, FALSE);
2187 TEST_MATCH_SIMPLE("\\p{Latin}", "%", 0, 0, FALSE);
2188 TEST_MATCH_SIMPLE("\\p{Latin}", "1", 0, 0, FALSE);
2189 TEST_MATCH_SIMPLE("\\p{Ethiopic}", SHEEN, 0, 0, FALSE);
2190 TEST_MATCH_SIMPLE("\\p{Ethiopic}", "a", 0, 0, FALSE);
2191 TEST_MATCH_SIMPLE("\\p{Ethiopic}", AGRAVE, 0, 0, FALSE);
2192 TEST_MATCH_SIMPLE("\\p{Ethiopic}", AGRAVE_UPPER, 0, 0, FALSE);
2193 TEST_MATCH_SIMPLE("\\p{Ethiopic}", ETH30, 0, 0, TRUE);
2194 TEST_MATCH_SIMPLE("\\p{Ethiopic}", "%", 0, 0, FALSE);
2195 TEST_MATCH_SIMPLE("\\p{Ethiopic}", "1", 0, 0, FALSE);
2196 TEST_MATCH_SIMPLE("\\p{L}(?<=\\p{Arabic})", SHEEN, 0, 0, TRUE);
2197 TEST_MATCH_SIMPLE("\\p{L}(?<=\\p{Latin})", SHEEN, 0, 0, FALSE);
2198 /* Invalid patterns. */
2199 TEST_MATCH_SIMPLE("\\", "a", 0, 0, FALSE);
2200 TEST_MATCH_SIMPLE("[", "", 0, 0, FALSE);
2202 /* TEST_MATCH(pattern, compile_opts, match_opts, string,
2203 * string_len, start_position, match_opts2, expected) */
2204 TEST_MATCH("a", 0, 0, "a", -1, 0, 0, TRUE);
2205 TEST_MATCH("a", 0, 0, "A", -1, 0, 0, FALSE);
2206 TEST_MATCH("a", G_REGEX_CASELESS, 0, "A", -1, 0, 0, TRUE);
2207 TEST_MATCH("a", 0, 0, "ab", -1, 1, 0, FALSE);
2208 TEST_MATCH("a", 0, 0, "ba", 1, 0, 0, FALSE);
2209 TEST_MATCH("a", 0, 0, "bab", -1, 0, 0, TRUE);
2210 TEST_MATCH("a", 0, 0, "b", -1, 0, 0, FALSE);
2211 TEST_MATCH("a", 0, G_REGEX_ANCHORED, "a", -1, 0, 0, TRUE);
2212 TEST_MATCH("a", 0, G_REGEX_ANCHORED, "ab", -1, 1, 0, FALSE);
2213 TEST_MATCH("a", 0, G_REGEX_ANCHORED, "ba", 1, 0, 0, FALSE);
2214 TEST_MATCH("a", 0, G_REGEX_ANCHORED, "bab", -1, 0, 0, FALSE);
2215 TEST_MATCH("a", 0, G_REGEX_ANCHORED, "b", -1, 0, 0, FALSE);
2216 TEST_MATCH("a", 0, 0, "a", -1, 0, G_REGEX_ANCHORED, TRUE);
2217 TEST_MATCH("a", 0, 0, "ab", -1, 1, G_REGEX_ANCHORED, FALSE);
2218 TEST_MATCH("a", 0, 0, "ba", 1, 0, G_REGEX_ANCHORED, FALSE);
2219 TEST_MATCH("a", 0, 0, "bab", -1, 0, G_REGEX_ANCHORED, FALSE);
2220 TEST_MATCH("a", 0, 0, "b", -1, 0, G_REGEX_ANCHORED, FALSE);
2221 TEST_MATCH("a|b", 0, 0, "a", -1, 0, 0, TRUE);
2222 TEST_MATCH("\\d", 0, 0, EURO, -1, 0, 0, FALSE);
2223 TEST_MATCH("^.$", 0, 0, EURO, -1, 0, 0, TRUE);
2224 TEST_MATCH("^.{3}$", 0, 0, EURO, -1, 0, 0, FALSE);
2225 TEST_MATCH("^.$", G_REGEX_RAW, 0, EURO, -1, 0, 0, FALSE);
2226 TEST_MATCH("^.{3}$", G_REGEX_RAW, 0, EURO, -1, 0, 0, TRUE);
2227 TEST_MATCH(AGRAVE, G_REGEX_CASELESS, 0, AGRAVE_UPPER, -1, 0, 0, TRUE);
2229 /* New lines handling. */
2230 TEST_MATCH("^a\\Rb$", 0, 0, "a\r\nb", -1, 0, 0, TRUE);
2231 TEST_MATCH("^a\\Rb$", 0, 0, "a\nb", -1, 0, 0, TRUE);
2232 TEST_MATCH("^a\\Rb$", 0, 0, "a\rb", -1, 0, 0, TRUE);
2233 TEST_MATCH("^a\\Rb$", 0, 0, "a\n\rb", -1, 0, 0, FALSE);
2234 TEST_MATCH("^a\\R\\Rb$", 0, 0, "a\n\rb", -1, 0, 0, TRUE);
2235 TEST_MATCH("^a\\nb$", 0, 0, "a\r\nb", -1, 0, 0, FALSE);
2236 TEST_MATCH("^a\\r\\nb$", 0, 0, "a\r\nb", -1, 0, 0, TRUE);
2238 TEST_MATCH("^b$", 0, 0, "a\nb\nc", -1, 0, 0, FALSE);
2239 TEST_MATCH("^b$", G_REGEX_MULTILINE, 0, "a\nb\nc", -1, 0, 0, TRUE);
2240 TEST_MATCH("^b$", G_REGEX_MULTILINE, 0, "a\r\nb\r\nc", -1, 0, 0, TRUE);
2241 TEST_MATCH("^b$", G_REGEX_MULTILINE, 0, "a\rb\rc", -1, 0, 0, TRUE);
2242 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, 0, "a\nb\nc", -1, 0, 0, FALSE);
2243 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_LF, 0, "a\nb\nc", -1, 0, 0, TRUE);
2244 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CRLF, 0, "a\nb\nc", -1, 0, 0, FALSE);
2245 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, 0, "a\r\nb\r\nc", -1, 0, 0, FALSE);
2246 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_LF, 0, "a\r\nb\r\nc", -1, 0, 0, FALSE);
2247 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CRLF, 0, "a\r\nb\r\nc", -1, 0, 0, TRUE);
2248 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, 0, "a\rb\rc", -1, 0, 0, TRUE);
2249 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_LF, 0, "a\rb\rc", -1, 0, 0, FALSE);
2250 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CRLF, 0, "a\rb\rc", -1, 0, 0, FALSE);
2251 TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_CR, "a\nb\nc", -1, 0, 0, FALSE);
2252 TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_LF, "a\nb\nc", -1, 0, 0, TRUE);
2253 TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_CRLF, "a\nb\nc", -1, 0, 0, FALSE);
2254 TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_CR, "a\r\nb\r\nc", -1, 0, 0, FALSE);
2255 TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_LF, "a\r\nb\r\nc", -1, 0, 0, FALSE);
2256 TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_CRLF, "a\r\nb\r\nc", -1, 0, 0, TRUE);
2257 TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_CR, "a\rb\rc", -1, 0, 0, TRUE);
2258 TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_LF, "a\rb\rc", -1, 0, 0, FALSE);
2259 TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_CRLF, "a\rb\rc", -1, 0, 0, FALSE);
2261 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_ANY, "a\nb\nc", -1, 0, 0, TRUE);
2262 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_ANY, "a\rb\rc", -1, 0, 0, TRUE);
2263 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_ANY, "a\r\nb\r\nc", -1, 0, 0, TRUE);
2264 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_LF, "a\nb\nc", -1, 0, 0, TRUE);
2265 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_LF, "a\rb\rc", -1, 0, 0, FALSE);
2266 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_CRLF, "a\r\nb\r\nc", -1, 0, 0, TRUE);
2267 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_CRLF, "a\rb\rc", -1, 0, 0, FALSE);
2269 TEST_MATCH("a#\nb", G_REGEX_EXTENDED, 0, "a", -1, 0, 0, FALSE);
2270 TEST_MATCH("a#\r\nb", G_REGEX_EXTENDED, 0, "a", -1, 0, 0, FALSE);
2271 TEST_MATCH("a#\rb", G_REGEX_EXTENDED, 0, "a", -1, 0, 0, FALSE);
2272 TEST_MATCH("a#\nb", G_REGEX_EXTENDED, G_REGEX_MATCH_NEWLINE_CR, "a", -1, 0, 0, FALSE);
2273 TEST_MATCH("a#\nb", G_REGEX_EXTENDED | G_REGEX_NEWLINE_CR, 0, "a", -1, 0, 0, TRUE);
2275 /* TEST_MATCH_NEXT#(pattern, string, string_len, start_position, ...) */
2276 TEST_MATCH_NEXT0("a", "x", -1, 0);
2277 TEST_MATCH_NEXT0("a", "ax", -1, 1);
2278 TEST_MATCH_NEXT0("a", "xa", 1, 0);
2279 TEST_MATCH_NEXT0("a", "axa", 1, 2);
2280 TEST_MATCH_NEXT1("a", "a", -1, 0, "a", 0, 1);
2281 TEST_MATCH_NEXT1("a", "xax", -1, 0, "a", 1, 2);
2282 TEST_MATCH_NEXT1(EURO, ENG EURO, -1, 0, EURO, 2, 5);
2283 TEST_MATCH_NEXT1("a*", "", -1, 0, "", 0, 0);
2284 TEST_MATCH_NEXT2("a*", "aa", -1, 0, "aa", 0, 2, "", 2, 2);
2285 TEST_MATCH_NEXT2(EURO "*", EURO EURO, -1, 0, EURO EURO, 0, 6, "", 6, 6);
2286 TEST_MATCH_NEXT2("a", "axa", -1, 0, "a", 0, 1, "a", 2, 3);
2287 TEST_MATCH_NEXT2("a+", "aaxa", -1, 0, "aa", 0, 2, "a", 3, 4);
2288 TEST_MATCH_NEXT2("a", "aa", -1, 0, "a", 0, 1, "a", 1, 2);
2289 TEST_MATCH_NEXT2("a", "ababa", -1, 2, "a", 2, 3, "a", 4, 5);
2290 TEST_MATCH_NEXT2(EURO "+", EURO "-" EURO, -1, 0, EURO, 0, 3, EURO, 4, 7);
2291 TEST_MATCH_NEXT3("", "ab", -1, 0, "", 0, 0, "", 1, 1, "", 2, 2);
2292 TEST_MATCH_NEXT3("", AGRAVE "b", -1, 0, "", 0, 0, "", 2, 2, "", 3, 3);
2293 TEST_MATCH_NEXT3("a", "aaxa", -1, 0, "a", 0, 1, "a", 1, 2, "a", 3, 4);
2294 TEST_MATCH_NEXT3("a", "aa" OGRAVE "a", -1, 0, "a", 0, 1, "a", 1, 2, "a", 4, 5);
2295 TEST_MATCH_NEXT3("a*", "aax", -1, 0, "aa", 0, 2, "", 2, 2, "", 3, 3);
2296 TEST_MATCH_NEXT3("(?=[A-Z0-9])", "RegExTest", -1, 0, "", 0, 0, "", 3, 3, "", 5, 5);
2297 TEST_MATCH_NEXT4("a*", "aaxa", -1, 0, "aa", 0, 2, "", 2, 2, "a", 3, 4, "", 4, 4);
2299 /* TEST_MATCH_COUNT(pattern, string, start_position, match_opts, expected_count) */
2300 TEST_MATCH_COUNT("a", "", 0, 0, 0);
2301 TEST_MATCH_COUNT("a", "a", 0, 0, 1);
2302 TEST_MATCH_COUNT("a", "a", 1, 0, 0);
2303 TEST_MATCH_COUNT("(.)", "a", 0, 0, 2);
2304 TEST_MATCH_COUNT("(.)", EURO, 0, 0, 2);
2305 TEST_MATCH_COUNT("(?:.)", "a", 0, 0, 1);
2306 TEST_MATCH_COUNT("(?P<A>.)", "a", 0, 0, 2);
2307 TEST_MATCH_COUNT("a$", "a", 0, G_REGEX_MATCH_NOTEOL, 0);
2308 TEST_MATCH_COUNT("(a)?(b)", "b", 0, 0, 3);
2309 TEST_MATCH_COUNT("(a)?(b)", "ab", 0, 0, 3);
2311 /* TEST_PARTIAL(pattern, string, expected) */
2312 TEST_PARTIAL("^ab", "a", TRUE);
2313 TEST_PARTIAL("^ab", "xa", FALSE);
2314 TEST_PARTIAL("ab", "xa", TRUE);
2315 TEST_PARTIAL("ab", "ab", FALSE); /* normal match. */
2316 TEST_PARTIAL("a+b", "aa", TRUE);
2317 TEST_PARTIAL("(a)+b", "aa", TRUE);
2318 TEST_PARTIAL("a?b", "a", TRUE);
2320 /* TEST_SUB_PATTERN(pattern, string, start_position, sub_n, expected_sub,
2321 * expected_start, expected_end) */
2322 TEST_SUB_PATTERN("a", "a", 0, 0, "a", 0, 1);
2323 TEST_SUB_PATTERN("a(.)", "ab", 0, 1, "b", 1, 2);
2324 TEST_SUB_PATTERN("a(.)", "a" EURO, 0, 1, EURO, 1, 4);
2325 TEST_SUB_PATTERN("(?:.*)(a)(.)", "xxa" ENG, 0, 2, ENG, 3, 5);
2326 TEST_SUB_PATTERN("(" HSTROKE ")", "a" HSTROKE ENG, 0, 1, HSTROKE, 1, 3);
2327 TEST_SUB_PATTERN("a", "a", 0, 1, NULL, UNTOUCHED, UNTOUCHED);
2328 TEST_SUB_PATTERN("a", "a", 0, 1, NULL, UNTOUCHED, UNTOUCHED);
2329 TEST_SUB_PATTERN("(a)?(b)", "b", 0, 0, "b", 0, 1);
2330 TEST_SUB_PATTERN("(a)?(b)", "b", 0, 1, "", -1, -1);
2331 TEST_SUB_PATTERN("(a)?(b)", "b", 0, 2, "b", 0, 1);
2333 /* TEST_NAMED_SUB_PATTERN(pattern, string, start_position, sub_name,
2334 * expected_sub, expected_start, expected_end) */
2335 TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?", "ab", 0, "A", "b", 1, 2);
2336 TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?", "aab", 1, "A", "b", 2, 3);
2337 TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?", EURO "ab", 0, "A", "b", 4, 5);
2338 TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?", EURO "ab", 0, "B", NULL, UNTOUCHED, UNTOUCHED);
2339 TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?", EURO "ab", 0, "C", NULL, UNTOUCHED, UNTOUCHED);
2340 TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?", "a" EGRAVE "x", 0, "A", EGRAVE, 1, 3);
2341 TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?", "a" EGRAVE "x", 0, "B", "x", 3, 4);
2342 TEST_NAMED_SUB_PATTERN("(?P<A>a)?(?P<B>b)", "b", 0, "A", "", -1, -1);
2343 TEST_NAMED_SUB_PATTERN("(?P<A>a)?(?P<B>b)", "b", 0, "B", "b", 0, 1);
2345 /* TEST_NAMED_SUB_PATTERN_DUPNAMES(pattern, string, start_position, sub_name,
2346 * expected_sub, expected_start, expected_end) */
2347 TEST_NAMED_SUB_PATTERN_DUPNAMES("(?P<N>a)|(?P<N>b)", "ab", 0, "N", "a", 0, 1);
2348 TEST_NAMED_SUB_PATTERN_DUPNAMES("(?P<N>aa)|(?P<N>a)", "aa", 0, "N", "aa", 0, 2);
2349 TEST_NAMED_SUB_PATTERN_DUPNAMES("(?P<N>aa)(?P<N>a)", "aaa", 0, "N", "aa", 0, 2);
2350 TEST_NAMED_SUB_PATTERN_DUPNAMES("(?P<N>x)|(?P<N>a)", "a", 0, "N", "a", 0, 1);
2351 TEST_NAMED_SUB_PATTERN_DUPNAMES("(?P<N>x)y|(?P<N>a)b", "ab", 0, "N", "a", 0, 1);
2353 /* DUPNAMES option inside the pattern */
2354 TEST_NAMED_SUB_PATTERN("(?J)(?P<N>a)|(?P<N>b)", "ab", 0, "N", "a", 0, 1);
2355 TEST_NAMED_SUB_PATTERN("(?J)(?P<N>aa)|(?P<N>a)", "aa", 0, "N", "aa", 0, 2);
2356 TEST_NAMED_SUB_PATTERN("(?J)(?P<N>aa)(?P<N>a)", "aaa", 0, "N", "aa", 0, 2);
2357 TEST_NAMED_SUB_PATTERN("(?J)(?P<N>x)|(?P<N>a)", "a", 0, "N", "a", 0, 1);
2358 TEST_NAMED_SUB_PATTERN("(?J)(?P<N>x)y|(?P<N>a)b", "ab", 0, "N", "a", 0, 1);
2360 /* TEST_FETCH_ALL#(pattern, string, ...) */
2361 TEST_FETCH_ALL0("a", "");
2362 TEST_FETCH_ALL0("a", "b");
2363 TEST_FETCH_ALL1("a", "a", "a");
2364 TEST_FETCH_ALL1("a+", "aa", "aa");
2365 TEST_FETCH_ALL1("(?:a)", "a", "a");
2366 TEST_FETCH_ALL2("(a)", "a", "a", "a");
2367 TEST_FETCH_ALL2("a(.)", "ab", "ab", "b");
2368 TEST_FETCH_ALL2("a(.)", "a" HSTROKE, "a" HSTROKE, HSTROKE);
2369 TEST_FETCH_ALL3("(?:.*)(a)(.)", "xyazk", "xyaz", "a", "z");
2370 TEST_FETCH_ALL3("(?P<A>.)(a)", "xa", "xa", "x", "a");
2371 TEST_FETCH_ALL3("(?P<A>.)(a)", ENG "a", ENG "a", ENG, "a");
2372 TEST_FETCH_ALL3("(a)?(b)", "b", "b", "", "b");
2373 TEST_FETCH_ALL3("(a)?(b)", "ab", "ab", "a", "b");
2375 /* TEST_SPLIT_SIMPLE#(pattern, string, ...) */
2376 TEST_SPLIT_SIMPLE0("", "");
2377 TEST_SPLIT_SIMPLE0("a", "");
2378 TEST_SPLIT_SIMPLE1(",", "a", "a");
2379 TEST_SPLIT_SIMPLE1("(,)\\s*", "a", "a");
2380 TEST_SPLIT_SIMPLE2(",", "a,b", "a", "b");
2381 TEST_SPLIT_SIMPLE3(",", "a,b,c", "a", "b", "c");
2382 TEST_SPLIT_SIMPLE3(",\\s*", "a,b,c", "a", "b", "c");
2383 TEST_SPLIT_SIMPLE3(",\\s*", "a, b, c", "a", "b", "c");
2384 TEST_SPLIT_SIMPLE3("(,)\\s*", "a,b", "a", ",", "b");
2385 TEST_SPLIT_SIMPLE3("(,)\\s*", "a, b", "a", ",", "b");
2386 TEST_SPLIT_SIMPLE2("\\s", "ab c", "ab", "c");
2387 TEST_SPLIT_SIMPLE3("\\s*", "ab c", "a", "b", "c");
2388 /* Not matched sub-strings. */
2389 TEST_SPLIT_SIMPLE2("a|(b)", "xay", "x", "y");
2390 TEST_SPLIT_SIMPLE3("a|(b)", "xby", "x", "b", "y");
2391 /* Empty matches. */
2392 TEST_SPLIT_SIMPLE3("", "abc", "a", "b", "c");
2393 TEST_SPLIT_SIMPLE3(" *", "ab c", "a", "b", "c");
2394 /* Invalid patterns. */
2395 TEST_SPLIT_SIMPLE0("\\", "");
2396 TEST_SPLIT_SIMPLE0("[", "");
2398 /* TEST_SPLIT#(pattern, string, start_position, max_tokens, ...) */
2399 TEST_SPLIT0("", "", 0, 0);
2400 TEST_SPLIT0("a", "", 0, 0);
2401 TEST_SPLIT0("a", "", 0, 1);
2402 TEST_SPLIT0("a", "", 0, 2);
2403 TEST_SPLIT0("a", "a", 1, 0);
2404 TEST_SPLIT1(",", "a", 0, 0, "a");
2405 TEST_SPLIT1(",", "a,b", 0, 1, "a,b");
2406 TEST_SPLIT1("(,)\\s*", "a", 0, 0, "a");
2407 TEST_SPLIT1(",", "a,b", 2, 0, "b");
2408 TEST_SPLIT2(",", "a,b", 0, 0, "a", "b");
2409 TEST_SPLIT2(",", "a,b,c", 0, 2, "a", "b,c");
2410 TEST_SPLIT2(",", "a,b", 1, 0, "", "b");
2411 TEST_SPLIT2(",", "a,", 0, 0, "a", "");
2412 TEST_SPLIT3(",", "a,b,c", 0, 0, "a", "b", "c");
2413 TEST_SPLIT3(",\\s*", "a,b,c", 0, 0, "a", "b", "c");
2414 TEST_SPLIT3(",\\s*", "a, b, c", 0, 0, "a", "b", "c");
2415 TEST_SPLIT3("(,)\\s*", "a,b", 0, 0, "a", ",", "b");
2416 TEST_SPLIT3("(,)\\s*", "a, b", 0, 0, "a", ",", "b");
2417 /* Not matched sub-strings. */
2418 TEST_SPLIT2("a|(b)", "xay", 0, 0, "x", "y");
2419 TEST_SPLIT3("a|(b)", "xby", 0, -1, "x", "b", "y");
2420 /* Empty matches. */
2421 TEST_SPLIT2(" *", "ab c", 1, 0, "b", "c");
2422 TEST_SPLIT3("", "abc", 0, 0, "a", "b", "c");
2423 TEST_SPLIT3(" *", "ab c", 0, 0, "a", "b", "c");
2424 TEST_SPLIT1(" *", "ab c", 0, 1, "ab c");
2425 TEST_SPLIT2(" *", "ab c", 0, 2, "a", "b c");
2426 TEST_SPLIT3(" *", "ab c", 0, 3, "a", "b", "c");
2427 TEST_SPLIT3(" *", "ab c", 0, 4, "a", "b", "c");
2429 /* TEST_CHECK_REPLACEMENT(string_to_expand, expected, expected_refs) */
2430 TEST_CHECK_REPLACEMENT("", TRUE, FALSE);
2431 TEST_CHECK_REPLACEMENT("a", TRUE, FALSE);
2432 TEST_CHECK_REPLACEMENT("\\t\\n\\v\\r\\f\\a\\b\\\\\\x{61}", TRUE, FALSE);
2433 TEST_CHECK_REPLACEMENT("\\0", TRUE, TRUE);
2434 TEST_CHECK_REPLACEMENT("\\n\\2", TRUE, TRUE);
2435 TEST_CHECK_REPLACEMENT("\\g<foo>", TRUE, TRUE);
2436 /* Invalid strings */
2437 TEST_CHECK_REPLACEMENT("\\Q", FALSE, FALSE);
2438 TEST_CHECK_REPLACEMENT("x\\Ay", FALSE, FALSE);
2440 /* TEST_EXPAND(pattern, string, string_to_expand, raw, expected) */
2441 TEST_EXPAND("a", "a", "", FALSE, "");
2442 TEST_EXPAND("a", "a", "\\0", FALSE, "a");
2443 TEST_EXPAND("a", "a", "\\1", FALSE, "");
2444 TEST_EXPAND("(a)", "ab", "\\1", FALSE, "a");
2445 TEST_EXPAND("(a)", "a", "\\1", FALSE, "a");
2446 TEST_EXPAND("(a)", "a", "\\g<1>", FALSE, "a");
2447 TEST_EXPAND("a", "a", "\\0130", FALSE, "X");
2448 TEST_EXPAND("a", "a", "\\\\\\0", FALSE, "\\a");
2449 TEST_EXPAND("a(?P<G>.)c", "xabcy", "X\\g<G>X", FALSE, "XbX");
2450 TEST_EXPAND("(.)(?P<1>.)", "ab", "\\1", FALSE, "a");
2451 TEST_EXPAND("(.)(?P<1>.)", "ab", "\\g<1>", FALSE, "a");
2452 TEST_EXPAND(".", EURO, "\\0", FALSE, EURO);
2453 TEST_EXPAND("(.)", EURO, "\\1", FALSE, EURO);
2454 TEST_EXPAND("(?P<G>.)", EURO, "\\g<G>", FALSE, EURO);
2455 TEST_EXPAND(".", "a", EURO, FALSE, EURO);
2456 TEST_EXPAND(".", "a", EURO "\\0", FALSE, EURO "a");
2457 TEST_EXPAND(".", "", "\\Lab\\Ec", FALSE, "abc");
2458 TEST_EXPAND(".", "", "\\LaB\\EC", FALSE, "abC");
2459 TEST_EXPAND(".", "", "\\Uab\\Ec", FALSE, "ABc");
2460 TEST_EXPAND(".", "", "a\\ubc", FALSE, "aBc");
2461 TEST_EXPAND(".", "", "a\\lbc", FALSE, "abc");
2462 TEST_EXPAND(".", "", "A\\uBC", FALSE, "ABC");
2463 TEST_EXPAND(".", "", "A\\lBC", FALSE, "AbC");
2464 TEST_EXPAND(".", "", "A\\l\\\\BC", FALSE, "A\\BC");
2465 TEST_EXPAND(".", "", "\\L" AGRAVE "\\E", FALSE, AGRAVE);
2466 TEST_EXPAND(".", "", "\\U" AGRAVE "\\E", FALSE, AGRAVE_UPPER);
2467 TEST_EXPAND(".", "", "\\u" AGRAVE "a", FALSE, AGRAVE_UPPER "a");
2468 TEST_EXPAND(".", "ab", "x\\U\\0y\\Ez", FALSE, "xAYz");
2469 TEST_EXPAND(".(.)", "AB", "x\\L\\1y\\Ez", FALSE, "xbyz");
2470 TEST_EXPAND(".", "ab", "x\\u\\0y\\Ez", FALSE, "xAyz");
2471 TEST_EXPAND(".(.)", "AB", "x\\l\\1y\\Ez", FALSE, "xbyz");
2472 TEST_EXPAND(".(.)", "a" AGRAVE_UPPER, "x\\l\\1y", FALSE, "x" AGRAVE "y");
2473 TEST_EXPAND("a", "bab", "\\x{61}", FALSE, "a");
2474 TEST_EXPAND("a", "bab", "\\x61", FALSE, "a");
2475 TEST_EXPAND("a", "bab", "\\x5a", FALSE, "Z");
2476 TEST_EXPAND("a", "bab", "\\0\\x5A", FALSE, "aZ");
2477 TEST_EXPAND("a", "bab", "\\1\\x{5A}", FALSE, "Z");
2478 TEST_EXPAND("a", "bab", "\\x{00E0}", FALSE, AGRAVE);
2479 TEST_EXPAND("", "bab", "\\x{0634}", FALSE, SHEEN);
2480 TEST_EXPAND("", "bab", "\\x{634}", FALSE, SHEEN);
2481 TEST_EXPAND("", "", "\\t", FALSE, "\t");
2482 TEST_EXPAND("", "", "\\v", FALSE, "\v");
2483 TEST_EXPAND("", "", "\\r", FALSE, "\r");
2484 TEST_EXPAND("", "", "\\n", FALSE, "\n");
2485 TEST_EXPAND("", "", "\\f", FALSE, "\f");
2486 TEST_EXPAND("", "", "\\a", FALSE, "\a");
2487 TEST_EXPAND("", "", "\\b", FALSE, "\b");
2488 TEST_EXPAND("a(.)", "abc", "\\0\\b\\1", FALSE, "ab\bb");
2489 TEST_EXPAND("a(.)", "abc", "\\0141", FALSE, "a");
2490 TEST_EXPAND("a(.)", "abc", "\\078", FALSE, "\a8");
2491 TEST_EXPAND("a(.)", "abc", "\\077", FALSE, "?");
2492 TEST_EXPAND("a(.)", "abc", "\\0778", FALSE, "?8");
2493 TEST_EXPAND("a(.)", "a" AGRAVE "b", "\\1", FALSE, AGRAVE);
2494 TEST_EXPAND("a(.)", "a" AGRAVE "b", "\\1", TRUE, "\xc3");
2495 TEST_EXPAND("a(.)", "a" AGRAVE "b", "\\0", TRUE, "a\xc3");
2496 /* Invalid strings. */
2497 TEST_EXPAND("", "", "\\Q", FALSE, NULL);
2498 TEST_EXPAND("", "", "x\\Ay", FALSE, NULL);
2499 TEST_EXPAND("", "", "\\g<", FALSE, NULL);
2500 TEST_EXPAND("", "", "\\g<>", FALSE, NULL);
2501 TEST_EXPAND("", "", "\\g<1a>", FALSE, NULL);
2502 TEST_EXPAND("", "", "\\g<a$>", FALSE, NULL);
2503 TEST_EXPAND("", "", "\\", FALSE, NULL);
2504 TEST_EXPAND("a", "a", "\\x{61", FALSE, NULL);
2505 TEST_EXPAND("a", "a", "\\x6X", FALSE, NULL);
2507 TEST_EXPAND(NULL, NULL, "", FALSE, "");
2508 TEST_EXPAND(NULL, NULL, "\\n", FALSE, "\n");
2509 /* Invalid strings */
2510 TEST_EXPAND(NULL, NULL, "\\Q", FALSE, NULL);
2511 TEST_EXPAND(NULL, NULL, "x\\Ay", FALSE, NULL);
2513 /* TEST_REPLACE(pattern, string, start_position, replacement, expected) */
2514 TEST_REPLACE("a", "ababa", 0, "A", "AbAbA");
2515 TEST_REPLACE("a", "ababa", 1, "A", "abAbA");
2516 TEST_REPLACE("a", "ababa", 2, "A", "abAbA");
2517 TEST_REPLACE("a", "ababa", 3, "A", "ababA");
2518 TEST_REPLACE("a", "ababa", 4, "A", "ababA");
2519 TEST_REPLACE("a", "ababa", 5, "A", "ababa");
2520 TEST_REPLACE("a", "ababa", 6, "A", "ababa");
2521 TEST_REPLACE("a", "abababa", 2, "A", "abAbAbA");
2522 TEST_REPLACE("a", "abab", 0, "A", "AbAb");
2523 TEST_REPLACE("a", "baba", 0, "A", "bAbA");
2524 TEST_REPLACE("a", "bab", 0, "A", "bAb");
2525 TEST_REPLACE("$^", "abc", 0, "X", "abc");
2526 TEST_REPLACE("(.)a", "ciao", 0, "a\\1", "caio");
2527 TEST_REPLACE("a.", "abc", 0, "\\0\\0", "ababc");
2528 TEST_REPLACE("a", "asd", 0, "\\0101", "Asd");
2529 TEST_REPLACE("(a).\\1", "aba cda", 0, "\\1\\n", "a\n cda");
2530 TEST_REPLACE("a" AGRAVE "a", "a" AGRAVE "a", 0, "x", "x");
2531 TEST_REPLACE("a" AGRAVE "a", "a" AGRAVE "a", 0, OGRAVE, OGRAVE);
2532 TEST_REPLACE("[^-]", "-" EURO "-x-" HSTROKE, 0, "a", "-a-a-a");
2533 TEST_REPLACE("[^-]", "-" EURO "-" HSTROKE, 0, "a\\g<0>a", "-a" EURO "a-a" HSTROKE "a");
2534 TEST_REPLACE("-", "-" EURO "-" HSTROKE, 0, "", EURO HSTROKE);
2535 TEST_REPLACE(".*", "hello", 0, "\\U\\0\\E", "HELLO");
2536 TEST_REPLACE(".*", "hello", 0, "\\u\\0", "Hello");
2537 TEST_REPLACE("\\S+", "hello world", 0, "\\U-\\0-", "-HELLO- -WORLD-");
2538 TEST_REPLACE(".", "a", 0, "\\A", NULL);
2539 TEST_REPLACE(".", "a", 0, "\\g", NULL);
2541 /* TEST_REPLACE_LIT(pattern, string, start_position, replacement, expected) */
2542 TEST_REPLACE_LIT("a", "ababa", 0, "A", "AbAbA");
2543 TEST_REPLACE_LIT("a", "ababa", 1, "A", "abAbA");
2544 TEST_REPLACE_LIT("a", "ababa", 2, "A", "abAbA");
2545 TEST_REPLACE_LIT("a", "ababa", 3, "A", "ababA");
2546 TEST_REPLACE_LIT("a", "ababa", 4, "A", "ababA");
2547 TEST_REPLACE_LIT("a", "ababa", 5, "A", "ababa");
2548 TEST_REPLACE_LIT("a", "ababa", 6, "A", "ababa");
2549 TEST_REPLACE_LIT("a", "abababa", 2, "A", "abAbAbA");
2550 TEST_REPLACE_LIT("a", "abcadaa", 0, "A", "AbcAdAA");
2551 TEST_REPLACE_LIT("$^", "abc", 0, "X", "abc");
2552 TEST_REPLACE_LIT("(.)a", "ciao", 0, "a\\1", "ca\\1o");
2553 TEST_REPLACE_LIT("a.", "abc", 0, "\\0\\0\\n", "\\0\\0\\nc");
2554 TEST_REPLACE_LIT("a" AGRAVE "a", "a" AGRAVE "a", 0, "x", "x");
2555 TEST_REPLACE_LIT("a" AGRAVE "a", "a" AGRAVE "a", 0, OGRAVE, OGRAVE);
2556 TEST_REPLACE_LIT(AGRAVE, "-" AGRAVE "-" HSTROKE, 0, "a" ENG "a", "-a" ENG "a-" HSTROKE);
2557 TEST_REPLACE_LIT("[^-]", "-" EURO "-" AGRAVE "-" HSTROKE, 0, "a", "-a-a-a");
2558 TEST_REPLACE_LIT("[^-]", "-" EURO "-" AGRAVE, 0, "a\\g<0>a", "-a\\g<0>a-a\\g<0>a");
2559 TEST_REPLACE_LIT("-", "-" EURO "-" AGRAVE "-" HSTROKE, 0, "", EURO AGRAVE HSTROKE);
2560 TEST_REPLACE_LIT("(?=[A-Z0-9])", "RegExTest", 0, "_", "_Reg_Ex_Test");
2561 TEST_REPLACE_LIT("(?=[A-Z0-9])", "RegExTest", 1, "_", "Reg_Ex_Test");
2563 /* TEST_GET_STRING_NUMBER(pattern, name, expected_num) */
2564 TEST_GET_STRING_NUMBER("", "A", -1);
2565 TEST_GET_STRING_NUMBER("(?P<A>.)", "A", 1);
2566 TEST_GET_STRING_NUMBER("(?P<A>.)", "B", -1);
2567 TEST_GET_STRING_NUMBER("(?P<A>.)(?P<B>a)", "A", 1);
2568 TEST_GET_STRING_NUMBER("(?P<A>.)(?P<B>a)", "B", 2);
2569 TEST_GET_STRING_NUMBER("(?P<A>.)(?P<B>a)", "C", -1);
2570 TEST_GET_STRING_NUMBER("(?P<A>.)(.)(?P<B>a)", "A", 1);
2571 TEST_GET_STRING_NUMBER("(?P<A>.)(.)(?P<B>a)", "B", 3);
2572 TEST_GET_STRING_NUMBER("(?P<A>.)(.)(?P<B>a)", "C", -1);
2573 TEST_GET_STRING_NUMBER("(?:a)(?P<A>.)", "A", 1);
2574 TEST_GET_STRING_NUMBER("(?:a)(?P<A>.)", "B", -1);
2576 /* TEST_ESCAPE(string, length, expected) */
2577 TEST_ESCAPE("hello world", -1, "hello world");
2578 TEST_ESCAPE("hello world", 5, "hello");
2579 TEST_ESCAPE("hello.world", -1, "hello\\.world");
2580 TEST_ESCAPE("a(b\\b.$", -1, "a\\(b\\\\b\\.\\$");
2581 TEST_ESCAPE("hello\0world", -1, "hello");
2582 TEST_ESCAPE("hello\0world", 11, "hello\\0world");
2583 TEST_ESCAPE(EURO "*" ENG, -1, EURO "\\*" ENG);
2584 TEST_ESCAPE("a$", -1, "a\\$");
2585 TEST_ESCAPE("$a", -1, "\\$a");
2586 TEST_ESCAPE("a$a", -1, "a\\$a");
2587 TEST_ESCAPE("$a$", -1, "\\$a\\$");
2588 TEST_ESCAPE("$a$", 0, "");
2589 TEST_ESCAPE("$a$", 1, "\\$");
2590 TEST_ESCAPE("$a$", 2, "\\$a");
2591 TEST_ESCAPE("$a$", 3, "\\$a\\$");
2592 TEST_ESCAPE("$a$", 4, "\\$a\\$\\0");
2593 TEST_ESCAPE("|()[]{}^$*+?.", -1, "\\|\\(\\)\\[\\]\\{\\}\\^\\$\\*\\+\\?\\.");
2594 TEST_ESCAPE("a|a(a)a[a]a{a}a^a$a*a+a?a.a", -1,
2595 "a\\|a\\(a\\)a\\[a\\]a\\{a\\}a\\^a\\$a\\*a\\+a\\?a\\.a");
2597 /* TEST_MATCH_ALL#(pattern, string, string_len, start_position, ...) */
2598 TEST_MATCH_ALL0("<.*>", "", -1, 0);
2599 TEST_MATCH_ALL0("a+", "", -1, 0);
2600 TEST_MATCH_ALL0("a+", "a", 0, 0);
2601 TEST_MATCH_ALL0("a+", "a", -1, 1);
2602 TEST_MATCH_ALL1("<.*>", "<a>", -1, 0, "<a>", 0, 3);
2603 TEST_MATCH_ALL1("a+", "a", -1, 0, "a", 0, 1);
2604 TEST_MATCH_ALL1("a+", "aa", 1, 0, "a", 0, 1);
2605 TEST_MATCH_ALL1("a+", "aa", -1, 1, "a", 1, 2);
2606 TEST_MATCH_ALL1("a+", "aa", 2, 1, "a", 1, 2);
2607 TEST_MATCH_ALL1(".+", ENG, -1, 0, ENG, 0, 2);
2608 TEST_MATCH_ALL2("<.*>", "<a><b>", -1, 0, "<a><b>", 0, 6, "<a>", 0, 3);
2609 TEST_MATCH_ALL2("a+", "aa", -1, 0, "aa", 0, 2, "a", 0, 1);
2610 TEST_MATCH_ALL2(".+", ENG EURO, -1, 0, ENG EURO, 0, 5, ENG, 0, 2);
2611 TEST_MATCH_ALL3("<.*>", "<a><b><c>", -1, 0, "<a><b><c>", 0, 9,
2612 "<a><b>", 0, 6, "<a>", 0, 3);
2613 TEST_MATCH_ALL3("a+", "aaa", -1, 0, "aaa", 0, 3, "aa", 0, 2, "a", 0, 1);
2615 return g_test_run ();
2618 #else /* ENABLE_REGEX false */
2621 main (int argc, char *argv[])
2623 g_print ("GRegex is disabled.\n");
2627 #endif /* ENABLE_REGEX */