2 * Copyright (C) 2005 - 2006, Marco Barisione <marco@barisione.org>
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.
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.
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.
20 #undef G_DISABLE_ASSERT
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"
48 /* A random value use to mark untouched integer variables. */
49 #define UNTOUCHED -559038737
51 static gboolean noisy = FALSE;
52 static gboolean abort_on_fail = FALSE;
64 /* A replacement for strcmp that doesn't crash with null pointers. */
66 streq (const gchar *s1, const gchar *s2)
68 if (s1 == NULL && s2 == NULL)
75 return strcmp (s1, s2) == 0;
79 verbose (const gchar *format, ...)
81 /* Function copied from glib/tests/patterntest.c by Matthias Clasen. */
85 va_start (args, format);
86 msg = g_strdup_vprintf (format, args);
95 test_new (const gchar *pattern,
96 GRegexCompileFlags compile_opts,
97 GRegexMatchFlags match_opts)
101 verbose ("compiling \"%s\" \t", pattern);
103 regex = g_regex_new (pattern, compile_opts, match_opts, NULL);
106 g_print ("failed \t(pattern: \"%s\", compile: %d, match %d)\n",
107 pattern, compile_opts, match_opts);
111 if (!streq (g_regex_get_pattern (regex), pattern))
113 g_print ("failed \t(pattern: \"%s\")\n",
115 g_regex_unref (regex);
119 g_regex_unref (regex);
121 verbose ("passed\n");
125 #define TEST_NEW(pattern, compile_opts, match_opts) { \
127 if (test_new (pattern, compile_opts, match_opts)) \
134 test_new_fail (const gchar *pattern,
135 GRegexCompileFlags compile_opts)
139 verbose ("compiling \"%s\" (expected a failure) \t", pattern);
141 regex = g_regex_new (pattern, compile_opts, 0, NULL);
145 g_print ("failed \t(pattern: \"%s\", compile: %d)\n",
146 pattern, compile_opts);
147 g_regex_unref (regex);
151 verbose ("passed\n");
155 #define TEST_NEW_FAIL(pattern, compile_opts) { \
157 if (test_new_fail (pattern, compile_opts)) \
164 test_match_simple (const gchar *pattern,
166 GRegexCompileFlags compile_opts,
167 GRegexMatchFlags match_opts,
172 verbose ("matching \"%s\" against \"%s\" \t", string, pattern);
174 match = g_regex_match_simple (pattern, string, compile_opts, match_opts);
175 if (match != expected)
177 g_print ("failed \t(unexpected %s)\n", match ? "match" : "mismatch");
182 verbose ("passed (%s)\n", match ? "match" : "nomatch");
187 #define TEST_MATCH_SIMPLE(pattern, string, compile_opts, match_opts, expected) { \
189 if (test_match_simple (pattern, string, compile_opts, match_opts, expected)) \
196 test_match (const gchar *pattern,
197 GRegexCompileFlags compile_opts,
198 GRegexMatchFlags match_opts,
202 GRegexMatchFlags match_opts2,
208 verbose ("matching \"%s\" against \"%s\" (start: %d, len: %d) \t",
209 string, pattern, start_position, string_len);
211 regex = g_regex_new (pattern, compile_opts, match_opts, NULL);
212 match = g_regex_match_full (regex, string, string_len,
213 start_position, match_opts2, NULL, NULL);
214 if (match != expected)
216 gchar *e1 = g_strescape (pattern, NULL);
217 gchar *e2 = g_strescape (string, NULL);
218 g_print ("failed \t(unexpected %s) '%s' against '%s'\n", match ? "match" : "mismatch", e1, e2);
221 g_regex_unref (regex);
225 if (string_len == -1 && start_position == 0)
227 match = g_regex_match (regex, string, match_opts2, NULL);
228 if (match != expected)
230 g_print ("failed \t(pattern: \"%s\", string: \"%s\")\n",
232 g_regex_unref (regex);
237 g_regex_unref (regex);
239 verbose ("passed (%s)\n", match ? "match" : "nomatch");
243 #define TEST_MATCH(pattern, compile_opts, match_opts, string, \
244 string_len, start_position, match_opts2, expected) { \
246 if (test_match (pattern, compile_opts, match_opts, string, \
247 string_len, start_position, match_opts2, expected)) \
258 typedef struct _Match Match;
261 free_match (gpointer data, gpointer user_data)
266 g_free (match->string);
271 test_match_next (const gchar *pattern,
278 GMatchInfo *match_info;
280 GSList *matches = NULL;
281 GSList *expected = NULL;
282 GSList *l_exp, *l_match;
285 verbose ("matching \"%s\" against \"%s\" (start: %d, len: %d) \t",
286 string, pattern, start_position, string_len);
288 /* The va_list is a NULL-terminated sequence of: extected matched string,
289 * expected start and expected end. */
290 va_start (args, start_position);
294 const gchar *expected_string = va_arg (args, const gchar *);
295 if (expected_string == NULL)
297 match = g_new0 (Match, 1);
298 match->string = g_strdup (expected_string);
299 match->start = va_arg (args, gint);
300 match->end = va_arg (args, gint);
301 expected = g_slist_prepend (expected, match);
303 expected = g_slist_reverse (expected);
306 regex = g_regex_new (pattern, 0, 0, NULL);
308 g_regex_match_full (regex, string, string_len,
309 start_position, 0, &match_info, NULL);
310 while (g_match_info_matches (match_info))
312 Match *match = g_new0 (Match, 1);
313 match->string = g_match_info_fetch (match_info, 0);
314 match->start = UNTOUCHED;
315 match->end = UNTOUCHED;
316 g_match_info_fetch_pos (match_info, 0, &match->start, &match->end);
317 matches = g_slist_prepend (matches, match);
318 g_match_info_next (match_info, NULL);
320 g_assert (regex == g_match_info_get_regex (match_info));
321 g_assert (string == g_match_info_get_string (match_info));
322 g_match_info_free (match_info);
323 matches = g_slist_reverse (matches);
325 if (g_slist_length (matches) != g_slist_length (expected))
327 gint match_count = g_slist_length (matches);
328 g_print ("failed \t(got %d %s, expected %d)\n", match_count,
329 match_count == 1 ? "match" : "matches",
330 g_slist_length (expected));
337 while (l_exp != NULL)
339 Match *exp = l_exp->data;
340 Match *match = l_match->data;
342 if (!streq(exp->string, match->string))
344 g_print ("failed \t(got \"%s\", expected \"%s\")\n",
345 match->string, exp->string);
350 if (exp->start != match->start || exp->end != match->end)
352 g_print ("failed \t(got [%d, %d], expected [%d, %d])\n",
353 match->start, match->end, exp->start, exp->end);
358 l_exp = g_slist_next (l_exp);
359 l_match = g_slist_next (l_match);
365 gint count = g_slist_length (matches);
366 verbose ("passed (%d %s)\n", count, count == 1 ? "match" : "matches");
369 g_regex_unref (regex);
370 g_slist_foreach (expected, free_match, NULL);
371 g_slist_free (expected);
372 g_slist_foreach (matches, free_match, NULL);
373 g_slist_free (matches);
378 #define TEST_MATCH_NEXT0(pattern, string, string_len, start_position) { \
380 if (test_match_next (pattern, string, string_len, start_position, NULL)) \
386 #define TEST_MATCH_NEXT1(pattern, string, string_len, start_position, \
389 if (test_match_next (pattern, string, string_len, start_position, \
396 #define TEST_MATCH_NEXT2(pattern, string, string_len, start_position, \
397 t1, s1, e1, t2, s2, e2) { \
399 if (test_match_next (pattern, string, string_len, start_position, \
400 t1, s1, e1, t2, s2, e2, NULL)) \
406 #define TEST_MATCH_NEXT3(pattern, string, string_len, start_position, \
407 t1, s1, e1, t2, s2, e2, t3, s3, e3) { \
409 if (test_match_next (pattern, string, string_len, start_position, \
410 t1, s1, e1, t2, s2, e2, t3, s3, e3, NULL)) \
416 #define TEST_MATCH_NEXT4(pattern, string, string_len, start_position, \
417 t1, s1, e1, t2, s2, e2, t3, s3, e3, t4, s4, e4) { \
419 if (test_match_next (pattern, string, string_len, start_position, \
420 t1, s1, e1, t2, s2, e2, t3, s3, e3, t4, s4, e4, NULL)) \
427 test_match_count (const gchar *pattern,
430 GRegexMatchFlags match_opts,
434 GMatchInfo *match_info;
437 verbose ("fetching match count (string: \"%s\", pattern: \"%s\", start: %d) \t",
438 string, pattern, start_position);
440 regex = g_regex_new (pattern, 0, 0, NULL);
442 g_regex_match_full (regex, string, -1, start_position,
443 match_opts, &match_info, NULL);
444 count = g_match_info_get_match_count (match_info);
446 if (count != expected_count)
448 g_print ("failed \t(got %d, expected: %d)\n", count, expected_count);
452 g_match_info_free (match_info);
453 g_regex_unref (regex);
455 verbose ("passed\n");
459 #define TEST_MATCH_COUNT(pattern, string, start_position, match_opts, expected_count) { \
461 if (test_match_count (pattern, string, start_position, match_opts, expected_count)) \
468 test_partial (const gchar *pattern,
473 GMatchInfo *match_info;
475 verbose ("partial matching (string: \"%s\", pattern: \"%s\") \t",
478 regex = g_regex_new (pattern, 0, 0, NULL);
480 g_regex_match (regex, string, G_REGEX_MATCH_PARTIAL, &match_info);
481 if (expected != g_match_info_is_partial_match (match_info))
483 g_print ("failed \t(got %d, expected: %d)\n", !expected, expected);
484 g_regex_unref (regex);
488 if (expected && g_match_info_fetch_pos (match_info, 0, NULL, NULL))
490 g_print ("failed \t(got sub-pattern 0)\n");
491 g_regex_unref (regex);
495 if (expected && g_match_info_fetch_pos (match_info, 1, NULL, NULL))
497 g_print ("failed \t(got sub-pattern 1)\n");
498 g_regex_unref (regex);
502 g_match_info_free (match_info);
503 g_regex_unref (regex);
505 verbose ("passed\n");
509 #define TEST_PARTIAL(pattern, string, expected) { \
511 if (test_partial (pattern, string, expected)) \
518 test_sub_pattern (const gchar *pattern,
522 const gchar *expected_sub,
527 GMatchInfo *match_info;
529 gint start = UNTOUCHED, end = UNTOUCHED;
531 verbose ("fetching sub-pattern %d from \"%s\" (pattern: \"%s\") \t",
532 sub_n, string, pattern);
534 regex = g_regex_new (pattern, 0, 0, NULL);
535 g_regex_match_full (regex, string, -1, start_position, 0, &match_info, NULL);
537 sub_expr = g_match_info_fetch (match_info, sub_n);
538 if (!streq(sub_expr, expected_sub))
540 g_print ("failed \t(got \"%s\", expected \"%s\")\n",
541 sub_expr, expected_sub);
543 g_regex_unref (regex);
548 g_match_info_fetch_pos (match_info, sub_n, &start, &end);
549 if (start != expected_start || end != expected_end)
551 g_print ("failed \t(got [%d, %d], expected [%d, %d])\n",
552 start, end, expected_start, expected_end);
553 g_regex_unref (regex);
557 g_match_info_free (match_info);
558 g_regex_unref (regex);
560 verbose ("passed\n");
564 #define TEST_SUB_PATTERN(pattern, string, start_position, sub_n, expected_sub, \
565 expected_start, expected_end) { \
567 if (test_sub_pattern (pattern, string, start_position, sub_n, expected_sub, \
568 expected_start, expected_end)) \
575 test_named_sub_pattern (const gchar *pattern,
576 GRegexCompileFlags flags,
579 const gchar *sub_name,
580 const gchar *expected_sub,
585 GMatchInfo *match_info;
586 gint start = UNTOUCHED, end = UNTOUCHED;
589 verbose ("fetching sub-pattern \"%s\" from \"%s\" (pattern: \"%s\") \t",
590 sub_name, string, pattern);
592 regex = g_regex_new (pattern, flags, 0, NULL);
594 g_regex_match_full (regex, string, -1, start_position, 0, &match_info, NULL);
595 sub_expr = g_match_info_fetch_named (match_info, sub_name);
596 if (!streq (sub_expr, expected_sub))
598 g_print ("failed \t(got \"%s\", expected \"%s\")\n",
599 sub_expr, expected_sub);
601 g_regex_unref (regex);
606 g_match_info_fetch_named_pos (match_info, sub_name, &start, &end);
607 if (start != expected_start || end != expected_end)
609 g_print ("failed \t(got [%d, %d], expected [%d, %d])\n",
610 start, end, expected_start, expected_end);
611 g_regex_unref (regex);
615 g_match_info_free (match_info);
616 g_regex_unref (regex);
618 verbose ("passed\n");
622 #define TEST_NAMED_SUB_PATTERN(pattern, string, start_position, sub_name, \
623 expected_sub, expected_start, expected_end) { \
625 if (test_named_sub_pattern (pattern, 0, string, start_position, sub_name, \
626 expected_sub, expected_start, expected_end)) \
632 #define TEST_NAMED_SUB_PATTERN_DUPNAMES(pattern, string, start_position, sub_name, \
633 expected_sub, expected_start, expected_end) { \
635 if (test_named_sub_pattern (pattern, G_REGEX_DUPNAMES, string, start_position, \
636 sub_name, expected_sub, expected_start, expected_end)) \
643 test_fetch_all (const gchar *pattern,
648 GMatchInfo *match_info;
650 GSList *expected = NULL;
657 verbose ("fetching all sub-patterns from \"%s\" (pattern: \"%s\") \t",
660 /* The va_list is a NULL-terminated sequence of extected strings. */
661 va_start (args, string);
664 gchar *expected_string = va_arg (args, gchar *);
665 if (expected_string == NULL)
668 expected = g_slist_prepend (expected, g_strdup (expected_string));
670 expected = g_slist_reverse (expected);
673 regex = g_regex_new (pattern, 0, 0, NULL);
674 g_regex_match (regex, string, 0, &match_info);
675 matches = g_match_info_fetch_all (match_info);
677 match_count = g_strv_length (matches);
681 if (match_count != g_slist_length (expected))
683 g_print ("failed \t(got %d %s, expected %d)\n", match_count,
684 match_count == 1 ? "match" : "matches",
685 g_slist_length (expected));
691 for (i = 0; l_exp != NULL; i++, l_exp = g_slist_next (l_exp))
693 if (!streq(l_exp->data, matches [i]))
695 g_print ("failed \t(got \"%s\", expected \"%s\")\n",
696 matches [i], (gchar *)l_exp->data);
702 verbose ("passed (%d %s)\n", match_count,
703 match_count == 1 ? "match" : "matches");
706 g_match_info_free (match_info);
707 g_regex_unref (regex);
708 g_slist_foreach (expected, (GFunc)g_free, NULL);
709 g_slist_free (expected);
710 g_strfreev (matches);
715 #define TEST_FETCH_ALL0(pattern, string) { \
717 if (test_fetch_all (pattern, string, NULL)) \
723 #define TEST_FETCH_ALL1(pattern, string, e1) { \
725 if (test_fetch_all (pattern, string, e1, NULL)) \
731 #define TEST_FETCH_ALL2(pattern, string, e1, e2) { \
733 if (test_fetch_all (pattern, string, e1, e2, NULL)) \
739 #define TEST_FETCH_ALL3(pattern, string, e1, e2, e3) { \
741 if (test_fetch_all (pattern, string, e1, e2, e3, NULL)) \
748 test_split_simple (const gchar *pattern,
753 GSList *expected = NULL;
760 verbose ("splitting \"%s\" against \"%s\" \t", string, pattern);
762 /* The va_list is a NULL-terminated sequence of extected strings. */
763 va_start (args, string);
766 gchar *expected_string = va_arg (args, gchar *);
767 if (expected_string == NULL)
770 expected = g_slist_prepend (expected, g_strdup (expected_string));
772 expected = g_slist_reverse (expected);
775 tokens = g_regex_split_simple (pattern, string, 0, 0);
777 token_count = g_strv_length (tokens);
781 if (token_count != g_slist_length (expected))
783 g_print ("failed \t(got %d %s, expected %d)\n", token_count,
784 token_count == 1 ? "match" : "matches",
785 g_slist_length (expected));
791 for (i = 0; l_exp != NULL; i++, l_exp = g_slist_next (l_exp))
793 if (!streq(l_exp->data, tokens [i]))
795 g_print ("failed \t(got \"%s\", expected \"%s\")\n",
796 tokens[i], (gchar *)l_exp->data);
802 verbose ("passed (%d %s)\n", token_count,
803 token_count == 1 ? "token" : "tokens");
806 g_slist_foreach (expected, (GFunc)g_free, NULL);
807 g_slist_free (expected);
813 #define TEST_SPLIT_SIMPLE0(pattern, string) { \
815 if (test_split_simple (pattern, string, NULL)) \
821 #define TEST_SPLIT_SIMPLE1(pattern, string, e1) { \
823 if (test_split_simple (pattern, string, e1, NULL)) \
829 #define TEST_SPLIT_SIMPLE2(pattern, string, e1, e2) { \
831 if (test_split_simple (pattern, string, e1, e2, NULL)) \
837 #define TEST_SPLIT_SIMPLE3(pattern, string, e1, e2, e3) { \
839 if (test_split_simple (pattern, string, e1, e2, e3, NULL)) \
846 test_split_full (const gchar *pattern,
854 GSList *expected = NULL;
861 verbose ("splitting \"%s\" against \"%s\" (start: %d, max: %d) \t",
862 string, pattern, start_position, max_tokens);
864 /* The va_list is a NULL-terminated sequence of extected strings. */
865 va_start (args, max_tokens);
868 gchar *expected_string = va_arg (args, gchar *);
869 if (expected_string == NULL)
872 expected = g_slist_prepend (expected, g_strdup (expected_string));
874 expected = g_slist_reverse (expected);
877 regex = g_regex_new (pattern, 0, 0, NULL);
878 tokens = g_regex_split_full (regex, string, -1, start_position,
879 0, max_tokens, NULL);
881 token_count = g_strv_length (tokens);
885 if (token_count != g_slist_length (expected))
887 g_print ("failed \t(got %d %s, expected %d)\n", token_count,
888 token_count == 1 ? "match" : "matches",
889 g_slist_length (expected));
895 for (i = 0; l_exp != NULL; i++, l_exp = g_slist_next (l_exp))
897 if (!streq(l_exp->data, tokens [i]))
899 g_print ("failed \t(got \"%s\", expected \"%s\")\n",
900 tokens[i], (gchar *)l_exp->data);
906 verbose ("passed (%d %s)\n", token_count,
907 token_count == 1 ? "token" : "tokens");
910 g_regex_unref (regex);
911 g_slist_foreach (expected, (GFunc)g_free, NULL);
912 g_slist_free (expected);
919 test_split (const gchar *pattern,
925 GSList *expected = NULL;
932 verbose ("splitting \"%s\" against \"%s\" \t", string, pattern);
934 /* The va_list is a NULL-terminated sequence of extected strings. */
935 va_start (args, string);
938 gchar *expected_string = va_arg (args, gchar *);
939 if (expected_string == NULL)
942 expected = g_slist_prepend (expected, g_strdup (expected_string));
944 expected = g_slist_reverse (expected);
947 regex = g_regex_new (pattern, 0, 0, NULL);
948 tokens = g_regex_split (regex, string, 0);
950 token_count = g_strv_length (tokens);
954 if (token_count != g_slist_length (expected))
956 g_print ("failed \t(got %d %s, expected %d)\n", token_count,
957 token_count == 1 ? "match" : "matches",
958 g_slist_length (expected));
964 for (i = 0; l_exp != NULL; i++, l_exp = g_slist_next (l_exp))
966 if (!streq(l_exp->data, tokens [i]))
968 g_print ("failed \t(got \"%s\", expected \"%s\")\n",
969 tokens[i], (gchar *)l_exp->data);
975 verbose ("passed (%d %s)\n", token_count,
976 token_count == 1 ? "token" : "tokens");
979 g_regex_unref (regex);
980 g_slist_foreach (expected, (GFunc)g_free, NULL);
981 g_slist_free (expected);
987 #define TEST_SPLIT0(pattern, string, start_position, max_tokens) { \
989 if (test_split_full (pattern, string, start_position, max_tokens, NULL)) \
993 if (start_position == 0 && max_tokens <= 0) \
996 if (test_split (pattern, string, NULL)) \
1003 #define TEST_SPLIT1(pattern, string, start_position, max_tokens, e1) { \
1005 if (test_split_full (pattern, string, start_position, max_tokens, e1, NULL)) \
1009 if (start_position == 0 && max_tokens <= 0) \
1012 if (test_split (pattern, string, e1, NULL)) \
1019 #define TEST_SPLIT2(pattern, string, start_position, max_tokens, e1, e2) { \
1021 if (test_split_full (pattern, string, start_position, max_tokens, e1, e2, NULL)) \
1025 if (start_position == 0 && max_tokens <= 0) \
1028 if (test_split (pattern, string, e1, e2, NULL)) \
1035 #define TEST_SPLIT3(pattern, string, start_position, max_tokens, e1, e2, e3) { \
1037 if (test_split_full (pattern, string, start_position, max_tokens, e1, e2, e3, NULL)) \
1041 if (start_position == 0 && max_tokens <= 0) \
1044 if (test_split (pattern, string, e1, e2, e3, NULL)) \
1052 test_check_replacement (const gchar *string_to_expand,
1054 gboolean expected_refs)
1059 verbose ("checking replacement string \"%s\" \t", string_to_expand);
1061 result = g_regex_check_replacement (string_to_expand, &has_refs, NULL);
1062 if (expected != result)
1064 g_print ("failed \t(got \"%s\", expected \"%s\")\n",
1065 result ? "TRUE" : "FALSE",
1066 expected ? "TRUE" : "FALSE");
1070 if (expected && expected_refs != has_refs)
1072 g_print ("failed \t(got has_references \"%s\", expected \"%s\")\n",
1073 has_refs ? "TRUE" : "FALSE",
1074 expected_refs ? "TRUE" : "FALSE");
1078 verbose ("passed\n");
1082 #define TEST_CHECK_REPLACEMENT(string_to_expand, expected, expected_refs) { \
1084 if (test_check_replacement (string_to_expand, expected, expected_refs)) \
1090 test_expand (const gchar *pattern,
1091 const gchar *string,
1092 const gchar *string_to_expand,
1094 const gchar *expected)
1096 GRegex *regex = NULL;
1097 GMatchInfo *match_info = NULL;
1100 verbose ("expanding the references in \"%s\" (pattern: \"%s\", string: \"%s\") \t",
1101 string_to_expand, pattern, string);
1105 regex = g_regex_new (pattern, raw ? G_REGEX_RAW : 0, 0, NULL);
1106 g_regex_match (regex, string, 0, &match_info);
1109 res = g_match_info_expand_references (match_info, string_to_expand, NULL);
1110 if (!streq (res, expected))
1112 g_print ("failed \t(got \"%s\", expected \"%s\")\n", res, expected);
1114 g_match_info_free (match_info);
1115 g_regex_unref (regex);
1120 g_match_info_free (match_info);
1122 g_regex_unref (regex);
1124 verbose ("passed\n");
1128 #define TEST_EXPAND(pattern, string, string_to_expand, raw, expected) { \
1130 if (test_expand (pattern, string, string_to_expand, raw, expected)) \
1137 test_replace (const gchar *pattern,
1138 const gchar *string,
1139 gint start_position,
1140 const gchar *replacement,
1141 const gchar *expected)
1146 verbose ("replacing \"%s\" in \"%s\" (pattern: \"%s\", start: %d) \t",
1147 replacement, string, pattern, start_position);
1149 regex = g_regex_new (pattern, 0, 0, NULL);
1150 res = g_regex_replace (regex, string, -1, start_position, replacement, 0, NULL);
1151 if (!streq (res, expected))
1153 g_print ("failed \t(got \"%s\", expected \"%s\")\n", res, expected);
1155 g_regex_unref (regex);
1160 g_regex_unref (regex);
1162 verbose ("passed\n");
1166 #define TEST_REPLACE(pattern, string, start_position, replacement, expected) { \
1168 if (test_replace (pattern, string, start_position, replacement, expected)) \
1175 test_replace_lit (const gchar *pattern,
1176 const gchar *string,
1177 gint start_position,
1178 const gchar *replacement,
1179 const gchar *expected)
1184 verbose ("replacing literally \"%s\" in \"%s\" (pattern: \"%s\", start: %d) \t",
1185 replacement, string, pattern, start_position);
1187 regex = g_regex_new (pattern, 0, 0, NULL);
1188 res = g_regex_replace_literal (regex, string, -1, start_position,
1189 replacement, 0, NULL);
1190 if (!streq (res, expected))
1192 g_print ("failed \t(got \"%s\", expected \"%s\")\n", res, expected);
1194 g_regex_unref (regex);
1199 g_regex_unref (regex);
1201 verbose ("passed\n");
1205 #define TEST_REPLACE_LIT(pattern, string, start_position, replacement, expected) { \
1207 if (test_replace_lit (pattern, string, start_position, replacement, expected)) \
1214 test_get_string_number (const gchar *pattern,
1221 verbose ("getting the number of \"%s\" (pattern: \"%s\") \t",
1224 regex = g_regex_new (pattern, 0, 0, NULL);
1225 num = g_regex_get_string_number (regex, name);
1226 g_regex_unref (regex);
1228 if (num != expected_num)
1230 g_print ("failed \t(got %d, expected %d)\n", num, expected_num);
1235 verbose ("passed\n");
1240 #define TEST_GET_STRING_NUMBER(pattern, name, expected_num) { \
1242 if (test_get_string_number (pattern, name, expected_num)) \
1249 test_escape (const gchar *string,
1251 const gchar *expected)
1255 verbose ("escaping \"%s\" (len: %d) \t", string, length);
1257 escaped = g_regex_escape_string (string, length);
1259 if (!streq (escaped, expected))
1261 g_print ("failed \t(got \"%s\", expected \"%s\")\n", escaped, expected);
1268 verbose ("passed\n");
1272 #define TEST_ESCAPE(string, length, expected) { \
1274 if (test_escape (string, length, expected)) \
1281 test_match_all_full (const gchar *pattern,
1282 const gchar *string,
1284 gint start_position,
1288 GMatchInfo *match_info;
1290 GSList *expected = NULL;
1293 gboolean ret = TRUE;
1297 verbose ("matching all in \"%s\" against \"%s\" (start: %d, len: %d) \t",
1298 string, pattern, start_position, string_len);
1300 /* The va_list is a NULL-terminated sequence of: extected matched string,
1301 * expected start and expected end. */
1302 va_start (args, start_position);
1306 const gchar *expected_string = va_arg (args, const gchar *);
1307 if (expected_string == NULL)
1309 match = g_new0 (Match, 1);
1310 match->string = g_strdup (expected_string);
1311 match->start = va_arg (args, gint);
1312 match->end = va_arg (args, gint);
1313 expected = g_slist_prepend (expected, match);
1315 expected = g_slist_reverse (expected);
1318 regex = g_regex_new (pattern, 0, 0, NULL);
1319 match_ok = g_regex_match_all_full (regex, string, string_len, start_position,
1320 0, &match_info, NULL);
1322 if (match_ok && g_slist_length (expected) == 0)
1324 g_print ("failed\n");
1328 if (!match_ok && g_slist_length (expected) != 0)
1330 g_print ("failed\n");
1335 match_count = g_match_info_get_match_count (match_info);
1336 if (match_count != g_slist_length (expected))
1338 g_print ("failed \t(got %d %s, expected %d)\n", match_count,
1339 match_count == 1 ? "match" : "matches",
1340 g_slist_length (expected));
1346 for (i = 0; i < match_count; i++)
1349 gchar *matched_string;
1350 Match *exp = l_exp->data;
1352 matched_string = g_match_info_fetch (match_info, i);
1353 g_match_info_fetch_pos (match_info, i, &start, &end);
1355 if (!streq(exp->string, matched_string))
1357 g_print ("failed \t(got \"%s\", expected \"%s\")\n",
1358 matched_string, exp->string);
1359 g_free (matched_string);
1363 g_free (matched_string);
1365 if (exp->start != start || exp->end != end)
1367 g_print ("failed \t(got [%d, %d], expected [%d, %d])\n",
1368 start, end, exp->start, exp->end);
1373 l_exp = g_slist_next (l_exp);
1379 verbose ("passed (%d %s)\n", match_count, match_count == 1 ? "match" : "matches");
1382 g_match_info_free (match_info);
1383 g_regex_unref (regex);
1384 g_slist_foreach (expected, free_match, NULL);
1385 g_slist_free (expected);
1391 test_match_all (const gchar *pattern,
1392 const gchar *string,
1396 GMatchInfo *match_info;
1398 GSList *expected = NULL;
1401 gboolean ret = TRUE;
1405 verbose ("matching all in \"%s\" against \"%s\" \t", string, pattern);
1407 /* The va_list is a NULL-terminated sequence of: extected matched string,
1408 * expected start and expected end. */
1409 va_start (args, string);
1413 const gchar *expected_string = va_arg (args, const gchar *);
1414 if (expected_string == NULL)
1416 match = g_new0 (Match, 1);
1417 match->string = g_strdup (expected_string);
1418 match->start = va_arg (args, gint);
1419 match->end = va_arg (args, gint);
1420 expected = g_slist_prepend (expected, match);
1422 expected = g_slist_reverse (expected);
1425 regex = g_regex_new (pattern, 0, 0, NULL);
1426 match_ok = g_regex_match_all (regex, string, 0, &match_info);
1428 if (match_ok && g_slist_length (expected) == 0)
1430 g_print ("failed\n");
1434 if (!match_ok && g_slist_length (expected) != 0)
1436 g_print ("failed\n");
1441 match_count = g_match_info_get_match_count (match_info);
1442 if (match_count != g_slist_length (expected))
1444 g_print ("failed \t(got %d %s, expected %d)\n", match_count,
1445 match_count == 1 ? "match" : "matches",
1446 g_slist_length (expected));
1452 for (i = 0; i < match_count; i++)
1455 gchar *matched_string;
1456 Match *exp = l_exp->data;
1458 matched_string = g_match_info_fetch (match_info, i);
1459 g_match_info_fetch_pos (match_info, i, &start, &end);
1461 if (!streq(exp->string, matched_string))
1463 g_print ("failed \t(got \"%s\", expected \"%s\")\n",
1464 matched_string, exp->string);
1465 g_free (matched_string);
1469 g_free (matched_string);
1471 if (exp->start != start || exp->end != end)
1473 g_print ("failed \t(got [%d, %d], expected [%d, %d])\n",
1474 start, end, exp->start, exp->end);
1479 l_exp = g_slist_next (l_exp);
1485 verbose ("passed (%d %s)\n", match_count, match_count == 1 ? "match" : "matches");
1488 g_match_info_free (match_info);
1489 g_regex_unref (regex);
1490 g_slist_foreach (expected, free_match, NULL);
1491 g_slist_free (expected);
1496 #define TEST_MATCH_ALL0(pattern, string, string_len, start_position) { \
1498 if (test_match_all_full (pattern, string, string_len, start_position, NULL)) \
1502 if (string_len == -1 && start_position == 0) \
1505 if (test_match_all (pattern, string, NULL)) \
1512 #define TEST_MATCH_ALL1(pattern, string, string_len, start_position, \
1515 if (test_match_all_full (pattern, string, string_len, start_position, \
1516 t1, s1, e1, NULL)) \
1520 if (string_len == -1 && start_position == 0) \
1523 if (test_match_all (pattern, string, t1, s1, e1, NULL)) \
1530 #define TEST_MATCH_ALL2(pattern, string, string_len, start_position, \
1531 t1, s1, e1, t2, s2, e2) { \
1533 if (test_match_all_full (pattern, string, string_len, start_position, \
1534 t1, s1, e1, t2, s2, e2, NULL)) \
1538 if (string_len == -1 && start_position == 0) \
1541 if (test_match_all (pattern, string, t1, s1, e1, t2, s2, e2, NULL)) \
1548 #define TEST_MATCH_ALL3(pattern, string, string_len, start_position, \
1549 t1, s1, e1, t2, s2, e2, t3, s3, e3) { \
1551 if (test_match_all_full (pattern, string, string_len, start_position, \
1552 t1, s1, e1, t2, s2, e2, t3, s3, e3, NULL)) \
1556 if (string_len == -1 && start_position == 0) \
1559 if (test_match_all (pattern, string, t1, s1, e1, t2, s2, e2, t3, s3, e3, NULL)) \
1567 main (int argc, char *argv[])
1574 setlocale (LC_ALL, "");
1576 for (i = 1; i < argc; i++)
1578 if (streq ("--noisy", argv[i]))
1580 else if (streq ("--abort", argv[i]))
1581 abort_on_fail = TRUE;
1584 g_setenv ("G_DEBUG", "fatal_warnings", TRUE);
1586 /* TEST_NEW(pattern, compile_opts, match_opts) */
1588 TEST_NEW(".*", 0, 0);
1589 TEST_NEW(".*", G_REGEX_OPTIMIZE, 0);
1590 TEST_NEW(".*", G_REGEX_MULTILINE, 0);
1591 TEST_NEW(".*", G_REGEX_DOTALL, 0);
1592 TEST_NEW(".*", G_REGEX_DOTALL, G_REGEX_MATCH_NOTBOL);
1593 TEST_NEW("(123\\d*)[a-zA-Z]+(?P<hello>.*)", 0, 0);
1594 TEST_NEW("(123\\d*)[a-zA-Z]+(?P<hello>.*)", G_REGEX_CASELESS, 0);
1595 TEST_NEW("(123\\d*)[a-zA-Z]+(?P<hello>.*)", G_REGEX_CASELESS | G_REGEX_OPTIMIZE, 0);
1596 TEST_NEW("(?P<A>x)|(?P<A>y)", G_REGEX_DUPNAMES, 0);
1597 TEST_NEW("(?P<A>x)|(?P<A>y)", G_REGEX_DUPNAMES | G_REGEX_OPTIMIZE, 0);
1598 /* This gives "internal error: code overflow" with pcre 6.0 */
1599 TEST_NEW("(?i)(?-i)", 0, 0);
1601 /* TEST_NEW_FAIL(pattern, compile_opts) */
1602 TEST_NEW_FAIL("(", 0);
1603 TEST_NEW_FAIL(")", 0);
1604 TEST_NEW_FAIL("[", 0);
1605 TEST_NEW_FAIL("*", 0);
1606 TEST_NEW_FAIL("?", 0);
1607 TEST_NEW_FAIL("(?P<A>x)|(?P<A>y)", 0);
1609 /* TEST_MATCH_SIMPLE(pattern, string, compile_opts, match_opts, expected) */
1610 TEST_MATCH_SIMPLE("a", "", 0, 0, FALSE);
1611 TEST_MATCH_SIMPLE("a", "a", 0, 0, TRUE);
1612 TEST_MATCH_SIMPLE("a", "ba", 0, 0, TRUE);
1613 TEST_MATCH_SIMPLE("^a", "ba", 0, 0, FALSE);
1614 TEST_MATCH_SIMPLE("a", "ba", G_REGEX_ANCHORED, 0, FALSE);
1615 TEST_MATCH_SIMPLE("a", "ba", 0, G_REGEX_MATCH_ANCHORED, FALSE);
1616 TEST_MATCH_SIMPLE("a", "ab", G_REGEX_ANCHORED, 0, TRUE);
1617 TEST_MATCH_SIMPLE("a", "ab", 0, G_REGEX_MATCH_ANCHORED, TRUE);
1618 TEST_MATCH_SIMPLE("a", "a", G_REGEX_CASELESS, 0, TRUE);
1619 TEST_MATCH_SIMPLE("a", "A", G_REGEX_CASELESS, 0, TRUE);
1620 /* These are needed to test extended properties. */
1621 TEST_MATCH_SIMPLE(AGRAVE, AGRAVE, G_REGEX_CASELESS, 0, TRUE);
1622 TEST_MATCH_SIMPLE(AGRAVE, AGRAVE_UPPER, G_REGEX_CASELESS, 0, TRUE);
1623 TEST_MATCH_SIMPLE("\\p{L}", "a", 0, 0, TRUE);
1624 TEST_MATCH_SIMPLE("\\p{L}", "1", 0, 0, FALSE);
1625 TEST_MATCH_SIMPLE("\\p{L}", AGRAVE, 0, 0, TRUE);
1626 TEST_MATCH_SIMPLE("\\p{L}", AGRAVE_UPPER, 0, 0, TRUE);
1627 TEST_MATCH_SIMPLE("\\p{L}", SHEEN, 0, 0, TRUE);
1628 TEST_MATCH_SIMPLE("\\p{L}", ETH30, 0, 0, FALSE);
1629 TEST_MATCH_SIMPLE("\\p{Ll}", "a", 0, 0, TRUE);
1630 TEST_MATCH_SIMPLE("\\p{Ll}", AGRAVE, 0, 0, TRUE);
1631 TEST_MATCH_SIMPLE("\\p{Ll}", AGRAVE_UPPER, 0, 0, FALSE);
1632 TEST_MATCH_SIMPLE("\\p{Ll}", ETH30, 0, 0, FALSE);
1633 TEST_MATCH_SIMPLE("\\p{Sc}", AGRAVE, 0, 0, FALSE);
1634 TEST_MATCH_SIMPLE("\\p{Sc}", EURO, 0, 0, TRUE);
1635 TEST_MATCH_SIMPLE("\\p{Sc}", ETH30, 0, 0, FALSE);
1636 TEST_MATCH_SIMPLE("\\p{N}", "a", 0, 0, FALSE);
1637 TEST_MATCH_SIMPLE("\\p{N}", "1", 0, 0, TRUE);
1638 TEST_MATCH_SIMPLE("\\p{N}", AGRAVE, 0, 0, FALSE);
1639 TEST_MATCH_SIMPLE("\\p{N}", AGRAVE_UPPER, 0, 0, FALSE);
1640 TEST_MATCH_SIMPLE("\\p{N}", SHEEN, 0, 0, FALSE);
1641 TEST_MATCH_SIMPLE("\\p{N}", ETH30, 0, 0, TRUE);
1642 TEST_MATCH_SIMPLE("\\p{Nd}", "a", 0, 0, FALSE);
1643 TEST_MATCH_SIMPLE("\\p{Nd}", "1", 0, 0, TRUE);
1644 TEST_MATCH_SIMPLE("\\p{Nd}", AGRAVE, 0, 0, FALSE);
1645 TEST_MATCH_SIMPLE("\\p{Nd}", AGRAVE_UPPER, 0, 0, FALSE);
1646 TEST_MATCH_SIMPLE("\\p{Nd}", SHEEN, 0, 0, FALSE);
1647 TEST_MATCH_SIMPLE("\\p{Nd}", ETH30, 0, 0, FALSE);
1648 TEST_MATCH_SIMPLE("\\p{Common}", SHEEN, 0, 0, FALSE);
1649 TEST_MATCH_SIMPLE("\\p{Common}", "a", 0, 0, FALSE);
1650 TEST_MATCH_SIMPLE("\\p{Common}", AGRAVE, 0, 0, FALSE);
1651 TEST_MATCH_SIMPLE("\\p{Common}", AGRAVE_UPPER, 0, 0, FALSE);
1652 TEST_MATCH_SIMPLE("\\p{Common}", ETH30, 0, 0, FALSE);
1653 TEST_MATCH_SIMPLE("\\p{Common}", "%", 0, 0, TRUE);
1654 TEST_MATCH_SIMPLE("\\p{Common}", "1", 0, 0, TRUE);
1655 TEST_MATCH_SIMPLE("\\p{Arabic}", SHEEN, 0, 0, TRUE);
1656 TEST_MATCH_SIMPLE("\\p{Arabic}", "a", 0, 0, FALSE);
1657 TEST_MATCH_SIMPLE("\\p{Arabic}", AGRAVE, 0, 0, FALSE);
1658 TEST_MATCH_SIMPLE("\\p{Arabic}", AGRAVE_UPPER, 0, 0, FALSE);
1659 TEST_MATCH_SIMPLE("\\p{Arabic}", ETH30, 0, 0, FALSE);
1660 TEST_MATCH_SIMPLE("\\p{Arabic}", "%", 0, 0, FALSE);
1661 TEST_MATCH_SIMPLE("\\p{Arabic}", "1", 0, 0, FALSE);
1662 TEST_MATCH_SIMPLE("\\p{Latin}", SHEEN, 0, 0, FALSE);
1663 TEST_MATCH_SIMPLE("\\p{Latin}", "a", 0, 0, TRUE);
1664 TEST_MATCH_SIMPLE("\\p{Latin}", AGRAVE, 0, 0, TRUE);
1665 TEST_MATCH_SIMPLE("\\p{Latin}", AGRAVE_UPPER, 0, 0, TRUE);
1666 TEST_MATCH_SIMPLE("\\p{Latin}", ETH30, 0, 0, FALSE);
1667 TEST_MATCH_SIMPLE("\\p{Latin}", "%", 0, 0, FALSE);
1668 TEST_MATCH_SIMPLE("\\p{Latin}", "1", 0, 0, FALSE);
1669 TEST_MATCH_SIMPLE("\\p{Ethiopic}", SHEEN, 0, 0, FALSE);
1670 TEST_MATCH_SIMPLE("\\p{Ethiopic}", "a", 0, 0, FALSE);
1671 TEST_MATCH_SIMPLE("\\p{Ethiopic}", AGRAVE, 0, 0, FALSE);
1672 TEST_MATCH_SIMPLE("\\p{Ethiopic}", AGRAVE_UPPER, 0, 0, FALSE);
1673 TEST_MATCH_SIMPLE("\\p{Ethiopic}", ETH30, 0, 0, TRUE);
1674 TEST_MATCH_SIMPLE("\\p{Ethiopic}", "%", 0, 0, FALSE);
1675 TEST_MATCH_SIMPLE("\\p{Ethiopic}", "1", 0, 0, FALSE);
1676 TEST_MATCH_SIMPLE("\\p{L}(?<=\\p{Arabic})", SHEEN, 0, 0, TRUE);
1677 TEST_MATCH_SIMPLE("\\p{L}(?<=\\p{Latin})", SHEEN, 0, 0, FALSE);
1678 /* Invalid patterns. */
1679 TEST_MATCH_SIMPLE("\\", "a", 0, 0, FALSE);
1680 TEST_MATCH_SIMPLE("[", "", 0, 0, FALSE);
1682 /* TEST_MATCH(pattern, compile_opts, match_opts, string,
1683 * string_len, start_position, match_opts2, expected) */
1684 TEST_MATCH("a", 0, 0, "a", -1, 0, 0, TRUE);
1685 TEST_MATCH("a", 0, 0, "A", -1, 0, 0, FALSE);
1686 TEST_MATCH("a", G_REGEX_CASELESS, 0, "A", -1, 0, 0, TRUE);
1687 TEST_MATCH("a", 0, 0, "ab", -1, 1, 0, FALSE);
1688 TEST_MATCH("a", 0, 0, "ba", 1, 0, 0, FALSE);
1689 TEST_MATCH("a", 0, 0, "bab", -1, 0, 0, TRUE);
1690 TEST_MATCH("a", 0, 0, "b", -1, 0, 0, FALSE);
1691 TEST_MATCH("a", 0, G_REGEX_ANCHORED, "a", -1, 0, 0, TRUE);
1692 TEST_MATCH("a", 0, G_REGEX_ANCHORED, "ab", -1, 1, 0, FALSE);
1693 TEST_MATCH("a", 0, G_REGEX_ANCHORED, "ba", 1, 0, 0, FALSE);
1694 TEST_MATCH("a", 0, G_REGEX_ANCHORED, "bab", -1, 0, 0, FALSE);
1695 TEST_MATCH("a", 0, G_REGEX_ANCHORED, "b", -1, 0, 0, FALSE);
1696 TEST_MATCH("a", 0, 0, "a", -1, 0, G_REGEX_ANCHORED, TRUE);
1697 TEST_MATCH("a", 0, 0, "ab", -1, 1, G_REGEX_ANCHORED, FALSE);
1698 TEST_MATCH("a", 0, 0, "ba", 1, 0, G_REGEX_ANCHORED, FALSE);
1699 TEST_MATCH("a", 0, 0, "bab", -1, 0, G_REGEX_ANCHORED, FALSE);
1700 TEST_MATCH("a", 0, 0, "b", -1, 0, G_REGEX_ANCHORED, FALSE);
1701 TEST_MATCH("a|b", 0, 0, "a", -1, 0, 0, TRUE);
1702 TEST_MATCH("\\d", 0, 0, EURO, -1, 0, 0, FALSE);
1703 TEST_MATCH("^.$", 0, 0, EURO, -1, 0, 0, TRUE);
1704 TEST_MATCH("^.{3}$", 0, 0, EURO, -1, 0, 0, FALSE);
1705 TEST_MATCH("^.$", G_REGEX_RAW, 0, EURO, -1, 0, 0, FALSE);
1706 TEST_MATCH("^.{3}$", G_REGEX_RAW, 0, EURO, -1, 0, 0, TRUE);
1707 TEST_MATCH(AGRAVE, G_REGEX_CASELESS, 0, AGRAVE_UPPER, -1, 0, 0, TRUE);
1709 /* New lines handling. */
1710 TEST_MATCH("^a\\Rb$", 0, 0, "a\r\nb", -1, 0, 0, TRUE);
1711 TEST_MATCH("^a\\Rb$", 0, 0, "a\nb", -1, 0, 0, TRUE);
1712 TEST_MATCH("^a\\Rb$", 0, 0, "a\rb", -1, 0, 0, TRUE);
1713 TEST_MATCH("^a\\Rb$", 0, 0, "a\n\rb", -1, 0, 0, FALSE);
1714 TEST_MATCH("^a\\R\\Rb$", 0, 0, "a\n\rb", -1, 0, 0, TRUE);
1715 TEST_MATCH("^a\\nb$", 0, 0, "a\r\nb", -1, 0, 0, FALSE);
1716 TEST_MATCH("^a\\r\\nb$", 0, 0, "a\r\nb", -1, 0, 0, TRUE);
1718 TEST_MATCH("^b$", 0, 0, "a\nb\nc", -1, 0, 0, FALSE);
1719 TEST_MATCH("^b$", G_REGEX_MULTILINE, 0, "a\nb\nc", -1, 0, 0, TRUE);
1720 TEST_MATCH("^b$", G_REGEX_MULTILINE, 0, "a\r\nb\r\nc", -1, 0, 0, TRUE);
1721 TEST_MATCH("^b$", G_REGEX_MULTILINE, 0, "a\rb\rc", -1, 0, 0, TRUE);
1722 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, 0, "a\nb\nc", -1, 0, 0, FALSE);
1723 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_LF, 0, "a\nb\nc", -1, 0, 0, TRUE);
1724 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CRLF, 0, "a\nb\nc", -1, 0, 0, FALSE);
1725 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, 0, "a\r\nb\r\nc", -1, 0, 0, FALSE);
1726 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_LF, 0, "a\r\nb\r\nc", -1, 0, 0, FALSE);
1727 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CRLF, 0, "a\r\nb\r\nc", -1, 0, 0, TRUE);
1728 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, 0, "a\rb\rc", -1, 0, 0, TRUE);
1729 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_LF, 0, "a\rb\rc", -1, 0, 0, FALSE);
1730 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CRLF, 0, "a\rb\rc", -1, 0, 0, FALSE);
1731 TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_CR, "a\nb\nc", -1, 0, 0, FALSE);
1732 TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_LF, "a\nb\nc", -1, 0, 0, TRUE);
1733 TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_CRLF, "a\nb\nc", -1, 0, 0, FALSE);
1734 TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_CR, "a\r\nb\r\nc", -1, 0, 0, FALSE);
1735 TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_LF, "a\r\nb\r\nc", -1, 0, 0, FALSE);
1736 TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_CRLF, "a\r\nb\r\nc", -1, 0, 0, TRUE);
1737 TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_CR, "a\rb\rc", -1, 0, 0, TRUE);
1738 TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_LF, "a\rb\rc", -1, 0, 0, FALSE);
1739 TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_CRLF, "a\rb\rc", -1, 0, 0, FALSE);
1741 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_ANY, "a\nb\nc", -1, 0, 0, TRUE);
1742 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_ANY, "a\rb\rc", -1, 0, 0, TRUE);
1743 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_ANY, "a\r\nb\r\nc", -1, 0, 0, TRUE);
1744 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_LF, "a\nb\nc", -1, 0, 0, TRUE);
1745 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_LF, "a\rb\rc", -1, 0, 0, FALSE);
1746 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_CRLF, "a\r\nb\r\nc", -1, 0, 0, TRUE);
1747 TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_CRLF, "a\rb\rc", -1, 0, 0, FALSE);
1749 TEST_MATCH("a#\nb", G_REGEX_EXTENDED, 0, "a", -1, 0, 0, FALSE);
1750 TEST_MATCH("a#\r\nb", G_REGEX_EXTENDED, 0, "a", -1, 0, 0, FALSE);
1751 TEST_MATCH("a#\rb", G_REGEX_EXTENDED, 0, "a", -1, 0, 0, FALSE);
1752 TEST_MATCH("a#\nb", G_REGEX_EXTENDED, G_REGEX_MATCH_NEWLINE_CR, "a", -1, 0, 0, FALSE);
1753 TEST_MATCH("a#\nb", G_REGEX_EXTENDED | G_REGEX_NEWLINE_CR, 0, "a", -1, 0, 0, TRUE);
1755 /* TEST_MATCH_NEXT#(pattern, string, string_len, start_position, ...) */
1756 TEST_MATCH_NEXT0("a", "x", -1, 0);
1757 TEST_MATCH_NEXT0("a", "ax", -1, 1);
1758 TEST_MATCH_NEXT0("a", "xa", 1, 0);
1759 TEST_MATCH_NEXT0("a", "axa", 1, 2);
1760 TEST_MATCH_NEXT1("a", "a", -1, 0, "a", 0, 1);
1761 TEST_MATCH_NEXT1("a", "xax", -1, 0, "a", 1, 2);
1762 TEST_MATCH_NEXT1(EURO, ENG EURO, -1, 0, EURO, 2, 5);
1763 TEST_MATCH_NEXT1("a*", "", -1, 0, "", 0, 0);
1764 TEST_MATCH_NEXT2("a*", "aa", -1, 0, "aa", 0, 2, "", 2, 2);
1765 TEST_MATCH_NEXT2(EURO "*", EURO EURO, -1, 0, EURO EURO, 0, 6, "", 6, 6);
1766 TEST_MATCH_NEXT2("a", "axa", -1, 0, "a", 0, 1, "a", 2, 3);
1767 TEST_MATCH_NEXT2("a+", "aaxa", -1, 0, "aa", 0, 2, "a", 3, 4);
1768 TEST_MATCH_NEXT2("a", "aa", -1, 0, "a", 0, 1, "a", 1, 2);
1769 TEST_MATCH_NEXT2("a", "ababa", -1, 2, "a", 2, 3, "a", 4, 5);
1770 TEST_MATCH_NEXT2(EURO "+", EURO "-" EURO, -1, 0, EURO, 0, 3, EURO, 4, 7);
1771 TEST_MATCH_NEXT3("", "ab", -1, 0, "", 0, 0, "", 1, 1, "", 2, 2);
1772 TEST_MATCH_NEXT3("", AGRAVE "b", -1, 0, "", 0, 0, "", 2, 2, "", 3, 3);
1773 TEST_MATCH_NEXT3("a", "aaxa", -1, 0, "a", 0, 1, "a", 1, 2, "a", 3, 4);
1774 TEST_MATCH_NEXT3("a", "aa" OGRAVE "a", -1, 0, "a", 0, 1, "a", 1, 2, "a", 4, 5);
1775 TEST_MATCH_NEXT3("a*", "aax", -1, 0, "aa", 0, 2, "", 2, 2, "", 3, 3);
1776 TEST_MATCH_NEXT4("a*", "aaxa", -1, 0, "aa", 0, 2, "", 2, 2, "a", 3, 4, "", 4, 4);
1778 /* TEST_MATCH_COUNT(pattern, string, start_position, match_opts, expected_count) */
1779 TEST_MATCH_COUNT("a", "", 0, 0, 0);
1780 TEST_MATCH_COUNT("a", "a", 0, 0, 1);
1781 TEST_MATCH_COUNT("a", "a", 1, 0, 0);
1782 TEST_MATCH_COUNT("(.)", "a", 0, 0, 2);
1783 TEST_MATCH_COUNT("(.)", EURO, 0, 0, 2);
1784 TEST_MATCH_COUNT("(?:.)", "a", 0, 0, 1);
1785 TEST_MATCH_COUNT("(?P<A>.)", "a", 0, 0, 2);
1786 TEST_MATCH_COUNT("a$", "a", 0, G_REGEX_MATCH_NOTEOL, 0);
1787 TEST_MATCH_COUNT("(a)?(b)", "b", 0, 0, 3);
1788 TEST_MATCH_COUNT("(a)?(b)", "ab", 0, 0, 3);
1790 /* TEST_PARTIAL(pattern, string, expected) */
1791 TEST_PARTIAL("^ab", "a", TRUE);
1792 TEST_PARTIAL("^ab", "xa", FALSE);
1793 TEST_PARTIAL("ab", "xa", TRUE);
1794 TEST_PARTIAL("ab", "ab", FALSE); /* normal match. */
1795 TEST_PARTIAL("a+b", "aa", FALSE); /* PCRE_ERROR_BAD_PARTIAL */
1796 TEST_PARTIAL("(a)+b", "aa", TRUE);
1797 TEST_PARTIAL("a?b", "a", TRUE);
1799 /* TEST_SUB_PATTERN(pattern, string, start_position, sub_n, expected_sub,
1800 * expected_start, expected_end) */
1801 TEST_SUB_PATTERN("a", "a", 0, 0, "a", 0, 1);
1802 TEST_SUB_PATTERN("a(.)", "ab", 0, 1, "b", 1, 2);
1803 TEST_SUB_PATTERN("a(.)", "a" EURO, 0, 1, EURO, 1, 4);
1804 TEST_SUB_PATTERN("(?:.*)(a)(.)", "xxa" ENG, 0, 2, ENG, 3, 5);
1805 TEST_SUB_PATTERN("(" HSTROKE ")", "a" HSTROKE ENG, 0, 1, HSTROKE, 1, 3);
1806 TEST_SUB_PATTERN("a", "a", 0, 1, NULL, UNTOUCHED, UNTOUCHED);
1807 TEST_SUB_PATTERN("a", "a", 0, 1, NULL, UNTOUCHED, UNTOUCHED);
1808 TEST_SUB_PATTERN("(a)?(b)", "b", 0, 0, "b", 0, 1);
1809 TEST_SUB_PATTERN("(a)?(b)", "b", 0, 1, "", -1, -1);
1810 TEST_SUB_PATTERN("(a)?(b)", "b", 0, 2, "b", 0, 1);
1812 /* TEST_NAMED_SUB_PATTERN(pattern, string, start_position, sub_name,
1813 * expected_sub, expected_start, expected_end) */
1814 TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?", "ab", 0, "A", "b", 1, 2);
1815 TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?", "aab", 1, "A", "b", 2, 3);
1816 TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?", EURO "ab", 0, "A", "b", 4, 5);
1817 TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?", EURO "ab", 0, "B", NULL, UNTOUCHED, UNTOUCHED);
1818 TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?", EURO "ab", 0, "C", NULL, UNTOUCHED, UNTOUCHED);
1819 TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?", "a" EGRAVE "x", 0, "A", EGRAVE, 1, 3);
1820 TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?", "a" EGRAVE "x", 0, "B", "x", 3, 4);
1821 TEST_NAMED_SUB_PATTERN("(?P<A>a)?(?P<B>b)", "b", 0, "A", "", -1, -1);
1822 TEST_NAMED_SUB_PATTERN("(?P<A>a)?(?P<B>b)", "b", 0, "B", "b", 0, 1);
1824 /* TEST_NAMED_SUB_PATTERN_DUPNAMES(pattern, string, start_position, sub_name,
1825 * expected_sub, expected_start, expected_end) */
1826 TEST_NAMED_SUB_PATTERN_DUPNAMES("(?P<N>a)|(?P<N>b)", "ab", 0, "N", "a", 0, 1);
1827 TEST_NAMED_SUB_PATTERN_DUPNAMES("(?P<N>aa)|(?P<N>a)", "aa", 0, "N", "aa", 0, 2);
1828 TEST_NAMED_SUB_PATTERN_DUPNAMES("(?P<N>aa)(?P<N>a)", "aaa", 0, "N", "aa", 0, 2);
1829 TEST_NAMED_SUB_PATTERN_DUPNAMES("(?P<N>x)|(?P<N>a)", "a", 0, "N", "a", 0, 1);
1830 TEST_NAMED_SUB_PATTERN_DUPNAMES("(?P<N>x)y|(?P<N>a)b", "ab", 0, "N", "a", 0, 1);
1832 /* DUPNAMES option inside the pattern */
1833 TEST_NAMED_SUB_PATTERN("(?J)(?P<N>a)|(?P<N>b)", "ab", 0, "N", "a", 0, 1);
1834 TEST_NAMED_SUB_PATTERN("(?J)(?P<N>aa)|(?P<N>a)", "aa", 0, "N", "aa", 0, 2);
1835 TEST_NAMED_SUB_PATTERN("(?J)(?P<N>aa)(?P<N>a)", "aaa", 0, "N", "aa", 0, 2);
1836 TEST_NAMED_SUB_PATTERN("(?J)(?P<N>x)|(?P<N>a)", "a", 0, "N", "a", 0, 1);
1837 TEST_NAMED_SUB_PATTERN("(?J)(?P<N>x)y|(?P<N>a)b", "ab", 0, "N", "a", 0, 1);
1839 /* TEST_FETCH_ALL#(pattern, string, ...) */
1840 TEST_FETCH_ALL0("a", "");
1841 TEST_FETCH_ALL0("a", "b");
1842 TEST_FETCH_ALL1("a", "a", "a");
1843 TEST_FETCH_ALL1("a+", "aa", "aa");
1844 TEST_FETCH_ALL1("(?:a)", "a", "a");
1845 TEST_FETCH_ALL2("(a)", "a", "a", "a");
1846 TEST_FETCH_ALL2("a(.)", "ab", "ab", "b");
1847 TEST_FETCH_ALL2("a(.)", "a" HSTROKE, "a" HSTROKE, HSTROKE);
1848 TEST_FETCH_ALL3("(?:.*)(a)(.)", "xyazk", "xyaz", "a", "z");
1849 TEST_FETCH_ALL3("(?P<A>.)(a)", "xa", "xa", "x", "a");
1850 TEST_FETCH_ALL3("(?P<A>.)(a)", ENG "a", ENG "a", ENG, "a");
1851 TEST_FETCH_ALL3("(a)?(b)", "b", "b", "", "b");
1852 TEST_FETCH_ALL3("(a)?(b)", "ab", "ab", "a", "b");
1854 /* TEST_SPLIT_SIMPLE#(pattern, string, ...) */
1855 TEST_SPLIT_SIMPLE0("", "");
1856 TEST_SPLIT_SIMPLE0("a", "");
1857 TEST_SPLIT_SIMPLE1(",", "a", "a");
1858 TEST_SPLIT_SIMPLE1("(,)\\s*", "a", "a");
1859 TEST_SPLIT_SIMPLE2(",", "a,b", "a", "b");
1860 TEST_SPLIT_SIMPLE3(",", "a,b,c", "a", "b", "c");
1861 TEST_SPLIT_SIMPLE3(",\\s*", "a,b,c", "a", "b", "c");
1862 TEST_SPLIT_SIMPLE3(",\\s*", "a, b, c", "a", "b", "c");
1863 TEST_SPLIT_SIMPLE3("(,)\\s*", "a,b", "a", ",", "b");
1864 TEST_SPLIT_SIMPLE3("(,)\\s*", "a, b", "a", ",", "b");
1865 /* Not matched sub-strings. */
1866 TEST_SPLIT_SIMPLE2("a|(b)", "xay", "x", "y");
1867 TEST_SPLIT_SIMPLE3("a|(b)", "xby", "x", "b", "y");
1868 /* Empty matches. */
1869 TEST_SPLIT_SIMPLE3("", "abc", "a", "b", "c");
1870 TEST_SPLIT_SIMPLE3(" *", "ab c", "a", "b", "c");
1871 /* Invalid patterns. */
1872 TEST_SPLIT_SIMPLE0("\\", "");
1873 TEST_SPLIT_SIMPLE0("[", "");
1875 /* TEST_SPLIT#(pattern, string, start_position, max_tokens, ...) */
1876 TEST_SPLIT0("", "", 0, 0);
1877 TEST_SPLIT0("a", "", 0, 0);
1878 TEST_SPLIT0("a", "", 0, 1);
1879 TEST_SPLIT0("a", "", 0, 2);
1880 TEST_SPLIT0("a", "a", 1, 0);
1881 TEST_SPLIT1(",", "a", 0, 0, "a");
1882 TEST_SPLIT1(",", "a,b", 0, 1, "a,b");
1883 TEST_SPLIT1("(,)\\s*", "a", 0, 0, "a");
1884 TEST_SPLIT1(",", "a,b", 2, 0, "b");
1885 TEST_SPLIT2(",", "a,b", 0, 0, "a", "b");
1886 TEST_SPLIT2(",", "a,b,c", 0, 2, "a", "b,c");
1887 TEST_SPLIT2(",", "a,b", 1, 0, "", "b");
1888 TEST_SPLIT2(",", "a,", 0, 0, "a", "");
1889 TEST_SPLIT3(",", "a,b,c", 0, 0, "a", "b", "c");
1890 TEST_SPLIT3(",\\s*", "a,b,c", 0, 0, "a", "b", "c");
1891 TEST_SPLIT3(",\\s*", "a, b, c", 0, 0, "a", "b", "c");
1892 TEST_SPLIT3("(,)\\s*", "a,b", 0, 0, "a", ",", "b");
1893 TEST_SPLIT3("(,)\\s*", "a, b", 0, 0, "a", ",", "b");
1894 /* Not matched sub-strings. */
1895 TEST_SPLIT2("a|(b)", "xay", 0, 0, "x", "y");
1896 TEST_SPLIT3("a|(b)", "xby", 0, -1, "x", "b", "y");
1897 /* Empty matches. */
1898 TEST_SPLIT2(" *", "ab c", 1, 0, "b", "c");
1899 TEST_SPLIT3("", "abc", 0, 0, "a", "b", "c");
1900 TEST_SPLIT3(" *", "ab c", 0, 0, "a", "b", "c");
1901 TEST_SPLIT1(" *", "ab c", 0, 1, "ab c");
1902 TEST_SPLIT2(" *", "ab c", 0, 2, "a", "b c");
1903 TEST_SPLIT3(" *", "ab c", 0, 3, "a", "b", "c");
1904 TEST_SPLIT3(" *", "ab c", 0, 4, "a", "b", "c");
1906 /* TEST_CHECK_REPLACEMENT(string_to_expand, expected, expected_refs) */
1907 TEST_CHECK_REPLACEMENT("", TRUE, FALSE);
1908 TEST_CHECK_REPLACEMENT("a", TRUE, FALSE);
1909 TEST_CHECK_REPLACEMENT("\\t\\n\\v\\r\\f\\a\\b\\\\\\x{61}", TRUE, FALSE);
1910 TEST_CHECK_REPLACEMENT("\\0", TRUE, TRUE);
1911 TEST_CHECK_REPLACEMENT("\\n\\2", TRUE, TRUE);
1912 TEST_CHECK_REPLACEMENT("\\g<foo>", TRUE, TRUE);
1913 /* Invalid strings */
1914 TEST_CHECK_REPLACEMENT("\\Q", FALSE, FALSE);
1915 TEST_CHECK_REPLACEMENT("x\\Ay", FALSE, FALSE);
1917 /* TEST_EXPAND(pattern, string, string_to_expand, raw, expected) */
1918 TEST_EXPAND("a", "a", "", FALSE, "");
1919 TEST_EXPAND("a", "a", "\\0", FALSE, "a");
1920 TEST_EXPAND("a", "a", "\\1", FALSE, "");
1921 TEST_EXPAND("(a)", "ab", "\\1", FALSE, "a");
1922 TEST_EXPAND("(a)", "a", "\\1", FALSE, "a");
1923 TEST_EXPAND("(a)", "a", "\\g<1>", FALSE, "a");
1924 TEST_EXPAND("a", "a", "\\0130", FALSE, "X");
1925 TEST_EXPAND("a", "a", "\\\\\\0", FALSE, "\\a");
1926 TEST_EXPAND("a(?P<G>.)c", "xabcy", "X\\g<G>X", FALSE, "XbX");
1927 TEST_EXPAND("(.)(?P<1>.)", "ab", "\\1", FALSE, "a");
1928 TEST_EXPAND("(.)(?P<1>.)", "ab", "\\g<1>", FALSE, "a");
1929 TEST_EXPAND(".", EURO, "\\0", FALSE, EURO);
1930 TEST_EXPAND("(.)", EURO, "\\1", FALSE, EURO);
1931 TEST_EXPAND("(?P<G>.)", EURO, "\\g<G>", FALSE, EURO);
1932 TEST_EXPAND(".", "a", EURO, FALSE, EURO);
1933 TEST_EXPAND(".", "a", EURO "\\0", FALSE, EURO "a");
1934 TEST_EXPAND(".", "", "\\Lab\\Ec", FALSE, "abc");
1935 TEST_EXPAND(".", "", "\\LaB\\EC", FALSE, "abC");
1936 TEST_EXPAND(".", "", "\\Uab\\Ec", FALSE, "ABc");
1937 TEST_EXPAND(".", "", "a\\ubc", FALSE, "aBc");
1938 TEST_EXPAND(".", "", "a\\lbc", FALSE, "abc");
1939 TEST_EXPAND(".", "", "A\\uBC", FALSE, "ABC");
1940 TEST_EXPAND(".", "", "A\\lBC", FALSE, "AbC");
1941 TEST_EXPAND(".", "", "A\\l\\\\BC", FALSE, "A\\BC");
1942 TEST_EXPAND(".", "", "\\L" AGRAVE "\\E", FALSE, AGRAVE);
1943 TEST_EXPAND(".", "", "\\U" AGRAVE "\\E", FALSE, AGRAVE_UPPER);
1944 TEST_EXPAND(".", "", "\\u" AGRAVE "a", FALSE, AGRAVE_UPPER "a");
1945 TEST_EXPAND(".", "ab", "x\\U\\0y\\Ez", FALSE, "xAYz");
1946 TEST_EXPAND(".(.)", "AB", "x\\L\\1y\\Ez", FALSE, "xbyz");
1947 TEST_EXPAND(".", "ab", "x\\u\\0y\\Ez", FALSE, "xAyz");
1948 TEST_EXPAND(".(.)", "AB", "x\\l\\1y\\Ez", FALSE, "xbyz");
1949 TEST_EXPAND(".(.)", "a" AGRAVE_UPPER, "x\\l\\1y", FALSE, "x" AGRAVE "y");
1950 TEST_EXPAND("a", "bab", "\\x{61}", FALSE, "a");
1951 TEST_EXPAND("a", "bab", "\\x61", FALSE, "a");
1952 TEST_EXPAND("a", "bab", "\\x5a", FALSE, "Z");
1953 TEST_EXPAND("a", "bab", "\\0\\x5A", FALSE, "aZ");
1954 TEST_EXPAND("a", "bab", "\\1\\x{5A}", FALSE, "Z");
1955 TEST_EXPAND("a", "bab", "\\x{00E0}", FALSE, AGRAVE);
1956 TEST_EXPAND("", "bab", "\\x{0634}", FALSE, SHEEN);
1957 TEST_EXPAND("", "bab", "\\x{634}", FALSE, SHEEN);
1958 TEST_EXPAND("", "", "\\t", FALSE, "\t");
1959 TEST_EXPAND("", "", "\\v", FALSE, "\v");
1960 TEST_EXPAND("", "", "\\r", FALSE, "\r");
1961 TEST_EXPAND("", "", "\\n", FALSE, "\n");
1962 TEST_EXPAND("", "", "\\f", FALSE, "\f");
1963 TEST_EXPAND("", "", "\\a", FALSE, "\a");
1964 TEST_EXPAND("", "", "\\b", FALSE, "\b");
1965 TEST_EXPAND("a(.)", "abc", "\\0\\b\\1", FALSE, "ab\bb");
1966 TEST_EXPAND("a(.)", "abc", "\\0141", FALSE, "a");
1967 TEST_EXPAND("a(.)", "abc", "\\078", FALSE, "\a8");
1968 TEST_EXPAND("a(.)", "abc", "\\077", FALSE, "?");
1969 TEST_EXPAND("a(.)", "abc", "\\0778", FALSE, "?8");
1970 TEST_EXPAND("a(.)", "a" AGRAVE "b", "\\1", FALSE, AGRAVE);
1971 TEST_EXPAND("a(.)", "a" AGRAVE "b", "\\1", TRUE, "\xc3");
1972 TEST_EXPAND("a(.)", "a" AGRAVE "b", "\\0", TRUE, "a\xc3");
1973 /* Invalid strings. */
1974 TEST_EXPAND("", "", "\\Q", FALSE, NULL);
1975 TEST_EXPAND("", "", "x\\Ay", FALSE, NULL);
1976 TEST_EXPAND("", "", "\\g<", FALSE, NULL);
1977 TEST_EXPAND("", "", "\\g<>", FALSE, NULL);
1978 TEST_EXPAND("", "", "\\g<1a>", FALSE, NULL);
1979 TEST_EXPAND("", "", "\\g<a$>", FALSE, NULL);
1980 TEST_EXPAND("", "", "\\", FALSE, NULL);
1981 TEST_EXPAND("a", "a", "\\x{61", FALSE, NULL);
1982 TEST_EXPAND("a", "a", "\\x6X", FALSE, NULL);
1984 TEST_EXPAND(NULL, NULL, "", FALSE, "");
1985 TEST_EXPAND(NULL, NULL, "\\n", FALSE, "\n");
1986 /* Invalid strings */
1987 TEST_EXPAND(NULL, NULL, "\\Q", FALSE, NULL);
1988 TEST_EXPAND(NULL, NULL, "x\\Ay", FALSE, NULL);
1990 /* TEST_REPLACE(pattern, string, start_position, replacement, expected) */
1991 TEST_REPLACE("a", "ababa", 0, "A", "AbAbA");
1992 TEST_REPLACE("a", "ababa", 1, "A", "abAbA");
1993 TEST_REPLACE("a", "ababa", 2, "A", "abAbA");
1994 TEST_REPLACE("a", "ababa", 3, "A", "ababA");
1995 TEST_REPLACE("a", "ababa", 4, "A", "ababA");
1996 TEST_REPLACE("a", "ababa", 5, "A", "ababa");
1997 TEST_REPLACE("a", "ababa", 6, "A", "ababa");
1998 TEST_REPLACE("a", "abababa", 2, "A", "abAbAbA");
1999 TEST_REPLACE("a", "abab", 0, "A", "AbAb");
2000 TEST_REPLACE("a", "baba", 0, "A", "bAbA");
2001 TEST_REPLACE("a", "bab", 0, "A", "bAb");
2002 TEST_REPLACE("$^", "abc", 0, "X", "abc");
2003 TEST_REPLACE("(.)a", "ciao", 0, "a\\1", "caio");
2004 TEST_REPLACE("a.", "abc", 0, "\\0\\0", "ababc");
2005 TEST_REPLACE("a", "asd", 0, "\\0101", "Asd");
2006 TEST_REPLACE("(a).\\1", "aba cda", 0, "\\1\\n", "a\n cda");
2007 TEST_REPLACE("a" AGRAVE "a", "a" AGRAVE "a", 0, "x", "x");
2008 TEST_REPLACE("a" AGRAVE "a", "a" AGRAVE "a", 0, OGRAVE, OGRAVE);
2009 TEST_REPLACE("[^-]", "-" EURO "-x-" HSTROKE, 0, "a", "-a-a-a");
2010 TEST_REPLACE("[^-]", "-" EURO "-" HSTROKE, 0, "a\\g<0>a", "-a" EURO "a-a" HSTROKE "a");
2011 TEST_REPLACE("-", "-" EURO "-" HSTROKE, 0, "", EURO HSTROKE);
2012 TEST_REPLACE(".*", "hello", 0, "\\U\\0\\E", "HELLO");
2013 TEST_REPLACE(".*", "hello", 0, "\\u\\0", "Hello");
2014 TEST_REPLACE("\\S+", "hello world", 0, "\\U-\\0-", "-HELLO- -WORLD-");
2015 TEST_REPLACE(".", "a", 0, "\\A", NULL);
2016 TEST_REPLACE(".", "a", 0, "\\g", NULL);
2018 /* TEST_REPLACE_LIT(pattern, string, start_position, replacement, expected) */
2019 TEST_REPLACE_LIT("a", "ababa", 0, "A", "AbAbA");
2020 TEST_REPLACE_LIT("a", "ababa", 1, "A", "abAbA");
2021 TEST_REPLACE_LIT("a", "ababa", 2, "A", "abAbA");
2022 TEST_REPLACE_LIT("a", "ababa", 3, "A", "ababA");
2023 TEST_REPLACE_LIT("a", "ababa", 4, "A", "ababA");
2024 TEST_REPLACE_LIT("a", "ababa", 5, "A", "ababa");
2025 TEST_REPLACE_LIT("a", "ababa", 6, "A", "ababa");
2026 TEST_REPLACE_LIT("a", "abababa", 2, "A", "abAbAbA");
2027 TEST_REPLACE_LIT("a", "abcadaa", 0, "A", "AbcAdAA");
2028 TEST_REPLACE_LIT("$^", "abc", 0, "X", "abc");
2029 TEST_REPLACE_LIT("(.)a", "ciao", 0, "a\\1", "ca\\1o");
2030 TEST_REPLACE_LIT("a.", "abc", 0, "\\0\\0\\n", "\\0\\0\\nc");
2031 TEST_REPLACE_LIT("a" AGRAVE "a", "a" AGRAVE "a", 0, "x", "x");
2032 TEST_REPLACE_LIT("a" AGRAVE "a", "a" AGRAVE "a", 0, OGRAVE, OGRAVE);
2033 TEST_REPLACE_LIT(AGRAVE, "-" AGRAVE "-" HSTROKE, 0, "a" ENG "a", "-a" ENG "a-" HSTROKE);
2034 TEST_REPLACE_LIT("[^-]", "-" EURO "-" AGRAVE "-" HSTROKE, 0, "a", "-a-a-a");
2035 TEST_REPLACE_LIT("[^-]", "-" EURO "-" AGRAVE, 0, "a\\g<0>a", "-a\\g<0>a-a\\g<0>a");
2036 TEST_REPLACE_LIT("-", "-" EURO "-" AGRAVE "-" HSTROKE, 0, "", EURO AGRAVE HSTROKE);
2038 /* TEST_GET_STRING_NUMBER(pattern, name, expected_num) */
2039 TEST_GET_STRING_NUMBER("", "A", -1);
2040 TEST_GET_STRING_NUMBER("(?P<A>.)", "A", 1);
2041 TEST_GET_STRING_NUMBER("(?P<A>.)", "B", -1);
2042 TEST_GET_STRING_NUMBER("(?P<A>.)(?P<B>a)", "A", 1);
2043 TEST_GET_STRING_NUMBER("(?P<A>.)(?P<B>a)", "B", 2);
2044 TEST_GET_STRING_NUMBER("(?P<A>.)(?P<B>a)", "C", -1);
2045 TEST_GET_STRING_NUMBER("(?P<A>.)(.)(?P<B>a)", "A", 1);
2046 TEST_GET_STRING_NUMBER("(?P<A>.)(.)(?P<B>a)", "B", 3);
2047 TEST_GET_STRING_NUMBER("(?P<A>.)(.)(?P<B>a)", "C", -1);
2048 TEST_GET_STRING_NUMBER("(?:a)(?P<A>.)", "A", 1);
2049 TEST_GET_STRING_NUMBER("(?:a)(?P<A>.)", "B", -1);
2051 /* TEST_ESCAPE(string, length, expected) */
2052 TEST_ESCAPE("hello world", -1, "hello world");
2053 TEST_ESCAPE("hello world", 5, "hello");
2054 TEST_ESCAPE("hello.world", -1, "hello\\.world");
2055 TEST_ESCAPE("a(b\\b.$", -1, "a\\(b\\\\b\\.\\$");
2056 TEST_ESCAPE("hello\0world", -1, "hello");
2057 TEST_ESCAPE("hello\0world", 11, "hello\\0world");
2058 TEST_ESCAPE(EURO "*" ENG, -1, EURO "\\*" ENG);
2059 TEST_ESCAPE("a$", -1, "a\\$");
2060 TEST_ESCAPE("$a", -1, "\\$a");
2061 TEST_ESCAPE("a$a", -1, "a\\$a");
2062 TEST_ESCAPE("$a$", -1, "\\$a\\$");
2063 TEST_ESCAPE("$a$", 0, "");
2064 TEST_ESCAPE("$a$", 1, "\\$");
2065 TEST_ESCAPE("$a$", 2, "\\$a");
2066 TEST_ESCAPE("$a$", 3, "\\$a\\$");
2067 TEST_ESCAPE("$a$", 4, "\\$a\\$\\0");
2068 TEST_ESCAPE("|()[]{}^$*+?.", -1, "\\|\\(\\)\\[\\]\\{\\}\\^\\$\\*\\+\\?\\.");
2069 TEST_ESCAPE("a|a(a)a[a]a{a}a^a$a*a+a?a.a", -1,
2070 "a\\|a\\(a\\)a\\[a\\]a\\{a\\}a\\^a\\$a\\*a\\+a\\?a\\.a");
2072 /* TEST_MATCH_ALL#(pattern, string, string_len, start_position, ...) */
2073 TEST_MATCH_ALL0("<.*>", "", -1, 0);
2074 TEST_MATCH_ALL0("a+", "", -1, 0);
2075 TEST_MATCH_ALL0("a+", "a", 0, 0);
2076 TEST_MATCH_ALL0("a+", "a", -1, 1);
2077 TEST_MATCH_ALL1("<.*>", "<a>", -1, 0, "<a>", 0, 3);
2078 TEST_MATCH_ALL1("a+", "a", -1, 0, "a", 0, 1);
2079 TEST_MATCH_ALL1("a+", "aa", 1, 0, "a", 0, 1);
2080 TEST_MATCH_ALL1("a+", "aa", -1, 1, "a", 1, 2);
2081 TEST_MATCH_ALL1("a+", "aa", 2, 1, "a", 1, 2);
2082 TEST_MATCH_ALL1(".+", ENG, -1, 0, ENG, 0, 2);
2083 TEST_MATCH_ALL2("<.*>", "<a><b>", -1, 0, "<a><b>", 0, 6, "<a>", 0, 3);
2084 TEST_MATCH_ALL2("a+", "aa", -1, 0, "aa", 0, 2, "a", 0, 1);
2085 TEST_MATCH_ALL2(".+", ENG EURO, -1, 0, ENG EURO, 0, 5, ENG, 0, 2);
2086 TEST_MATCH_ALL3("<.*>", "<a><b><c>", -1, 0, "<a><b><c>", 0, 9,
2087 "<a><b>", 0, 6, "<a>", 0, 3);
2088 TEST_MATCH_ALL3("a+", "aaa", -1, 0, "aaa", 0, 3, "aa", 0, 2, "a", 0, 1);
2090 end: /* if abort_on_fail is TRUE the flow passes to this label. */
2091 verbose ("\n%u tests passed, %u failed\n", passed, failed);
2095 #else /* ENABLE_REGEX false */
2098 main (int argc, char *argv[])
2100 g_print ("GRegex is disabled.\n");
2104 #endif /* ENABLE_REGEX */