5e5b88dd143021733763266407633c311c505955
[platform/upstream/glib.git] / glib / tests / regex.c
1 /*
2  * Copyright (C) 2005 - 2006, Marco Barisione <marco@barisione.org>
3  * Copyright (C) 2010 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #undef G_DISABLE_ASSERT
20 #undef G_LOG_DOMAIN
21
22 #include "config.h"
23
24 #include <string.h>
25 #include <locale.h>
26 #include "glib.h"
27
28 #ifdef USE_SYSTEM_PCRE
29 #include <pcre.h>
30 #else
31 #include "glib/pcre/pcre.h"
32 #endif
33
34 /* U+20AC EURO SIGN (symbol, currency) */
35 #define EURO "\xe2\x82\xac"
36 /* U+00E0 LATIN SMALL LETTER A WITH GRAVE (letter, lowercase) */
37 #define AGRAVE "\xc3\xa0"
38 /* U+00C0 LATIN CAPITAL LETTER A WITH GRAVE (letter, uppercase) */
39 #define AGRAVE_UPPER "\xc3\x80"
40 /* U+00E8 LATIN SMALL LETTER E WITH GRAVE (letter, lowercase) */
41 #define EGRAVE "\xc3\xa8"
42 /* U+00F2 LATIN SMALL LETTER O WITH GRAVE (letter, lowercase) */
43 #define OGRAVE "\xc3\xb2"
44 /* U+014B LATIN SMALL LETTER ENG (letter, lowercase) */
45 #define ENG "\xc5\x8b"
46 /* U+0127 LATIN SMALL LETTER H WITH STROKE (letter, lowercase) */
47 #define HSTROKE "\xc4\xa7"
48 /* U+0634 ARABIC LETTER SHEEN (letter, other) */
49 #define SHEEN "\xd8\xb4"
50 /* U+1374 ETHIOPIC NUMBER THIRTY (number, other) */
51 #define ETH30 "\xe1\x8d\xb4"
52
53 /* A random value use to mark untouched integer variables. */
54 #define UNTOUCHED -559038737
55
56 static gint total;
57
58 typedef struct {
59   const gchar *pattern;
60   GRegexCompileFlags compile_opts;
61   GRegexMatchFlags   match_opts;
62   gint expected_error;
63   gboolean check_flags;
64   GRegexCompileFlags real_compile_opts;
65   GRegexMatchFlags real_match_opts;
66 } TestNewData;
67
68 static void
69 test_new (gconstpointer d)
70 {
71   const TestNewData *data = d;
72   GRegex *regex;
73   GError *error = NULL;
74
75   regex = g_regex_new (data->pattern, data->compile_opts, data->match_opts, &error);
76   g_assert (regex != NULL);
77   g_assert_no_error (error);
78   g_assert_cmpstr (data->pattern, ==, g_regex_get_pattern (regex));
79
80   if (data->check_flags)
81     {
82       g_assert_cmphex (g_regex_get_compile_flags (regex), ==, data->real_compile_opts);
83       g_assert_cmphex (g_regex_get_match_flags (regex), ==, data->real_match_opts);
84     }
85
86   g_regex_unref (regex);
87 }
88
89 #define TEST_NEW(_pattern, _compile_opts, _match_opts) {    \
90   TestNewData *data;                                        \
91   gchar *path;                                              \
92   data = g_new0 (TestNewData, 1);                           \
93   data->pattern = _pattern;                                 \
94   data->compile_opts = _compile_opts;                       \
95   data->match_opts = _match_opts;                           \
96   data->expected_error = 0;                                 \
97   data->check_flags = FALSE;                                \
98   path = g_strdup_printf ("/regex/new/%d", ++total);        \
99   g_test_add_data_func_full (path, data, test_new, g_free); \
100   g_free (path);                                            \
101 }
102
103 #define TEST_NEW_CHECK_FLAGS(_pattern, _compile_opts, _match_opts, _real_compile_opts, _real_match_opts) { \
104   TestNewData *data;                                             \
105   gchar *path;                                                   \
106   data = g_new0 (TestNewData, 1);                                \
107   data->pattern = _pattern;                                      \
108   data->compile_opts = _compile_opts;                            \
109   data->match_opts = 0;                                          \
110   data->expected_error = 0;                                      \
111   data->check_flags = TRUE;                                      \
112   data->real_compile_opts = _real_compile_opts;                  \
113   data->real_match_opts = _real_match_opts;                      \
114   path = g_strdup_printf ("/regex/new-check-flags/%d", ++total); \
115   g_test_add_data_func_full (path, data, test_new, g_free);      \
116   g_free (path);                                                 \
117 }
118
119 static void
120 test_new_fail (gconstpointer d)
121 {
122   const TestNewData *data = d;
123   GRegex *regex;
124   GError *error = NULL;
125
126   regex = g_regex_new (data->pattern, data->compile_opts, data->match_opts, &error);
127
128   g_assert (regex == NULL);
129   g_assert_error (error, G_REGEX_ERROR, data->expected_error);
130   g_error_free (error);
131 }
132
133 #define TEST_NEW_FAIL(_pattern, _compile_opts, _expected_error) { \
134   TestNewData *data;                                              \
135   gchar *path;                                                    \
136   data = g_new0 (TestNewData, 1);                                 \
137   data->pattern = _pattern;                                       \
138   data->compile_opts = _compile_opts;                             \
139   data->match_opts = 0;                                           \
140   data->expected_error = _expected_error;                         \
141   path = g_strdup_printf ("/regex/new-fail/%d", ++total);         \
142   g_test_add_data_func_full (path, data, test_new_fail, g_free);  \
143   g_free (path);                                                  \
144 }
145
146 typedef struct {
147   const gchar *pattern;
148   const gchar *string;
149   GRegexCompileFlags compile_opts;
150   GRegexMatchFlags match_opts;
151   gboolean expected;
152   gssize string_len;
153   gint start_position;
154   GRegexMatchFlags match_opts2;
155 } TestMatchData;
156
157 static void
158 test_match_simple (gconstpointer d)
159 {
160   const TestMatchData *data = d;
161   gboolean match;
162
163   match = g_regex_match_simple (data->pattern, data->string, data->compile_opts, data->match_opts);
164   g_assert_cmpint (match, ==, data->expected);
165 }
166
167 #define TEST_MATCH_SIMPLE_NAMED(_name, _pattern, _string, _compile_opts, _match_opts, _expected) { \
168   TestMatchData *data;                                                  \
169   gchar *path;                                                          \
170   data = g_new0 (TestMatchData, 1);                                     \
171   data->pattern = _pattern;                                             \
172   data->string = _string;                                               \
173   data->compile_opts = _compile_opts;                                   \
174   data->match_opts = _match_opts;                                       \
175   data->expected = _expected;                                           \
176   path = g_strdup_printf ("/regex/match-%s/%d", _name, ++total);        \
177   g_test_add_data_func_full (path, data, test_match_simple, g_free);    \
178   g_free (path);                                                        \
179 }
180
181 #define TEST_MATCH_SIMPLE(_pattern, _string, _compile_opts, _match_opts, _expected) \
182   TEST_MATCH_SIMPLE_NAMED("simple", _pattern, _string, _compile_opts, _match_opts, _expected)
183 #define TEST_MATCH_NOTEMPTY(_pattern, _string, _expected) \
184   TEST_MATCH_SIMPLE_NAMED("notempty", _pattern, _string, 0, G_REGEX_MATCH_NOTEMPTY, _expected)
185 #define TEST_MATCH_NOTEMPTY_ATSTART(_pattern, _string, _expected) \
186   TEST_MATCH_SIMPLE_NAMED("notempty-atstart", _pattern, _string, 0, G_REGEX_MATCH_NOTEMPTY_ATSTART, _expected)
187
188 static void
189 test_match (gconstpointer d)
190 {
191   const TestMatchData *data = d;
192   GRegex *regex;
193   gboolean match;
194   GError *error = NULL;
195
196   regex = g_regex_new (data->pattern, data->compile_opts, data->match_opts, &error);
197   g_assert (regex != NULL);
198   g_assert_no_error (error);
199
200   match = g_regex_match_full (regex, data->string, data->string_len,
201                               data->start_position, data->match_opts2, NULL, NULL);
202
203   if (data->expected)
204     {
205       if (!match)
206         g_error ("Regex '%s' (with compile options %u and "
207             "match options %u) should have matched '%.*s' "
208             "(of length %d, at position %d, with match options %u) but did not",
209             data->pattern, data->compile_opts, data->match_opts,
210             data->string_len == -1 ? (int) strlen (data->string) :
211               (int) data->string_len,
212             data->string, (int) data->string_len,
213             data->start_position, data->match_opts2);
214
215       g_assert_cmpint (match, ==, TRUE);
216     }
217   else
218     {
219       if (match)
220         g_error ("Regex '%s' (with compile options %u and "
221             "match options %u) should not have matched '%.*s' "
222             "(of length %d, at position %d, with match options %u) but did",
223             data->pattern, data->compile_opts, data->match_opts,
224             data->string_len == -1 ? (int) strlen (data->string) :
225               (int) data->string_len,
226             data->string, (int) data->string_len,
227             data->start_position, data->match_opts2);
228     }
229
230   if (data->string_len == -1 && data->start_position == 0)
231     {
232       match = g_regex_match (regex, data->string, data->match_opts2, NULL);
233       g_assert_cmpint (match, ==, data->expected);
234     }
235
236   g_regex_unref (regex);
237 }
238
239 #define TEST_MATCH(_pattern, _compile_opts, _match_opts, _string, \
240                    _string_len, _start_position, _match_opts2, _expected) { \
241   TestMatchData *data;                                                  \
242   gchar *path;                                                          \
243   data = g_new0 (TestMatchData, 1);                                     \
244   data->pattern = _pattern;                                             \
245   data->compile_opts = _compile_opts;                                   \
246   data->match_opts = _match_opts;                                       \
247   data->string = _string;                                               \
248   data->string_len = _string_len;                                       \
249   data->start_position = _start_position;                               \
250   data->match_opts2 = _match_opts2;                                     \
251   data->expected = _expected;                                           \
252   path = g_strdup_printf ("/regex/match/%d", ++total);                  \
253   g_test_add_data_func_full (path, data, test_match, g_free);           \
254   g_free (path);                                                        \
255 }
256
257 struct _Match
258 {
259   gchar *string;
260   gint start, end;
261 };
262 typedef struct _Match Match;
263
264 static void
265 free_match (gpointer data)
266 {
267   Match *match = data;
268   if (match == NULL)
269     return;
270   g_free (match->string);
271   g_free (match);
272 }
273
274 typedef struct {
275   const gchar *pattern;
276   const gchar *string;
277   gssize string_len;
278   gint start_position;
279   GSList *expected;
280 } TestMatchNextData;
281
282 static void
283 test_match_next (gconstpointer d)
284 {
285    const TestMatchNextData *data = d;
286   GRegex *regex;
287   GMatchInfo *match_info;
288   GSList *matches;
289   GSList *l_exp, *l_match;
290
291   regex = g_regex_new (data->pattern, 0, 0, NULL);
292
293   g_assert (regex != NULL);
294
295   g_regex_match_full (regex, data->string, data->string_len,
296                       data->start_position, 0, &match_info, NULL);
297   matches = NULL;
298   while (g_match_info_matches (match_info))
299     {
300       Match *match = g_new0 (Match, 1);
301       match->string = g_match_info_fetch (match_info, 0);
302       match->start = UNTOUCHED;
303       match->end = UNTOUCHED;
304       g_match_info_fetch_pos (match_info, 0, &match->start, &match->end);
305       matches = g_slist_prepend (matches, match);
306       g_match_info_next (match_info, NULL);
307     }
308   g_assert (regex == g_match_info_get_regex (match_info));
309   g_assert_cmpstr (data->string, ==, g_match_info_get_string (match_info));
310   g_match_info_free (match_info);
311   matches = g_slist_reverse (matches);
312
313   g_assert_cmpint (g_slist_length (matches), ==, g_slist_length (data->expected));
314
315   l_exp = data->expected;
316   l_match = matches;
317   while (l_exp != NULL)
318     {
319       Match *exp = l_exp->data;
320       Match *match = l_match->data;
321
322       g_assert_cmpstr (exp->string, ==, match->string);
323       g_assert_cmpint (exp->start, ==, match->start);
324       g_assert_cmpint (exp->end, ==, match->end);
325
326       l_exp = g_slist_next (l_exp);
327       l_match = g_slist_next (l_match);
328     }
329
330   g_regex_unref (regex);
331   g_slist_free_full (matches, free_match);
332 }
333
334 static void
335 free_match_next_data (gpointer _data)
336 {
337   TestMatchNextData *data = _data;
338
339   g_slist_free_full (data->expected, g_free);
340   g_free (data);
341 }
342
343 #define TEST_MATCH_NEXT0(_pattern, _string, _string_len, _start_position) { \
344   TestMatchNextData *data;                                              \
345   gchar *path;                                                          \
346   data = g_new0 (TestMatchNextData, 1);                                 \
347   data->pattern = _pattern;                                             \
348   data->string = _string;                                               \
349   data->string_len = _string_len;                                       \
350   data->start_position = _start_position;                               \
351   data->expected = NULL;                                                \
352   path = g_strdup_printf ("/regex/match/next0/%d", ++total);            \
353   g_test_add_data_func_full (path, data, test_match_next, free_match_next_data); \
354   g_free (path);                                                        \
355 }
356
357 #define TEST_MATCH_NEXT1(_pattern, _string, _string_len, _start_position,   \
358                          t1, s1, e1) {                                  \
359   TestMatchNextData *data;                                              \
360   Match *match;                                                         \
361   gchar *path;                                                          \
362   data = g_new0 (TestMatchNextData, 1);                                 \
363   data->pattern = _pattern;                                             \
364   data->string = _string;                                               \
365   data->string_len = _string_len;                                       \
366   data->start_position = _start_position;                               \
367   match = g_new0 (Match, 1);                                            \
368   match->string = t1;                                                   \
369   match->start = s1;                                                    \
370   match->end = e1;                                                      \
371   data->expected = g_slist_append (NULL, match);                        \
372   path = g_strdup_printf ("/regex/match/next1/%d", ++total);            \
373   g_test_add_data_func_full (path, data, test_match_next, free_match_next_data); \
374   g_free (path);                                                        \
375 }
376
377 #define TEST_MATCH_NEXT2(_pattern, _string, _string_len, _start_position,   \
378                          t1, s1, e1, t2, s2, e2) {                      \
379   TestMatchNextData *data;                                              \
380   Match *match;                                                         \
381   gchar *path;                                                          \
382   data = g_new0 (TestMatchNextData, 1);                                 \
383   data->pattern = _pattern;                                             \
384   data->string = _string;                                               \
385   data->string_len = _string_len;                                       \
386   data->start_position = _start_position;                               \
387   match = g_new0 (Match, 1);                                            \
388   match->string = t1;                                                   \
389   match->start = s1;                                                    \
390   match->end = e1;                                                      \
391   data->expected = g_slist_append (NULL, match);                        \
392   match = g_new0 (Match, 1);                                            \
393   match->string = t2;                                                   \
394   match->start = s2;                                                    \
395   match->end = e2;                                                      \
396   data->expected = g_slist_append (data->expected, match);              \
397   path = g_strdup_printf ("/regex/match/next2/%d", ++total);            \
398   g_test_add_data_func_full (path, data, test_match_next, free_match_next_data); \
399   g_free (path);                                                        \
400 }
401
402 #define TEST_MATCH_NEXT3(_pattern, _string, _string_len, _start_position,   \
403                          t1, s1, e1, t2, s2, e2, t3, s3, e3) {          \
404   TestMatchNextData *data;                                              \
405   Match *match;                                                         \
406   gchar *path;                                                          \
407   data = g_new0 (TestMatchNextData, 1);                                 \
408   data->pattern = _pattern;                                             \
409   data->string = _string;                                               \
410   data->string_len = _string_len;                                       \
411   data->start_position = _start_position;                               \
412   match = g_new0 (Match, 1);                                            \
413   match->string = t1;                                                   \
414   match->start = s1;                                                    \
415   match->end = e1;                                                      \
416   data->expected = g_slist_append (NULL, match);                        \
417   match = g_new0 (Match, 1);                                            \
418   match->string = t2;                                                   \
419   match->start = s2;                                                    \
420   match->end = e2;                                                      \
421   data->expected = g_slist_append (data->expected, match);              \
422   match = g_new0 (Match, 1);                                            \
423   match->string = t3;                                                   \
424   match->start = s3;                                                    \
425   match->end = e3;                                                      \
426   data->expected = g_slist_append (data->expected, match);              \
427   path = g_strdup_printf ("/regex/match/next3/%d", ++total);            \
428   g_test_add_data_func_full (path, data, test_match_next, free_match_next_data); \
429   g_free (path);                                                        \
430 }
431
432 #define TEST_MATCH_NEXT4(_pattern, _string, _string_len, _start_position,   \
433                          t1, s1, e1, t2, s2, e2, t3, s3, e3, t4, s4, e4) { \
434   TestMatchNextData *data;                                              \
435   Match *match;                                                         \
436   gchar *path;                                                          \
437   data = g_new0 (TestMatchNextData, 1);                                 \
438   data->pattern = _pattern;                                             \
439   data->string = _string;                                               \
440   data->string_len = _string_len;                                       \
441   data->start_position = _start_position;                               \
442   match = g_new0 (Match, 1);                                            \
443   match->string = t1;                                                   \
444   match->start = s1;                                                    \
445   match->end = e1;                                                      \
446   data->expected = g_slist_append (NULL, match);                        \
447   match = g_new0 (Match, 1);                                            \
448   match->string = t2;                                                   \
449   match->start = s2;                                                    \
450   match->end = e2;                                                      \
451   data->expected = g_slist_append (data->expected, match);              \
452   match = g_new0 (Match, 1);                                            \
453   match->string = t3;                                                   \
454   match->start = s3;                                                    \
455   match->end = e3;                                                      \
456   data->expected = g_slist_append (data->expected, match);              \
457   match = g_new0 (Match, 1);                                            \
458   match->string = t4;                                                   \
459   match->start = s4;                                                    \
460   match->end = e4;                                                      \
461   data->expected = g_slist_append (data->expected, match);              \
462   path = g_strdup_printf ("/regex/match/next4/%d", ++total);            \
463   g_test_add_data_func_full (path, data, test_match_next, free_match_next_data); \
464   g_free (path);                                                        \
465 }
466
467 typedef struct {
468   const gchar *pattern;
469   const gchar *string;
470   gint start_position;
471   GRegexMatchFlags match_opts;
472   gint expected_count;
473 } TestMatchCountData;
474
475 static void
476 test_match_count (gconstpointer d)
477 {
478   const TestMatchCountData *data = d;
479   GRegex *regex;
480   GMatchInfo *match_info;
481   gint count;
482
483   regex = g_regex_new (data->pattern, 0, 0, NULL);
484
485   g_assert (regex != NULL);
486
487   g_regex_match_full (regex, data->string, -1, data->start_position,
488                       data->match_opts, &match_info, NULL);
489   count = g_match_info_get_match_count (match_info);
490
491   g_assert_cmpint (count, ==, data->expected_count);
492
493   g_match_info_ref (match_info);
494   g_match_info_unref (match_info);
495   g_match_info_unref (match_info);
496   g_regex_unref (regex);
497 }
498
499 #define TEST_MATCH_COUNT(_pattern, _string, _start_position, _match_opts, _expected_count) { \
500   TestMatchCountData *data;                                             \
501   gchar *path;                                                          \
502   data = g_new0 (TestMatchCountData, 1);                                \
503   data->pattern = _pattern;                                             \
504   data->string = _string;                                               \
505   data->start_position = _start_position;                               \
506   data->match_opts = _match_opts;                                       \
507   data->expected_count = _expected_count;                               \
508   path = g_strdup_printf ("/regex/match/count/%d", ++total);            \
509   g_test_add_data_func_full (path, data, test_match_count, g_free);     \
510   g_free (path);                                                        \
511 }
512
513 static void
514 test_partial (gconstpointer d)
515 {
516   const TestMatchData *data = d;
517   GRegex *regex;
518   GMatchInfo *match_info;
519
520   regex = g_regex_new (data->pattern, 0, 0, NULL);
521
522   g_assert (regex != NULL);
523
524   g_regex_match (regex, data->string, data->match_opts, &match_info);
525
526   g_assert_cmpint (data->expected, ==, g_match_info_is_partial_match (match_info));
527
528   if (data->expected)
529     {
530       g_assert (!g_match_info_fetch_pos (match_info, 0, NULL, NULL));
531       g_assert (!g_match_info_fetch_pos (match_info, 1, NULL, NULL));
532     }
533
534   g_match_info_free (match_info);
535   g_regex_unref (regex);
536 }
537
538 #define TEST_PARTIAL_FULL(_pattern, _string, _match_opts, _expected) { \
539   TestMatchData *data;                                          \
540   gchar *path;                                                  \
541   data = g_new0 (TestMatchData, 1);                             \
542   data->pattern = _pattern;                                     \
543   data->string = _string;                                       \
544   data->match_opts = _match_opts;                               \
545   data->expected = _expected;                                   \
546   path = g_strdup_printf ("/regex/match/partial/%d", ++total);  \
547   g_test_add_data_func_full (path, data, test_partial, g_free); \
548   g_free (path);                                                \
549 }
550
551 #define TEST_PARTIAL(_pattern, _string, _expected) TEST_PARTIAL_FULL(_pattern, _string, G_REGEX_MATCH_PARTIAL, _expected)
552
553 typedef struct {
554   const gchar *pattern;
555   const gchar *string;
556   gint         start_position;
557   gint         sub_n;
558   const gchar *expected_sub;
559   gint         expected_start;
560   gint         expected_end;
561 } TestSubData;
562
563 static void
564 test_sub_pattern (gconstpointer d)
565 {
566   const TestSubData *data = d;
567   GRegex *regex;
568   GMatchInfo *match_info;
569   gchar *sub_expr;
570   gint start = UNTOUCHED, end = UNTOUCHED;
571
572   regex = g_regex_new (data->pattern, 0, 0, NULL);
573
574   g_assert (regex != NULL);
575
576   g_regex_match_full (regex, data->string, -1, data->start_position, 0, &match_info, NULL);
577
578   sub_expr = g_match_info_fetch (match_info, data->sub_n);
579   g_assert_cmpstr (sub_expr, ==, data->expected_sub);
580   g_free (sub_expr);
581
582   g_match_info_fetch_pos (match_info, data->sub_n, &start, &end);
583   g_assert_cmpint (start, ==, data->expected_start);
584   g_assert_cmpint (end, ==, data->expected_end);
585
586   g_match_info_free (match_info);
587   g_regex_unref (regex);
588 }
589
590 #define TEST_SUB_PATTERN(_pattern, _string, _start_position, _sub_n, _expected_sub, \
591                          _expected_start, _expected_end) {                       \
592   TestSubData *data;                                                    \
593   gchar *path;                                                          \
594   data = g_new0 (TestSubData, 1);                                       \
595   data->pattern = _pattern;                                             \
596   data->string = _string;                                               \
597   data->start_position = _start_position;                               \
598   data->sub_n = _sub_n;                                                 \
599   data->expected_sub = _expected_sub;                                   \
600   data->expected_start = _expected_start;                               \
601   data->expected_end = _expected_end;                                   \
602   path = g_strdup_printf ("/regex/match/subpattern/%d", ++total);       \
603   g_test_add_data_func_full (path, data, test_sub_pattern, g_free);     \
604   g_free (path);                                                        \
605 }
606
607 typedef struct {
608   const gchar *pattern;
609   GRegexCompileFlags flags;
610   const gchar *string;
611   gint         start_position;
612   const gchar *sub_name;
613   const gchar *expected_sub;
614   gint         expected_start;
615   gint         expected_end;
616 } TestNamedSubData;
617
618 static void
619 test_named_sub_pattern (gconstpointer d)
620 {
621   const TestNamedSubData *data = d;
622   GRegex *regex;
623   GMatchInfo *match_info;
624   gint start = UNTOUCHED, end = UNTOUCHED;
625   gchar *sub_expr;
626
627   regex = g_regex_new (data->pattern, data->flags, 0, NULL);
628
629   g_assert (regex != NULL);
630
631   g_regex_match_full (regex, data->string, -1, data->start_position, 0, &match_info, NULL);
632   sub_expr = g_match_info_fetch_named (match_info, data->sub_name);
633   g_assert_cmpstr (sub_expr, ==, data->expected_sub);
634   g_free (sub_expr);
635
636   g_match_info_fetch_named_pos (match_info, data->sub_name, &start, &end);
637   g_assert_cmpint (start, ==, data->expected_start);
638   g_assert_cmpint (end, ==, data->expected_end);
639
640   g_match_info_free (match_info);
641   g_regex_unref (regex);
642 }
643
644 #define TEST_NAMED_SUB_PATTERN(_pattern, _string, _start_position, _sub_name, \
645                                _expected_sub, _expected_start, _expected_end) { \
646   TestNamedSubData *data;                                                 \
647   gchar *path;                                                            \
648   data = g_new0 (TestNamedSubData, 1);                                    \
649   data->pattern = _pattern;                                               \
650   data->string = _string;                                                 \
651   data->flags = 0;                                                        \
652   data->start_position = _start_position;                                 \
653   data->sub_name = _sub_name;                                             \
654   data->expected_sub = _expected_sub;                                     \
655   data->expected_start = _expected_start;                                 \
656   data->expected_end = _expected_end;                                     \
657   path = g_strdup_printf ("/regex/match/named/subpattern/%d", ++total);   \
658   g_test_add_data_func_full (path, data, test_named_sub_pattern, g_free); \
659   g_free (path);                                                          \
660 }
661
662 #define TEST_NAMED_SUB_PATTERN_DUPNAMES(_pattern, _string, _start_position, _sub_name, \
663                                         _expected_sub, _expected_start, _expected_end) { \
664   TestNamedSubData *data;                                                        \
665   gchar *path;                                                                   \
666   data = g_new0 (TestNamedSubData, 1);                                           \
667   data->pattern = _pattern;                                                      \
668   data->string = _string;                                                        \
669   data->flags = G_REGEX_DUPNAMES;                                                \
670   data->start_position = _start_position;                                        \
671   data->sub_name = _sub_name;                                                    \
672   data->expected_sub = _expected_sub;                                            \
673   data->expected_start = _expected_start;                                        \
674   data->expected_end = _expected_end;                                            \
675   path = g_strdup_printf ("/regex/match/subpattern/named/dupnames/%d", ++total); \
676   g_test_add_data_func_full (path, data, test_named_sub_pattern, g_free);        \
677   g_free (path);                                                                 \
678 }
679
680 typedef struct {
681   const gchar *pattern;
682   const gchar *string;
683   GSList *expected;
684   gint start_position;
685   gint max_tokens;
686 } TestFetchAllData;
687
688 static void
689 test_fetch_all (gconstpointer d)
690 {
691   const TestFetchAllData *data = d;
692   GRegex *regex;
693   GMatchInfo *match_info;
694   GSList *l_exp;
695   gchar **matches;
696   gint match_count;
697   gint i;
698
699   regex = g_regex_new (data->pattern, 0, 0, NULL);
700
701   g_assert (regex != NULL);
702
703   g_regex_match (regex, data->string, 0, &match_info);
704   matches = g_match_info_fetch_all (match_info);
705   if (matches)
706     match_count = g_strv_length (matches);
707   else
708     match_count = 0;
709
710   g_assert_cmpint (match_count, ==, g_slist_length (data->expected));
711
712   l_exp = data->expected;
713   for (i = 0; l_exp != NULL; i++, l_exp = g_slist_next (l_exp))
714     {
715       g_assert_cmpstr (l_exp->data, ==, matches[i]);
716     }
717
718   g_match_info_free (match_info);
719   g_regex_unref (regex);
720   g_strfreev (matches);
721 }
722
723 static void
724 free_fetch_all_data (gpointer _data)
725 {
726   TestFetchAllData *data = _data;
727
728   g_slist_free (data->expected);
729   g_free (data);
730 }
731
732 #define TEST_FETCH_ALL0(_pattern, _string) {                                   \
733   TestFetchAllData *data;                                                      \
734   gchar *path;                                                                 \
735   data = g_new0 (TestFetchAllData, 1);                                         \
736   data->pattern = _pattern;                                                    \
737   data->string = _string;                                                      \
738   data->expected = NULL;                                                       \
739   path = g_strdup_printf ("/regex/fetch-all0/%d", ++total);                    \
740   g_test_add_data_func_full (path, data, test_fetch_all, free_fetch_all_data); \
741   g_free (path);                                                               \
742 }
743
744 #define TEST_FETCH_ALL1(_pattern, _string, e1) {                               \
745   TestFetchAllData *data;                                                      \
746   gchar *path;                                                                 \
747   data = g_new0 (TestFetchAllData, 1);                                         \
748   data->pattern = _pattern;                                                    \
749   data->string = _string;                                                      \
750   data->expected = g_slist_append (NULL, e1);                                  \
751   path = g_strdup_printf ("/regex/fetch-all1/%d", ++total);                    \
752   g_test_add_data_func_full (path, data, test_fetch_all, free_fetch_all_data); \
753   g_free (path);                                                               \
754 }
755
756 #define TEST_FETCH_ALL2(_pattern, _string, e1, e2) {                           \
757   TestFetchAllData *data;                                                      \
758   gchar *path;                                                                 \
759   data = g_new0 (TestFetchAllData, 1);                                         \
760   data->pattern = _pattern;                                                    \
761   data->string = _string;                                                      \
762   data->expected = g_slist_append (NULL, e1);                                  \
763   data->expected = g_slist_append (data->expected, e2);                        \
764   path = g_strdup_printf ("/regex/fetch-all2/%d", ++total);                    \
765   g_test_add_data_func_full (path, data, test_fetch_all, free_fetch_all_data); \
766   g_free (path);                                                               \
767 }
768
769 #define TEST_FETCH_ALL3(_pattern, _string, e1, e2, e3) {                       \
770   TestFetchAllData *data;                                                      \
771   gchar *path;                                                                 \
772   data = g_new0 (TestFetchAllData, 1);                                         \
773   data->pattern = _pattern;                                                    \
774   data->string = _string;                                                      \
775   data->expected = g_slist_append (NULL, e1);                                  \
776   data->expected = g_slist_append (data->expected, e2);                        \
777   data->expected = g_slist_append (data->expected, e3);                        \
778   path = g_strdup_printf ("/regex/fetch-all3/%d", ++total);                    \
779   g_test_add_data_func_full (path, data, test_fetch_all, free_fetch_all_data); \
780   g_free (path);                                                               \
781 }
782
783 static void
784 test_split_simple (gconstpointer d)
785 {
786   const TestFetchAllData *data = d;
787   GSList *l_exp;
788   gchar **tokens;
789   gint token_count;
790   gint i;
791
792   tokens = g_regex_split_simple (data->pattern, data->string, 0, 0);
793   if (tokens)
794     token_count = g_strv_length (tokens);
795   else
796     token_count = 0;
797
798   g_assert_cmpint (token_count, ==, g_slist_length (data->expected));
799
800   l_exp = data->expected;
801   for (i = 0; l_exp != NULL; i++, l_exp = g_slist_next (l_exp))
802     {
803       g_assert_cmpstr (l_exp->data, ==, tokens[i]);
804     }
805
806   g_strfreev (tokens);
807 }
808
809 #define TEST_SPLIT_SIMPLE0(_pattern, _string) {                                   \
810   TestFetchAllData *data;                                                         \
811   gchar *path;                                                                    \
812   data = g_new0 (TestFetchAllData, 1);                                            \
813   data->pattern = _pattern;                                                       \
814   data->string = _string;                                                         \
815   data->expected = NULL;                                                          \
816   path = g_strdup_printf ("/regex/split/simple0/%d", ++total);                    \
817   g_test_add_data_func_full (path, data, test_split_simple, free_fetch_all_data); \
818   g_free (path);                                                                  \
819 }
820
821 #define TEST_SPLIT_SIMPLE1(_pattern, _string, e1) {                               \
822   TestFetchAllData *data;                                                         \
823   gchar *path;                                                                    \
824   data = g_new0 (TestFetchAllData, 1);                                            \
825   data->pattern = _pattern;                                                       \
826   data->string = _string;                                                         \
827   data->expected = g_slist_append (NULL, e1);                                     \
828   path = g_strdup_printf ("/regex/split/simple1/%d", ++total);                    \
829   g_test_add_data_func_full (path, data, test_split_simple, free_fetch_all_data); \
830   g_free (path);                                                                  \
831 }
832
833 #define TEST_SPLIT_SIMPLE2(_pattern, _string, e1, e2) {                           \
834   TestFetchAllData *data;                                                         \
835   gchar *path;                                                                    \
836   data = g_new0 (TestFetchAllData, 1);                                            \
837   data->pattern = _pattern;                                                       \
838   data->string = _string;                                                         \
839   data->expected = g_slist_append (NULL, e1);                                     \
840   data->expected = g_slist_append (data->expected, e2);                           \
841   path = g_strdup_printf ("/regex/split/simple2/%d", ++total);                    \
842   g_test_add_data_func_full (path, data, test_split_simple, free_fetch_all_data); \
843   g_free (path);                                                                  \
844 }
845
846 #define TEST_SPLIT_SIMPLE3(_pattern, _string, e1, e2, e3) {                       \
847   TestFetchAllData *data;                                                         \
848   gchar *path;                                                                    \
849   data = g_new0 (TestFetchAllData, 1);                                            \
850   data->pattern = _pattern;                                                       \
851   data->string = _string;                                                         \
852   data->expected = g_slist_append (NULL, e1);                                     \
853   data->expected = g_slist_append (data->expected, e2);                           \
854   data->expected = g_slist_append (data->expected, e3);                           \
855   path = g_strdup_printf ("/regex/split/simple3/%d", ++total);                    \
856   g_test_add_data_func_full (path, data, test_split_simple, free_fetch_all_data); \
857   g_free (path);                                                                  \
858 }
859
860 static void
861 test_split_full (gconstpointer d)
862 {
863   const TestFetchAllData *data = d;
864   GRegex *regex;
865   GSList *l_exp;
866   gchar **tokens;
867   gint token_count;
868   gint i;
869
870   regex = g_regex_new (data->pattern, 0, 0, NULL);
871
872   g_assert (regex != NULL);
873
874   tokens = g_regex_split_full (regex, data->string, -1, data->start_position,
875                                0, data->max_tokens, NULL);
876   if (tokens)
877     token_count = g_strv_length (tokens);
878   else
879     token_count = 0;
880
881   g_assert_cmpint (token_count, ==, g_slist_length (data->expected));
882
883   l_exp = data->expected;
884   for (i = 0; l_exp != NULL; i++, l_exp = g_slist_next (l_exp))
885     {
886       g_assert_cmpstr (l_exp->data, ==, tokens[i]);
887     }
888
889   g_regex_unref (regex);
890   g_strfreev (tokens);
891 }
892
893 static void
894 test_split (gconstpointer d)
895 {
896   const TestFetchAllData *data = d;
897   GRegex *regex;
898   GSList *l_exp;
899   gchar **tokens;
900   gint token_count;
901   gint i;
902
903   regex = g_regex_new (data->pattern, 0, 0, NULL);
904
905   g_assert (regex != NULL);
906
907   tokens = g_regex_split (regex, data->string, 0);
908   if (tokens)
909     token_count = g_strv_length (tokens);
910   else
911     token_count = 0;
912
913   g_assert_cmpint (token_count, ==, g_slist_length (data->expected));
914
915   l_exp = data->expected;
916   for (i = 0; l_exp != NULL; i++, l_exp = g_slist_next (l_exp))
917     {
918       g_assert_cmpstr (l_exp->data, ==, tokens[i]);
919     }
920
921   g_regex_unref (regex);
922   g_strfreev (tokens);
923 }
924
925 #define TEST_SPLIT0(_pattern, _string, _start_position, _max_tokens) {          \
926   TestFetchAllData *data;                                                       \
927   gchar *path;                                                                  \
928   data = g_new0 (TestFetchAllData, 1);                                          \
929   data->pattern = _pattern;                                                     \
930   data->string = _string;                                                       \
931   data->start_position = _start_position;                                       \
932   data->max_tokens = _max_tokens;                                               \
933   data->expected = NULL;                                                        \
934   if (_start_position == 0 && _max_tokens <= 0) {                               \
935     path = g_strdup_printf ("/regex/split0/%d", ++total);                       \
936     g_test_add_data_func (path, data, test_split);                              \
937     g_free (path);                                                              \
938   }                                                                             \
939   path = g_strdup_printf ("/regex/full-split0/%d", ++total);                    \
940   g_test_add_data_func_full (path, data, test_split_full, free_fetch_all_data); \
941   g_free (path);                                                                \
942 }
943
944 #define TEST_SPLIT1(_pattern, _string, _start_position, _max_tokens, e1) {      \
945   TestFetchAllData *data;                                                       \
946   gchar *path;                                                                  \
947   data = g_new0 (TestFetchAllData, 1);                                          \
948   data->pattern = _pattern;                                                     \
949   data->string = _string;                                                       \
950   data->start_position = _start_position;                                       \
951   data->max_tokens = _max_tokens;                                               \
952   data->expected = NULL;                                                        \
953   data->expected = g_slist_append (data->expected, e1);                         \
954   if (_start_position == 0 && _max_tokens <= 0) {                               \
955     path = g_strdup_printf ("/regex/split1/%d", ++total);                       \
956     g_test_add_data_func (path, data, test_split);                              \
957     g_free (path);                                                              \
958   }                                                                             \
959   path = g_strdup_printf ("/regex/full-split1/%d", ++total);                    \
960   g_test_add_data_func_full (path, data, test_split_full, free_fetch_all_data); \
961   g_free (path);                                                                \
962 }
963
964 #define TEST_SPLIT2(_pattern, _string, _start_position, _max_tokens, e1, e2) {  \
965   TestFetchAllData *data;                                                       \
966   gchar *path;                                                                  \
967   data = g_new0 (TestFetchAllData, 1);                                          \
968   data->pattern = _pattern;                                                     \
969   data->string = _string;                                                       \
970   data->start_position = _start_position;                                       \
971   data->max_tokens = _max_tokens;                                               \
972   data->expected = NULL;                                                        \
973   data->expected = g_slist_append (data->expected, e1);                         \
974   data->expected = g_slist_append (data->expected, e2);                         \
975   if (_start_position == 0 && _max_tokens <= 0) {                               \
976     path = g_strdup_printf ("/regex/split2/%d", ++total);                       \
977     g_test_add_data_func (path, data, test_split);                              \
978     g_free (path);                                                              \
979   }                                                                             \
980   path = g_strdup_printf ("/regex/full-split2/%d", ++total);                    \
981   g_test_add_data_func_full (path, data, test_split_full, free_fetch_all_data); \
982   g_free (path);                                                                \
983 }
984
985 #define TEST_SPLIT3(_pattern, _string, _start_position, _max_tokens, e1, e2, e3) { \
986   TestFetchAllData *data;                                                          \
987   gchar *path;                                                                     \
988   data = g_new0 (TestFetchAllData, 1);                                             \
989   data->pattern = _pattern;                                                        \
990   data->string = _string;                                                          \
991   data->start_position = _start_position;                                          \
992   data->max_tokens = _max_tokens;                                                  \
993   data->expected = NULL;                                                           \
994   data->expected = g_slist_append (data->expected, e1);                            \
995   data->expected = g_slist_append (data->expected, e2);                            \
996   data->expected = g_slist_append (data->expected, e3);                            \
997   if (_start_position == 0 && _max_tokens <= 0) {                                  \
998     path = g_strdup_printf ("/regex/split3/%d", ++total);                          \
999     g_test_add_data_func (path, data, test_split);                                 \
1000     g_free (path);                                                                 \
1001   }                                                                                \
1002   path = g_strdup_printf ("/regex/full-split3/%d", ++total);                       \
1003   g_test_add_data_func_full (path, data, test_split_full, free_fetch_all_data);    \
1004   g_free (path);                                                                   \
1005 }
1006
1007 typedef struct {
1008   const gchar *string_to_expand;
1009   gboolean expected;
1010   gboolean expected_refs;
1011 } TestCheckReplacementData;
1012
1013 static void
1014 test_check_replacement (gconstpointer d)
1015 {
1016   const TestCheckReplacementData *data = d;
1017   gboolean has_refs;
1018   gboolean result;
1019
1020   result = g_regex_check_replacement (data->string_to_expand, &has_refs, NULL);
1021   g_assert_cmpint (data->expected, ==, result);
1022
1023   if (data->expected)
1024     g_assert_cmpint (data->expected_refs, ==, has_refs);
1025 }
1026
1027 #define TEST_CHECK_REPLACEMENT(_string_to_expand, _expected, _expected_refs) { \
1028   TestCheckReplacementData *data;                                              \
1029   gchar *path;                                                                 \
1030   data = g_new0 (TestCheckReplacementData, 1);                                 \
1031   data->string_to_expand = _string_to_expand;                                  \
1032   data->expected = _expected;                                                  \
1033   data->expected_refs = _expected_refs;                                        \
1034   path = g_strdup_printf ("/regex/check-repacement/%d", ++total);              \
1035   g_test_add_data_func_full (path, data, test_check_replacement, g_free);      \
1036   g_free (path);                                                               \
1037 }
1038
1039 typedef struct {
1040   const gchar *pattern;
1041   const gchar *string;
1042   const gchar *string_to_expand;
1043   gboolean     raw;
1044   const gchar *expected;
1045 } TestExpandData;
1046
1047 static void
1048 test_expand (gconstpointer d)
1049 {
1050   const TestExpandData *data = d;
1051   GRegex *regex = NULL;
1052   GMatchInfo *match_info = NULL;
1053   gchar *res;
1054   GError *error = NULL;
1055
1056   if (data->pattern)
1057     {
1058       regex = g_regex_new (data->pattern, data->raw ? G_REGEX_RAW : 0, 0,
1059           &error);
1060       g_assert_no_error (error);
1061       g_regex_match (regex, data->string, 0, &match_info);
1062     }
1063
1064   res = g_match_info_expand_references (match_info, data->string_to_expand, NULL);
1065   g_assert_cmpstr (res, ==, data->expected);
1066   g_free (res);
1067   g_match_info_free (match_info);
1068   if (regex)
1069     g_regex_unref (regex);
1070 }
1071
1072 #define TEST_EXPAND(_pattern, _string, _string_to_expand, _raw, _expected) { \
1073   TestExpandData *data;                                                      \
1074   gchar *path;                                                               \
1075   data = g_new0 (TestExpandData, 1);                                         \
1076   data->pattern = _pattern;                                                  \
1077   data->string = _string;                                                    \
1078   data->string_to_expand = _string_to_expand;                                \
1079   data->raw = _raw;                                                          \
1080   data->expected = _expected;                                                \
1081   path = g_strdup_printf ("/regex/expand/%d", ++total);                      \
1082   g_test_add_data_func_full (path, data, test_expand, g_free);               \
1083   g_free (path);                                                             \
1084 }
1085
1086 typedef struct {
1087   const gchar *pattern;
1088   const gchar *string;
1089   gint         start_position;
1090   const gchar *replacement;
1091   const gchar *expected;
1092 } TestReplaceData;
1093
1094 static void
1095 test_replace (gconstpointer d)
1096 {
1097   const TestReplaceData *data = d;
1098   GRegex *regex;
1099   gchar *res;
1100
1101   regex = g_regex_new (data->pattern, 0, 0, NULL);
1102   res = g_regex_replace (regex, data->string, -1, data->start_position, data->replacement, 0, NULL);
1103
1104   g_assert_cmpstr (res, ==, data->expected);
1105
1106   g_free (res);
1107   g_regex_unref (regex);
1108 }
1109
1110 #define TEST_REPLACE(_pattern, _string, _start_position, _replacement, _expected) { \
1111   TestReplaceData *data;                                                \
1112   gchar *path;                                                          \
1113   data = g_new0 (TestReplaceData, 1);                                   \
1114   data->pattern = _pattern;                                             \
1115   data->string = _string;                                               \
1116   data->start_position = _start_position;                               \
1117   data->replacement = _replacement;                                     \
1118   data->expected = _expected;                                           \
1119   path = g_strdup_printf ("/regex/replace/%d", ++total);                \
1120   g_test_add_data_func_full (path, data, test_replace, g_free);         \
1121   g_free (path);                                                        \
1122 }
1123
1124 static void
1125 test_replace_lit (gconstpointer d)
1126 {
1127   const TestReplaceData *data = d;
1128   GRegex *regex;
1129   gchar *res;
1130
1131   regex = g_regex_new (data->pattern, 0, 0, NULL);
1132   res = g_regex_replace_literal (regex, data->string, -1, data->start_position,
1133                                  data->replacement, 0, NULL);
1134   g_assert_cmpstr (res, ==, data->expected);
1135
1136   g_free (res);
1137   g_regex_unref (regex);
1138 }
1139
1140 #define TEST_REPLACE_LIT(_pattern, _string, _start_position, _replacement, _expected) { \
1141   TestReplaceData *data;                                                \
1142   gchar *path;                                                          \
1143   data = g_new0 (TestReplaceData, 1);                                   \
1144   data->pattern = _pattern;                                             \
1145   data->string = _string;                                               \
1146   data->start_position = _start_position;                               \
1147   data->replacement = _replacement;                                     \
1148   data->expected = _expected;                                           \
1149   path = g_strdup_printf ("/regex/replace-literally/%d", ++total);      \
1150   g_test_add_data_func_full (path, data, test_replace_lit, g_free);     \
1151   g_free (path);                                                        \
1152 }
1153
1154 typedef struct {
1155   const gchar *pattern;
1156   const gchar *name;
1157   gint         expected_num;
1158 } TestStringNumData;
1159
1160 static void
1161 test_get_string_number (gconstpointer d)
1162 {
1163   const TestStringNumData *data = d;
1164   GRegex *regex;
1165   gint num;
1166
1167   regex = g_regex_new (data->pattern, 0, 0, NULL);
1168   num = g_regex_get_string_number (regex, data->name);
1169
1170   g_assert_cmpint (num, ==, data->expected_num);
1171   g_regex_unref (regex);
1172 }
1173
1174 #define TEST_GET_STRING_NUMBER(_pattern, _name, _expected_num) {          \
1175   TestStringNumData *data;                                                \
1176   gchar *path;                                                            \
1177   data = g_new0 (TestStringNumData, 1);                                   \
1178   data->pattern = _pattern;                                               \
1179   data->name = _name;                                                     \
1180   data->expected_num = _expected_num;                                     \
1181   path = g_strdup_printf ("/regex/string-number/%d", ++total);            \
1182   g_test_add_data_func_full (path, data, test_get_string_number, g_free); \
1183   g_free (path);                                                          \
1184 }
1185
1186 typedef struct {
1187   const gchar *string;
1188   gint         length;
1189   const gchar *expected;
1190 } TestEscapeData;
1191
1192 static void
1193 test_escape (gconstpointer d)
1194 {
1195   const TestEscapeData *data = d;
1196   gchar *escaped;
1197
1198   escaped = g_regex_escape_string (data->string, data->length);
1199
1200   g_assert_cmpstr (escaped, ==, data->expected);
1201
1202   g_free (escaped);
1203 }
1204
1205 #define TEST_ESCAPE(_string, _length, _expected) {             \
1206   TestEscapeData *data;                                        \
1207   gchar *path;                                                 \
1208   data = g_new0 (TestEscapeData, 1);                           \
1209   data->string = _string;                                      \
1210   data->length = _length;                                      \
1211   data->expected = _expected;                                  \
1212   path = g_strdup_printf ("/regex/escape/%d", ++total);        \
1213   g_test_add_data_func_full (path, data, test_escape, g_free); \
1214   g_free (path);                                               \
1215 }
1216
1217 static void
1218 test_escape_nul (gconstpointer d)
1219 {
1220   const TestEscapeData *data = d;
1221   gchar *escaped;
1222
1223   escaped = g_regex_escape_nul (data->string, data->length);
1224
1225   g_assert_cmpstr (escaped, ==, data->expected);
1226
1227   g_free (escaped);
1228 }
1229
1230 #define TEST_ESCAPE_NUL(_string, _length, _expected) {             \
1231   TestEscapeData *data;                                            \
1232   gchar *path;                                                     \
1233   data = g_new0 (TestEscapeData, 1);                               \
1234   data->string = _string;                                          \
1235   data->length = _length;                                          \
1236   data->expected = _expected;                                      \
1237   path = g_strdup_printf ("/regex/escape_nul/%d", ++total);        \
1238   g_test_add_data_func_full (path, data, test_escape_nul, g_free); \
1239   g_free (path);                                                   \
1240 }
1241
1242 typedef struct {
1243   const gchar *pattern;
1244   const gchar *string;
1245   gssize       string_len;
1246   gint         start_position;
1247   GSList *expected;
1248 } TestMatchAllData;
1249
1250 static void
1251 test_match_all_full (gconstpointer d)
1252 {
1253   const TestMatchAllData *data = d;
1254   GRegex *regex;
1255   GMatchInfo *match_info;
1256   GSList *l_exp;
1257   gboolean match_ok;
1258   gint match_count;
1259   gint i;
1260
1261   regex = g_regex_new (data->pattern, 0, 0, NULL);
1262   match_ok = g_regex_match_all_full (regex, data->string, data->string_len, data->start_position,
1263                                      0, &match_info, NULL);
1264
1265   if (g_slist_length (data->expected) == 0)
1266     g_assert (!match_ok);
1267   else
1268     g_assert (match_ok);
1269
1270   match_count = g_match_info_get_match_count (match_info);
1271   g_assert_cmpint (match_count, ==, g_slist_length (data->expected));
1272
1273   l_exp = data->expected;
1274   for (i = 0; i < match_count; i++)
1275     {
1276       gint start, end;
1277       gchar *matched_string;
1278       Match *exp = l_exp->data;
1279
1280       matched_string = g_match_info_fetch (match_info, i);
1281       g_match_info_fetch_pos (match_info, i, &start, &end);
1282
1283       g_assert_cmpstr (exp->string, ==, matched_string);
1284       g_assert_cmpint (exp->start, ==, start);
1285       g_assert_cmpint (exp->end, ==, end);
1286
1287       g_free (matched_string);
1288
1289       l_exp = g_slist_next (l_exp);
1290     }
1291
1292   g_match_info_free (match_info);
1293   g_regex_unref (regex);
1294 }
1295
1296 static void
1297 test_match_all (gconstpointer d)
1298 {
1299   const TestMatchAllData *data = d;
1300   GRegex *regex;
1301   GMatchInfo *match_info;
1302   GSList *l_exp;
1303   gboolean match_ok;
1304   gint match_count;
1305   gint i;
1306
1307   regex = g_regex_new (data->pattern, 0, 0, NULL);
1308   match_ok = g_regex_match_all (regex, data->string, 0, &match_info);
1309
1310   if (g_slist_length (data->expected) == 0)
1311     g_assert (!match_ok);
1312   else
1313     g_assert (match_ok);
1314
1315   match_count = g_match_info_get_match_count (match_info);
1316
1317   if (match_count != g_slist_length (data->expected))
1318     {
1319       g_message ("regex: %s", data->pattern);
1320       g_message ("string: %s", data->string);
1321       g_message ("matched strings:");
1322
1323       for (i = 0; i < match_count; i++)
1324         {
1325           gint start, end;
1326           gchar *matched_string;
1327
1328           matched_string = g_match_info_fetch (match_info, i);
1329           g_match_info_fetch_pos (match_info, i, &start, &end);
1330           g_message ("%d. %d-%d '%s'", i, start, end, matched_string);
1331           g_free (matched_string);
1332         }
1333
1334       g_message ("expected strings:");
1335       i = 0;
1336
1337       for (l_exp = data->expected; l_exp != NULL; l_exp = l_exp->next)
1338         {
1339           Match *exp = l_exp->data;
1340
1341           g_message ("%d. %d-%d '%s'", i, exp->start, exp->end, exp->string);
1342           i++;
1343         }
1344
1345       g_error ("match_count not as expected: %d != %d",
1346           match_count, g_slist_length (data->expected));
1347     }
1348
1349   l_exp = data->expected;
1350   for (i = 0; i < match_count; i++)
1351     {
1352       gint start, end;
1353       gchar *matched_string;
1354       Match *exp = l_exp->data;
1355
1356       matched_string = g_match_info_fetch (match_info, i);
1357       g_match_info_fetch_pos (match_info, i, &start, &end);
1358
1359       g_assert_cmpstr (exp->string, ==, matched_string);
1360       g_assert_cmpint (exp->start, ==, start);
1361       g_assert_cmpint (exp->end, ==, end);
1362
1363       g_free (matched_string);
1364
1365       l_exp = g_slist_next (l_exp);
1366     }
1367
1368   g_match_info_free (match_info);
1369   g_regex_unref (regex);
1370 }
1371
1372 static void
1373 free_match_all_data (gpointer _data)
1374 {
1375   TestMatchAllData *data = _data;
1376
1377   g_slist_free_full (data->expected, g_free);
1378   g_free (data);
1379 }
1380
1381 #define TEST_MATCH_ALL0(_pattern, _string, _string_len, _start_position) {          \
1382   TestMatchAllData *data;                                                           \
1383   gchar *path;                                                                      \
1384   data = g_new0 (TestMatchAllData, 1);                                              \
1385   data->pattern = _pattern;                                                         \
1386   data->string = _string;                                                           \
1387   data->string_len = _string_len;                                                   \
1388   data->start_position = _start_position;                                           \
1389   data->expected = NULL;                                                            \
1390   if (_string_len == -1 && _start_position == 0) {                                  \
1391     path = g_strdup_printf ("/regex/match-all0/%d", ++total);                       \
1392     g_test_add_data_func (path, data, test_match_all);                              \
1393     g_free (path);                                                                  \
1394   }                                                                                 \
1395   path = g_strdup_printf ("/regex/match-all-full0/%d", ++total);                    \
1396   g_test_add_data_func_full (path, data, test_match_all_full, free_match_all_data); \
1397   g_free (path);                                                                    \
1398 }
1399
1400 #define TEST_MATCH_ALL1(_pattern, _string, _string_len, _start_position,            \
1401                         t1, s1, e1) {                                               \
1402   TestMatchAllData *data;                                                           \
1403   Match *match;                                                                     \
1404   gchar *path;                                                                      \
1405   data = g_new0 (TestMatchAllData, 1);                                              \
1406   data->pattern = _pattern;                                                         \
1407   data->string = _string;                                                           \
1408   data->string_len = _string_len;                                                   \
1409   data->start_position = _start_position;                                           \
1410   data->expected = NULL;                                                            \
1411   match = g_new0 (Match, 1);                                                        \
1412   match->string = t1;                                                               \
1413   match->start = s1;                                                                \
1414   match->end = e1;                                                                  \
1415   data->expected = g_slist_append (data->expected, match);                          \
1416   if (_string_len == -1 && _start_position == 0) {                                  \
1417     path = g_strdup_printf ("/regex/match-all1/%d", ++total);                       \
1418     g_test_add_data_func (path, data, test_match_all);                              \
1419     g_free (path);                                                                  \
1420   }                                                                                 \
1421   path = g_strdup_printf ("/regex/match-all-full1/%d", ++total);                    \
1422   g_test_add_data_func_full (path, data, test_match_all_full, free_match_all_data); \
1423   g_free (path);                                                                    \
1424 }
1425
1426 #define TEST_MATCH_ALL2(_pattern, _string, _string_len, _start_position,            \
1427                         t1, s1, e1, t2, s2, e2) {                                   \
1428   TestMatchAllData *data;                                                           \
1429   Match *match;                                                                     \
1430   gchar *path;                                                                      \
1431   data = g_new0 (TestMatchAllData, 1);                                              \
1432   data->pattern = _pattern;                                                         \
1433   data->string = _string;                                                           \
1434   data->string_len = _string_len;                                                   \
1435   data->start_position = _start_position;                                           \
1436   data->expected = NULL;                                                            \
1437   match = g_new0 (Match, 1);                                                        \
1438   match->string = t1;                                                               \
1439   match->start = s1;                                                                \
1440   match->end = e1;                                                                  \
1441   data->expected = g_slist_append (data->expected, match);                          \
1442   match = g_new0 (Match, 1);                                                        \
1443   match->string = t2;                                                               \
1444   match->start = s2;                                                                \
1445   match->end = e2;                                                                  \
1446   data->expected = g_slist_append (data->expected, match);                          \
1447   if (_string_len == -1 && _start_position == 0) {                                  \
1448     path = g_strdup_printf ("/regex/match-all2/%d", ++total);                       \
1449     g_test_add_data_func (path, data, test_match_all);                              \
1450     g_free (path);                                                                  \
1451   }                                                                                 \
1452   path = g_strdup_printf ("/regex/match-all-full2/%d", ++total);                    \
1453   g_test_add_data_func_full (path, data, test_match_all_full, free_match_all_data); \
1454   g_free (path);                                                                    \
1455 }
1456
1457 #define TEST_MATCH_ALL3(_pattern, _string, _string_len, _start_position,            \
1458                         t1, s1, e1, t2, s2, e2, t3, s3, e3) {                       \
1459   TestMatchAllData *data;                                                           \
1460   Match *match;                                                                     \
1461   gchar *path;                                                                      \
1462   data = g_new0 (TestMatchAllData, 1);                                              \
1463   data->pattern = _pattern;                                                         \
1464   data->string = _string;                                                           \
1465   data->string_len = _string_len;                                                   \
1466   data->start_position = _start_position;                                           \
1467   data->expected = NULL;                                                            \
1468   match = g_new0 (Match, 1);                                                        \
1469   match->string = t1;                                                               \
1470   match->start = s1;                                                                \
1471   match->end = e1;                                                                  \
1472   data->expected = g_slist_append (data->expected, match);                          \
1473   match = g_new0 (Match, 1);                                                        \
1474   match->string = t2;                                                               \
1475   match->start = s2;                                                                \
1476   match->end = e2;                                                                  \
1477   data->expected = g_slist_append (data->expected, match);                          \
1478   match = g_new0 (Match, 1);                                                        \
1479   match->string = t3;                                                               \
1480   match->start = s3;                                                                \
1481   match->end = e3;                                                                  \
1482   data->expected = g_slist_append (data->expected, match);                          \
1483   if (_string_len == -1 && _start_position == 0) {                                  \
1484     path = g_strdup_printf ("/regex/match-all3/%d", ++total);                       \
1485     g_test_add_data_func (path, data, test_match_all);                              \
1486     g_free (path);                                                                  \
1487   }                                                                                 \
1488   path = g_strdup_printf ("/regex/match-all-full3/%d", ++total);                    \
1489   g_test_add_data_func_full (path, data, test_match_all_full, free_match_all_data); \
1490   g_free (path);                                                                    \
1491 }
1492
1493 static void
1494 test_properties (void)
1495 {
1496   GRegex *regex;
1497   GError *error;
1498   gboolean res;
1499   GMatchInfo *match;
1500   gchar *str;
1501
1502   error = NULL;
1503   regex = g_regex_new ("\\p{L}\\p{Ll}\\p{Lu}\\p{L&}\\p{N}\\p{Nd}", G_REGEX_OPTIMIZE, 0, &error);
1504   res = g_regex_match (regex, "ppPP01", 0, &match);
1505   g_assert (res);
1506   str = g_match_info_fetch (match, 0);
1507   g_assert_cmpstr (str, ==, "ppPP01");
1508   g_free (str);
1509
1510   g_match_info_free (match);
1511   g_regex_unref (regex);
1512 }
1513
1514 static void
1515 test_class (void)
1516 {
1517   GRegex *regex;
1518   GError *error;
1519   GMatchInfo *match;
1520   gboolean res;
1521   gchar *str;
1522
1523   error = NULL;
1524   regex = g_regex_new ("[abc\\x{0B1E}\\p{Mn}\\x{0391}-\\x{03A9}]", G_REGEX_OPTIMIZE, 0, &error);
1525   res = g_regex_match (regex, "a:b:\340\254\236:\333\253:\316\240", 0, &match);
1526   g_assert (res);
1527   str = g_match_info_fetch (match, 0);
1528   g_assert_cmpstr (str, ==, "a");
1529   g_free (str);
1530   res = g_match_info_next (match, NULL);
1531   g_assert (res);
1532   str = g_match_info_fetch (match, 0);
1533   g_assert_cmpstr (str, ==, "b");
1534   g_free (str);
1535   res = g_match_info_next (match, NULL);
1536   g_assert (res);
1537   str = g_match_info_fetch (match, 0);
1538   g_assert_cmpstr (str, ==, "\340\254\236");
1539   g_free (str);
1540   res = g_match_info_next (match, NULL);
1541   g_assert (res);
1542   str = g_match_info_fetch (match, 0);
1543   g_assert_cmpstr (str, ==, "\333\253");
1544   g_free (str);
1545   res = g_match_info_next (match, NULL);
1546   g_assert (res);
1547   str = g_match_info_fetch (match, 0);
1548   g_assert_cmpstr (str, ==, "\316\240");
1549   g_free (str);
1550
1551   res = g_match_info_next (match, NULL);
1552   g_assert (!res);
1553
1554   g_match_info_free (match);
1555   g_regex_unref (regex);
1556 }
1557
1558 /* examples for lookahead assertions taken from pcrepattern(3) */
1559 static void
1560 test_lookahead (void)
1561 {
1562   GRegex *regex;
1563   GError *error;
1564   GMatchInfo *match;
1565   gboolean res;
1566   gchar *str;
1567   gint start, end;
1568
1569   error = NULL;
1570   regex = g_regex_new ("\\w+(?=;)", G_REGEX_OPTIMIZE, 0, &error);
1571   g_assert (regex);
1572   g_assert_no_error (error);
1573   res = g_regex_match (regex, "word1 word2: word3;", 0, &match);
1574   g_assert (res);
1575   g_assert (g_match_info_matches (match));
1576   g_assert_cmpint (g_match_info_get_match_count (match), ==, 1);
1577   str = g_match_info_fetch (match, 0);
1578   g_assert_cmpstr (str, ==, "word3");
1579   g_free (str);
1580   g_match_info_free (match);
1581   g_regex_unref (regex);
1582
1583   error = NULL;
1584   regex = g_regex_new ("foo(?!bar)", G_REGEX_OPTIMIZE, 0, &error);
1585   g_assert (regex);
1586   g_assert_no_error (error);
1587   res = g_regex_match (regex, "foobar foobaz", 0, &match);
1588   g_assert (res);
1589   g_assert (g_match_info_matches (match));
1590   g_assert_cmpint (g_match_info_get_match_count (match), ==, 1);
1591   res = g_match_info_fetch_pos (match, 0, &start, &end);
1592   g_assert (res);
1593   g_assert_cmpint (start, ==, 7);
1594   g_assert_cmpint (end, ==, 10);
1595   g_match_info_free (match);
1596   g_regex_unref (regex);
1597
1598   error = NULL;
1599   regex = g_regex_new ("(?!bar)foo", G_REGEX_OPTIMIZE, 0, &error);
1600   g_assert (regex);
1601   g_assert_no_error (error);
1602   res = g_regex_match (regex, "foobar foobaz", 0, &match);
1603   g_assert (res);
1604   g_assert (g_match_info_matches (match));
1605   g_assert_cmpint (g_match_info_get_match_count (match), ==, 1);
1606   res = g_match_info_fetch_pos (match, 0, &start, &end);
1607   g_assert (res);
1608   g_assert_cmpint (start, ==, 0);
1609   g_assert_cmpint (end, ==, 3);
1610   res = g_match_info_next (match, &error);
1611   g_assert (res);
1612   g_assert_no_error (error);
1613   res = g_match_info_fetch_pos (match, 0, &start, &end);
1614   g_assert (res);
1615   g_assert_cmpint (start, ==, 7);
1616   g_assert_cmpint (end, ==, 10);
1617   g_match_info_free (match);
1618   g_regex_unref (regex);
1619 }
1620
1621 /* examples for lookbehind assertions taken from pcrepattern(3) */
1622 static void
1623 test_lookbehind (void)
1624 {
1625   GRegex *regex;
1626   GError *error;
1627   GMatchInfo *match;
1628   gboolean res;
1629   gint start, end;
1630
1631   error = NULL;
1632   regex = g_regex_new ("(?<!foo)bar", G_REGEX_OPTIMIZE, 0, &error);
1633   g_assert (regex);
1634   g_assert_no_error (error);
1635   res = g_regex_match (regex, "foobar boobar", 0, &match);
1636   g_assert (res);
1637   g_assert (g_match_info_matches (match));
1638   g_assert_cmpint (g_match_info_get_match_count (match), ==, 1);
1639   res = g_match_info_fetch_pos (match, 0, &start, &end);
1640   g_assert (res);
1641   g_assert_cmpint (start, ==, 10);
1642   g_assert_cmpint (end, ==, 13);
1643   g_match_info_free (match);
1644   g_regex_unref (regex);
1645
1646   error = NULL;
1647   regex = g_regex_new ("(?<=bullock|donkey) poo", G_REGEX_OPTIMIZE, 0, &error);
1648   g_assert (regex);
1649   g_assert_no_error (error);
1650   res = g_regex_match (regex, "don poo, and bullock poo", 0, &match);
1651   g_assert (res);
1652   g_assert (g_match_info_matches (match));
1653   g_assert_cmpint (g_match_info_get_match_count (match), ==, 1);
1654   res = g_match_info_fetch_pos (match, 0, &start, NULL);
1655   g_assert (res);
1656   g_assert_cmpint (start, ==, 20);
1657   g_match_info_free (match);
1658   g_regex_unref (regex);
1659
1660   regex = g_regex_new ("(?<!dogs?|cats?) x", G_REGEX_OPTIMIZE, 0, &error);
1661   g_assert (regex == NULL);
1662   g_assert_error (error, G_REGEX_ERROR, G_REGEX_ERROR_VARIABLE_LENGTH_LOOKBEHIND);
1663   g_clear_error (&error);
1664
1665   regex = g_regex_new ("(?<=ab(c|de)) foo", G_REGEX_OPTIMIZE, 0, &error);
1666   g_assert (regex == NULL);
1667   g_assert_error (error, G_REGEX_ERROR, G_REGEX_ERROR_VARIABLE_LENGTH_LOOKBEHIND);
1668   g_clear_error (&error);
1669
1670   regex = g_regex_new ("(?<=abc|abde)foo", G_REGEX_OPTIMIZE, 0, &error);
1671   g_assert (regex);
1672   g_assert_no_error (error);
1673   res = g_regex_match (regex, "abfoo, abdfoo, abcfoo", 0, &match);
1674   g_assert (res);
1675   g_assert (g_match_info_matches (match));
1676   res = g_match_info_fetch_pos (match, 0, &start, NULL);
1677   g_assert (res);
1678   g_assert_cmpint (start, ==, 18);
1679   g_match_info_free (match);
1680   g_regex_unref (regex);
1681
1682   regex = g_regex_new ("^.*+(?<=abcd)", G_REGEX_OPTIMIZE, 0, &error);
1683   g_assert (regex);
1684   g_assert_no_error (error);
1685   res = g_regex_match (regex, "abcabcabcabcabcabcabcabcabcd", 0, &match);
1686   g_assert (res);
1687   g_assert (g_match_info_matches (match));
1688   g_match_info_free (match);
1689   g_regex_unref (regex);
1690
1691   regex = g_regex_new ("(?<=\\d{3})(?<!999)foo", G_REGEX_OPTIMIZE, 0, &error);
1692   g_assert (regex);
1693   g_assert_no_error (error);
1694   res = g_regex_match (regex, "999foo 123abcfoo 123foo", 0, &match);
1695   g_assert (res);
1696   g_assert (g_match_info_matches (match));
1697   res = g_match_info_fetch_pos (match, 0, &start, NULL);
1698   g_assert (res);
1699   g_assert_cmpint (start, ==, 20);
1700   g_match_info_free (match);
1701   g_regex_unref (regex);
1702
1703   regex = g_regex_new ("(?<=\\d{3}...)(?<!999)foo", G_REGEX_OPTIMIZE, 0, &error);
1704   g_assert (regex);
1705   g_assert_no_error (error);
1706   res = g_regex_match (regex, "999foo 123abcfoo 123foo", 0, &match);
1707   g_assert (res);
1708   g_assert (g_match_info_matches (match));
1709   res = g_match_info_fetch_pos (match, 0, &start, NULL);
1710   g_assert (res);
1711   g_assert_cmpint (start, ==, 13);
1712   g_match_info_free (match);
1713   g_regex_unref (regex);
1714
1715   regex = g_regex_new ("(?<=\\d{3}(?!999)...)foo", G_REGEX_OPTIMIZE, 0, &error);
1716   g_assert (regex);
1717   g_assert_no_error (error);
1718   res = g_regex_match (regex, "999foo 123abcfoo 123foo", 0, &match);
1719   g_assert (res);
1720   g_assert (g_match_info_matches (match));
1721   res = g_match_info_fetch_pos (match, 0, &start, NULL);
1722   g_assert (res);
1723   g_assert_cmpint (start, ==, 13);
1724   g_match_info_free (match);
1725   g_regex_unref (regex);
1726
1727   regex = g_regex_new ("(?<=(?<!foo)bar)baz", G_REGEX_OPTIMIZE, 0, &error);
1728   g_assert (regex);
1729   g_assert_no_error (error);
1730   res = g_regex_match (regex, "foobarbaz barfoobaz barbarbaz", 0, &match);
1731   g_assert (res);
1732   g_assert (g_match_info_matches (match));
1733   res = g_match_info_fetch_pos (match, 0, &start, NULL);
1734   g_assert (res);
1735   g_assert_cmpint (start, ==, 26);
1736   g_match_info_free (match);
1737   g_regex_unref (regex);
1738 }
1739
1740 /* examples for subpatterns taken from pcrepattern(3) */
1741 static void
1742 test_subpattern (void)
1743 {
1744   GRegex *regex;
1745   GError *error;
1746   GMatchInfo *match;
1747   gboolean res;
1748   gchar *str;
1749   gint start;
1750
1751   error = NULL;
1752   regex = g_regex_new ("cat(aract|erpillar|)", G_REGEX_OPTIMIZE, 0, &error);
1753   g_assert (regex);
1754   g_assert_no_error (error);
1755   g_assert_cmpint (g_regex_get_capture_count (regex), ==, 1);
1756   g_assert_cmpint (g_regex_get_max_backref (regex), ==, 0);
1757   res = g_regex_match_all (regex, "caterpillar", 0, &match);
1758   g_assert (res);
1759   g_assert (g_match_info_matches (match));
1760   g_assert_cmpint (g_match_info_get_match_count (match), ==, 2);
1761   str = g_match_info_fetch (match, 0);
1762   g_assert_cmpstr (str, ==, "caterpillar");
1763   g_free (str);
1764   str = g_match_info_fetch (match, 1);
1765   g_assert_cmpstr (str, ==, "cat");
1766   g_free (str);
1767   g_match_info_free (match);
1768   g_regex_unref (regex);
1769
1770   regex = g_regex_new ("the ((red|white) (king|queen))", G_REGEX_OPTIMIZE, 0, &error);
1771   g_assert (regex);
1772   g_assert_no_error (error);
1773   g_assert_cmpint (g_regex_get_capture_count (regex), ==, 3);
1774   g_assert_cmpint (g_regex_get_max_backref (regex), ==, 0);
1775   res = g_regex_match (regex, "the red king", 0, &match);
1776   g_assert (res);
1777   g_assert (g_match_info_matches (match));
1778   g_assert_cmpint (g_match_info_get_match_count (match), ==, 4);
1779   str = g_match_info_fetch (match, 0);
1780   g_assert_cmpstr (str, ==, "the red king");
1781   g_free (str);
1782   str = g_match_info_fetch (match, 1);
1783   g_assert_cmpstr (str, ==, "red king");
1784   g_free (str);
1785   str = g_match_info_fetch (match, 2);
1786   g_assert_cmpstr (str, ==, "red");
1787   g_free (str);
1788   str = g_match_info_fetch (match, 3);
1789   g_assert_cmpstr (str, ==, "king");
1790   g_free (str);
1791   g_match_info_free (match);
1792   g_regex_unref (regex);
1793
1794   regex = g_regex_new ("the ((?:red|white) (king|queen))", G_REGEX_OPTIMIZE, 0, &error);
1795   g_assert (regex);
1796   g_assert_no_error (error);
1797   res = g_regex_match (regex, "the white queen", 0, &match);
1798   g_assert (res);
1799   g_assert (g_match_info_matches (match));
1800   g_assert_cmpint (g_match_info_get_match_count (match), ==, 3);
1801   g_assert_cmpint (g_regex_get_max_backref (regex), ==, 0);
1802   str = g_match_info_fetch (match, 0);
1803   g_assert_cmpstr (str, ==, "the white queen");
1804   g_free (str);
1805   str = g_match_info_fetch (match, 1);
1806   g_assert_cmpstr (str, ==, "white queen");
1807   g_free (str);
1808   str = g_match_info_fetch (match, 2);
1809   g_assert_cmpstr (str, ==, "queen");
1810   g_free (str);
1811   g_match_info_free (match);
1812   g_regex_unref (regex);
1813
1814   regex = g_regex_new ("(?|(Sat)(ur)|(Sun))day (morning|afternoon)", G_REGEX_OPTIMIZE, 0, &error);
1815   g_assert (regex);
1816   g_assert_no_error (error);
1817   g_assert_cmpint (g_regex_get_capture_count (regex), ==, 3);
1818   res = g_regex_match (regex, "Saturday morning", 0, &match);
1819   g_assert (res);
1820   g_assert (g_match_info_matches (match));
1821   g_assert_cmpint (g_match_info_get_match_count (match), ==, 4);
1822   str = g_match_info_fetch (match, 1);
1823   g_assert_cmpstr (str, ==, "Sat");
1824   g_free (str);
1825   str = g_match_info_fetch (match, 2);
1826   g_assert_cmpstr (str, ==, "ur");
1827   g_free (str);
1828   str = g_match_info_fetch (match, 3);
1829   g_assert_cmpstr (str, ==, "morning");
1830   g_free (str);
1831   g_match_info_free (match);
1832   g_regex_unref (regex);
1833
1834   regex = g_regex_new ("(?|(abc)|(def))\\1", G_REGEX_OPTIMIZE, 0, &error);
1835   g_assert (regex);
1836   g_assert_no_error (error);
1837   g_assert_cmpint (g_regex_get_max_backref (regex), ==, 1);
1838   res = g_regex_match (regex, "abcabc abcdef defabc defdef", 0, &match);
1839   g_assert (res);
1840   g_assert (g_match_info_matches (match));
1841   res = g_match_info_fetch_pos (match, 0, &start, NULL);
1842   g_assert (res);
1843   g_assert_cmpint (start, ==, 0);
1844   res = g_match_info_next (match, &error);
1845   g_assert (res);
1846   res = g_match_info_fetch_pos (match, 0, &start, NULL);
1847   g_assert (res);
1848   g_assert_cmpint (start, ==, 21);
1849   g_match_info_free (match);
1850   g_regex_unref (regex);
1851
1852   regex = g_regex_new ("(?|(abc)|(def))(?1)", G_REGEX_OPTIMIZE, 0, &error);
1853   g_assert (regex);
1854   g_assert_no_error (error);
1855   res = g_regex_match (regex, "abcabc abcdef defabc defdef", 0, &match);
1856   g_assert (res);
1857   g_assert (g_match_info_matches (match));
1858   res = g_match_info_fetch_pos (match, 0, &start, NULL);
1859   g_assert (res);
1860   g_assert_cmpint (start, ==, 0);
1861   res = g_match_info_next (match, &error);
1862   g_assert (res);
1863   res = g_match_info_fetch_pos (match, 0, &start, NULL);
1864   g_assert (res);
1865   g_assert_cmpint (start, ==, 14);
1866   g_match_info_free (match);
1867   g_regex_unref (regex);
1868
1869   regex = g_regex_new ("(?<DN>Mon|Fri|Sun)(?:day)?|(?<DN>Tue)(?:sday)?|(?<DN>Wed)(?:nesday)?|(?<DN>Thu)(?:rsday)?|(?<DN>Sat)(?:urday)?", G_REGEX_OPTIMIZE|G_REGEX_DUPNAMES, 0, &error);
1870   g_assert (regex);
1871   g_assert_no_error (error);
1872   res = g_regex_match (regex, "Mon Tuesday Wed Saturday", 0, &match);
1873   g_assert (res);
1874   g_assert (g_match_info_matches (match));
1875   str = g_match_info_fetch_named (match, "DN");
1876   g_assert_cmpstr (str, ==, "Mon");
1877   g_free (str);
1878   res = g_match_info_next (match, &error);
1879   g_assert (res);
1880   str = g_match_info_fetch_named (match, "DN");
1881   g_assert_cmpstr (str, ==, "Tue");
1882   g_free (str);
1883   res = g_match_info_next (match, &error);
1884   g_assert (res);
1885   str = g_match_info_fetch_named (match, "DN");
1886   g_assert_cmpstr (str, ==, "Wed");
1887   g_free (str);
1888   res = g_match_info_next (match, &error);
1889   g_assert (res);
1890   str = g_match_info_fetch_named (match, "DN");
1891   g_assert_cmpstr (str, ==, "Sat");
1892   g_free (str);
1893   g_match_info_free (match);
1894   g_regex_unref (regex);
1895
1896   regex = g_regex_new ("^(a|b\\1)+$", G_REGEX_OPTIMIZE|G_REGEX_DUPNAMES, 0, &error);
1897   g_assert (regex);
1898   g_assert_no_error (error);
1899   res = g_regex_match (regex, "aaaaaaaaaaaaaaaa", 0, &match);
1900   g_assert (res);
1901   g_assert (g_match_info_matches (match));
1902   g_match_info_free (match);
1903   res = g_regex_match (regex, "ababbaa", 0, &match);
1904   g_assert (res);
1905   g_assert (g_match_info_matches (match));
1906   g_match_info_free (match);
1907   g_regex_unref (regex);
1908 }
1909
1910 /* examples for conditions taken from pcrepattern(3) */
1911 static void
1912 test_condition (void)
1913 {
1914   GRegex *regex;
1915   GError *error;
1916   GMatchInfo *match;
1917   gboolean res;
1918
1919   error = NULL;
1920   regex = g_regex_new ("^(a+)(\\()?[^()]+(?(-1)\\))(b+)$", G_REGEX_OPTIMIZE, 0, &error);
1921   g_assert (regex);
1922   g_assert_no_error (error);
1923   res = g_regex_match (regex, "a(zzzzzz)b", 0, &match);
1924   g_assert (res);
1925   g_assert (g_match_info_matches (match));
1926   g_match_info_free (match);
1927   res = g_regex_match (regex, "aaazzzzzzbbb", 0, &match);
1928   g_assert (res);
1929   g_assert (g_match_info_matches (match));
1930   g_match_info_free (match);
1931   g_regex_unref (regex);
1932
1933   error = NULL;
1934   regex = g_regex_new ("^(a+)(?<OPEN>\\()?[^()]+(?(<OPEN>)\\))(b+)$", G_REGEX_OPTIMIZE, 0, &error);
1935   g_assert (regex);
1936   g_assert_no_error (error);
1937   res = g_regex_match (regex, "a(zzzzzz)b", 0, &match);
1938   g_assert (res);
1939   g_assert (g_match_info_matches (match));
1940   g_match_info_free (match);
1941   res = g_regex_match (regex, "aaazzzzzzbbb", 0, &match);
1942   g_assert (res);
1943   g_assert (g_match_info_matches (match));
1944   g_match_info_free (match);
1945   g_regex_unref (regex);
1946
1947   regex = g_regex_new ("^(a+)(?(+1)\\[|\\<)?[^()]+(\\])?(b+)$", G_REGEX_OPTIMIZE, 0, &error);
1948   g_assert (regex);
1949   g_assert_no_error (error);
1950   res = g_regex_match (regex, "a[zzzzzz]b", 0, &match);
1951   g_assert (res);
1952   g_assert (g_match_info_matches (match));
1953   g_match_info_free (match);
1954   res = g_regex_match (regex, "aaa<zzzzzzbbb", 0, &match);
1955   g_assert (res);
1956   g_assert (g_match_info_matches (match));
1957   g_match_info_free (match);
1958   g_regex_unref (regex);
1959
1960   regex = g_regex_new ("(?(DEFINE) (?<byte> 2[0-4]\\d | 25[0-5] | 1\\d\\d | [1-9]?\\d) )"
1961                        "\\b (?&byte) (\\.(?&byte)){3} \\b",
1962                        G_REGEX_OPTIMIZE|G_REGEX_EXTENDED, 0, &error);
1963   g_assert (regex);
1964   g_assert_no_error (error);
1965   res = g_regex_match (regex, "128.0.0.1", 0, &match);
1966   g_assert (res);
1967   g_assert (g_match_info_matches (match));
1968   g_match_info_free (match);
1969   res = g_regex_match (regex, "192.168.1.1", 0, &match);
1970   g_assert (res);
1971   g_assert (g_match_info_matches (match));
1972   g_match_info_free (match);
1973   res = g_regex_match (regex, "209.132.180.167", 0, &match);
1974   g_assert (res);
1975   g_assert (g_match_info_matches (match));
1976   g_match_info_free (match);
1977   g_regex_unref (regex);
1978
1979   regex = g_regex_new ("^(?(?=[^a-z]*[a-z])"
1980                        "\\d{2}-[a-z]{3}-\\d{2} | \\d{2}-\\d{2}-\\d{2} )$",
1981                        G_REGEX_OPTIMIZE|G_REGEX_EXTENDED, 0, &error);
1982   g_assert (regex);
1983   g_assert_no_error (error);
1984   res = g_regex_match (regex, "01-abc-24", 0, &match);
1985   g_assert (res);
1986   g_assert (g_match_info_matches (match));
1987   g_match_info_free (match);
1988   res = g_regex_match (regex, "01-23-45", 0, &match);
1989   g_assert (res);
1990   g_assert (g_match_info_matches (match));
1991   g_match_info_free (match);
1992   res = g_regex_match (regex, "01-uv-45", 0, &match);
1993   g_assert (!res);
1994   g_assert (!g_match_info_matches (match));
1995   g_match_info_free (match);
1996   res = g_regex_match (regex, "01-234-45", 0, &match);
1997   g_assert (!res);
1998   g_assert (!g_match_info_matches (match));
1999   g_match_info_free (match);
2000   g_regex_unref (regex);
2001 }
2002
2003 /* examples for recursion taken from pcrepattern(3) */
2004 static void
2005 test_recursion (void)
2006 {
2007   GRegex *regex;
2008   GError *error;
2009   GMatchInfo *match;
2010   gboolean res;
2011   gint start;
2012
2013   error = NULL;
2014   regex = g_regex_new ("\\( ( [^()]++ | (?R) )* \\)", G_REGEX_OPTIMIZE|G_REGEX_EXTENDED, 0, &error);
2015   g_assert (regex);
2016   g_assert_no_error (error);
2017   res = g_regex_match (regex, "(middle)", 0, &match);
2018   g_assert (res);
2019   g_assert (g_match_info_matches (match));
2020   g_match_info_free (match);
2021   res = g_regex_match (regex, "((((((((((((((((middle))))))))))))))))", 0, &match);
2022   g_assert (res);
2023   g_assert (g_match_info_matches (match));
2024   g_match_info_free (match);
2025   res = g_regex_match (regex, "(((xxx(((", 0, &match);
2026   g_assert (!res);
2027   g_assert (!g_match_info_matches (match));
2028   g_match_info_free (match);
2029   g_regex_unref (regex);
2030
2031   regex = g_regex_new ("^( \\( ( [^()]++ | (?1) )* \\) )$", G_REGEX_OPTIMIZE|G_REGEX_EXTENDED, 0, &error);
2032   g_assert (regex);
2033   g_assert_no_error (error);
2034   res = g_regex_match (regex, "((((((((((((((((middle))))))))))))))))", 0, &match);
2035   g_assert (res);
2036   g_assert (g_match_info_matches (match));
2037   g_match_info_free (match);
2038   res = g_regex_match (regex, "(((xxx((()", 0, &match);
2039   g_assert (!res);
2040   g_assert (!g_match_info_matches (match));
2041   g_match_info_free (match);
2042   g_regex_unref (regex);
2043
2044   regex = g_regex_new ("^(?<pn> \\( ( [^()]++ | (?&pn) )* \\) )$", G_REGEX_OPTIMIZE|G_REGEX_EXTENDED, 0, &error);
2045   g_assert (regex);
2046   g_assert_no_error (error);
2047   g_regex_match (regex, "(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()", 0, &match);
2048   g_assert (!res);
2049   g_assert (!g_match_info_matches (match));
2050   g_match_info_free (match);
2051   g_regex_unref (regex);
2052
2053   regex = g_regex_new ("< (?: (?(R) \\d++ | [^<>]*+) | (?R)) * >", G_REGEX_OPTIMIZE|G_REGEX_EXTENDED, 0, &error);
2054   g_assert (regex);
2055   g_assert_no_error (error);
2056   res = g_regex_match (regex, "<ab<01<23<4>>>>", 0, &match);
2057   g_assert (res);
2058   g_assert (g_match_info_matches (match));
2059   res = g_match_info_fetch_pos (match, 0, &start, NULL);
2060   g_assert (res);
2061   g_assert_cmpint (start, ==, 0);
2062   g_match_info_free (match);
2063   res = g_regex_match (regex, "<ab<01<xx<x>>>>", 0, &match);
2064   g_assert (res);
2065   g_assert (g_match_info_matches (match));
2066   res = g_match_info_fetch_pos (match, 0, &start, NULL);
2067   g_assert (res);
2068   g_assert_cmpint (start, >, 0);
2069   g_match_info_free (match);
2070   g_regex_unref (regex);
2071
2072   regex = g_regex_new ("^((.)(?1)\\2|.)$", G_REGEX_OPTIMIZE, 0, &error);
2073   g_assert (regex);
2074   g_assert_no_error (error);
2075   res = g_regex_match (regex, "abcdcba", 0, &match);
2076   g_assert (res);
2077   g_assert (g_match_info_matches (match));
2078   g_match_info_free (match);
2079   res = g_regex_match (regex, "abcddcba", 0, &match);
2080   g_assert (!res);
2081   g_assert (!g_match_info_matches (match));
2082   g_match_info_free (match);
2083   g_regex_unref (regex);
2084
2085   regex = g_regex_new ("^(?:((.)(?1)\\2|)|((.)(?3)\\4|.))$", G_REGEX_OPTIMIZE, 0, &error);
2086   g_assert (regex);
2087   g_assert_no_error (error);
2088   res = g_regex_match (regex, "abcdcba", 0, &match);
2089   g_assert (res);
2090   g_assert (g_match_info_matches (match));
2091   g_match_info_free (match);
2092   res = g_regex_match (regex, "abcddcba", 0, &match);
2093   g_assert (res);
2094   g_assert (g_match_info_matches (match));
2095   g_match_info_free (match);
2096   g_regex_unref (regex);
2097
2098   regex = g_regex_new ("^\\W*+(?:((.)\\W*+(?1)\\W*+\\2|)|((.)\\W*+(?3)\\W*+\\4|\\W*+.\\W*+))\\W*+$", G_REGEX_OPTIMIZE|G_REGEX_CASELESS, 0, &error);
2099   g_assert (regex);
2100   g_assert_no_error (error);
2101   res = g_regex_match (regex, "abcdcba", 0, &match);
2102   g_assert (res);
2103   g_assert (g_match_info_matches (match));
2104   g_match_info_free (match);
2105   res = g_regex_match (regex, "A man, a plan, a canal: Panama!", 0, &match);
2106   g_assert (res);
2107   g_assert (g_match_info_matches (match));
2108   g_match_info_free (match);
2109   res = g_regex_match (regex, "Oozy rat in a sanitary zoo", 0, &match);
2110   g_assert (res);
2111   g_assert (g_match_info_matches (match));
2112   g_match_info_free (match);
2113   g_regex_unref (regex);
2114 }
2115
2116 static void
2117 test_multiline (void)
2118 {
2119   GRegex *regex;
2120   GMatchInfo *info;
2121   gint count;
2122
2123   g_test_bug ("640489");
2124
2125   regex = g_regex_new ("^a$", G_REGEX_MULTILINE|G_REGEX_DOTALL, 0, NULL);
2126
2127   count = 0;
2128   g_regex_match (regex, "a\nb\na", 0, &info);
2129   while (g_match_info_matches (info))
2130     {
2131       count++;
2132       g_match_info_next (info, NULL);
2133     }
2134   g_match_info_free (info);
2135   g_regex_unref (regex);
2136
2137   g_assert_cmpint (count, ==, 2);
2138 }
2139
2140 static void
2141 test_explicit_crlf (void)
2142 {
2143   GRegex *regex;
2144
2145   regex = g_regex_new ("[\r\n]a", 0, 0, NULL);
2146   g_assert_cmpint (g_regex_get_has_cr_or_lf (regex), ==, TRUE);
2147   g_regex_unref (regex);
2148 }
2149
2150 static void
2151 test_max_lookbehind (void)
2152 {
2153   GRegex *regex;
2154
2155   regex = g_regex_new ("abc", 0, 0, NULL);
2156   g_assert_cmpint (g_regex_get_max_lookbehind (regex), ==, 0);
2157   g_regex_unref (regex);
2158
2159   regex = g_regex_new ("\\babc", 0, 0, NULL);
2160   g_assert_cmpint (g_regex_get_max_lookbehind (regex), ==, 1);
2161   g_regex_unref (regex);
2162
2163   regex = g_regex_new ("(?<=123)abc", 0, 0, NULL);
2164   g_assert_cmpint (g_regex_get_max_lookbehind (regex), ==, 3);
2165   g_regex_unref (regex);
2166 }
2167
2168 int
2169 main (int argc, char *argv[])
2170 {
2171   setlocale (LC_ALL, "");
2172
2173   g_test_init (&argc, &argv, NULL);
2174
2175   g_test_bug_base ("http://bugzilla.gnome.org/");
2176
2177   g_test_add_func ("/regex/properties", test_properties);
2178   g_test_add_func ("/regex/class", test_class);
2179   g_test_add_func ("/regex/lookahead", test_lookahead);
2180   g_test_add_func ("/regex/lookbehind", test_lookbehind);
2181   g_test_add_func ("/regex/subpattern", test_subpattern);
2182   g_test_add_func ("/regex/condition", test_condition);
2183   g_test_add_func ("/regex/recursion", test_recursion);
2184   g_test_add_func ("/regex/multiline", test_multiline);
2185   g_test_add_func ("/regex/explicit-crlf", test_explicit_crlf);
2186   g_test_add_func ("/regex/max-lookbehind", test_max_lookbehind);
2187
2188   /* TEST_NEW(pattern, compile_opts, match_opts) */
2189   TEST_NEW("[A-Z]+", G_REGEX_CASELESS | G_REGEX_EXTENDED | G_REGEX_OPTIMIZE, G_REGEX_MATCH_NOTBOL | G_REGEX_MATCH_PARTIAL);
2190   TEST_NEW("", 0, 0);
2191   TEST_NEW(".*", 0, 0);
2192   TEST_NEW(".*", G_REGEX_OPTIMIZE, 0);
2193   TEST_NEW(".*", G_REGEX_MULTILINE, 0);
2194   TEST_NEW(".*", G_REGEX_DOTALL, 0);
2195   TEST_NEW(".*", G_REGEX_DOTALL, G_REGEX_MATCH_NOTBOL);
2196   TEST_NEW("(123\\d*)[a-zA-Z]+(?P<hello>.*)", 0, 0);
2197   TEST_NEW("(123\\d*)[a-zA-Z]+(?P<hello>.*)", G_REGEX_CASELESS, 0);
2198   TEST_NEW("(123\\d*)[a-zA-Z]+(?P<hello>.*)", G_REGEX_CASELESS | G_REGEX_OPTIMIZE, 0);
2199   TEST_NEW("(?P<A>x)|(?P<A>y)", G_REGEX_DUPNAMES, 0);
2200   TEST_NEW("(?P<A>x)|(?P<A>y)", G_REGEX_DUPNAMES | G_REGEX_OPTIMIZE, 0);
2201   /* This gives "internal error: code overflow" with pcre 6.0 */
2202   TEST_NEW("(?i)(?-i)", 0, 0);
2203
2204   /* Check that flags are correct if the pattern modifies them */
2205   /* TEST_NEW_CHECK_FLAGS(pattern, compile_opts, match_ops, real_compile_opts, real_match_opts) */
2206   TEST_NEW_CHECK_FLAGS ("a", G_REGEX_OPTIMIZE, 0, G_REGEX_OPTIMIZE, 0);
2207   TEST_NEW_CHECK_FLAGS ("a", G_REGEX_RAW, 0, G_REGEX_RAW, 0);
2208   TEST_NEW_CHECK_FLAGS ("(?i)a", 0, 0, G_REGEX_CASELESS, 0);
2209   TEST_NEW_CHECK_FLAGS ("(?m)a", 0, 0, G_REGEX_MULTILINE, 0);
2210   TEST_NEW_CHECK_FLAGS ("(?s)a", 0, 0, G_REGEX_DOTALL, 0);
2211   TEST_NEW_CHECK_FLAGS ("(?x)a", 0, 0, G_REGEX_EXTENDED, 0);
2212   TEST_NEW_CHECK_FLAGS ("(?J)a", 0, 0, G_REGEX_DUPNAMES, 0);
2213   TEST_NEW_CHECK_FLAGS ("(?U)[a-z]+", 0, 0, G_REGEX_UNGREEDY, 0);
2214   TEST_NEW_CHECK_FLAGS ("(?X)a", 0, 0, 0 /* not exposed by GRegex */, 0);
2215   TEST_NEW_CHECK_FLAGS ("^.*", 0, 0, G_REGEX_ANCHORED, 0);
2216   TEST_NEW_CHECK_FLAGS ("(*UTF8)a", 0, 0, 0 /* this is the default in GRegex */, 0);
2217   TEST_NEW_CHECK_FLAGS ("(*UCP)a", 0, 0, 0 /* this always on in GRegex */, 0);
2218   TEST_NEW_CHECK_FLAGS ("(*CR)a", 0, 0, G_REGEX_NEWLINE_CR, 0);
2219   TEST_NEW_CHECK_FLAGS ("(*LF)a", 0, 0, G_REGEX_NEWLINE_LF, 0);
2220   TEST_NEW_CHECK_FLAGS ("(*CRLF)a", 0, 0, G_REGEX_NEWLINE_CRLF, 0);
2221   TEST_NEW_CHECK_FLAGS ("(*ANY)a", 0, 0, 0 /* this is the default in GRegex */, 0);
2222   TEST_NEW_CHECK_FLAGS ("(*ANYCRLF)a", 0, 0, G_REGEX_NEWLINE_ANYCRLF, 0);
2223   TEST_NEW_CHECK_FLAGS ("(*BSR_ANYCRLF)a", 0, 0, G_REGEX_BSR_ANYCRLF, 0);
2224   TEST_NEW_CHECK_FLAGS ("(*BSR_UNICODE)a", 0, 0, 0 /* this is the default in GRegex */, 0);
2225   TEST_NEW_CHECK_FLAGS ("(*NO_START_OPT)a", 0, 0, 0 /* not exposed in GRegex */, 0);
2226
2227   /* TEST_NEW_FAIL(pattern, compile_opts, expected_error) */
2228   TEST_NEW_FAIL("(", 0, G_REGEX_ERROR_UNMATCHED_PARENTHESIS);
2229   TEST_NEW_FAIL(")", 0, G_REGEX_ERROR_UNMATCHED_PARENTHESIS);
2230   TEST_NEW_FAIL("[", 0, G_REGEX_ERROR_UNTERMINATED_CHARACTER_CLASS);
2231   TEST_NEW_FAIL("*", 0, G_REGEX_ERROR_NOTHING_TO_REPEAT);
2232   TEST_NEW_FAIL("?", 0, G_REGEX_ERROR_NOTHING_TO_REPEAT);
2233   TEST_NEW_FAIL("(?P<A>x)|(?P<A>y)", 0, G_REGEX_ERROR_DUPLICATE_SUBPATTERN_NAME);
2234
2235   /* Check all GRegexError codes */
2236   TEST_NEW_FAIL ("a\\", 0, G_REGEX_ERROR_STRAY_BACKSLASH);
2237   TEST_NEW_FAIL ("a\\c", 0, G_REGEX_ERROR_MISSING_CONTROL_CHAR);
2238   TEST_NEW_FAIL ("a\\l", 0, G_REGEX_ERROR_UNRECOGNIZED_ESCAPE);
2239   TEST_NEW_FAIL ("a{4,2}", 0, G_REGEX_ERROR_QUANTIFIERS_OUT_OF_ORDER);
2240   TEST_NEW_FAIL ("a{999999,}", 0, G_REGEX_ERROR_QUANTIFIER_TOO_BIG);
2241   TEST_NEW_FAIL ("[a-z", 0, G_REGEX_ERROR_UNTERMINATED_CHARACTER_CLASS);
2242   TEST_NEW_FAIL ("(?X)[\\B]", 0, G_REGEX_ERROR_INVALID_ESCAPE_IN_CHARACTER_CLASS);
2243   TEST_NEW_FAIL ("[z-a]", 0, G_REGEX_ERROR_RANGE_OUT_OF_ORDER);
2244   TEST_NEW_FAIL ("{2,4}", 0, G_REGEX_ERROR_NOTHING_TO_REPEAT);
2245   TEST_NEW_FAIL ("a(?u)", 0, G_REGEX_ERROR_UNRECOGNIZED_CHARACTER);
2246   TEST_NEW_FAIL ("a(?<$foo)bar", 0, G_REGEX_ERROR_UNRECOGNIZED_CHARACTER);
2247   TEST_NEW_FAIL ("a[:alpha:]b", 0, G_REGEX_ERROR_POSIX_NAMED_CLASS_OUTSIDE_CLASS);
2248   TEST_NEW_FAIL ("a(b", 0, G_REGEX_ERROR_UNMATCHED_PARENTHESIS);
2249   TEST_NEW_FAIL ("a)b", 0, G_REGEX_ERROR_UNMATCHED_PARENTHESIS);
2250   TEST_NEW_FAIL ("a(?R", 0, G_REGEX_ERROR_UNMATCHED_PARENTHESIS);
2251   TEST_NEW_FAIL ("a(?-54", 0, G_REGEX_ERROR_UNMATCHED_PARENTHESIS);
2252   TEST_NEW_FAIL ("(ab\\2)", 0, G_REGEX_ERROR_INEXISTENT_SUBPATTERN_REFERENCE);
2253   TEST_NEW_FAIL ("a(?#abc", 0, G_REGEX_ERROR_UNTERMINATED_COMMENT);
2254   TEST_NEW_FAIL ("(?<=a+)b", 0, G_REGEX_ERROR_VARIABLE_LENGTH_LOOKBEHIND);
2255   TEST_NEW_FAIL ("(?(1?)a|b)", 0, G_REGEX_ERROR_MALFORMED_CONDITION);
2256   TEST_NEW_FAIL ("(a)(?(1)a|b|c)", 0, G_REGEX_ERROR_TOO_MANY_CONDITIONAL_BRANCHES);
2257   TEST_NEW_FAIL ("(?(?i))", 0, G_REGEX_ERROR_ASSERTION_EXPECTED);
2258   TEST_NEW_FAIL ("a[[:fubar:]]b", 0, G_REGEX_ERROR_UNKNOWN_POSIX_CLASS_NAME);
2259   TEST_NEW_FAIL ("[[.ch.]]", 0, G_REGEX_ERROR_POSIX_COLLATING_ELEMENTS_NOT_SUPPORTED);
2260   TEST_NEW_FAIL ("\\x{110000}", 0, G_REGEX_ERROR_HEX_CODE_TOO_LARGE);
2261   TEST_NEW_FAIL ("^(?(0)f|b)oo", 0, G_REGEX_ERROR_INVALID_CONDITION);
2262   TEST_NEW_FAIL ("(?<=\\C)X", 0, G_REGEX_ERROR_SINGLE_BYTE_MATCH_IN_LOOKBEHIND);
2263   TEST_NEW_FAIL ("(?!\\w)(?R)", 0, G_REGEX_ERROR_INFINITE_LOOP);
2264   TEST_NEW_FAIL ("(?(?<ab))", 0, G_REGEX_ERROR_MISSING_SUBPATTERN_NAME_TERMINATOR);
2265   TEST_NEW_FAIL ("(?P<x>eks)(?P<x>eccs)", 0, G_REGEX_ERROR_DUPLICATE_SUBPATTERN_NAME);
2266 #if 0
2267   TEST_NEW_FAIL (?, 0, G_REGEX_ERROR_MALFORMED_PROPERTY);
2268   TEST_NEW_FAIL (?, 0, G_REGEX_ERROR_UNKNOWN_PROPERTY);
2269 #endif
2270   TEST_NEW_FAIL ("\\666", G_REGEX_RAW, G_REGEX_ERROR_INVALID_OCTAL_VALUE);
2271   TEST_NEW_FAIL ("^(?(DEFINE) abc | xyz ) ", 0, G_REGEX_ERROR_TOO_MANY_BRANCHES_IN_DEFINE);
2272   TEST_NEW_FAIL ("a", G_REGEX_NEWLINE_CRLF | G_REGEX_NEWLINE_ANYCRLF, G_REGEX_ERROR_INCONSISTENT_NEWLINE_OPTIONS);
2273   TEST_NEW_FAIL ("^(a)\\g{3", 0, G_REGEX_ERROR_MISSING_BACK_REFERENCE);
2274   TEST_NEW_FAIL ("^(a)\\g{0}", 0, G_REGEX_ERROR_INVALID_RELATIVE_REFERENCE);
2275   TEST_NEW_FAIL ("abc(*FAIL:123)xyz", 0, G_REGEX_ERROR_BACKTRACKING_CONTROL_VERB_ARGUMENT_FORBIDDEN);
2276   TEST_NEW_FAIL ("a(*FOOBAR)b", 0, G_REGEX_ERROR_UNKNOWN_BACKTRACKING_CONTROL_VERB);
2277   TEST_NEW_FAIL ("(?i:A{1,}\\6666666666)", 0, G_REGEX_ERROR_NUMBER_TOO_BIG);
2278   TEST_NEW_FAIL ("(?<a>)(?&)", 0, G_REGEX_ERROR_MISSING_SUBPATTERN_NAME);
2279   TEST_NEW_FAIL ("(?+-a)", 0, G_REGEX_ERROR_MISSING_DIGIT);
2280   TEST_NEW_FAIL ("TA]", G_REGEX_JAVASCRIPT_COMPAT, G_REGEX_ERROR_INVALID_DATA_CHARACTER);
2281   TEST_NEW_FAIL ("(?|(?<a>A)|(?<b>B))", 0, G_REGEX_ERROR_EXTRA_SUBPATTERN_NAME);
2282   TEST_NEW_FAIL ("a(*MARK)b", 0, G_REGEX_ERROR_BACKTRACKING_CONTROL_VERB_ARGUMENT_REQUIRED);
2283   TEST_NEW_FAIL ("^\\c€", 0, G_REGEX_ERROR_INVALID_CONTROL_CHAR);
2284   TEST_NEW_FAIL ("\\k", 0, G_REGEX_ERROR_MISSING_NAME);
2285   TEST_NEW_FAIL ("a[\\NB]c", 0, G_REGEX_ERROR_NOT_SUPPORTED_IN_CLASS);
2286   TEST_NEW_FAIL ("(*:0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEFG)XX", 0, G_REGEX_ERROR_NAME_TOO_LONG);
2287   TEST_NEW_FAIL ("\\u0100", G_REGEX_RAW | G_REGEX_JAVASCRIPT_COMPAT, G_REGEX_ERROR_CHARACTER_VALUE_TOO_LARGE);
2288
2289   /* These errors can't really be tested sanely:
2290    * G_REGEX_ERROR_EXPRESSION_TOO_LARGE
2291    * G_REGEX_ERROR_MEMORY_ERROR
2292    * G_REGEX_ERROR_SUBPATTERN_NAME_TOO_LONG
2293    * G_REGEX_ERROR_TOO_MANY_SUBPATTERNS
2294    * G_REGEX_ERROR_TOO_MANY_FORWARD_REFERENCES
2295    *
2296    * These errors are obsolete and never raised by PCRE:
2297    * G_REGEX_ERROR_DEFINE_REPETION
2298    */
2299
2300   /* TEST_MATCH_SIMPLE(pattern, string, compile_opts, match_opts, expected) */
2301   TEST_MATCH_SIMPLE("a", "", 0, 0, FALSE);
2302   TEST_MATCH_SIMPLE("a", "a", 0, 0, TRUE);
2303   TEST_MATCH_SIMPLE("a", "ba", 0, 0, TRUE);
2304   TEST_MATCH_SIMPLE("^a", "ba", 0, 0, FALSE);
2305   TEST_MATCH_SIMPLE("a", "ba", G_REGEX_ANCHORED, 0, FALSE);
2306   TEST_MATCH_SIMPLE("a", "ba", 0, G_REGEX_MATCH_ANCHORED, FALSE);
2307   TEST_MATCH_SIMPLE("a", "ab", G_REGEX_ANCHORED, 0, TRUE);
2308   TEST_MATCH_SIMPLE("a", "ab", 0, G_REGEX_MATCH_ANCHORED, TRUE);
2309   TEST_MATCH_SIMPLE("a", "a", G_REGEX_CASELESS, 0, TRUE);
2310   TEST_MATCH_SIMPLE("a", "A", G_REGEX_CASELESS, 0, TRUE);
2311   /* These are needed to test extended properties. */
2312   TEST_MATCH_SIMPLE(AGRAVE, AGRAVE, G_REGEX_CASELESS, 0, TRUE);
2313   TEST_MATCH_SIMPLE(AGRAVE, AGRAVE_UPPER, G_REGEX_CASELESS, 0, TRUE);
2314   TEST_MATCH_SIMPLE("\\p{L}", "a", 0, 0, TRUE);
2315   TEST_MATCH_SIMPLE("\\p{L}", "1", 0, 0, FALSE);
2316   TEST_MATCH_SIMPLE("\\p{L}", AGRAVE, 0, 0, TRUE);
2317   TEST_MATCH_SIMPLE("\\p{L}", AGRAVE_UPPER, 0, 0, TRUE);
2318   TEST_MATCH_SIMPLE("\\p{L}", SHEEN, 0, 0, TRUE);
2319   TEST_MATCH_SIMPLE("\\p{L}", ETH30, 0, 0, FALSE);
2320   TEST_MATCH_SIMPLE("\\p{Ll}", "a", 0, 0, TRUE);
2321   TEST_MATCH_SIMPLE("\\p{Ll}", AGRAVE, 0, 0, TRUE);
2322   TEST_MATCH_SIMPLE("\\p{Ll}", AGRAVE_UPPER, 0, 0, FALSE);
2323   TEST_MATCH_SIMPLE("\\p{Ll}", ETH30, 0, 0, FALSE);
2324   TEST_MATCH_SIMPLE("\\p{Sc}", AGRAVE, 0, 0, FALSE);
2325   TEST_MATCH_SIMPLE("\\p{Sc}", EURO, 0, 0, TRUE);
2326   TEST_MATCH_SIMPLE("\\p{Sc}", ETH30, 0, 0, FALSE);
2327   TEST_MATCH_SIMPLE("\\p{N}", "a", 0, 0, FALSE);
2328   TEST_MATCH_SIMPLE("\\p{N}", "1", 0, 0, TRUE);
2329   TEST_MATCH_SIMPLE("\\p{N}", AGRAVE, 0, 0, FALSE);
2330   TEST_MATCH_SIMPLE("\\p{N}", AGRAVE_UPPER, 0, 0, FALSE);
2331   TEST_MATCH_SIMPLE("\\p{N}", SHEEN, 0, 0, FALSE);
2332   TEST_MATCH_SIMPLE("\\p{N}", ETH30, 0, 0, TRUE);
2333   TEST_MATCH_SIMPLE("\\p{Nd}", "a", 0, 0, FALSE);
2334   TEST_MATCH_SIMPLE("\\p{Nd}", "1", 0, 0, TRUE);
2335   TEST_MATCH_SIMPLE("\\p{Nd}", AGRAVE, 0, 0, FALSE);
2336   TEST_MATCH_SIMPLE("\\p{Nd}", AGRAVE_UPPER, 0, 0, FALSE);
2337   TEST_MATCH_SIMPLE("\\p{Nd}", SHEEN, 0, 0, FALSE);
2338   TEST_MATCH_SIMPLE("\\p{Nd}", ETH30, 0, 0, FALSE);
2339   TEST_MATCH_SIMPLE("\\p{Common}", SHEEN, 0, 0, FALSE);
2340   TEST_MATCH_SIMPLE("\\p{Common}", "a", 0, 0, FALSE);
2341   TEST_MATCH_SIMPLE("\\p{Common}", AGRAVE, 0, 0, FALSE);
2342   TEST_MATCH_SIMPLE("\\p{Common}", AGRAVE_UPPER, 0, 0, FALSE);
2343   TEST_MATCH_SIMPLE("\\p{Common}", ETH30, 0, 0, FALSE);
2344   TEST_MATCH_SIMPLE("\\p{Common}", "%", 0, 0, TRUE);
2345   TEST_MATCH_SIMPLE("\\p{Common}", "1", 0, 0, TRUE);
2346   TEST_MATCH_SIMPLE("\\p{Arabic}", SHEEN, 0, 0, TRUE);
2347   TEST_MATCH_SIMPLE("\\p{Arabic}", "a", 0, 0, FALSE);
2348   TEST_MATCH_SIMPLE("\\p{Arabic}", AGRAVE, 0, 0, FALSE);
2349   TEST_MATCH_SIMPLE("\\p{Arabic}", AGRAVE_UPPER, 0, 0, FALSE);
2350   TEST_MATCH_SIMPLE("\\p{Arabic}", ETH30, 0, 0, FALSE);
2351   TEST_MATCH_SIMPLE("\\p{Arabic}", "%", 0, 0, FALSE);
2352   TEST_MATCH_SIMPLE("\\p{Arabic}", "1", 0, 0, FALSE);
2353   TEST_MATCH_SIMPLE("\\p{Latin}", SHEEN, 0, 0, FALSE);
2354   TEST_MATCH_SIMPLE("\\p{Latin}", "a", 0, 0, TRUE);
2355   TEST_MATCH_SIMPLE("\\p{Latin}", AGRAVE, 0, 0, TRUE);
2356   TEST_MATCH_SIMPLE("\\p{Latin}", AGRAVE_UPPER, 0, 0, TRUE);
2357   TEST_MATCH_SIMPLE("\\p{Latin}", ETH30, 0, 0, FALSE);
2358   TEST_MATCH_SIMPLE("\\p{Latin}", "%", 0, 0, FALSE);
2359   TEST_MATCH_SIMPLE("\\p{Latin}", "1", 0, 0, FALSE);
2360   TEST_MATCH_SIMPLE("\\p{Ethiopic}", SHEEN, 0, 0, FALSE);
2361   TEST_MATCH_SIMPLE("\\p{Ethiopic}", "a", 0, 0, FALSE);
2362   TEST_MATCH_SIMPLE("\\p{Ethiopic}", AGRAVE, 0, 0, FALSE);
2363   TEST_MATCH_SIMPLE("\\p{Ethiopic}", AGRAVE_UPPER, 0, 0, FALSE);
2364   TEST_MATCH_SIMPLE("\\p{Ethiopic}", ETH30, 0, 0, TRUE);
2365   TEST_MATCH_SIMPLE("\\p{Ethiopic}", "%", 0, 0, FALSE);
2366   TEST_MATCH_SIMPLE("\\p{Ethiopic}", "1", 0, 0, FALSE);
2367   TEST_MATCH_SIMPLE("\\p{L}(?<=\\p{Arabic})", SHEEN, 0, 0, TRUE);
2368   TEST_MATCH_SIMPLE("\\p{L}(?<=\\p{Latin})", SHEEN, 0, 0, FALSE);
2369   /* Invalid patterns. */
2370   TEST_MATCH_SIMPLE("\\", "a", 0, 0, FALSE);
2371   TEST_MATCH_SIMPLE("[", "", 0, 0, FALSE);
2372
2373   /* TEST_MATCH(pattern, compile_opts, match_opts, string,
2374    *            string_len, start_position, match_opts2, expected) */
2375   TEST_MATCH("a", 0, 0, "a", -1, 0, 0, TRUE);
2376   TEST_MATCH("a", 0, 0, "A", -1, 0, 0, FALSE);
2377   TEST_MATCH("a", G_REGEX_CASELESS, 0, "A", -1, 0, 0, TRUE);
2378   TEST_MATCH("a", 0, 0, "ab", -1, 1, 0, FALSE);
2379   TEST_MATCH("a", 0, 0, "ba", 1, 0, 0, FALSE);
2380   TEST_MATCH("a", 0, 0, "bab", -1, 0, 0, TRUE);
2381   TEST_MATCH("a", 0, 0, "b", -1, 0, 0, FALSE);
2382   TEST_MATCH("a", 0, G_REGEX_ANCHORED, "a", -1, 0, 0, TRUE);
2383   TEST_MATCH("a", 0, G_REGEX_ANCHORED, "ab", -1, 1, 0, FALSE);
2384   TEST_MATCH("a", 0, G_REGEX_ANCHORED, "ba", 1, 0, 0, FALSE);
2385   TEST_MATCH("a", 0, G_REGEX_ANCHORED, "bab", -1, 0, 0, FALSE);
2386   TEST_MATCH("a", 0, G_REGEX_ANCHORED, "b", -1, 0, 0, FALSE);
2387   TEST_MATCH("a", 0, 0, "a", -1, 0, G_REGEX_ANCHORED, TRUE);
2388   TEST_MATCH("a", 0, 0, "ab", -1, 1, G_REGEX_ANCHORED, FALSE);
2389   TEST_MATCH("a", 0, 0, "ba", 1, 0, G_REGEX_ANCHORED, FALSE);
2390   TEST_MATCH("a", 0, 0, "bab", -1, 0, G_REGEX_ANCHORED, FALSE);
2391   TEST_MATCH("a", 0, 0, "b", -1, 0, G_REGEX_ANCHORED, FALSE);
2392   TEST_MATCH("a|b", 0, 0, "a", -1, 0, 0, TRUE);
2393   TEST_MATCH("\\d", 0, 0, EURO, -1, 0, 0, FALSE);
2394   TEST_MATCH("^.$", 0, 0, EURO, -1, 0, 0, TRUE);
2395   TEST_MATCH("^.{3}$", 0, 0, EURO, -1, 0, 0, FALSE);
2396   TEST_MATCH("^.$", G_REGEX_RAW, 0, EURO, -1, 0, 0, FALSE);
2397   TEST_MATCH("^.{3}$", G_REGEX_RAW, 0, EURO, -1, 0, 0, TRUE);
2398   TEST_MATCH(AGRAVE, G_REGEX_CASELESS, 0, AGRAVE_UPPER, -1, 0, 0, TRUE);
2399
2400   /* New lines handling. */
2401   TEST_MATCH("^a\\Rb$", 0, 0, "a\r\nb", -1, 0, 0, TRUE);
2402   TEST_MATCH("^a\\Rb$", 0, 0, "a\nb", -1, 0, 0, TRUE);
2403   TEST_MATCH("^a\\Rb$", 0, 0, "a\rb", -1, 0, 0, TRUE);
2404   TEST_MATCH("^a\\Rb$", 0, 0, "a\n\rb", -1, 0, 0, FALSE);
2405   TEST_MATCH("^a\\R\\Rb$", 0, 0, "a\n\rb", -1, 0, 0, TRUE);
2406   TEST_MATCH("^a\\nb$", 0, 0, "a\r\nb", -1, 0, 0, FALSE);
2407   TEST_MATCH("^a\\r\\nb$", 0, 0, "a\r\nb", -1, 0, 0, TRUE);
2408
2409   TEST_MATCH("^b$", 0, 0, "a\nb\nc", -1, 0, 0, FALSE);
2410   TEST_MATCH("^b$", G_REGEX_MULTILINE, 0, "a\nb\nc", -1, 0, 0, TRUE);
2411   TEST_MATCH("^b$", G_REGEX_MULTILINE, 0, "a\r\nb\r\nc", -1, 0, 0, TRUE);
2412   TEST_MATCH("^b$", G_REGEX_MULTILINE, 0, "a\rb\rc", -1, 0, 0, TRUE);
2413   TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, 0, "a\nb\nc", -1, 0, 0, FALSE);
2414   TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_LF, 0, "a\nb\nc", -1, 0, 0, TRUE);
2415   TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CRLF, 0, "a\nb\nc", -1, 0, 0, FALSE);
2416   TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, 0, "a\r\nb\r\nc", -1, 0, 0, FALSE);
2417   TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_LF, 0, "a\r\nb\r\nc", -1, 0, 0, FALSE);
2418   TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CRLF, 0, "a\r\nb\r\nc", -1, 0, 0, TRUE);
2419   TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, 0, "a\rb\rc", -1, 0, 0, TRUE);
2420   TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_LF, 0, "a\rb\rc", -1, 0, 0, FALSE);
2421   TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CRLF, 0, "a\rb\rc", -1, 0, 0, FALSE);
2422   TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_CR, "a\nb\nc", -1, 0, 0, FALSE);
2423   TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_LF, "a\nb\nc", -1, 0, 0, TRUE);
2424   TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_CRLF, "a\nb\nc", -1, 0, 0, FALSE);
2425   TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_CR, "a\r\nb\r\nc", -1, 0, 0, FALSE);
2426   TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_LF, "a\r\nb\r\nc", -1, 0, 0, FALSE);
2427   TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_CRLF, "a\r\nb\r\nc", -1, 0, 0, TRUE);
2428   TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_CR, "a\rb\rc", -1, 0, 0, TRUE);
2429   TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_LF, "a\rb\rc", -1, 0, 0, FALSE);
2430   TEST_MATCH("^b$", G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_CRLF, "a\rb\rc", -1, 0, 0, FALSE);
2431
2432   TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_ANY, "a\nb\nc", -1, 0, 0, TRUE);
2433   TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_ANY, "a\rb\rc", -1, 0, 0, TRUE);
2434   TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_ANY, "a\r\nb\r\nc", -1, 0, 0, TRUE);
2435   TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_LF, "a\nb\nc", -1, 0, 0, TRUE);
2436   TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_LF, "a\rb\rc", -1, 0, 0, FALSE);
2437   TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_CRLF, "a\r\nb\r\nc", -1, 0, 0, TRUE);
2438   TEST_MATCH("^b$", G_REGEX_MULTILINE | G_REGEX_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_CRLF, "a\rb\rc", -1, 0, 0, FALSE);
2439
2440   TEST_MATCH("a#\nb", G_REGEX_EXTENDED, 0, "a", -1, 0, 0, FALSE);
2441   TEST_MATCH("a#\r\nb", G_REGEX_EXTENDED, 0, "a", -1, 0, 0, FALSE);
2442   TEST_MATCH("a#\rb", G_REGEX_EXTENDED, 0, "a", -1, 0, 0, FALSE);
2443   TEST_MATCH("a#\nb", G_REGEX_EXTENDED, G_REGEX_MATCH_NEWLINE_CR, "a", -1, 0, 0, FALSE);
2444   TEST_MATCH("a#\nb", G_REGEX_EXTENDED | G_REGEX_NEWLINE_CR, 0, "a", -1, 0, 0, TRUE);
2445
2446   TEST_MATCH("line\nbreak", G_REGEX_MULTILINE, 0, "this is a line\nbreak", -1, 0, 0, TRUE);
2447   TEST_MATCH("line\nbreak", G_REGEX_MULTILINE | G_REGEX_FIRSTLINE, 0, "first line\na line\nbreak", -1, 0, 0, FALSE);
2448
2449   /* This failed with PCRE 7.2 (gnome bug #455640) */
2450   TEST_MATCH(".*$", 0, 0, "\xe1\xbb\x85", -1, 0, 0, TRUE);
2451
2452   /* Test that othercasing in our pcre/glib integration is bug-for-bug compatible
2453    * with pcre's internal tables. Bug #678273 */
2454   TEST_MATCH("[Ç„]", G_REGEX_CASELESS, 0, "Ç„", -1, 0, 0, TRUE);
2455   TEST_MATCH("[Ç„]", G_REGEX_CASELESS, 0, "dž", -1, 0, 0, TRUE);
2456 #if PCRE_MAJOR > 8 || (PCRE_MAJOR == 8 && PCRE_MINOR >= 32)
2457   /* This would incorrectly fail to match in pcre < 8.32, so only assert
2458    * this for known-good pcre. */
2459   TEST_MATCH("[Ç„]", G_REGEX_CASELESS, 0, "Ç…", -1, 0, 0, TRUE);
2460 #endif
2461
2462   /* TEST_MATCH_NEXT#(pattern, string, string_len, start_position, ...) */
2463   TEST_MATCH_NEXT0("a", "x", -1, 0);
2464   TEST_MATCH_NEXT0("a", "ax", -1, 1);
2465   TEST_MATCH_NEXT0("a", "xa", 1, 0);
2466   TEST_MATCH_NEXT0("a", "axa", 1, 2);
2467   TEST_MATCH_NEXT1("a", "a", -1, 0, "a", 0, 1);
2468   TEST_MATCH_NEXT1("a", "xax", -1, 0, "a", 1, 2);
2469   TEST_MATCH_NEXT1(EURO, ENG EURO, -1, 0, EURO, 2, 5);
2470   TEST_MATCH_NEXT1("a*", "", -1, 0, "", 0, 0);
2471   TEST_MATCH_NEXT2("a*", "aa", -1, 0, "aa", 0, 2, "", 2, 2);
2472   TEST_MATCH_NEXT2(EURO "*", EURO EURO, -1, 0, EURO EURO, 0, 6, "", 6, 6);
2473   TEST_MATCH_NEXT2("a", "axa", -1, 0, "a", 0, 1, "a", 2, 3);
2474   TEST_MATCH_NEXT2("a+", "aaxa", -1, 0, "aa", 0, 2, "a", 3, 4);
2475   TEST_MATCH_NEXT2("a", "aa", -1, 0, "a", 0, 1, "a", 1, 2);
2476   TEST_MATCH_NEXT2("a", "ababa", -1, 2, "a", 2, 3, "a", 4, 5);
2477   TEST_MATCH_NEXT2(EURO "+", EURO "-" EURO, -1, 0, EURO, 0, 3, EURO, 4, 7);
2478   TEST_MATCH_NEXT3("", "ab", -1, 0, "", 0, 0, "", 1, 1, "", 2, 2);
2479   TEST_MATCH_NEXT3("", AGRAVE "b", -1, 0, "", 0, 0, "", 2, 2, "", 3, 3);
2480   TEST_MATCH_NEXT3("a", "aaxa", -1, 0, "a", 0, 1, "a", 1, 2, "a", 3, 4);
2481   TEST_MATCH_NEXT3("a", "aa" OGRAVE "a", -1, 0, "a", 0, 1, "a", 1, 2, "a", 4, 5);
2482   TEST_MATCH_NEXT3("a*", "aax", -1, 0, "aa", 0, 2, "", 2, 2, "", 3, 3);
2483   TEST_MATCH_NEXT3("(?=[A-Z0-9])", "RegExTest", -1, 0, "", 0, 0, "", 3, 3, "", 5, 5);
2484   TEST_MATCH_NEXT4("a*", "aaxa", -1, 0, "aa", 0, 2, "", 2, 2, "a", 3, 4, "", 4, 4);
2485
2486   /* TEST_MATCH_COUNT(pattern, string, start_position, match_opts, expected_count) */
2487   TEST_MATCH_COUNT("a", "", 0, 0, 0);
2488   TEST_MATCH_COUNT("a", "a", 0, 0, 1);
2489   TEST_MATCH_COUNT("a", "a", 1, 0, 0);
2490   TEST_MATCH_COUNT("(.)", "a", 0, 0, 2);
2491   TEST_MATCH_COUNT("(.)", EURO, 0, 0, 2);
2492   TEST_MATCH_COUNT("(?:.)", "a", 0, 0, 1);
2493   TEST_MATCH_COUNT("(?P<A>.)", "a", 0, 0, 2);
2494   TEST_MATCH_COUNT("a$", "a", 0, G_REGEX_MATCH_NOTEOL, 0);
2495   TEST_MATCH_COUNT("(a)?(b)", "b", 0, 0, 3);
2496   TEST_MATCH_COUNT("(a)?(b)", "ab", 0, 0, 3);
2497
2498   /* TEST_PARTIAL(pattern, string, expected) */
2499   TEST_PARTIAL("^ab", "a", TRUE);
2500   TEST_PARTIAL("^ab", "xa", FALSE);
2501   TEST_PARTIAL("ab", "xa", TRUE);
2502   TEST_PARTIAL("ab", "ab", FALSE); /* normal match. */
2503   TEST_PARTIAL("a+b", "aa", TRUE);
2504   TEST_PARTIAL("(a)+b", "aa", TRUE);
2505   TEST_PARTIAL("a?b", "a", TRUE);
2506
2507   /* Test soft vs. hard partial matching */
2508   TEST_PARTIAL_FULL("cat(fish)?", "cat", G_REGEX_MATCH_PARTIAL_SOFT, FALSE);
2509   TEST_PARTIAL_FULL("cat(fish)?", "cat", G_REGEX_MATCH_PARTIAL_HARD, TRUE);
2510
2511   /* TEST_SUB_PATTERN(pattern, string, start_position, sub_n, expected_sub,
2512    *                  expected_start, expected_end) */
2513   TEST_SUB_PATTERN("a", "a", 0, 0, "a", 0, 1);
2514   TEST_SUB_PATTERN("a(.)", "ab", 0, 1, "b", 1, 2);
2515   TEST_SUB_PATTERN("a(.)", "a" EURO, 0, 1, EURO, 1, 4);
2516   TEST_SUB_PATTERN("(?:.*)(a)(.)", "xxa" ENG, 0, 2, ENG, 3, 5);
2517   TEST_SUB_PATTERN("(" HSTROKE ")", "a" HSTROKE ENG, 0, 1, HSTROKE, 1, 3);
2518   TEST_SUB_PATTERN("a", "a", 0, 1, NULL, UNTOUCHED, UNTOUCHED);
2519   TEST_SUB_PATTERN("a", "a", 0, 1, NULL, UNTOUCHED, UNTOUCHED);
2520   TEST_SUB_PATTERN("(a)?(b)", "b", 0, 0, "b", 0, 1);
2521   TEST_SUB_PATTERN("(a)?(b)", "b", 0, 1, "", -1, -1);
2522   TEST_SUB_PATTERN("(a)?(b)", "b", 0, 2, "b", 0, 1);
2523
2524   /* TEST_NAMED_SUB_PATTERN(pattern, string, start_position, sub_name,
2525    *                        expected_sub, expected_start, expected_end) */
2526   TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?", "ab", 0, "A", "b", 1, 2);
2527   TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?", "aab", 1, "A", "b", 2, 3);
2528   TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?", EURO "ab", 0, "A", "b", 4, 5);
2529   TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?", EURO "ab", 0, "B", NULL, UNTOUCHED, UNTOUCHED);
2530   TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?", EURO "ab", 0, "C", NULL, UNTOUCHED, UNTOUCHED);
2531   TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?", "a" EGRAVE "x", 0, "A", EGRAVE, 1, 3);
2532   TEST_NAMED_SUB_PATTERN("a(?P<A>.)(?P<B>.)?", "a" EGRAVE "x", 0, "B", "x", 3, 4);
2533   TEST_NAMED_SUB_PATTERN("(?P<A>a)?(?P<B>b)", "b", 0, "A", "", -1, -1);
2534   TEST_NAMED_SUB_PATTERN("(?P<A>a)?(?P<B>b)", "b", 0, "B", "b", 0, 1);
2535
2536   /* TEST_NAMED_SUB_PATTERN_DUPNAMES(pattern, string, start_position, sub_name,
2537    *                                 expected_sub, expected_start, expected_end) */
2538   TEST_NAMED_SUB_PATTERN_DUPNAMES("(?P<N>a)|(?P<N>b)", "ab", 0, "N", "a", 0, 1);
2539   TEST_NAMED_SUB_PATTERN_DUPNAMES("(?P<N>aa)|(?P<N>a)", "aa", 0, "N", "aa", 0, 2);
2540   TEST_NAMED_SUB_PATTERN_DUPNAMES("(?P<N>aa)(?P<N>a)", "aaa", 0, "N", "aa", 0, 2);
2541   TEST_NAMED_SUB_PATTERN_DUPNAMES("(?P<N>x)|(?P<N>a)", "a", 0, "N", "a", 0, 1);
2542   TEST_NAMED_SUB_PATTERN_DUPNAMES("(?P<N>x)y|(?P<N>a)b", "ab", 0, "N", "a", 0, 1);
2543
2544   /* DUPNAMES option inside the pattern */
2545   TEST_NAMED_SUB_PATTERN("(?J)(?P<N>a)|(?P<N>b)", "ab", 0, "N", "a", 0, 1);
2546   TEST_NAMED_SUB_PATTERN("(?J)(?P<N>aa)|(?P<N>a)", "aa", 0, "N", "aa", 0, 2);
2547   TEST_NAMED_SUB_PATTERN("(?J)(?P<N>aa)(?P<N>a)", "aaa", 0, "N", "aa", 0, 2);
2548   TEST_NAMED_SUB_PATTERN("(?J)(?P<N>x)|(?P<N>a)", "a", 0, "N", "a", 0, 1);
2549   TEST_NAMED_SUB_PATTERN("(?J)(?P<N>x)y|(?P<N>a)b", "ab", 0, "N", "a", 0, 1);
2550
2551   /* TEST_FETCH_ALL#(pattern, string, ...) */
2552   TEST_FETCH_ALL0("a", "");
2553   TEST_FETCH_ALL0("a", "b");
2554   TEST_FETCH_ALL1("a", "a", "a");
2555   TEST_FETCH_ALL1("a+", "aa", "aa");
2556   TEST_FETCH_ALL1("(?:a)", "a", "a");
2557   TEST_FETCH_ALL2("(a)", "a", "a", "a");
2558   TEST_FETCH_ALL2("a(.)", "ab", "ab", "b");
2559   TEST_FETCH_ALL2("a(.)", "a" HSTROKE, "a" HSTROKE, HSTROKE);
2560   TEST_FETCH_ALL3("(?:.*)(a)(.)", "xyazk", "xyaz", "a", "z");
2561   TEST_FETCH_ALL3("(?P<A>.)(a)", "xa", "xa", "x", "a");
2562   TEST_FETCH_ALL3("(?P<A>.)(a)", ENG "a", ENG "a", ENG, "a");
2563   TEST_FETCH_ALL3("(a)?(b)", "b", "b", "", "b");
2564   TEST_FETCH_ALL3("(a)?(b)", "ab", "ab", "a", "b");
2565
2566   /* TEST_SPLIT_SIMPLE#(pattern, string, ...) */
2567   TEST_SPLIT_SIMPLE0("", "");
2568   TEST_SPLIT_SIMPLE0("a", "");
2569   TEST_SPLIT_SIMPLE1(",", "a", "a");
2570   TEST_SPLIT_SIMPLE1("(,)\\s*", "a", "a");
2571   TEST_SPLIT_SIMPLE2(",", "a,b", "a", "b");
2572   TEST_SPLIT_SIMPLE3(",", "a,b,c", "a", "b", "c");
2573   TEST_SPLIT_SIMPLE3(",\\s*", "a,b,c", "a", "b", "c");
2574   TEST_SPLIT_SIMPLE3(",\\s*", "a, b, c", "a", "b", "c");
2575   TEST_SPLIT_SIMPLE3("(,)\\s*", "a,b", "a", ",", "b");
2576   TEST_SPLIT_SIMPLE3("(,)\\s*", "a, b", "a", ",", "b");
2577   TEST_SPLIT_SIMPLE2("\\s", "ab c", "ab", "c");
2578   TEST_SPLIT_SIMPLE3("\\s*", "ab c", "a", "b", "c");
2579   /* Not matched sub-strings. */
2580   TEST_SPLIT_SIMPLE2("a|(b)", "xay", "x", "y");
2581   TEST_SPLIT_SIMPLE3("a|(b)", "xby", "x", "b", "y");
2582   /* Empty matches. */
2583   TEST_SPLIT_SIMPLE3("", "abc", "a", "b", "c");
2584   TEST_SPLIT_SIMPLE3(" *", "ab c", "a", "b", "c");
2585   /* Invalid patterns. */
2586   TEST_SPLIT_SIMPLE0("\\", "");
2587   TEST_SPLIT_SIMPLE0("[", "");
2588
2589   /* TEST_SPLIT#(pattern, string, start_position, max_tokens, ...) */
2590   TEST_SPLIT0("", "", 0, 0);
2591   TEST_SPLIT0("a", "", 0, 0);
2592   TEST_SPLIT0("a", "", 0, 1);
2593   TEST_SPLIT0("a", "", 0, 2);
2594   TEST_SPLIT0("a", "a", 1, 0);
2595   TEST_SPLIT1(",", "a", 0, 0, "a");
2596   TEST_SPLIT1(",", "a,b", 0, 1, "a,b");
2597   TEST_SPLIT1("(,)\\s*", "a", 0, 0, "a");
2598   TEST_SPLIT1(",", "a,b", 2, 0, "b");
2599   TEST_SPLIT2(",", "a,b", 0, 0, "a", "b");
2600   TEST_SPLIT2(",", "a,b,c", 0, 2, "a", "b,c");
2601   TEST_SPLIT2(",", "a,b", 1, 0, "", "b");
2602   TEST_SPLIT2(",", "a,", 0, 0, "a", "");
2603   TEST_SPLIT3(",", "a,b,c", 0, 0, "a", "b", "c");
2604   TEST_SPLIT3(",\\s*", "a,b,c", 0, 0, "a", "b", "c");
2605   TEST_SPLIT3(",\\s*", "a, b, c", 0, 0, "a", "b", "c");
2606   TEST_SPLIT3("(,)\\s*", "a,b", 0, 0, "a", ",", "b");
2607   TEST_SPLIT3("(,)\\s*", "a, b", 0, 0, "a", ",", "b");
2608   /* Not matched sub-strings. */
2609   TEST_SPLIT2("a|(b)", "xay", 0, 0, "x", "y");
2610   TEST_SPLIT3("a|(b)", "xby", 0, -1, "x", "b", "y");
2611   /* Empty matches. */
2612   TEST_SPLIT2(" *", "ab c", 1, 0, "b", "c");
2613   TEST_SPLIT3("", "abc", 0, 0, "a", "b", "c");
2614   TEST_SPLIT3(" *", "ab c", 0, 0, "a", "b", "c");
2615   TEST_SPLIT1(" *", "ab c", 0, 1, "ab c");
2616   TEST_SPLIT2(" *", "ab c", 0, 2, "a", "b c");
2617   TEST_SPLIT3(" *", "ab c", 0, 3, "a", "b", "c");
2618   TEST_SPLIT3(" *", "ab c", 0, 4, "a", "b", "c");
2619
2620   /* TEST_CHECK_REPLACEMENT(string_to_expand, expected, expected_refs) */
2621   TEST_CHECK_REPLACEMENT("", TRUE, FALSE);
2622   TEST_CHECK_REPLACEMENT("a", TRUE, FALSE);
2623   TEST_CHECK_REPLACEMENT("\\t\\n\\v\\r\\f\\a\\b\\\\\\x{61}", TRUE, FALSE);
2624   TEST_CHECK_REPLACEMENT("\\0", TRUE, TRUE);
2625   TEST_CHECK_REPLACEMENT("\\n\\2", TRUE, TRUE);
2626   TEST_CHECK_REPLACEMENT("\\g<foo>", TRUE, TRUE);
2627   /* Invalid strings */
2628   TEST_CHECK_REPLACEMENT("\\Q", FALSE, FALSE);
2629   TEST_CHECK_REPLACEMENT("x\\Ay", FALSE, FALSE);
2630
2631   /* TEST_EXPAND(pattern, string, string_to_expand, raw, expected) */
2632   TEST_EXPAND("a", "a", "", FALSE, "");
2633   TEST_EXPAND("a", "a", "\\0", FALSE, "a");
2634   TEST_EXPAND("a", "a", "\\1", FALSE, "");
2635   TEST_EXPAND("(a)", "ab", "\\1", FALSE, "a");
2636   TEST_EXPAND("(a)", "a", "\\1", FALSE, "a");
2637   TEST_EXPAND("(a)", "a", "\\g<1>", FALSE, "a");
2638   TEST_EXPAND("a", "a", "\\0130", FALSE, "X");
2639   TEST_EXPAND("a", "a", "\\\\\\0", FALSE, "\\a");
2640   TEST_EXPAND("a(?P<G>.)c", "xabcy", "X\\g<G>X", FALSE, "XbX");
2641 #ifndef USE_SYSTEM_PCRE
2642   /* PCRE >= 8.34 no longer allows this usage. */
2643   TEST_EXPAND("(.)(?P<1>.)", "ab", "\\1", FALSE, "a");
2644   TEST_EXPAND("(.)(?P<1>.)", "ab", "\\g<1>", FALSE, "a");
2645 #endif
2646   TEST_EXPAND(".", EURO, "\\0", FALSE, EURO);
2647   TEST_EXPAND("(.)", EURO, "\\1", FALSE, EURO);
2648   TEST_EXPAND("(?P<G>.)", EURO, "\\g<G>", FALSE, EURO);
2649   TEST_EXPAND(".", "a", EURO, FALSE, EURO);
2650   TEST_EXPAND(".", "a", EURO "\\0", FALSE, EURO "a");
2651   TEST_EXPAND(".", "", "\\Lab\\Ec", FALSE, "abc");
2652   TEST_EXPAND(".", "", "\\LaB\\EC", FALSE, "abC");
2653   TEST_EXPAND(".", "", "\\Uab\\Ec", FALSE, "ABc");
2654   TEST_EXPAND(".", "", "a\\ubc", FALSE, "aBc");
2655   TEST_EXPAND(".", "", "a\\lbc", FALSE, "abc");
2656   TEST_EXPAND(".", "", "A\\uBC", FALSE, "ABC");
2657   TEST_EXPAND(".", "", "A\\lBC", FALSE, "AbC");
2658   TEST_EXPAND(".", "", "A\\l\\\\BC", FALSE, "A\\BC");
2659   TEST_EXPAND(".", "", "\\L" AGRAVE "\\E", FALSE, AGRAVE);
2660   TEST_EXPAND(".", "", "\\U" AGRAVE "\\E", FALSE, AGRAVE_UPPER);
2661   TEST_EXPAND(".", "", "\\u" AGRAVE "a", FALSE, AGRAVE_UPPER "a");
2662   TEST_EXPAND(".", "ab", "x\\U\\0y\\Ez", FALSE, "xAYz");
2663   TEST_EXPAND(".(.)", "AB", "x\\L\\1y\\Ez", FALSE, "xbyz");
2664   TEST_EXPAND(".", "ab", "x\\u\\0y\\Ez", FALSE, "xAyz");
2665   TEST_EXPAND(".(.)", "AB", "x\\l\\1y\\Ez", FALSE, "xbyz");
2666   TEST_EXPAND(".(.)", "a" AGRAVE_UPPER, "x\\l\\1y", FALSE, "x" AGRAVE "y");
2667   TEST_EXPAND("a", "bab", "\\x{61}", FALSE, "a");
2668   TEST_EXPAND("a", "bab", "\\x61", FALSE, "a");
2669   TEST_EXPAND("a", "bab", "\\x5a", FALSE, "Z");
2670   TEST_EXPAND("a", "bab", "\\0\\x5A", FALSE, "aZ");
2671   TEST_EXPAND("a", "bab", "\\1\\x{5A}", FALSE, "Z");
2672   TEST_EXPAND("a", "bab", "\\x{00E0}", FALSE, AGRAVE);
2673   TEST_EXPAND("", "bab", "\\x{0634}", FALSE, SHEEN);
2674   TEST_EXPAND("", "bab", "\\x{634}", FALSE, SHEEN);
2675   TEST_EXPAND("", "", "\\t", FALSE, "\t");
2676   TEST_EXPAND("", "", "\\v", FALSE, "\v");
2677   TEST_EXPAND("", "", "\\r", FALSE, "\r");
2678   TEST_EXPAND("", "", "\\n", FALSE, "\n");
2679   TEST_EXPAND("", "", "\\f", FALSE, "\f");
2680   TEST_EXPAND("", "", "\\a", FALSE, "\a");
2681   TEST_EXPAND("", "", "\\b", FALSE, "\b");
2682   TEST_EXPAND("a(.)", "abc", "\\0\\b\\1", FALSE, "ab\bb");
2683   TEST_EXPAND("a(.)", "abc", "\\0141", FALSE, "a");
2684   TEST_EXPAND("a(.)", "abc", "\\078", FALSE, "\a8");
2685   TEST_EXPAND("a(.)", "abc", "\\077", FALSE, "?");
2686   TEST_EXPAND("a(.)", "abc", "\\0778", FALSE, "?8");
2687   TEST_EXPAND("a(.)", "a" AGRAVE "b", "\\1", FALSE, AGRAVE);
2688   TEST_EXPAND("a(.)", "a" AGRAVE "b", "\\1", TRUE, "\xc3");
2689   TEST_EXPAND("a(.)", "a" AGRAVE "b", "\\0", TRUE, "a\xc3");
2690   /* Invalid strings. */
2691   TEST_EXPAND("", "", "\\Q", FALSE, NULL);
2692   TEST_EXPAND("", "", "x\\Ay", FALSE, NULL);
2693   TEST_EXPAND("", "", "\\g<", FALSE, NULL);
2694   TEST_EXPAND("", "", "\\g<>", FALSE, NULL);
2695   TEST_EXPAND("", "", "\\g<1a>", FALSE, NULL);
2696   TEST_EXPAND("", "", "\\g<a$>", FALSE, NULL);
2697   TEST_EXPAND("", "", "\\", FALSE, NULL);
2698   TEST_EXPAND("a", "a", "\\x{61", FALSE, NULL);
2699   TEST_EXPAND("a", "a", "\\x6X", FALSE, NULL);
2700   /* Pattern-less. */
2701   TEST_EXPAND(NULL, NULL, "", FALSE, "");
2702   TEST_EXPAND(NULL, NULL, "\\n", FALSE, "\n");
2703   /* Invalid strings */
2704   TEST_EXPAND(NULL, NULL, "\\Q", FALSE, NULL);
2705   TEST_EXPAND(NULL, NULL, "x\\Ay", FALSE, NULL);
2706
2707   /* TEST_REPLACE(pattern, string, start_position, replacement, expected) */
2708   TEST_REPLACE("a", "ababa", 0, "A", "AbAbA");
2709   TEST_REPLACE("a", "ababa", 1, "A", "abAbA");
2710   TEST_REPLACE("a", "ababa", 2, "A", "abAbA");
2711   TEST_REPLACE("a", "ababa", 3, "A", "ababA");
2712   TEST_REPLACE("a", "ababa", 4, "A", "ababA");
2713   TEST_REPLACE("a", "ababa", 5, "A", "ababa");
2714   TEST_REPLACE("a", "ababa", 6, "A", "ababa");
2715   TEST_REPLACE("a", "abababa", 2, "A", "abAbAbA");
2716   TEST_REPLACE("a", "abab", 0, "A", "AbAb");
2717   TEST_REPLACE("a", "baba", 0, "A", "bAbA");
2718   TEST_REPLACE("a", "bab", 0, "A", "bAb");
2719   TEST_REPLACE("$^", "abc", 0, "X", "abc");
2720   TEST_REPLACE("(.)a", "ciao", 0, "a\\1", "caio");
2721   TEST_REPLACE("a.", "abc", 0, "\\0\\0", "ababc");
2722   TEST_REPLACE("a", "asd", 0, "\\0101", "Asd");
2723   TEST_REPLACE("(a).\\1", "aba cda", 0, "\\1\\n", "a\n cda");
2724   TEST_REPLACE("a" AGRAVE "a", "a" AGRAVE "a", 0, "x", "x");
2725   TEST_REPLACE("a" AGRAVE "a", "a" AGRAVE "a", 0, OGRAVE, OGRAVE);
2726   TEST_REPLACE("[^-]", "-" EURO "-x-" HSTROKE, 0, "a", "-a-a-a");
2727   TEST_REPLACE("[^-]", "-" EURO "-" HSTROKE, 0, "a\\g<0>a", "-a" EURO "a-a" HSTROKE "a");
2728   TEST_REPLACE("-", "-" EURO "-" HSTROKE, 0, "", EURO HSTROKE);
2729   TEST_REPLACE(".*", "hello", 0, "\\U\\0\\E", "HELLO");
2730   TEST_REPLACE(".*", "hello", 0, "\\u\\0", "Hello");
2731   TEST_REPLACE("\\S+", "hello world", 0, "\\U-\\0-", "-HELLO- -WORLD-");
2732   TEST_REPLACE(".", "a", 0, "\\A", NULL);
2733   TEST_REPLACE(".", "a", 0, "\\g", NULL);
2734
2735   /* TEST_REPLACE_LIT(pattern, string, start_position, replacement, expected) */
2736   TEST_REPLACE_LIT("a", "ababa", 0, "A", "AbAbA");
2737   TEST_REPLACE_LIT("a", "ababa", 1, "A", "abAbA");
2738   TEST_REPLACE_LIT("a", "ababa", 2, "A", "abAbA");
2739   TEST_REPLACE_LIT("a", "ababa", 3, "A", "ababA");
2740   TEST_REPLACE_LIT("a", "ababa", 4, "A", "ababA");
2741   TEST_REPLACE_LIT("a", "ababa", 5, "A", "ababa");
2742   TEST_REPLACE_LIT("a", "ababa", 6, "A", "ababa");
2743   TEST_REPLACE_LIT("a", "abababa", 2, "A", "abAbAbA");
2744   TEST_REPLACE_LIT("a", "abcadaa", 0, "A", "AbcAdAA");
2745   TEST_REPLACE_LIT("$^", "abc", 0, "X", "abc");
2746   TEST_REPLACE_LIT("(.)a", "ciao", 0, "a\\1", "ca\\1o");
2747   TEST_REPLACE_LIT("a.", "abc", 0, "\\0\\0\\n", "\\0\\0\\nc");
2748   TEST_REPLACE_LIT("a" AGRAVE "a", "a" AGRAVE "a", 0, "x", "x");
2749   TEST_REPLACE_LIT("a" AGRAVE "a", "a" AGRAVE "a", 0, OGRAVE, OGRAVE);
2750   TEST_REPLACE_LIT(AGRAVE, "-" AGRAVE "-" HSTROKE, 0, "a" ENG "a", "-a" ENG "a-" HSTROKE);
2751   TEST_REPLACE_LIT("[^-]", "-" EURO "-" AGRAVE "-" HSTROKE, 0, "a", "-a-a-a");
2752   TEST_REPLACE_LIT("[^-]", "-" EURO "-" AGRAVE, 0, "a\\g<0>a", "-a\\g<0>a-a\\g<0>a");
2753   TEST_REPLACE_LIT("-", "-" EURO "-" AGRAVE "-" HSTROKE, 0, "", EURO AGRAVE HSTROKE);
2754   TEST_REPLACE_LIT("(?=[A-Z0-9])", "RegExTest", 0, "_", "_Reg_Ex_Test");
2755   TEST_REPLACE_LIT("(?=[A-Z0-9])", "RegExTest", 1, "_", "Reg_Ex_Test");
2756
2757   /* TEST_GET_STRING_NUMBER(pattern, name, expected_num) */
2758   TEST_GET_STRING_NUMBER("", "A", -1);
2759   TEST_GET_STRING_NUMBER("(?P<A>.)", "A", 1);
2760   TEST_GET_STRING_NUMBER("(?P<A>.)", "B", -1);
2761   TEST_GET_STRING_NUMBER("(?P<A>.)(?P<B>a)", "A", 1);
2762   TEST_GET_STRING_NUMBER("(?P<A>.)(?P<B>a)", "B", 2);
2763   TEST_GET_STRING_NUMBER("(?P<A>.)(?P<B>a)", "C", -1);
2764   TEST_GET_STRING_NUMBER("(?P<A>.)(.)(?P<B>a)", "A", 1);
2765   TEST_GET_STRING_NUMBER("(?P<A>.)(.)(?P<B>a)", "B", 3);
2766   TEST_GET_STRING_NUMBER("(?P<A>.)(.)(?P<B>a)", "C", -1);
2767   TEST_GET_STRING_NUMBER("(?:a)(?P<A>.)", "A", 1);
2768   TEST_GET_STRING_NUMBER("(?:a)(?P<A>.)", "B", -1);
2769
2770   /* TEST_ESCAPE_NUL(string, length, expected) */
2771   TEST_ESCAPE_NUL("hello world", -1, "hello world");
2772   TEST_ESCAPE_NUL("hello\0world", -1, "hello");
2773   TEST_ESCAPE_NUL("\0world", -1, "");
2774   TEST_ESCAPE_NUL("hello world", 5, "hello");
2775   TEST_ESCAPE_NUL("hello.world", 11, "hello.world");
2776   TEST_ESCAPE_NUL("a(b\\b.$", 7, "a(b\\b.$");
2777   TEST_ESCAPE_NUL("hello\0", 6, "hello\\x00");
2778   TEST_ESCAPE_NUL("\0world", 6, "\\x00world");
2779   TEST_ESCAPE_NUL("\0\0", 2, "\\x00\\x00");
2780   TEST_ESCAPE_NUL("hello\0world", 11, "hello\\x00world");
2781   TEST_ESCAPE_NUL("hello\0world\0", 12, "hello\\x00world\\x00");
2782   TEST_ESCAPE_NUL("hello\\\0world", 12, "hello\\x00world");
2783   TEST_ESCAPE_NUL("hello\\\\\0world", 13, "hello\\\\\\x00world");
2784   TEST_ESCAPE_NUL("|()[]{}^$*+?.", 13, "|()[]{}^$*+?.");
2785   TEST_ESCAPE_NUL("|()[]{}^$*+?.\\\\", 15, "|()[]{}^$*+?.\\\\");
2786
2787   /* TEST_ESCAPE(string, length, expected) */
2788   TEST_ESCAPE("hello world", -1, "hello world");
2789   TEST_ESCAPE("hello world", 5, "hello");
2790   TEST_ESCAPE("hello.world", -1, "hello\\.world");
2791   TEST_ESCAPE("a(b\\b.$", -1, "a\\(b\\\\b\\.\\$");
2792   TEST_ESCAPE("hello\0world", -1, "hello");
2793   TEST_ESCAPE("hello\0world", 11, "hello\\0world");
2794   TEST_ESCAPE(EURO "*" ENG, -1, EURO "\\*" ENG);
2795   TEST_ESCAPE("a$", -1, "a\\$");
2796   TEST_ESCAPE("$a", -1, "\\$a");
2797   TEST_ESCAPE("a$a", -1, "a\\$a");
2798   TEST_ESCAPE("$a$", -1, "\\$a\\$");
2799   TEST_ESCAPE("$a$", 0, "");
2800   TEST_ESCAPE("$a$", 1, "\\$");
2801   TEST_ESCAPE("$a$", 2, "\\$a");
2802   TEST_ESCAPE("$a$", 3, "\\$a\\$");
2803   TEST_ESCAPE("$a$", 4, "\\$a\\$\\0");
2804   TEST_ESCAPE("|()[]{}^$*+?.", -1, "\\|\\(\\)\\[\\]\\{\\}\\^\\$\\*\\+\\?\\.");
2805   TEST_ESCAPE("a|a(a)a[a]a{a}a^a$a*a+a?a.a", -1,
2806               "a\\|a\\(a\\)a\\[a\\]a\\{a\\}a\\^a\\$a\\*a\\+a\\?a\\.a");
2807
2808   /* TEST_MATCH_ALL#(pattern, string, string_len, start_position, ...) */
2809   TEST_MATCH_ALL0("<.*>", "", -1, 0);
2810   TEST_MATCH_ALL0("a+", "", -1, 0);
2811   TEST_MATCH_ALL0("a+", "a", 0, 0);
2812   TEST_MATCH_ALL0("a+", "a", -1, 1);
2813   TEST_MATCH_ALL1("<.*>", "<a>", -1, 0, "<a>", 0, 3);
2814   TEST_MATCH_ALL1("a+", "a", -1, 0, "a", 0, 1);
2815   TEST_MATCH_ALL1("a+", "aa", 1, 0, "a", 0, 1);
2816   TEST_MATCH_ALL1("a+", "aa", -1, 1, "a", 1, 2);
2817   TEST_MATCH_ALL1("a+", "aa", 2, 1, "a", 1, 2);
2818   TEST_MATCH_ALL1(".+", ENG, -1, 0, ENG, 0, 2);
2819   TEST_MATCH_ALL2("<.*>", "<a><b>", -1, 0, "<a><b>", 0, 6, "<a>", 0, 3);
2820   TEST_MATCH_ALL2("a+", "aa", -1, 0, "aa", 0, 2, "a", 0, 1);
2821   TEST_MATCH_ALL2(".+", ENG EURO, -1, 0, ENG EURO, 0, 5, ENG, 0, 2);
2822   TEST_MATCH_ALL3("<.*>", "<a><b><c>", -1, 0, "<a><b><c>", 0, 9,
2823                   "<a><b>", 0, 6, "<a>", 0, 3);
2824   TEST_MATCH_ALL3("a+", "aaa", -1, 0, "aaa", 0, 3, "aa", 0, 2, "a", 0, 1);
2825
2826   /* NOTEMPTY matching */
2827   TEST_MATCH_NOTEMPTY("a?b?", "xyz", FALSE);
2828   TEST_MATCH_NOTEMPTY_ATSTART("a?b?", "xyz", TRUE);
2829
2830   return g_test_run ();
2831 }