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