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