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
,
136 GRegexError expected_error
)
139 GError
*error
= NULL
;
141 verbose ("compiling \"%s\" (expected a failure) \t", pattern
);
143 regex
= g_regex_new (pattern
, compile_opts
, 0, &error
);
147 g_print ("failed \t(pattern: \"%s\", compile: %d)\n",
148 pattern
, compile_opts
);
149 g_regex_unref (regex
);
153 if (error
->code
!= expected_error
)
155 g_print ("failed \t(pattern: \"%s\", compile: %d, got error: %d, "
156 "expected error: %d)\n",
157 pattern
, compile_opts
, error
->code
, expected_error
);
158 g_error_free (error
);
162 verbose ("passed\n");
166 #define TEST_NEW_FAIL(pattern, compile_opts, expected_error) { \
168 if (test_new_fail (pattern, compile_opts, expected_error)) \
175 test_match_simple (const gchar
*pattern
,
177 GRegexCompileFlags compile_opts
,
178 GRegexMatchFlags match_opts
,
183 verbose ("matching \"%s\" against \"%s\" \t", string
, pattern
);
185 match
= g_regex_match_simple (pattern
, string
, compile_opts
, match_opts
);
186 if (match
!= expected
)
188 g_print ("failed \t(unexpected %s)\n", match
? "match" : "mismatch");
193 verbose ("passed (%s)\n", match
? "match" : "nomatch");
198 #define TEST_MATCH_SIMPLE(pattern, string, compile_opts, match_opts, expected) { \
200 if (test_match_simple (pattern, string, compile_opts, match_opts, expected)) \
207 test_match (const gchar
*pattern
,
208 GRegexCompileFlags compile_opts
,
209 GRegexMatchFlags match_opts
,
213 GRegexMatchFlags match_opts2
,
219 verbose ("matching \"%s\" against \"%s\" (start: %d, len: %d) \t",
220 string
, pattern
, start_position
, string_len
);
222 regex
= g_regex_new (pattern
, compile_opts
, match_opts
, NULL
);
223 match
= g_regex_match_full (regex
, string
, string_len
,
224 start_position
, match_opts2
, NULL
, NULL
);
225 if (match
!= expected
)
227 gchar
*e1
= g_strescape (pattern
, NULL
);
228 gchar
*e2
= g_strescape (string
, NULL
);
229 g_print ("failed \t(unexpected %s) '%s' against '%s'\n", match
? "match" : "mismatch", e1
, e2
);
232 g_regex_unref (regex
);
236 if (string_len
== -1 && start_position
== 0)
238 match
= g_regex_match (regex
, string
, match_opts2
, NULL
);
239 if (match
!= expected
)
241 g_print ("failed \t(pattern: \"%s\", string: \"%s\")\n",
243 g_regex_unref (regex
);
248 g_regex_unref (regex
);
250 verbose ("passed (%s)\n", match
? "match" : "nomatch");
254 #define TEST_MATCH(pattern, compile_opts, match_opts, string, \
255 string_len, start_position, match_opts2, expected) { \
257 if (test_match (pattern, compile_opts, match_opts, string, \
258 string_len, start_position, match_opts2, expected)) \
269 typedef struct _Match Match
;
272 free_match (gpointer data
, gpointer user_data
)
277 g_free (match
->string
);
282 test_match_next (const gchar
*pattern
,
289 GMatchInfo
*match_info
;
291 GSList
*matches
= NULL
;
292 GSList
*expected
= NULL
;
293 GSList
*l_exp
, *l_match
;
296 verbose ("matching \"%s\" against \"%s\" (start: %d, len: %d) \t",
297 string
, pattern
, start_position
, string_len
);
299 /* The va_list is a NULL-terminated sequence of: extected matched string,
300 * expected start and expected end. */
301 va_start (args
, start_position
);
305 const gchar
*expected_string
= va_arg (args
, const gchar
*);
306 if (expected_string
== NULL
)
308 match
= g_new0 (Match
, 1);
309 match
->string
= g_strdup (expected_string
);
310 match
->start
= va_arg (args
, gint
);
311 match
->end
= va_arg (args
, gint
);
312 expected
= g_slist_prepend (expected
, match
);
314 expected
= g_slist_reverse (expected
);
317 regex
= g_regex_new (pattern
, 0, 0, NULL
);
319 g_regex_match_full (regex
, string
, string_len
,
320 start_position
, 0, &match_info
, NULL
);
321 while (g_match_info_matches (match_info
))
323 Match
*match
= g_new0 (Match
, 1);
324 match
->string
= g_match_info_fetch (match_info
, 0);
325 match
->start
= UNTOUCHED
;
326 match
->end
= UNTOUCHED
;
327 g_match_info_fetch_pos (match_info
, 0, &match
->start
, &match
->end
);
328 matches
= g_slist_prepend (matches
, match
);
329 g_match_info_next (match_info
, NULL
);
331 g_assert (regex
== g_match_info_get_regex (match_info
));
332 g_assert (string
== g_match_info_get_string (match_info
));
333 g_match_info_free (match_info
);
334 matches
= g_slist_reverse (matches
);
336 if (g_slist_length (matches
) != g_slist_length (expected
))
338 gint match_count
= g_slist_length (matches
);
339 g_print ("failed \t(got %d %s, expected %d)\n", match_count
,
340 match_count
== 1 ? "match" : "matches",
341 g_slist_length (expected
));
348 while (l_exp
!= NULL
)
350 Match
*exp
= l_exp
->data
;
351 Match
*match
= l_match
->data
;
353 if (!streq(exp
->string
, match
->string
))
355 g_print ("failed \t(got \"%s\", expected \"%s\")\n",
356 match
->string
, exp
->string
);
361 if (exp
->start
!= match
->start
|| exp
->end
!= match
->end
)
363 g_print ("failed \t(got [%d, %d], expected [%d, %d])\n",
364 match
->start
, match
->end
, exp
->start
, exp
->end
);
369 l_exp
= g_slist_next (l_exp
);
370 l_match
= g_slist_next (l_match
);
376 gint count
= g_slist_length (matches
);
377 verbose ("passed (%d %s)\n", count
, count
== 1 ? "match" : "matches");
380 g_regex_unref (regex
);
381 g_slist_foreach (expected
, free_match
, NULL
);
382 g_slist_free (expected
);
383 g_slist_foreach (matches
, free_match
, NULL
);
384 g_slist_free (matches
);
389 #define TEST_MATCH_NEXT0(pattern, string, string_len, start_position) { \
391 if (test_match_next (pattern, string, string_len, start_position, NULL)) \
397 #define TEST_MATCH_NEXT1(pattern, string, string_len, start_position, \
400 if (test_match_next (pattern, string, string_len, start_position, \
407 #define TEST_MATCH_NEXT2(pattern, string, string_len, start_position, \
408 t1, s1, e1, t2, s2, e2) { \
410 if (test_match_next (pattern, string, string_len, start_position, \
411 t1, s1, e1, t2, s2, e2, NULL)) \
417 #define TEST_MATCH_NEXT3(pattern, string, string_len, start_position, \
418 t1, s1, e1, t2, s2, e2, t3, s3, e3) { \
420 if (test_match_next (pattern, string, string_len, start_position, \
421 t1, s1, e1, t2, s2, e2, t3, s3, e3, NULL)) \
427 #define TEST_MATCH_NEXT4(pattern, string, string_len, start_position, \
428 t1, s1, e1, t2, s2, e2, t3, s3, e3, t4, s4, e4) { \
430 if (test_match_next (pattern, string, string_len, start_position, \
431 t1, s1, e1, t2, s2, e2, t3, s3, e3, t4, s4, e4, NULL)) \
438 test_match_count (const gchar
*pattern
,
441 GRegexMatchFlags match_opts
,
445 GMatchInfo
*match_info
;
448 verbose ("fetching match count (string: \"%s\", pattern: \"%s\", start: %d) \t",
449 string
, pattern
, start_position
);
451 regex
= g_regex_new (pattern
, 0, 0, NULL
);
453 g_regex_match_full (regex
, string
, -1, start_position
,
454 match_opts
, &match_info
, NULL
);
455 count
= g_match_info_get_match_count (match_info
);
457 if (count
!= expected_count
)
459 g_print ("failed \t(got %d, expected: %d)\n", count
, expected_count
);
463 g_match_info_free (match_info
);
464 g_regex_unref (regex
);
466 verbose ("passed\n");
470 #define TEST_MATCH_COUNT(pattern, string, start_position, match_opts, expected_count) { \
472 if (test_match_count (pattern, string, start_position, match_opts, expected_count)) \
479 test_partial (const gchar
*pattern
,
484 GMatchInfo
*match_info
;
486 verbose ("partial matching (string: \"%s\", pattern: \"%s\") \t",
489 regex
= g_regex_new (pattern
, 0, 0, NULL
);
491 g_regex_match (regex
, string
, G_REGEX_MATCH_PARTIAL
, &match_info
);
492 if (expected
!= g_match_info_is_partial_match (match_info
))
494 g_print ("failed \t(got %d, expected: %d)\n", !expected
, expected
);
495 g_regex_unref (regex
);
499 if (expected
&& g_match_info_fetch_pos (match_info
, 0, NULL
, NULL
))
501 g_print ("failed \t(got sub-pattern 0)\n");
502 g_regex_unref (regex
);
506 if (expected
&& g_match_info_fetch_pos (match_info
, 1, NULL
, NULL
))
508 g_print ("failed \t(got sub-pattern 1)\n");
509 g_regex_unref (regex
);
513 g_match_info_free (match_info
);
514 g_regex_unref (regex
);
516 verbose ("passed\n");
520 #define TEST_PARTIAL(pattern, string, expected) { \
522 if (test_partial (pattern, string, expected)) \
529 test_sub_pattern (const gchar
*pattern
,
533 const gchar
*expected_sub
,
538 GMatchInfo
*match_info
;
540 gint start
= UNTOUCHED
, end
= UNTOUCHED
;
542 verbose ("fetching sub-pattern %d from \"%s\" (pattern: \"%s\") \t",
543 sub_n
, string
, pattern
);
545 regex
= g_regex_new (pattern
, 0, 0, NULL
);
546 g_regex_match_full (regex
, string
, -1, start_position
, 0, &match_info
, NULL
);
548 sub_expr
= g_match_info_fetch (match_info
, sub_n
);
549 if (!streq(sub_expr
, expected_sub
))
551 g_print ("failed \t(got \"%s\", expected \"%s\")\n",
552 sub_expr
, expected_sub
);
554 g_regex_unref (regex
);
559 g_match_info_fetch_pos (match_info
, sub_n
, &start
, &end
);
560 if (start
!= expected_start
|| end
!= expected_end
)
562 g_print ("failed \t(got [%d, %d], expected [%d, %d])\n",
563 start
, end
, expected_start
, expected_end
);
564 g_regex_unref (regex
);
568 g_match_info_free (match_info
);
569 g_regex_unref (regex
);
571 verbose ("passed\n");
575 #define TEST_SUB_PATTERN(pattern, string, start_position, sub_n, expected_sub, \
576 expected_start, expected_end) { \
578 if (test_sub_pattern (pattern, string, start_position, sub_n, expected_sub, \
579 expected_start, expected_end)) \
586 test_named_sub_pattern (const gchar
*pattern
,
587 GRegexCompileFlags flags
,
590 const gchar
*sub_name
,
591 const gchar
*expected_sub
,
596 GMatchInfo
*match_info
;
597 gint start
= UNTOUCHED
, end
= UNTOUCHED
;
600 verbose ("fetching sub-pattern \"%s\" from \"%s\" (pattern: \"%s\") \t",
601 sub_name
, string
, pattern
);
603 regex
= g_regex_new (pattern
, flags
, 0, NULL
);
605 g_regex_match_full (regex
, string
, -1, start_position
, 0, &match_info
, NULL
);
606 sub_expr
= g_match_info_fetch_named (match_info
, sub_name
);
607 if (!streq (sub_expr
, expected_sub
))
609 g_print ("failed \t(got \"%s\", expected \"%s\")\n",
610 sub_expr
, expected_sub
);
612 g_regex_unref (regex
);
617 g_match_info_fetch_named_pos (match_info
, sub_name
, &start
, &end
);
618 if (start
!= expected_start
|| end
!= expected_end
)
620 g_print ("failed \t(got [%d, %d], expected [%d, %d])\n",
621 start
, end
, expected_start
, expected_end
);
622 g_regex_unref (regex
);
626 g_match_info_free (match_info
);
627 g_regex_unref (regex
);
629 verbose ("passed\n");
633 #define TEST_NAMED_SUB_PATTERN(pattern, string, start_position, sub_name, \
634 expected_sub, expected_start, expected_end) { \
636 if (test_named_sub_pattern (pattern, 0, string, start_position, sub_name, \
637 expected_sub, expected_start, expected_end)) \
643 #define TEST_NAMED_SUB_PATTERN_DUPNAMES(pattern, string, start_position, sub_name, \
644 expected_sub, expected_start, expected_end) { \
646 if (test_named_sub_pattern (pattern, G_REGEX_DUPNAMES, string, start_position, \
647 sub_name, expected_sub, expected_start, expected_end)) \
654 test_fetch_all (const gchar
*pattern
,
659 GMatchInfo
*match_info
;
661 GSList
*expected
= NULL
;
668 verbose ("fetching all sub-patterns from \"%s\" (pattern: \"%s\") \t",
671 /* The va_list is a NULL-terminated sequence of extected strings. */
672 va_start (args
, string
);
675 gchar
*expected_string
= va_arg (args
, gchar
*);
676 if (expected_string
== NULL
)
679 expected
= g_slist_prepend (expected
, g_strdup (expected_string
));
681 expected
= g_slist_reverse (expected
);
684 regex
= g_regex_new (pattern
, 0, 0, NULL
);
685 g_regex_match (regex
, string
, 0, &match_info
);
686 matches
= g_match_info_fetch_all (match_info
);
688 match_count
= g_strv_length (matches
);
692 if (match_count
!= g_slist_length (expected
))
694 g_print ("failed \t(got %d %s, expected %d)\n", match_count
,
695 match_count
== 1 ? "match" : "matches",
696 g_slist_length (expected
));
702 for (i
= 0; l_exp
!= NULL
; i
++, l_exp
= g_slist_next (l_exp
))
704 if (!streq(l_exp
->data
, matches
[i
]))
706 g_print ("failed \t(got \"%s\", expected \"%s\")\n",
707 matches
[i
], (gchar
*)l_exp
->data
);
713 verbose ("passed (%d %s)\n", match_count
,
714 match_count
== 1 ? "match" : "matches");
717 g_match_info_free (match_info
);
718 g_regex_unref (regex
);
719 g_slist_foreach (expected
, (GFunc
)g_free
, NULL
);
720 g_slist_free (expected
);
721 g_strfreev (matches
);
726 #define TEST_FETCH_ALL0(pattern, string) { \
728 if (test_fetch_all (pattern, string, NULL)) \
734 #define TEST_FETCH_ALL1(pattern, string, e1) { \
736 if (test_fetch_all (pattern, string, e1, NULL)) \
742 #define TEST_FETCH_ALL2(pattern, string, e1, e2) { \
744 if (test_fetch_all (pattern, string, e1, e2, NULL)) \
750 #define TEST_FETCH_ALL3(pattern, string, e1, e2, e3) { \
752 if (test_fetch_all (pattern, string, e1, e2, e3, NULL)) \
759 test_split_simple (const gchar
*pattern
,
764 GSList
*expected
= NULL
;
771 verbose ("splitting \"%s\" against \"%s\" \t", string
, pattern
);
773 /* The va_list is a NULL-terminated sequence of extected strings. */
774 va_start (args
, string
);
777 gchar
*expected_string
= va_arg (args
, gchar
*);
778 if (expected_string
== NULL
)
781 expected
= g_slist_prepend (expected
, g_strdup (expected_string
));
783 expected
= g_slist_reverse (expected
);
786 tokens
= g_regex_split_simple (pattern
, string
, 0, 0);
788 token_count
= g_strv_length (tokens
);
792 if (token_count
!= g_slist_length (expected
))
794 g_print ("failed \t(got %d %s, expected %d)\n", token_count
,
795 token_count
== 1 ? "match" : "matches",
796 g_slist_length (expected
));
802 for (i
= 0; l_exp
!= NULL
; i
++, l_exp
= g_slist_next (l_exp
))
804 if (!streq(l_exp
->data
, tokens
[i
]))
806 g_print ("failed \t(got \"%s\", expected \"%s\")\n",
807 tokens
[i
], (gchar
*)l_exp
->data
);
813 verbose ("passed (%d %s)\n", token_count
,
814 token_count
== 1 ? "token" : "tokens");
817 g_slist_foreach (expected
, (GFunc
)g_free
, NULL
);
818 g_slist_free (expected
);
824 #define TEST_SPLIT_SIMPLE0(pattern, string) { \
826 if (test_split_simple (pattern, string, NULL)) \
832 #define TEST_SPLIT_SIMPLE1(pattern, string, e1) { \
834 if (test_split_simple (pattern, string, e1, NULL)) \
840 #define TEST_SPLIT_SIMPLE2(pattern, string, e1, e2) { \
842 if (test_split_simple (pattern, string, e1, e2, NULL)) \
848 #define TEST_SPLIT_SIMPLE3(pattern, string, e1, e2, e3) { \
850 if (test_split_simple (pattern, string, e1, e2, e3, NULL)) \
857 test_split_full (const gchar
*pattern
,
865 GSList
*expected
= NULL
;
872 verbose ("splitting \"%s\" against \"%s\" (start: %d, max: %d) \t",
873 string
, pattern
, start_position
, max_tokens
);
875 /* The va_list is a NULL-terminated sequence of extected strings. */
876 va_start (args
, max_tokens
);
879 gchar
*expected_string
= va_arg (args
, gchar
*);
880 if (expected_string
== NULL
)
883 expected
= g_slist_prepend (expected
, g_strdup (expected_string
));
885 expected
= g_slist_reverse (expected
);
888 regex
= g_regex_new (pattern
, 0, 0, NULL
);
889 tokens
= g_regex_split_full (regex
, string
, -1, start_position
,
890 0, max_tokens
, NULL
);
892 token_count
= g_strv_length (tokens
);
896 if (token_count
!= g_slist_length (expected
))
898 g_print ("failed \t(got %d %s, expected %d)\n", token_count
,
899 token_count
== 1 ? "match" : "matches",
900 g_slist_length (expected
));
906 for (i
= 0; l_exp
!= NULL
; i
++, l_exp
= g_slist_next (l_exp
))
908 if (!streq(l_exp
->data
, tokens
[i
]))
910 g_print ("failed \t(got \"%s\", expected \"%s\")\n",
911 tokens
[i
], (gchar
*)l_exp
->data
);
917 verbose ("passed (%d %s)\n", token_count
,
918 token_count
== 1 ? "token" : "tokens");
921 g_regex_unref (regex
);
922 g_slist_foreach (expected
, (GFunc
)g_free
, NULL
);
923 g_slist_free (expected
);
930 test_split (const gchar
*pattern
,
936 GSList
*expected
= NULL
;
943 verbose ("splitting \"%s\" against \"%s\" \t", string
, pattern
);
945 /* The va_list is a NULL-terminated sequence of extected strings. */
946 va_start (args
, string
);
949 gchar
*expected_string
= va_arg (args
, gchar
*);
950 if (expected_string
== NULL
)
953 expected
= g_slist_prepend (expected
, g_strdup (expected_string
));
955 expected
= g_slist_reverse (expected
);
958 regex
= g_regex_new (pattern
, 0, 0, NULL
);
959 tokens
= g_regex_split (regex
, string
, 0);
961 token_count
= g_strv_length (tokens
);
965 if (token_count
!= g_slist_length (expected
))
967 g_print ("failed \t(got %d %s, expected %d)\n", token_count
,
968 token_count
== 1 ? "match" : "matches",
969 g_slist_length (expected
));
975 for (i
= 0; l_exp
!= NULL
; i
++, l_exp
= g_slist_next (l_exp
))
977 if (!streq(l_exp
->data
, tokens
[i
]))
979 g_print ("failed \t(got \"%s\", expected \"%s\")\n",
980 tokens
[i
], (gchar
*)l_exp
->data
);
986 verbose ("passed (%d %s)\n", token_count
,
987 token_count
== 1 ? "token" : "tokens");
990 g_regex_unref (regex
);
991 g_slist_foreach (expected
, (GFunc
)g_free
, NULL
);
992 g_slist_free (expected
);
998 #define TEST_SPLIT0(pattern, string, start_position, max_tokens) { \
1000 if (test_split_full (pattern, string, start_position, max_tokens, NULL)) \
1004 if (start_position == 0 && max_tokens <= 0) \
1007 if (test_split (pattern, string, NULL)) \
1014 #define TEST_SPLIT1(pattern, string, start_position, max_tokens, e1) { \
1016 if (test_split_full (pattern, string, start_position, max_tokens, e1, NULL)) \
1020 if (start_position == 0 && max_tokens <= 0) \
1023 if (test_split (pattern, string, e1, NULL)) \
1030 #define TEST_SPLIT2(pattern, string, start_position, max_tokens, e1, e2) { \
1032 if (test_split_full (pattern, string, start_position, max_tokens, e1, e2, NULL)) \
1036 if (start_position == 0 && max_tokens <= 0) \
1039 if (test_split (pattern, string, e1, e2, NULL)) \
1046 #define TEST_SPLIT3(pattern, string, start_position, max_tokens, e1, e2, e3) { \
1048 if (test_split_full (pattern, string, start_position, max_tokens, e1, e2, e3, NULL)) \
1052 if (start_position == 0 && max_tokens <= 0) \
1055 if (test_split (pattern, string, e1, e2, e3, NULL)) \
1063 test_check_replacement (const gchar
*string_to_expand
,
1065 gboolean expected_refs
)
1070 verbose ("checking replacement string \"%s\" \t", string_to_expand
);
1072 result
= g_regex_check_replacement (string_to_expand
, &has_refs
, NULL
);
1073 if (expected
!= result
)
1075 g_print ("failed \t(got \"%s\", expected \"%s\")\n",
1076 result
? "TRUE" : "FALSE",
1077 expected
? "TRUE" : "FALSE");
1081 if (expected
&& expected_refs
!= has_refs
)
1083 g_print ("failed \t(got has_references \"%s\", expected \"%s\")\n",
1084 has_refs
? "TRUE" : "FALSE",
1085 expected_refs
? "TRUE" : "FALSE");
1089 verbose ("passed\n");
1093 #define TEST_CHECK_REPLACEMENT(string_to_expand, expected, expected_refs) { \
1095 if (test_check_replacement (string_to_expand, expected, expected_refs)) \
1101 test_expand (const gchar
*pattern
,
1102 const gchar
*string
,
1103 const gchar
*string_to_expand
,
1105 const gchar
*expected
)
1107 GRegex
*regex
= NULL
;
1108 GMatchInfo
*match_info
= NULL
;
1111 verbose ("expanding the references in \"%s\" (pattern: \"%s\", string: \"%s\") \t",
1113 pattern
? pattern
: "(null)",
1114 string
? string
: "(null)");
1118 regex
= g_regex_new (pattern
, raw
? G_REGEX_RAW
: 0, 0, NULL
);
1119 g_regex_match (regex
, string
, 0, &match_info
);
1122 res
= g_match_info_expand_references (match_info
, string_to_expand
, NULL
);
1123 if (!streq (res
, expected
))
1125 g_print ("failed \t(got \"%s\", expected \"%s\")\n", res
, expected
);
1127 g_match_info_free (match_info
);
1128 g_regex_unref (regex
);
1133 g_match_info_free (match_info
);
1135 g_regex_unref (regex
);
1137 verbose ("passed\n");
1141 #define TEST_EXPAND(pattern, string, string_to_expand, raw, expected) { \
1143 if (test_expand (pattern, string, string_to_expand, raw, expected)) \
1150 test_replace (const gchar
*pattern
,
1151 const gchar
*string
,
1152 gint start_position
,
1153 const gchar
*replacement
,
1154 const gchar
*expected
)
1159 verbose ("replacing \"%s\" in \"%s\" (pattern: \"%s\", start: %d) \t",
1160 replacement
, string
, pattern
, start_position
);
1162 regex
= g_regex_new (pattern
, 0, 0, NULL
);
1163 res
= g_regex_replace (regex
, string
, -1, start_position
, replacement
, 0, NULL
);
1164 if (!streq (res
, expected
))
1166 g_print ("failed \t(got \"%s\", expected \"%s\")\n", res
, expected
);
1168 g_regex_unref (regex
);
1173 g_regex_unref (regex
);
1175 verbose ("passed\n");
1179 #define TEST_REPLACE(pattern, string, start_position, replacement, expected) { \
1181 if (test_replace (pattern, string, start_position, replacement, expected)) \
1188 test_replace_lit (const gchar
*pattern
,
1189 const gchar
*string
,
1190 gint start_position
,
1191 const gchar
*replacement
,
1192 const gchar
*expected
)
1197 verbose ("replacing literally \"%s\" in \"%s\" (pattern: \"%s\", start: %d) \t",
1198 replacement
, string
, pattern
, start_position
);
1200 regex
= g_regex_new (pattern
, 0, 0, NULL
);
1201 res
= g_regex_replace_literal (regex
, string
, -1, start_position
,
1202 replacement
, 0, NULL
);
1203 if (!streq (res
, expected
))
1205 g_print ("failed \t(got \"%s\", expected \"%s\")\n", res
, expected
);
1207 g_regex_unref (regex
);
1212 g_regex_unref (regex
);
1214 verbose ("passed\n");
1218 #define TEST_REPLACE_LIT(pattern, string, start_position, replacement, expected) { \
1220 if (test_replace_lit (pattern, string, start_position, replacement, expected)) \
1227 test_get_string_number (const gchar
*pattern
,
1234 verbose ("getting the number of \"%s\" (pattern: \"%s\") \t",
1237 regex
= g_regex_new (pattern
, 0, 0, NULL
);
1238 num
= g_regex_get_string_number (regex
, name
);
1239 g_regex_unref (regex
);
1241 if (num
!= expected_num
)
1243 g_print ("failed \t(got %d, expected %d)\n", num
, expected_num
);
1248 verbose ("passed\n");
1253 #define TEST_GET_STRING_NUMBER(pattern, name, expected_num) { \
1255 if (test_get_string_number (pattern, name, expected_num)) \
1262 test_escape (const gchar
*string
,
1264 const gchar
*expected
)
1268 verbose ("escaping \"%s\" (len: %d) \t", string
, length
);
1270 escaped
= g_regex_escape_string (string
, length
);
1272 if (!streq (escaped
, expected
))
1274 g_print ("failed \t(got \"%s\", expected \"%s\")\n", escaped
, expected
);
1281 verbose ("passed\n");
1285 #define TEST_ESCAPE(string, length, expected) { \
1287 if (test_escape (string, length, expected)) \
1294 test_match_all_full (const gchar
*pattern
,
1295 const gchar
*string
,
1297 gint start_position
,
1301 GMatchInfo
*match_info
;
1303 GSList
*expected
= NULL
;
1306 gboolean ret
= TRUE
;
1310 verbose ("matching all in \"%s\" against \"%s\" (start: %d, len: %d) \t",
1311 string
, pattern
, start_position
, string_len
);
1313 /* The va_list is a NULL-terminated sequence of: extected matched string,
1314 * expected start and expected end. */
1315 va_start (args
, start_position
);
1319 const gchar
*expected_string
= va_arg (args
, const gchar
*);
1320 if (expected_string
== NULL
)
1322 match
= g_new0 (Match
, 1);
1323 match
->string
= g_strdup (expected_string
);
1324 match
->start
= va_arg (args
, gint
);
1325 match
->end
= va_arg (args
, gint
);
1326 expected
= g_slist_prepend (expected
, match
);
1328 expected
= g_slist_reverse (expected
);
1331 regex
= g_regex_new (pattern
, 0, 0, NULL
);
1332 match_ok
= g_regex_match_all_full (regex
, string
, string_len
, start_position
,
1333 0, &match_info
, NULL
);
1335 if (match_ok
&& g_slist_length (expected
) == 0)
1337 g_print ("failed\n");
1341 if (!match_ok
&& g_slist_length (expected
) != 0)
1343 g_print ("failed\n");
1348 match_count
= g_match_info_get_match_count (match_info
);
1349 if (match_count
!= g_slist_length (expected
))
1351 g_print ("failed \t(got %d %s, expected %d)\n", match_count
,
1352 match_count
== 1 ? "match" : "matches",
1353 g_slist_length (expected
));
1359 for (i
= 0; i
< match_count
; i
++)
1362 gchar
*matched_string
;
1363 Match
*exp
= l_exp
->data
;
1365 matched_string
= g_match_info_fetch (match_info
, i
);
1366 g_match_info_fetch_pos (match_info
, i
, &start
, &end
);
1368 if (!streq(exp
->string
, matched_string
))
1370 g_print ("failed \t(got \"%s\", expected \"%s\")\n",
1371 matched_string
, exp
->string
);
1372 g_free (matched_string
);
1376 g_free (matched_string
);
1378 if (exp
->start
!= start
|| exp
->end
!= end
)
1380 g_print ("failed \t(got [%d, %d], expected [%d, %d])\n",
1381 start
, end
, exp
->start
, exp
->end
);
1386 l_exp
= g_slist_next (l_exp
);
1392 verbose ("passed (%d %s)\n", match_count
, match_count
== 1 ? "match" : "matches");
1395 g_match_info_free (match_info
);
1396 g_regex_unref (regex
);
1397 g_slist_foreach (expected
, free_match
, NULL
);
1398 g_slist_free (expected
);
1404 test_match_all (const gchar
*pattern
,
1405 const gchar
*string
,
1409 GMatchInfo
*match_info
;
1411 GSList
*expected
= NULL
;
1414 gboolean ret
= TRUE
;
1418 verbose ("matching all in \"%s\" against \"%s\" \t", string
, pattern
);
1420 /* The va_list is a NULL-terminated sequence of: extected matched string,
1421 * expected start and expected end. */
1422 va_start (args
, string
);
1426 const gchar
*expected_string
= va_arg (args
, const gchar
*);
1427 if (expected_string
== NULL
)
1429 match
= g_new0 (Match
, 1);
1430 match
->string
= g_strdup (expected_string
);
1431 match
->start
= va_arg (args
, gint
);
1432 match
->end
= va_arg (args
, gint
);
1433 expected
= g_slist_prepend (expected
, match
);
1435 expected
= g_slist_reverse (expected
);
1438 regex
= g_regex_new (pattern
, 0, 0, NULL
);
1439 match_ok
= g_regex_match_all (regex
, string
, 0, &match_info
);
1441 if (match_ok
&& g_slist_length (expected
) == 0)
1443 g_print ("failed\n");
1447 if (!match_ok
&& g_slist_length (expected
) != 0)
1449 g_print ("failed\n");
1454 match_count
= g_match_info_get_match_count (match_info
);
1455 if (match_count
!= g_slist_length (expected
))
1457 g_print ("failed \t(got %d %s, expected %d)\n", match_count
,
1458 match_count
== 1 ? "match" : "matches",
1459 g_slist_length (expected
));
1465 for (i
= 0; i
< match_count
; i
++)
1468 gchar
*matched_string
;
1469 Match
*exp
= l_exp
->data
;
1471 matched_string
= g_match_info_fetch (match_info
, i
);
1472 g_match_info_fetch_pos (match_info
, i
, &start
, &end
);
1474 if (!streq(exp
->string
, matched_string
))
1476 g_print ("failed \t(got \"%s\", expected \"%s\")\n",
1477 matched_string
, exp
->string
);
1478 g_free (matched_string
);
1482 g_free (matched_string
);
1484 if (exp
->start
!= start
|| exp
->end
!= end
)
1486 g_print ("failed \t(got [%d, %d], expected [%d, %d])\n",
1487 start
, end
, exp
->start
, exp
->end
);
1492 l_exp
= g_slist_next (l_exp
);
1498 verbose ("passed (%d %s)\n", match_count
, match_count
== 1 ? "match" : "matches");
1501 g_match_info_free (match_info
);
1502 g_regex_unref (regex
);
1503 g_slist_foreach (expected
, free_match
, NULL
);
1504 g_slist_free (expected
);
1509 #define TEST_MATCH_ALL0(pattern, string, string_len, start_position) { \
1511 if (test_match_all_full (pattern, string, string_len, start_position, NULL)) \
1515 if (string_len == -1 && start_position == 0) \
1518 if (test_match_all (pattern, string, NULL)) \
1525 #define TEST_MATCH_ALL1(pattern, string, string_len, start_position, \
1528 if (test_match_all_full (pattern, string, string_len, start_position, \
1529 t1, s1, e1, NULL)) \
1533 if (string_len == -1 && start_position == 0) \
1536 if (test_match_all (pattern, string, t1, s1, e1, NULL)) \
1543 #define TEST_MATCH_ALL2(pattern, string, string_len, start_position, \
1544 t1, s1, e1, t2, s2, e2) { \
1546 if (test_match_all_full (pattern, string, string_len, start_position, \
1547 t1, s1, e1, t2, s2, e2, NULL)) \
1551 if (string_len == -1 && start_position == 0) \
1554 if (test_match_all (pattern, string, t1, s1, e1, t2, s2, e2, NULL)) \
1561 #define TEST_MATCH_ALL3(pattern, string, string_len, start_position, \
1562 t1, s1, e1, t2, s2, e2, t3, s3, e3) { \
1564 if (test_match_all_full (pattern, string, string_len, start_position, \
1565 t1, s1, e1, t2, s2, e2, t3, s3, e3, NULL)) \
1569 if (string_len == -1 && start_position == 0) \
1572 if (test_match_all (pattern, string, t1, s1, e1, t2, s2, e2, t3, s3, e3, NULL)) \
1580 main (int argc
, char *argv
[])
1587 setlocale (LC_ALL
, "");
1589 for (i
= 1; i
< argc
; i
++)
1591 if (streq ("--noisy", argv
[i
]))
1593 else if (streq ("--abort", argv
[i
]))
1594 abort_on_fail
= TRUE
;
1597 g_setenv ("G_DEBUG", "fatal_warnings", TRUE
);
1599 /* TEST_NEW(pattern, compile_opts, match_opts) */
1601 TEST_NEW(".*", 0, 0);
1602 TEST_NEW(".*", G_REGEX_OPTIMIZE
, 0);
1603 TEST_NEW(".*", G_REGEX_MULTILINE
, 0);
1604 TEST_NEW(".*", G_REGEX_DOTALL
, 0);
1605 TEST_NEW(".*", G_REGEX_DOTALL
, G_REGEX_MATCH_NOTBOL
);
1606 TEST_NEW("(123\\d*)[a-zA-Z]+(?P<hello>.*)", 0, 0);
1607 TEST_NEW("(123\\d*)[a-zA-Z]+(?P<hello>.*)", G_REGEX_CASELESS
, 0);
1608 TEST_NEW("(123\\d*)[a-zA-Z]+(?P<hello>.*)", G_REGEX_CASELESS
| G_REGEX_OPTIMIZE
, 0);
1609 TEST_NEW("(?P<A>x)|(?P<A>y)", G_REGEX_DUPNAMES
, 0);
1610 TEST_NEW("(?P<A>x)|(?P<A>y)", G_REGEX_DUPNAMES
| G_REGEX_OPTIMIZE
, 0);
1611 /* This gives "internal error: code overflow" with pcre 6.0 */
1612 TEST_NEW("(?i)(?-i)", 0, 0);
1614 /* TEST_NEW_FAIL(pattern, compile_opts, expected_error) */
1615 TEST_NEW_FAIL("(", 0, G_REGEX_ERROR_UNMATCHED_PARENTHESIS
);
1616 TEST_NEW_FAIL(")", 0, G_REGEX_ERROR_UNMATCHED_PARENTHESIS
);
1617 TEST_NEW_FAIL("[", 0, G_REGEX_ERROR_UNTERMINATED_CHARACTER_CLASS
);
1618 TEST_NEW_FAIL("*", 0, G_REGEX_ERROR_NOTHING_TO_REPEAT
);
1619 TEST_NEW_FAIL("?", 0, G_REGEX_ERROR_NOTHING_TO_REPEAT
);
1620 TEST_NEW_FAIL("(?P<A>x)|(?P<A>y)", 0, G_REGEX_ERROR_DUPLICATE_SUBPATTERN_NAME
);
1622 /* TEST_MATCH_SIMPLE(pattern, string, compile_opts, match_opts, expected) */
1623 TEST_MATCH_SIMPLE("a", "", 0, 0, FALSE
);
1624 TEST_MATCH_SIMPLE("a", "a", 0, 0, TRUE
);
1625 TEST_MATCH_SIMPLE("a", "ba", 0, 0, TRUE
);
1626 TEST_MATCH_SIMPLE("^a", "ba", 0, 0, FALSE
);
1627 TEST_MATCH_SIMPLE("a", "ba", G_REGEX_ANCHORED
, 0, FALSE
);
1628 TEST_MATCH_SIMPLE("a", "ba", 0, G_REGEX_MATCH_ANCHORED
, FALSE
);
1629 TEST_MATCH_SIMPLE("a", "ab", G_REGEX_ANCHORED
, 0, TRUE
);
1630 TEST_MATCH_SIMPLE("a", "ab", 0, G_REGEX_MATCH_ANCHORED
, TRUE
);
1631 TEST_MATCH_SIMPLE("a", "a", G_REGEX_CASELESS
, 0, TRUE
);
1632 TEST_MATCH_SIMPLE("a", "A", G_REGEX_CASELESS
, 0, TRUE
);
1633 /* These are needed to test extended properties. */
1634 TEST_MATCH_SIMPLE(AGRAVE
, AGRAVE
, G_REGEX_CASELESS
, 0, TRUE
);
1635 TEST_MATCH_SIMPLE(AGRAVE
, AGRAVE_UPPER
, G_REGEX_CASELESS
, 0, TRUE
);
1636 TEST_MATCH_SIMPLE("\\p{L}", "a", 0, 0, TRUE
);
1637 TEST_MATCH_SIMPLE("\\p{L}", "1", 0, 0, FALSE
);
1638 TEST_MATCH_SIMPLE("\\p{L}", AGRAVE
, 0, 0, TRUE
);
1639 TEST_MATCH_SIMPLE("\\p{L}", AGRAVE_UPPER
, 0, 0, TRUE
);
1640 TEST_MATCH_SIMPLE("\\p{L}", SHEEN
, 0, 0, TRUE
);
1641 TEST_MATCH_SIMPLE("\\p{L}", ETH30
, 0, 0, FALSE
);
1642 TEST_MATCH_SIMPLE("\\p{Ll}", "a", 0, 0, TRUE
);
1643 TEST_MATCH_SIMPLE("\\p{Ll}", AGRAVE
, 0, 0, TRUE
);
1644 TEST_MATCH_SIMPLE("\\p{Ll}", AGRAVE_UPPER
, 0, 0, FALSE
);
1645 TEST_MATCH_SIMPLE("\\p{Ll}", ETH30
, 0, 0, FALSE
);
1646 TEST_MATCH_SIMPLE("\\p{Sc}", AGRAVE
, 0, 0, FALSE
);
1647 TEST_MATCH_SIMPLE("\\p{Sc}", EURO
, 0, 0, TRUE
);
1648 TEST_MATCH_SIMPLE("\\p{Sc}", ETH30
, 0, 0, FALSE
);
1649 TEST_MATCH_SIMPLE("\\p{N}", "a", 0, 0, FALSE
);
1650 TEST_MATCH_SIMPLE("\\p{N}", "1", 0, 0, TRUE
);
1651 TEST_MATCH_SIMPLE("\\p{N}", AGRAVE
, 0, 0, FALSE
);
1652 TEST_MATCH_SIMPLE("\\p{N}", AGRAVE_UPPER
, 0, 0, FALSE
);
1653 TEST_MATCH_SIMPLE("\\p{N}", SHEEN
, 0, 0, FALSE
);
1654 TEST_MATCH_SIMPLE("\\p{N}", ETH30
, 0, 0, TRUE
);
1655 TEST_MATCH_SIMPLE("\\p{Nd}", "a", 0, 0, FALSE
);
1656 TEST_MATCH_SIMPLE("\\p{Nd}", "1", 0, 0, TRUE
);
1657 TEST_MATCH_SIMPLE("\\p{Nd}", AGRAVE
, 0, 0, FALSE
);
1658 TEST_MATCH_SIMPLE("\\p{Nd}", AGRAVE_UPPER
, 0, 0, FALSE
);
1659 TEST_MATCH_SIMPLE("\\p{Nd}", SHEEN
, 0, 0, FALSE
);
1660 TEST_MATCH_SIMPLE("\\p{Nd}", ETH30
, 0, 0, FALSE
);
1661 TEST_MATCH_SIMPLE("\\p{Common}", SHEEN
, 0, 0, FALSE
);
1662 TEST_MATCH_SIMPLE("\\p{Common}", "a", 0, 0, FALSE
);
1663 TEST_MATCH_SIMPLE("\\p{Common}", AGRAVE
, 0, 0, FALSE
);
1664 TEST_MATCH_SIMPLE("\\p{Common}", AGRAVE_UPPER
, 0, 0, FALSE
);
1665 TEST_MATCH_SIMPLE("\\p{Common}", ETH30
, 0, 0, FALSE
);
1666 TEST_MATCH_SIMPLE("\\p{Common}", "%", 0, 0, TRUE
);
1667 TEST_MATCH_SIMPLE("\\p{Common}", "1", 0, 0, TRUE
);
1668 TEST_MATCH_SIMPLE("\\p{Arabic}", SHEEN
, 0, 0, TRUE
);
1669 TEST_MATCH_SIMPLE("\\p{Arabic}", "a", 0, 0, FALSE
);
1670 TEST_MATCH_SIMPLE("\\p{Arabic}", AGRAVE
, 0, 0, FALSE
);
1671 TEST_MATCH_SIMPLE("\\p{Arabic}", AGRAVE_UPPER
, 0, 0, FALSE
);
1672 TEST_MATCH_SIMPLE("\\p{Arabic}", ETH30
, 0, 0, FALSE
);
1673 TEST_MATCH_SIMPLE("\\p{Arabic}", "%", 0, 0, FALSE
);
1674 TEST_MATCH_SIMPLE("\\p{Arabic}", "1", 0, 0, FALSE
);
1675 TEST_MATCH_SIMPLE("\\p{Latin}", SHEEN
, 0, 0, FALSE
);
1676 TEST_MATCH_SIMPLE("\\p{Latin}", "a", 0, 0, TRUE
);
1677 TEST_MATCH_SIMPLE("\\p{Latin}", AGRAVE
, 0, 0, TRUE
);
1678 TEST_MATCH_SIMPLE("\\p{Latin}", AGRAVE_UPPER
, 0, 0, TRUE
);
1679 TEST_MATCH_SIMPLE("\\p{Latin}", ETH30
, 0, 0, FALSE
);
1680 TEST_MATCH_SIMPLE("\\p{Latin}", "%", 0, 0, FALSE
);
1681 TEST_MATCH_SIMPLE("\\p{Latin}", "1", 0, 0, FALSE
);
1682 TEST_MATCH_SIMPLE("\\p{Ethiopic}", SHEEN
, 0, 0, FALSE
);
1683 TEST_MATCH_SIMPLE("\\p{Ethiopic}", "a", 0, 0, FALSE
);
1684 TEST_MATCH_SIMPLE("\\p{Ethiopic}", AGRAVE
, 0, 0, FALSE
);
1685 TEST_MATCH_SIMPLE("\\p{Ethiopic}", AGRAVE_UPPER
, 0, 0, FALSE
);
1686 TEST_MATCH_SIMPLE("\\p{Ethiopic}", ETH30
, 0, 0, TRUE
);
1687 TEST_MATCH_SIMPLE("\\p{Ethiopic}", "%", 0, 0, FALSE
);
1688 TEST_MATCH_SIMPLE("\\p{Ethiopic}", "1", 0, 0, FALSE
);
1689 TEST_MATCH_SIMPLE("\\p{L}(?<=\\p{Arabic})", SHEEN
, 0, 0, TRUE
);
1690 TEST_MATCH_SIMPLE("\\p{L}(?<=\\p{Latin})", SHEEN
, 0, 0, FALSE
);
1691 /* Invalid patterns. */
1692 TEST_MATCH_SIMPLE("\\", "a", 0, 0, FALSE
);
1693 TEST_MATCH_SIMPLE("[", "", 0, 0, FALSE
);
1695 /* TEST_MATCH(pattern, compile_opts, match_opts, string,
1696 * string_len, start_position, match_opts2, expected) */
1697 TEST_MATCH("a", 0, 0, "a", -1, 0, 0, TRUE
);
1698 TEST_MATCH("a", 0, 0, "A", -1, 0, 0, FALSE
);
1699 TEST_MATCH("a", G_REGEX_CASELESS
, 0, "A", -1, 0, 0, TRUE
);
1700 TEST_MATCH("a", 0, 0, "ab", -1, 1, 0, FALSE
);
1701 TEST_MATCH("a", 0, 0, "ba", 1, 0, 0, FALSE
);
1702 TEST_MATCH("a", 0, 0, "bab", -1, 0, 0, TRUE
);
1703 TEST_MATCH("a", 0, 0, "b", -1, 0, 0, FALSE
);
1704 TEST_MATCH("a", 0, G_REGEX_ANCHORED
, "a", -1, 0, 0, TRUE
);
1705 TEST_MATCH("a", 0, G_REGEX_ANCHORED
, "ab", -1, 1, 0, FALSE
);
1706 TEST_MATCH("a", 0, G_REGEX_ANCHORED
, "ba", 1, 0, 0, FALSE
);
1707 TEST_MATCH("a", 0, G_REGEX_ANCHORED
, "bab", -1, 0, 0, FALSE
);
1708 TEST_MATCH("a", 0, G_REGEX_ANCHORED
, "b", -1, 0, 0, FALSE
);
1709 TEST_MATCH("a", 0, 0, "a", -1, 0, G_REGEX_ANCHORED
, TRUE
);
1710 TEST_MATCH("a", 0, 0, "ab", -1, 1, G_REGEX_ANCHORED
, FALSE
);
1711 TEST_MATCH("a", 0, 0, "ba", 1, 0, G_REGEX_ANCHORED
, FALSE
);
1712 TEST_MATCH("a", 0, 0, "bab", -1, 0, G_REGEX_ANCHORED
, FALSE
);
1713 TEST_MATCH("a", 0, 0, "b", -1, 0, G_REGEX_ANCHORED
, FALSE
);
1714 TEST_MATCH("a|b", 0, 0, "a", -1, 0, 0, TRUE
);
1715 TEST_MATCH("\\d", 0, 0, EURO
, -1, 0, 0, FALSE
);
1716 TEST_MATCH("^.$", 0, 0, EURO
, -1, 0, 0, TRUE
);
1717 TEST_MATCH("^.{3}$", 0, 0, EURO
, -1, 0, 0, FALSE
);
1718 TEST_MATCH("^.$", G_REGEX_RAW
, 0, EURO
, -1, 0, 0, FALSE
);
1719 TEST_MATCH("^.{3}$", G_REGEX_RAW
, 0, EURO
, -1, 0, 0, TRUE
);
1720 TEST_MATCH(AGRAVE
, G_REGEX_CASELESS
, 0, AGRAVE_UPPER
, -1, 0, 0, TRUE
);
1722 /* New lines handling. */
1723 TEST_MATCH("^a\\Rb$", 0, 0, "a\r\nb", -1, 0, 0, TRUE
);
1724 TEST_MATCH("^a\\Rb$", 0, 0, "a\nb", -1, 0, 0, TRUE
);
1725 TEST_MATCH("^a\\Rb$", 0, 0, "a\rb", -1, 0, 0, TRUE
);
1726 TEST_MATCH("^a\\Rb$", 0, 0, "a\n\rb", -1, 0, 0, FALSE
);
1727 TEST_MATCH("^a\\R\\Rb$", 0, 0, "a\n\rb", -1, 0, 0, TRUE
);
1728 TEST_MATCH("^a\\nb$", 0, 0, "a\r\nb", -1, 0, 0, FALSE
);
1729 TEST_MATCH("^a\\r\\nb$", 0, 0, "a\r\nb", -1, 0, 0, TRUE
);
1731 TEST_MATCH("^b$", 0, 0, "a\nb\nc", -1, 0, 0, FALSE
);
1732 TEST_MATCH("^b$", G_REGEX_MULTILINE
, 0, "a\nb\nc", -1, 0, 0, TRUE
);
1733 TEST_MATCH("^b$", G_REGEX_MULTILINE
, 0, "a\r\nb\r\nc", -1, 0, 0, TRUE
);
1734 TEST_MATCH("^b$", G_REGEX_MULTILINE
, 0, "a\rb\rc", -1, 0, 0, TRUE
);
1735 TEST_MATCH("^b$", G_REGEX_MULTILINE
| G_REGEX_NEWLINE_CR
, 0, "a\nb\nc", -1, 0, 0, FALSE
);
1736 TEST_MATCH("^b$", G_REGEX_MULTILINE
| G_REGEX_NEWLINE_LF
, 0, "a\nb\nc", -1, 0, 0, TRUE
);
1737 TEST_MATCH("^b$", G_REGEX_MULTILINE
| G_REGEX_NEWLINE_CRLF
, 0, "a\nb\nc", -1, 0, 0, FALSE
);
1738 TEST_MATCH("^b$", G_REGEX_MULTILINE
| G_REGEX_NEWLINE_CR
, 0, "a\r\nb\r\nc", -1, 0, 0, FALSE
);
1739 TEST_MATCH("^b$", G_REGEX_MULTILINE
| G_REGEX_NEWLINE_LF
, 0, "a\r\nb\r\nc", -1, 0, 0, FALSE
);
1740 TEST_MATCH("^b$", G_REGEX_MULTILINE
| G_REGEX_NEWLINE_CRLF
, 0, "a\r\nb\r\nc", -1, 0, 0, TRUE
);
1741 TEST_MATCH("^b$", G_REGEX_MULTILINE
| G_REGEX_NEWLINE_CR
, 0, "a\rb\rc", -1, 0, 0, TRUE
);
1742 TEST_MATCH("^b$", G_REGEX_MULTILINE
| G_REGEX_NEWLINE_LF
, 0, "a\rb\rc", -1, 0, 0, FALSE
);
1743 TEST_MATCH("^b$", G_REGEX_MULTILINE
| G_REGEX_NEWLINE_CRLF
, 0, "a\rb\rc", -1, 0, 0, FALSE
);
1744 TEST_MATCH("^b$", G_REGEX_MULTILINE
, G_REGEX_MATCH_NEWLINE_CR
, "a\nb\nc", -1, 0, 0, FALSE
);
1745 TEST_MATCH("^b$", G_REGEX_MULTILINE
, G_REGEX_MATCH_NEWLINE_LF
, "a\nb\nc", -1, 0, 0, TRUE
);
1746 TEST_MATCH("^b$", G_REGEX_MULTILINE
, G_REGEX_MATCH_NEWLINE_CRLF
, "a\nb\nc", -1, 0, 0, FALSE
);
1747 TEST_MATCH("^b$", G_REGEX_MULTILINE
, G_REGEX_MATCH_NEWLINE_CR
, "a\r\nb\r\nc", -1, 0, 0, FALSE
);
1748 TEST_MATCH("^b$", G_REGEX_MULTILINE
, G_REGEX_MATCH_NEWLINE_LF
, "a\r\nb\r\nc", -1, 0, 0, FALSE
);
1749 TEST_MATCH("^b$", G_REGEX_MULTILINE
, G_REGEX_MATCH_NEWLINE_CRLF
, "a\r\nb\r\nc", -1, 0, 0, TRUE
);
1750 TEST_MATCH("^b$", G_REGEX_MULTILINE
, G_REGEX_MATCH_NEWLINE_CR
, "a\rb\rc", -1, 0, 0, TRUE
);
1751 TEST_MATCH("^b$", G_REGEX_MULTILINE
, G_REGEX_MATCH_NEWLINE_LF
, "a\rb\rc", -1, 0, 0, FALSE
);
1752 TEST_MATCH("^b$", G_REGEX_MULTILINE
, G_REGEX_MATCH_NEWLINE_CRLF
, "a\rb\rc", -1, 0, 0, FALSE
);
1754 TEST_MATCH("^b$", G_REGEX_MULTILINE
| G_REGEX_NEWLINE_CR
, G_REGEX_MATCH_NEWLINE_ANY
, "a\nb\nc", -1, 0, 0, TRUE
);
1755 TEST_MATCH("^b$", G_REGEX_MULTILINE
| G_REGEX_NEWLINE_CR
, G_REGEX_MATCH_NEWLINE_ANY
, "a\rb\rc", -1, 0, 0, TRUE
);
1756 TEST_MATCH("^b$", G_REGEX_MULTILINE
| G_REGEX_NEWLINE_CR
, G_REGEX_MATCH_NEWLINE_ANY
, "a\r\nb\r\nc", -1, 0, 0, TRUE
);
1757 TEST_MATCH("^b$", G_REGEX_MULTILINE
| G_REGEX_NEWLINE_CR
, G_REGEX_MATCH_NEWLINE_LF
, "a\nb\nc", -1, 0, 0, TRUE
);
1758 TEST_MATCH("^b$", G_REGEX_MULTILINE
| G_REGEX_NEWLINE_CR
, G_REGEX_MATCH_NEWLINE_LF
, "a\rb\rc", -1, 0, 0, FALSE
);
1759 TEST_MATCH("^b$", G_REGEX_MULTILINE
| G_REGEX_NEWLINE_CR
, G_REGEX_MATCH_NEWLINE_CRLF
, "a\r\nb\r\nc", -1, 0, 0, TRUE
);
1760 TEST_MATCH("^b$", G_REGEX_MULTILINE
| G_REGEX_NEWLINE_CR
, G_REGEX_MATCH_NEWLINE_CRLF
, "a\rb\rc", -1, 0, 0, FALSE
);
1762 TEST_MATCH("a#\nb", G_REGEX_EXTENDED
, 0, "a", -1, 0, 0, FALSE
);
1763 TEST_MATCH("a#\r\nb", G_REGEX_EXTENDED
, 0, "a", -1, 0, 0, FALSE
);
1764 TEST_MATCH("a#\rb", G_REGEX_EXTENDED
, 0, "a", -1, 0, 0, FALSE
);
1765 TEST_MATCH("a#\nb", G_REGEX_EXTENDED
, G_REGEX_MATCH_NEWLINE_CR
, "a", -1, 0, 0, FALSE
);
1766 TEST_MATCH("a#\nb", G_REGEX_EXTENDED
| G_REGEX_NEWLINE_CR
, 0, "a", -1, 0, 0, TRUE
);
1768 /* TEST_MATCH_NEXT#(pattern, string, string_len, start_position, ...) */
1769 TEST_MATCH_NEXT0("a", "x", -1, 0);
1770 TEST_MATCH_NEXT0("a", "ax", -1, 1);
1771 TEST_MATCH_NEXT0("a", "xa", 1, 0);
1772 TEST_MATCH_NEXT0("a", "axa", 1, 2);
1773 TEST_MATCH_NEXT1("a", "a", -1, 0, "a", 0, 1);
1774 TEST_MATCH_NEXT1("a", "xax", -1, 0, "a", 1, 2);
1775 TEST_MATCH_NEXT1(EURO
, ENG EURO
, -1, 0, EURO
, 2, 5);
1776 TEST_MATCH_NEXT1("a*", "", -1, 0, "", 0, 0);
1777 TEST_MATCH_NEXT2("a*", "aa", -1, 0, "aa", 0, 2, "", 2, 2);
1778 TEST_MATCH_NEXT2(EURO
"*", EURO EURO
, -1, 0, EURO EURO
, 0, 6, "", 6, 6);
1779 TEST_MATCH_NEXT2("a", "axa", -1, 0, "a", 0, 1, "a", 2, 3);
1780 TEST_MATCH_NEXT2("a+", "aaxa", -1, 0, "aa", 0, 2, "a", 3, 4);
1781 TEST_MATCH_NEXT2("a", "aa", -1, 0, "a", 0, 1, "a", 1, 2);
1782 TEST_MATCH_NEXT2("a", "ababa", -1, 2, "a", 2, 3, "a", 4, 5);
1783 TEST_MATCH_NEXT2(EURO
"+", EURO
"-" EURO
, -1, 0, EURO
, 0, 3, EURO
, 4, 7);
1784 TEST_MATCH_NEXT3("", "ab", -1, 0, "", 0, 0, "", 1, 1, "", 2, 2);
1785 TEST_MATCH_NEXT3("", AGRAVE
"b", -1, 0, "", 0, 0, "", 2, 2, "", 3, 3);
1786 TEST_MATCH_NEXT3("a", "aaxa", -1, 0, "a", 0, 1, "a", 1, 2, "a", 3, 4);
1787 TEST_MATCH_NEXT3("a", "aa" OGRAVE
"a", -1, 0, "a", 0, 1, "a", 1, 2, "a", 4, 5);
1788 TEST_MATCH_NEXT3("a*", "aax", -1, 0, "aa", 0, 2, "", 2, 2, "", 3, 3);
1789 TEST_MATCH_NEXT4("a*", "aaxa", -1, 0, "aa", 0, 2, "", 2, 2, "a", 3, 4, "", 4, 4);
1791 /* TEST_MATCH_COUNT(pattern, string, start_position, match_opts, expected_count) */
1792 TEST_MATCH_COUNT("a", "", 0, 0, 0);
1793 TEST_MATCH_COUNT("a", "a", 0, 0, 1);
1794 TEST_MATCH_COUNT("a", "a", 1, 0, 0);
1795 TEST_MATCH_COUNT("(.)", "a", 0, 0, 2);
1796 TEST_MATCH_COUNT("(.)", EURO
, 0, 0, 2);
1797 TEST_MATCH_COUNT("(?:.)", "a", 0, 0, 1);
1798 TEST_MATCH_COUNT("(?P<A>.)", "a", 0, 0, 2);
1799 TEST_MATCH_COUNT("a$", "a", 0, G_REGEX_MATCH_NOTEOL
, 0);
1800 TEST_MATCH_COUNT("(a)?(b)", "b", 0, 0, 3);
1801 TEST_MATCH_COUNT("(a)?(b)", "ab", 0, 0, 3);
1803 /* TEST_PARTIAL(pattern, string, expected) */
1804 TEST_PARTIAL("^ab", "a", TRUE
);
1805 TEST_PARTIAL("^ab", "xa", FALSE
);
1806 TEST_PARTIAL("ab", "xa", TRUE
);
1807 TEST_PARTIAL("ab", "ab", FALSE
); /* normal match. */
1808 TEST_PARTIAL("a+b", "aa", FALSE
); /* PCRE_ERROR_BAD_PARTIAL */
1809 TEST_PARTIAL("(a)+b", "aa", TRUE
);
1810 TEST_PARTIAL("a?b", "a", TRUE
);
1812 /* TEST_SUB_PATTERN(pattern, string, start_position, sub_n, expected_sub,
1813 * expected_start, expected_end) */
1814 TEST_SUB_PATTERN("a", "a", 0, 0, "a", 0, 1);
1815 TEST_SUB_PATTERN("a(.)", "ab", 0, 1, "b", 1, 2);
1816 TEST_SUB_PATTERN("a(.)", "a" EURO
, 0, 1, EURO
, 1, 4);
1817 TEST_SUB_PATTERN("(?:.*)(a)(.)", "xxa" ENG
, 0, 2, ENG
, 3, 5);
1818 TEST_SUB_PATTERN("(" HSTROKE
")", "a" HSTROKE ENG
, 0, 1, HSTROKE
, 1, 3);
1819 TEST_SUB_PATTERN("a", "a", 0, 1, NULL
, UNTOUCHED
, UNTOUCHED
);
1820 TEST_SUB_PATTERN("a", "a", 0, 1, NULL
, UNTOUCHED
, UNTOUCHED
);
1821 TEST_SUB_PATTERN("(a)?(b)", "b", 0, 0, "b", 0, 1);
1822 TEST_SUB_PATTERN("(a)?(b)", "b", 0, 1, "", -1, -1);
1823 TEST_SUB_PATTERN("(a)?(b)", "b", 0, 2, "b", 0, 1);
1825 /* TEST_NAMED_SUB_PATTERN(pattern, string, start_position, sub_name,
1826 * expected_sub, expected_start, expected_end) */
1827 TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?", "ab", 0, "A", "b", 1, 2);
1828 TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?", "aab", 1, "A", "b", 2, 3);
1829 TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?", EURO
"ab", 0, "A", "b", 4, 5);
1830 TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?", EURO
"ab", 0, "B", NULL
, UNTOUCHED
, UNTOUCHED
);
1831 TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?", EURO
"ab", 0, "C", NULL
, UNTOUCHED
, UNTOUCHED
);
1832 TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?", "a" EGRAVE
"x", 0, "A", EGRAVE
, 1, 3);
1833 TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?", "a" EGRAVE
"x", 0, "B", "x", 3, 4);
1834 TEST_NAMED_SUB_PATTERN("(?P<A>a)?(?P<B>b)", "b", 0, "A", "", -1, -1);
1835 TEST_NAMED_SUB_PATTERN("(?P<A>a)?(?P<B>b)", "b", 0, "B", "b", 0, 1);
1837 /* TEST_NAMED_SUB_PATTERN_DUPNAMES(pattern, string, start_position, sub_name,
1838 * expected_sub, expected_start, expected_end) */
1839 TEST_NAMED_SUB_PATTERN_DUPNAMES("(?P<N>a)|(?P<N>b)", "ab", 0, "N", "a", 0, 1);
1840 TEST_NAMED_SUB_PATTERN_DUPNAMES("(?P<N>aa)|(?P<N>a)", "aa", 0, "N", "aa", 0, 2);
1841 TEST_NAMED_SUB_PATTERN_DUPNAMES("(?P<N>aa)(?P<N>a)", "aaa", 0, "N", "aa", 0, 2);
1842 TEST_NAMED_SUB_PATTERN_DUPNAMES("(?P<N>x)|(?P<N>a)", "a", 0, "N", "a", 0, 1);
1843 TEST_NAMED_SUB_PATTERN_DUPNAMES("(?P<N>x)y|(?P<N>a)b", "ab", 0, "N", "a", 0, 1);
1845 /* DUPNAMES option inside the pattern */
1846 TEST_NAMED_SUB_PATTERN("(?J)(?P<N>a)|(?P<N>b)", "ab", 0, "N", "a", 0, 1);
1847 TEST_NAMED_SUB_PATTERN("(?J)(?P<N>aa)|(?P<N>a)", "aa", 0, "N", "aa", 0, 2);
1848 TEST_NAMED_SUB_PATTERN("(?J)(?P<N>aa)(?P<N>a)", "aaa", 0, "N", "aa", 0, 2);
1849 TEST_NAMED_SUB_PATTERN("(?J)(?P<N>x)|(?P<N>a)", "a", 0, "N", "a", 0, 1);
1850 TEST_NAMED_SUB_PATTERN("(?J)(?P<N>x)y|(?P<N>a)b", "ab", 0, "N", "a", 0, 1);
1852 /* TEST_FETCH_ALL#(pattern, string, ...) */
1853 TEST_FETCH_ALL0("a", "");
1854 TEST_FETCH_ALL0("a", "b");
1855 TEST_FETCH_ALL1("a", "a", "a");
1856 TEST_FETCH_ALL1("a+", "aa", "aa");
1857 TEST_FETCH_ALL1("(?:a)", "a", "a");
1858 TEST_FETCH_ALL2("(a)", "a", "a", "a");
1859 TEST_FETCH_ALL2("a(.)", "ab", "ab", "b");
1860 TEST_FETCH_ALL2("a(.)", "a" HSTROKE
, "a" HSTROKE
, HSTROKE
);
1861 TEST_FETCH_ALL3("(?:.*)(a)(.)", "xyazk", "xyaz", "a", "z");
1862 TEST_FETCH_ALL3("(?P<A>.)(a)", "xa", "xa", "x", "a");
1863 TEST_FETCH_ALL3("(?P<A>.)(a)", ENG
"a", ENG
"a", ENG
, "a");
1864 TEST_FETCH_ALL3("(a)?(b)", "b", "b", "", "b");
1865 TEST_FETCH_ALL3("(a)?(b)", "ab", "ab", "a", "b");
1867 /* TEST_SPLIT_SIMPLE#(pattern, string, ...) */
1868 TEST_SPLIT_SIMPLE0("", "");
1869 TEST_SPLIT_SIMPLE0("a", "");
1870 TEST_SPLIT_SIMPLE1(",", "a", "a");
1871 TEST_SPLIT_SIMPLE1("(,)\\s*", "a", "a");
1872 TEST_SPLIT_SIMPLE2(",", "a,b", "a", "b");
1873 TEST_SPLIT_SIMPLE3(",", "a,b,c", "a", "b", "c");
1874 TEST_SPLIT_SIMPLE3(",\\s*", "a,b,c", "a", "b", "c");
1875 TEST_SPLIT_SIMPLE3(",\\s*", "a, b, c", "a", "b", "c");
1876 TEST_SPLIT_SIMPLE3("(,)\\s*", "a,b", "a", ",", "b");
1877 TEST_SPLIT_SIMPLE3("(,)\\s*", "a, b", "a", ",", "b");
1878 /* Not matched sub-strings. */
1879 TEST_SPLIT_SIMPLE2("a|(b)", "xay", "x", "y");
1880 TEST_SPLIT_SIMPLE3("a|(b)", "xby", "x", "b", "y");
1881 /* Empty matches. */
1882 TEST_SPLIT_SIMPLE3("", "abc", "a", "b", "c");
1883 TEST_SPLIT_SIMPLE3(" *", "ab c", "a", "b", "c");
1884 /* Invalid patterns. */
1885 TEST_SPLIT_SIMPLE0("\\", "");
1886 TEST_SPLIT_SIMPLE0("[", "");
1888 /* TEST_SPLIT#(pattern, string, start_position, max_tokens, ...) */
1889 TEST_SPLIT0("", "", 0, 0);
1890 TEST_SPLIT0("a", "", 0, 0);
1891 TEST_SPLIT0("a", "", 0, 1);
1892 TEST_SPLIT0("a", "", 0, 2);
1893 TEST_SPLIT0("a", "a", 1, 0);
1894 TEST_SPLIT1(",", "a", 0, 0, "a");
1895 TEST_SPLIT1(",", "a,b", 0, 1, "a,b");
1896 TEST_SPLIT1("(,)\\s*", "a", 0, 0, "a");
1897 TEST_SPLIT1(",", "a,b", 2, 0, "b");
1898 TEST_SPLIT2(",", "a,b", 0, 0, "a", "b");
1899 TEST_SPLIT2(",", "a,b,c", 0, 2, "a", "b,c");
1900 TEST_SPLIT2(",", "a,b", 1, 0, "", "b");
1901 TEST_SPLIT2(",", "a,", 0, 0, "a", "");
1902 TEST_SPLIT3(",", "a,b,c", 0, 0, "a", "b", "c");
1903 TEST_SPLIT3(",\\s*", "a,b,c", 0, 0, "a", "b", "c");
1904 TEST_SPLIT3(",\\s*", "a, b, c", 0, 0, "a", "b", "c");
1905 TEST_SPLIT3("(,)\\s*", "a,b", 0, 0, "a", ",", "b");
1906 TEST_SPLIT3("(,)\\s*", "a, b", 0, 0, "a", ",", "b");
1907 /* Not matched sub-strings. */
1908 TEST_SPLIT2("a|(b)", "xay", 0, 0, "x", "y");
1909 TEST_SPLIT3("a|(b)", "xby", 0, -1, "x", "b", "y");
1910 /* Empty matches. */
1911 TEST_SPLIT2(" *", "ab c", 1, 0, "b", "c");
1912 TEST_SPLIT3("", "abc", 0, 0, "a", "b", "c");
1913 TEST_SPLIT3(" *", "ab c", 0, 0, "a", "b", "c");
1914 TEST_SPLIT1(" *", "ab c", 0, 1, "ab c");
1915 TEST_SPLIT2(" *", "ab c", 0, 2, "a", "b c");
1916 TEST_SPLIT3(" *", "ab c", 0, 3, "a", "b", "c");
1917 TEST_SPLIT3(" *", "ab c", 0, 4, "a", "b", "c");
1919 /* TEST_CHECK_REPLACEMENT(string_to_expand, expected, expected_refs) */
1920 TEST_CHECK_REPLACEMENT("", TRUE
, FALSE
);
1921 TEST_CHECK_REPLACEMENT("a", TRUE
, FALSE
);
1922 TEST_CHECK_REPLACEMENT("\\t\\n\\v\\r\\f\\a\\b\\\\\\x{61}", TRUE
, FALSE
);
1923 TEST_CHECK_REPLACEMENT("\\0", TRUE
, TRUE
);
1924 TEST_CHECK_REPLACEMENT("\\n\\2", TRUE
, TRUE
);
1925 TEST_CHECK_REPLACEMENT("\\g<foo>", TRUE
, TRUE
);
1926 /* Invalid strings */
1927 TEST_CHECK_REPLACEMENT("\\Q", FALSE
, FALSE
);
1928 TEST_CHECK_REPLACEMENT("x\\Ay", FALSE
, FALSE
);
1930 /* TEST_EXPAND(pattern, string, string_to_expand, raw, expected) */
1931 TEST_EXPAND("a", "a", "", FALSE
, "");
1932 TEST_EXPAND("a", "a", "\\0", FALSE
, "a");
1933 TEST_EXPAND("a", "a", "\\1", FALSE
, "");
1934 TEST_EXPAND("(a)", "ab", "\\1", FALSE
, "a");
1935 TEST_EXPAND("(a)", "a", "\\1", FALSE
, "a");
1936 TEST_EXPAND("(a)", "a", "\\g<1>", FALSE
, "a");
1937 TEST_EXPAND("a", "a", "\\0130", FALSE
, "X");
1938 TEST_EXPAND("a", "a", "\\\\\\0", FALSE
, "\\a");
1939 TEST_EXPAND("a(?P<G>.)c", "xabcy", "X\\g<G>X", FALSE
, "XbX");
1940 TEST_EXPAND("(.)(?P<1>.)", "ab", "\\1", FALSE
, "a");
1941 TEST_EXPAND("(.)(?P<1>.)", "ab", "\\g<1>", FALSE
, "a");
1942 TEST_EXPAND(".", EURO
, "\\0", FALSE
, EURO
);
1943 TEST_EXPAND("(.)", EURO
, "\\1", FALSE
, EURO
);
1944 TEST_EXPAND("(?P<G>.)", EURO
, "\\g<G>", FALSE
, EURO
);
1945 TEST_EXPAND(".", "a", EURO
, FALSE
, EURO
);
1946 TEST_EXPAND(".", "a", EURO
"\\0", FALSE
, EURO
"a");
1947 TEST_EXPAND(".", "", "\\Lab\\Ec", FALSE
, "abc");
1948 TEST_EXPAND(".", "", "\\LaB\\EC", FALSE
, "abC");
1949 TEST_EXPAND(".", "", "\\Uab\\Ec", FALSE
, "ABc");
1950 TEST_EXPAND(".", "", "a\\ubc", FALSE
, "aBc");
1951 TEST_EXPAND(".", "", "a\\lbc", FALSE
, "abc");
1952 TEST_EXPAND(".", "", "A\\uBC", FALSE
, "ABC");
1953 TEST_EXPAND(".", "", "A\\lBC", FALSE
, "AbC");
1954 TEST_EXPAND(".", "", "A\\l\\\\BC", FALSE
, "A\\BC");
1955 TEST_EXPAND(".", "", "\\L" AGRAVE
"\\E", FALSE
, AGRAVE
);
1956 TEST_EXPAND(".", "", "\\U" AGRAVE
"\\E", FALSE
, AGRAVE_UPPER
);
1957 TEST_EXPAND(".", "", "\\u" AGRAVE
"a", FALSE
, AGRAVE_UPPER
"a");
1958 TEST_EXPAND(".", "ab", "x\\U\\0y\\Ez", FALSE
, "xAYz");
1959 TEST_EXPAND(".(.)", "AB", "x\\L\\1y\\Ez", FALSE
, "xbyz");
1960 TEST_EXPAND(".", "ab", "x\\u\\0y\\Ez", FALSE
, "xAyz");
1961 TEST_EXPAND(".(.)", "AB", "x\\l\\1y\\Ez", FALSE
, "xbyz");
1962 TEST_EXPAND(".(.)", "a" AGRAVE_UPPER
, "x\\l\\1y", FALSE
, "x" AGRAVE
"y");
1963 TEST_EXPAND("a", "bab", "\\x{61}", FALSE
, "a");
1964 TEST_EXPAND("a", "bab", "\\x61", FALSE
, "a");
1965 TEST_EXPAND("a", "bab", "\\x5a", FALSE
, "Z");
1966 TEST_EXPAND("a", "bab", "\\0\\x5A", FALSE
, "aZ");
1967 TEST_EXPAND("a", "bab", "\\1\\x{5A}", FALSE
, "Z");
1968 TEST_EXPAND("a", "bab", "\\x{00E0}", FALSE
, AGRAVE
);
1969 TEST_EXPAND("", "bab", "\\x{0634}", FALSE
, SHEEN
);
1970 TEST_EXPAND("", "bab", "\\x{634}", FALSE
, SHEEN
);
1971 TEST_EXPAND("", "", "\\t", FALSE
, "\t");
1972 TEST_EXPAND("", "", "\\v", FALSE
, "\v");
1973 TEST_EXPAND("", "", "\\r", FALSE
, "\r");
1974 TEST_EXPAND("", "", "\\n", FALSE
, "\n");
1975 TEST_EXPAND("", "", "\\f", FALSE
, "\f");
1976 TEST_EXPAND("", "", "\\a", FALSE
, "\a");
1977 TEST_EXPAND("", "", "\\b", FALSE
, "\b");
1978 TEST_EXPAND("a(.)", "abc", "\\0\\b\\1", FALSE
, "ab\bb");
1979 TEST_EXPAND("a(.)", "abc", "\\0141", FALSE
, "a");
1980 TEST_EXPAND("a(.)", "abc", "\\078", FALSE
, "\a8");
1981 TEST_EXPAND("a(.)", "abc", "\\077", FALSE
, "?");
1982 TEST_EXPAND("a(.)", "abc", "\\0778", FALSE
, "?8");
1983 TEST_EXPAND("a(.)", "a" AGRAVE
"b", "\\1", FALSE
, AGRAVE
);
1984 TEST_EXPAND("a(.)", "a" AGRAVE
"b", "\\1", TRUE
, "\xc3");
1985 TEST_EXPAND("a(.)", "a" AGRAVE
"b", "\\0", TRUE
, "a\xc3");
1986 /* Invalid strings. */
1987 TEST_EXPAND("", "", "\\Q", FALSE
, NULL
);
1988 TEST_EXPAND("", "", "x\\Ay", FALSE
, NULL
);
1989 TEST_EXPAND("", "", "\\g<", FALSE
, NULL
);
1990 TEST_EXPAND("", "", "\\g<>", FALSE
, NULL
);
1991 TEST_EXPAND("", "", "\\g<1a>", FALSE
, NULL
);
1992 TEST_EXPAND("", "", "\\g<a$>", FALSE
, NULL
);
1993 TEST_EXPAND("", "", "\\", FALSE
, NULL
);
1994 TEST_EXPAND("a", "a", "\\x{61", FALSE
, NULL
);
1995 TEST_EXPAND("a", "a", "\\x6X", FALSE
, NULL
);
1997 TEST_EXPAND(NULL
, NULL
, "", FALSE
, "");
1998 TEST_EXPAND(NULL
, NULL
, "\\n", FALSE
, "\n");
1999 /* Invalid strings */
2000 TEST_EXPAND(NULL
, NULL
, "\\Q", FALSE
, NULL
);
2001 TEST_EXPAND(NULL
, NULL
, "x\\Ay", FALSE
, NULL
);
2003 /* TEST_REPLACE(pattern, string, start_position, replacement, expected) */
2004 TEST_REPLACE("a", "ababa", 0, "A", "AbAbA");
2005 TEST_REPLACE("a", "ababa", 1, "A", "abAbA");
2006 TEST_REPLACE("a", "ababa", 2, "A", "abAbA");
2007 TEST_REPLACE("a", "ababa", 3, "A", "ababA");
2008 TEST_REPLACE("a", "ababa", 4, "A", "ababA");
2009 TEST_REPLACE("a", "ababa", 5, "A", "ababa");
2010 TEST_REPLACE("a", "ababa", 6, "A", "ababa");
2011 TEST_REPLACE("a", "abababa", 2, "A", "abAbAbA");
2012 TEST_REPLACE("a", "abab", 0, "A", "AbAb");
2013 TEST_REPLACE("a", "baba", 0, "A", "bAbA");
2014 TEST_REPLACE("a", "bab", 0, "A", "bAb");
2015 TEST_REPLACE("$^", "abc", 0, "X", "abc");
2016 TEST_REPLACE("(.)a", "ciao", 0, "a\\1", "caio");
2017 TEST_REPLACE("a.", "abc", 0, "\\0\\0", "ababc");
2018 TEST_REPLACE("a", "asd", 0, "\\0101", "Asd");
2019 TEST_REPLACE("(a).\\1", "aba cda", 0, "\\1\\n", "a\n cda");
2020 TEST_REPLACE("a" AGRAVE
"a", "a" AGRAVE
"a", 0, "x", "x");
2021 TEST_REPLACE("a" AGRAVE
"a", "a" AGRAVE
"a", 0, OGRAVE
, OGRAVE
);
2022 TEST_REPLACE("[^-]", "-" EURO
"-x-" HSTROKE
, 0, "a", "-a-a-a");
2023 TEST_REPLACE("[^-]", "-" EURO
"-" HSTROKE
, 0, "a\\g<0>a", "-a" EURO
"a-a" HSTROKE
"a");
2024 TEST_REPLACE("-", "-" EURO
"-" HSTROKE
, 0, "", EURO HSTROKE
);
2025 TEST_REPLACE(".*", "hello", 0, "\\U\\0\\E", "HELLO");
2026 TEST_REPLACE(".*", "hello", 0, "\\u\\0", "Hello");
2027 TEST_REPLACE("\\S+", "hello world", 0, "\\U-\\0-", "-HELLO- -WORLD-");
2028 TEST_REPLACE(".", "a", 0, "\\A", NULL
);
2029 TEST_REPLACE(".", "a", 0, "\\g", NULL
);
2031 /* TEST_REPLACE_LIT(pattern, string, start_position, replacement, expected) */
2032 TEST_REPLACE_LIT("a", "ababa", 0, "A", "AbAbA");
2033 TEST_REPLACE_LIT("a", "ababa", 1, "A", "abAbA");
2034 TEST_REPLACE_LIT("a", "ababa", 2, "A", "abAbA");
2035 TEST_REPLACE_LIT("a", "ababa", 3, "A", "ababA");
2036 TEST_REPLACE_LIT("a", "ababa", 4, "A", "ababA");
2037 TEST_REPLACE_LIT("a", "ababa", 5, "A", "ababa");
2038 TEST_REPLACE_LIT("a", "ababa", 6, "A", "ababa");
2039 TEST_REPLACE_LIT("a", "abababa", 2, "A", "abAbAbA");
2040 TEST_REPLACE_LIT("a", "abcadaa", 0, "A", "AbcAdAA");
2041 TEST_REPLACE_LIT("$^", "abc", 0, "X", "abc");
2042 TEST_REPLACE_LIT("(.)a", "ciao", 0, "a\\1", "ca\\1o");
2043 TEST_REPLACE_LIT("a.", "abc", 0, "\\0\\0\\n", "\\0\\0\\nc");
2044 TEST_REPLACE_LIT("a" AGRAVE
"a", "a" AGRAVE
"a", 0, "x", "x");
2045 TEST_REPLACE_LIT("a" AGRAVE
"a", "a" AGRAVE
"a", 0, OGRAVE
, OGRAVE
);
2046 TEST_REPLACE_LIT(AGRAVE
, "-" AGRAVE
"-" HSTROKE
, 0, "a" ENG
"a", "-a" ENG
"a-" HSTROKE
);
2047 TEST_REPLACE_LIT("[^-]", "-" EURO
"-" AGRAVE
"-" HSTROKE
, 0, "a", "-a-a-a");
2048 TEST_REPLACE_LIT("[^-]", "-" EURO
"-" AGRAVE
, 0, "a\\g<0>a", "-a\\g<0>a-a\\g<0>a");
2049 TEST_REPLACE_LIT("-", "-" EURO
"-" AGRAVE
"-" HSTROKE
, 0, "", EURO AGRAVE HSTROKE
);
2051 /* TEST_GET_STRING_NUMBER(pattern, name, expected_num) */
2052 TEST_GET_STRING_NUMBER("", "A", -1);
2053 TEST_GET_STRING_NUMBER("(?P<A>.)", "A", 1);
2054 TEST_GET_STRING_NUMBER("(?P<A>.)", "B", -1);
2055 TEST_GET_STRING_NUMBER("(?P<A>.)(?P<B>a)", "A", 1);
2056 TEST_GET_STRING_NUMBER("(?P<A>.)(?P<B>a)", "B", 2);
2057 TEST_GET_STRING_NUMBER("(?P<A>.)(?P<B>a)", "C", -1);
2058 TEST_GET_STRING_NUMBER("(?P<A>.)(.)(?P<B>a)", "A", 1);
2059 TEST_GET_STRING_NUMBER("(?P<A>.)(.)(?P<B>a)", "B", 3);
2060 TEST_GET_STRING_NUMBER("(?P<A>.)(.)(?P<B>a)", "C", -1);
2061 TEST_GET_STRING_NUMBER("(?:a)(?P<A>.)", "A", 1);
2062 TEST_GET_STRING_NUMBER("(?:a)(?P<A>.)", "B", -1);
2064 /* TEST_ESCAPE(string, length, expected) */
2065 TEST_ESCAPE("hello world", -1, "hello world");
2066 TEST_ESCAPE("hello world", 5, "hello");
2067 TEST_ESCAPE("hello.world", -1, "hello\\.world");
2068 TEST_ESCAPE("a(b\\b.$", -1, "a\\(b\\\\b\\.\\$");
2069 TEST_ESCAPE("hello\0world", -1, "hello");
2070 TEST_ESCAPE("hello\0world", 11, "hello\\0world");
2071 TEST_ESCAPE(EURO
"*" ENG
, -1, EURO
"\\*" ENG
);
2072 TEST_ESCAPE("a$", -1, "a\\$");
2073 TEST_ESCAPE("$a", -1, "\\$a");
2074 TEST_ESCAPE("a$a", -1, "a\\$a");
2075 TEST_ESCAPE("$a$", -1, "\\$a\\$");
2076 TEST_ESCAPE("$a$", 0, "");
2077 TEST_ESCAPE("$a$", 1, "\\$");
2078 TEST_ESCAPE("$a$", 2, "\\$a");
2079 TEST_ESCAPE("$a$", 3, "\\$a\\$");
2080 TEST_ESCAPE("$a$", 4, "\\$a\\$\\0");
2081 TEST_ESCAPE("|()[]{}^$*+?.", -1, "\\|\\(\\)\\[\\]\\{\\}\\^\\$\\*\\+\\?\\.");
2082 TEST_ESCAPE("a|a(a)a[a]a{a}a^a$a*a+a?a.a", -1,
2083 "a\\|a\\(a\\)a\\[a\\]a\\{a\\}a\\^a\\$a\\*a\\+a\\?a\\.a");
2085 /* TEST_MATCH_ALL#(pattern, string, string_len, start_position, ...) */
2086 TEST_MATCH_ALL0("<.*>", "", -1, 0);
2087 TEST_MATCH_ALL0("a+", "", -1, 0);
2088 TEST_MATCH_ALL0("a+", "a", 0, 0);
2089 TEST_MATCH_ALL0("a+", "a", -1, 1);
2090 TEST_MATCH_ALL1("<.*>", "<a>", -1, 0, "<a>", 0, 3);
2091 TEST_MATCH_ALL1("a+", "a", -1, 0, "a", 0, 1);
2092 TEST_MATCH_ALL1("a+", "aa", 1, 0, "a", 0, 1);
2093 TEST_MATCH_ALL1("a+", "aa", -1, 1, "a", 1, 2);
2094 TEST_MATCH_ALL1("a+", "aa", 2, 1, "a", 1, 2);
2095 TEST_MATCH_ALL1(".+", ENG
, -1, 0, ENG
, 0, 2);
2096 TEST_MATCH_ALL2("<.*>", "<a><b>", -1, 0, "<a><b>", 0, 6, "<a>", 0, 3);
2097 TEST_MATCH_ALL2("a+", "aa", -1, 0, "aa", 0, 2, "a", 0, 1);
2098 TEST_MATCH_ALL2(".+", ENG EURO
, -1, 0, ENG EURO
, 0, 5, ENG
, 0, 2);
2099 TEST_MATCH_ALL3("<.*>", "<a><b><c>", -1, 0, "<a><b><c>", 0, 9,
2100 "<a><b>", 0, 6, "<a>", 0, 3);
2101 TEST_MATCH_ALL3("a+", "aaa", -1, 0, "aaa", 0, 3, "aa", 0, 2, "a", 0, 1);
2103 end
: /* if abort_on_fail is TRUE the flow passes to this label. */
2104 verbose ("\n%u tests passed, %u failed\n", passed
, failed
);
2108 #else /* ENABLE_REGEX false */
2111 main (int argc
, char *argv
[])
2113 g_print ("GRegex is disabled.\n");
2117 #endif /* ENABLE_REGEX */