1 /* Regular expression tests.
2 Copyright (C) 2003 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Jakub Jelinek <jakub@redhat.com>, 2003.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 #include <sys/types.h>
31 replace_special_chars (char *str)
33 for (; (str = strpbrk (str, "NTSZ")) != NULL; ++str)
36 case 'N': *str = '\n'; break;
37 case 'T': *str = '\t'; break;
38 case 'S': *str = ' '; break;
39 case 'Z': *str = '\0'; break;
44 glibc_re_syntax (char *str)
46 char *p, *end = strchr (str, '\0') + 1;
48 /* Replace [[:<:]] with \< and [[:>:]] with \>. */
49 for (p = str; (p = strstr (p, "[[:")) != NULL; )
50 if ((p[3] == '<' || p[3] == '>') && strncmp (p + 4, ":]]", 3) == 0)
54 memmove (p + 2, p + 7, end - p - 7);
63 mb_replace (char *dst, const char c)
67 /* Replace a with \'a and A with \'A. */
76 /* Replace b with \v{c} and B with \v{C}. */
85 /* Replace c with \v{d} and C with \v{D}. */
94 /* Replace d with \'e and D with \'E. */
108 mb_frob_string (const char *str, const char *letters)
116 ret = malloc (2 * strlen (str) + 1);
120 for (src = str, dst = ret; *src; ++src)
121 if (strchr (letters, *src))
122 dst = mb_replace (dst, *src);
129 /* Like mb_frob_string, but don't replace anything between
130 [: and :], [. and .] or [= and =]. */
133 mb_frob_pattern (const char *str, const char *letters)
142 ret = malloc (2 * strlen (str) + 1);
146 for (src = str, dst = ret; *src; ++src)
147 if (!in_class && strchr (letters, *src))
148 dst = mb_replace (dst, *src);
151 if (!in_class && *src == '[' && strchr (":.=", src[1]))
153 else if (in_class && *src == ']' && strchr (":.=", src[-1]))
162 check_match (regmatch_t *rm, int idx, const char *string,
163 const char *match, const char *fail)
165 if (match[0] == '-' && match[1] == '\0')
167 if (rm[idx].rm_so == -1 && rm[idx].rm_eo == -1)
169 printf ("%s rm[%d] unexpectedly matched\n", fail, idx);
173 if (rm[idx].rm_so == -1 || rm[idx].rm_eo == -1)
175 printf ("%s rm[%d] unexpectedly did not match\n", fail, idx);
181 if (rm[idx].rm_so != rm[idx].rm_eo)
183 printf ("%s rm[%d] not empty\n", fail, idx);
187 if (strncmp (string + rm[idx].rm_so, match + 1, strlen (match + 1) ?: 1))
189 printf ("%s rm[%d] not matching %s\n", fail, idx, match);
195 if (rm[idx].rm_eo - rm[idx].rm_so != strlen (match)
196 || strncmp (string + rm[idx].rm_so, match,
197 rm[idx].rm_eo - rm[idx].rm_so))
199 printf ("%s rm[%d] not matching %s\n", fail, idx, match);
207 test (const char *pattern, int cflags, const char *string, int eflags,
208 char *expect, char *matches, const char *fail)
214 n = regcomp (&re, pattern, cflags);
219 static struct { reg_errcode_t code; const char *name; } codes []
220 #define C(x) { REG_##x, #x }
221 = { C(NOERROR), C(NOMATCH), C(BADPAT), C(ECOLLATE),
222 C(ECTYPE), C(EESCAPE), C(ESUBREG), C(EBRACK),
223 C(EPAREN), C(EBRACE), C(BADBR), C(ERANGE),
224 C(ESPACE), C(BADRPT) };
226 for (int i = 0; i < sizeof (codes) / sizeof (codes[0]); ++i)
227 if (n == codes[i].code)
229 if (strcmp (string, codes[i].name))
231 printf ("%s regcomp returned REG_%s (expected REG_%s)\n",
232 fail, codes[i].name, string);
238 printf ("%s regcomp return value REG_%d\n", fail, n);
243 regerror (n, &re, buf, sizeof (buf));
244 printf ("%s regcomp failed: %s\n", fail, buf);
252 /* The test case file assumes something only guaranteed by the
253 rxspencer regex implementation. Namely that for empty
254 expressions regcomp() return REG_EMPTY. This is not the case
255 for us and so we ignore this error. */
256 if (strcmp (string, "EMPTY") == 0)
259 printf ("%s regcomp unexpectedly succeeded\n", fail);
263 if (regexec (&re, string, 10, rm, eflags))
268 printf ("%s regexec failed\n", fail);
276 printf ("%s regexec unexpectedly succeeded\n", fail);
280 if (cflags & REG_NOSUB)
283 ret = check_match (rm, 0, string, expect, fail);
287 for (n = 1; ret == 0 && n < 10; ++n)
293 p = strchr (matches, ',');
297 ret = check_match (rm, n, string, matches ?: "-", fail);
311 mb_test (const char *pattern, int cflags, const char *string, int eflags,
312 char *expect, const char *matches, const char *letters,
315 char *pattern_mb = mb_frob_pattern (pattern, letters);
316 const char *string_mb
317 = eflags == -1 ? string : mb_frob_string (string, letters);
318 char *expect_mb = mb_frob_string (expect, letters);
319 char *matches_mb = mb_frob_string (matches, letters);
322 if (!pattern_mb || !string_mb
323 || (expect && !expect_mb) || (matches && !matches_mb))
325 printf ("%s %m", fail);
329 ret = test (pattern_mb, cflags, string_mb, eflags, expect_mb,
334 if (string_mb != string)
335 free ((char *) string_mb);
341 mb_tests (const char *pattern, int cflags, const char *string, int eflags,
342 char *expect, const char *matches)
346 char letters[9], fail[20];
348 /* The tests aren't supposed to work with xdigit, since a-dA-D are
349 hex digits while \'a \'A \v{c}\v{C}\v{d}\v{D}\'e \'E are not. */
350 if (strstr (pattern, "[:xdigit:]"))
353 /* XXX: regex ATM handles only single byte equivalence classes. */
354 if (strstr (pattern, "[[=b=]]"))
357 for (i = 1; i < 16; ++i)
362 if (!strchr (pattern, 'a') && !strchr (string, 'a')
363 && !strchr (pattern, 'A') && !strchr (string, 'A'))
365 *p++ = 'a', *p++ = 'A';
369 if (!strchr (pattern, 'b') && !strchr (string, 'b')
370 && !strchr (pattern, 'B') && !strchr (string, 'B'))
372 *p++ = 'b', *p++ = 'B';
376 if (!strchr (pattern, 'c') && !strchr (string, 'c')
377 && !strchr (pattern, 'C') && !strchr (string, 'C'))
379 *p++ = 'c', *p++ = 'C';
383 if (!strchr (pattern, 'd') && !strchr (string, 'd')
384 && !strchr (pattern, 'D') && !strchr (string, 'D'))
386 *p++ = 'd', *p++ = 'D';
389 sprintf (fail, "UTF-8 %s FAIL", letters);
390 ret |= mb_test (pattern, cflags, string, eflags, expect, matches,
397 main (int argc, char **argv)
404 static int test_utf8 = 0;
405 static const struct option options[] =
407 {"utf8", no_argument, &test_utf8, 1},
413 while (getopt_long (argc, argv, "", options, NULL) >= 0);
415 if (optind + 1 != argc)
417 fprintf (stderr, "Missing test filename\n");
421 f = fopen (argv[optind], "r");
424 fprintf (stderr, "Couldn't open %s\n", argv[optind]);
428 while ((len = getline (&line, &line_len, f)) > 0)
430 char *pattern, *flagstr, *string, *expect, *matches, *p;
431 int cflags = REG_EXTENDED, eflags = 0, try_bre_ere = 0;
433 if (line[len - 1] == '\n')
434 line[len - 1] = '\0';
436 /* Skip comments and empty lines. */
437 if (*line == '#' || *line == '\0')
443 pattern = strtok (line, "\t");
447 if (strcmp (pattern, "\"\"") == 0)
450 flagstr = strtok (NULL, "\t");
454 string = strtok (NULL, "\t");
458 if (strcmp (string, "\"\"") == 0)
461 for (p = flagstr; *p; ++p)
467 cflags &= ~REG_EXTENDED;
482 cflags |= REG_NEWLINE;
485 eflags |= REG_NOTBOL;
488 eflags |= REG_NOTEOL;
501 replace_special_chars (pattern);
502 glibc_re_syntax (pattern);
504 replace_special_chars (string);
506 expect = strtok (NULL, "\t");
510 replace_special_chars (expect);
511 matches = strtok (NULL, "\t");
513 replace_special_chars (matches);
516 if (setlocale (LC_ALL, "C") == NULL)
518 puts ("setlocale C failed");
521 if (test (pattern, cflags, string, eflags, expect, matches, "FAIL")
523 && test (pattern, cflags & ~REG_EXTENDED, string, eflags,
524 expect, matches, "FAIL")))
528 if (setlocale (LC_ALL, "cs_CZ.UTF-8") == NULL)
530 puts ("setlocale cs_CZ.UTF-8 failed");
533 else if (test (pattern, cflags, string, eflags, expect, matches,
536 && test (pattern, cflags & ~REG_EXTENDED, string,
537 eflags, expect, matches, "UTF-8 FAIL")))
539 else if (mb_tests (pattern, cflags, string, eflags, expect, matches)
541 && mb_tests (pattern, cflags & ~REG_EXTENDED, string,
542 eflags, expect, matches)))