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