1 /* Regular expression tests.
2 Copyright (C) 2003-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C 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.1 of the License, or (at your option) any later version.
10 The GNU C 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.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <sys/types.h>
29 replace_special_chars (char *str
)
31 for (; (str
= strpbrk (str
, "NTSZ")) != NULL
; ++str
)
34 case 'N': *str
= '\n'; break;
35 case 'T': *str
= '\t'; break;
36 case 'S': *str
= ' '; break;
37 case 'Z': *str
= '\0'; break;
42 glibc_re_syntax (char *str
)
44 char *p
, *end
= strchr (str
, '\0') + 1;
46 /* Replace [[:<:]] with \< and [[:>:]] with \>. */
47 for (p
= str
; (p
= strstr (p
, "[[:")) != NULL
; )
48 if ((p
[3] == '<' || p
[3] == '>') && strncmp (p
+ 4, ":]]", 3) == 0)
52 memmove (p
+ 2, p
+ 7, end
- p
- 7);
61 mb_replace (char *dst
, const char c
)
65 /* Replace a with \'a and A with \'A. */
74 /* Replace b with \v{c} and B with \v{C}. */
83 /* Replace c with \v{d} and C with \v{D}. */
92 /* Replace d with \'e and D with \'E. */
106 mb_frob_string (const char *str
, const char *letters
)
114 ret
= malloc (2 * strlen (str
) + 1);
118 for (src
= str
, dst
= ret
; *src
; ++src
)
119 if (strchr (letters
, *src
))
120 dst
= mb_replace (dst
, *src
);
127 /* Like mb_frob_string, but don't replace anything between
128 [: and :], [. and .] or [= and =] or characters escaped
132 mb_frob_pattern (const char *str
, const char *letters
)
136 int in_class
= 0, escaped
= 0;
141 ret
= malloc (2 * strlen (str
) + 1);
145 for (src
= str
, dst
= ret
; *src
; ++src
)
157 else if (!in_class
&& strchr (letters
, *src
))
158 dst
= mb_replace (dst
, *src
);
161 if (!in_class
&& *src
== '[' && strchr (":.=", src
[1]))
163 else if (in_class
&& *src
== ']' && strchr (":.=", src
[-1]))
172 check_match (regmatch_t
*rm
, int idx
, const char *string
,
173 const char *match
, const char *fail
)
175 if (match
[0] == '-' && match
[1] == '\0')
177 if (rm
[idx
].rm_so
== -1 && rm
[idx
].rm_eo
== -1)
179 printf ("%s rm[%d] unexpectedly matched\n", fail
, idx
);
183 if (rm
[idx
].rm_so
== -1 || rm
[idx
].rm_eo
== -1)
185 printf ("%s rm[%d] unexpectedly did not match\n", fail
, idx
);
191 if (rm
[idx
].rm_so
!= rm
[idx
].rm_eo
)
193 printf ("%s rm[%d] not empty\n", fail
, idx
);
197 if (strncmp (string
+ rm
[idx
].rm_so
, match
+ 1, strlen (match
+ 1) ?: 1))
199 printf ("%s rm[%d] not matching %s\n", fail
, idx
, match
);
205 if (rm
[idx
].rm_eo
- rm
[idx
].rm_so
!= strlen (match
)
206 || strncmp (string
+ rm
[idx
].rm_so
, match
,
207 rm
[idx
].rm_eo
- rm
[idx
].rm_so
))
209 printf ("%s rm[%d] not matching %s\n", fail
, idx
, match
);
217 test (const char *pattern
, int cflags
, const char *string
, int eflags
,
218 char *expect
, char *matches
, const char *fail
)
224 n
= regcomp (&re
, pattern
, cflags
);
230 static struct { reg_errcode_t code
; const char *name
; } codes
[]
231 #define C(x) { REG_##x, #x }
232 = { C(NOERROR
), C(NOMATCH
), C(BADPAT
), C(ECOLLATE
),
233 C(ECTYPE
), C(EESCAPE
), C(ESUBREG
), C(EBRACK
),
234 C(EPAREN
), C(EBRACE
), C(BADBR
), C(ERANGE
),
235 C(ESPACE
), C(BADRPT
) };
237 for (int i
= 0; i
< sizeof (codes
) / sizeof (codes
[0]); ++i
)
238 if (n
== codes
[i
].code
)
240 if (strcmp (string
, codes
[i
].name
))
242 printf ("%s regcomp returned REG_%s (expected REG_%s)\n",
243 fail
, codes
[i
].name
, string
);
249 printf ("%s regcomp return value REG_%d\n", fail
, n
);
253 regerror (n
, &re
, buf
, sizeof (buf
));
254 printf ("%s regcomp failed: %s\n", fail
, buf
);
262 /* The test case file assumes something only guaranteed by the
263 rxspencer regex implementation. Namely that for empty
264 expressions regcomp() return REG_EMPTY. This is not the case
265 for us and so we ignore this error. */
266 if (strcmp (string
, "EMPTY") == 0)
269 printf ("%s regcomp unexpectedly succeeded\n", fail
);
273 if (regexec (&re
, string
, 10, rm
, eflags
))
278 printf ("%s regexec failed\n", fail
);
286 printf ("%s regexec unexpectedly succeeded\n", fail
);
290 if (cflags
& REG_NOSUB
)
293 ret
= check_match (rm
, 0, string
, expect
, fail
);
297 for (n
= 1; ret
== 0 && n
< 10; ++n
)
303 p
= strchr (matches
, ',');
307 ret
= check_match (rm
, n
, string
, matches
?: "-", fail
);
321 mb_test (const char *pattern
, int cflags
, const char *string
, int eflags
,
322 char *expect
, const char *matches
, const char *letters
,
325 char *pattern_mb
= mb_frob_pattern (pattern
, letters
);
326 const char *string_mb
327 = eflags
== -1 ? string
: mb_frob_string (string
, letters
);
328 char *expect_mb
= mb_frob_string (expect
, letters
);
329 char *matches_mb
= mb_frob_string (matches
, letters
);
332 if (!pattern_mb
|| !string_mb
333 || (expect
&& !expect_mb
) || (matches
&& !matches_mb
))
335 printf ("%s %m", fail
);
339 ret
= test (pattern_mb
, cflags
, string_mb
, eflags
, expect_mb
,
344 if (string_mb
!= string
)
345 free ((char *) string_mb
);
351 mb_tests (const char *pattern
, int cflags
, const char *string
, int eflags
,
352 char *expect
, const char *matches
)
356 char letters
[9], fail
[20];
358 /* The tests aren't supposed to work with xdigit, since a-dA-D are
359 hex digits while \'a \'A \v{c}\v{C}\v{d}\v{D}\'e \'E are not. */
360 if (strstr (pattern
, "[:xdigit:]"))
363 /* XXX: regex ATM handles only single byte equivalence classes. */
364 if (strstr (pattern
, "[[=b=]]"))
367 for (i
= 1; i
< 16; ++i
)
372 if (!strchr (pattern
, 'a') && !strchr (string
, 'a')
373 && !strchr (pattern
, 'A') && !strchr (string
, 'A'))
375 *p
++ = 'a', *p
++ = 'A';
379 if (!strchr (pattern
, 'b') && !strchr (string
, 'b')
380 && !strchr (pattern
, 'B') && !strchr (string
, 'B'))
382 *p
++ = 'b', *p
++ = 'B';
386 if (!strchr (pattern
, 'c') && !strchr (string
, 'c')
387 && !strchr (pattern
, 'C') && !strchr (string
, 'C'))
389 *p
++ = 'c', *p
++ = 'C';
393 if (!strchr (pattern
, 'd') && !strchr (string
, 'd')
394 && !strchr (pattern
, 'D') && !strchr (string
, 'D'))
396 *p
++ = 'd', *p
++ = 'D';
399 sprintf (fail
, "UTF-8 %s FAIL", letters
);
400 ret
|= mb_test (pattern
, cflags
, string
, eflags
, expect
, matches
,
407 main (int argc
, char **argv
)
414 static int test_utf8
= 0;
415 static const struct option options
[] =
417 {"utf8", no_argument
, &test_utf8
, 1},
423 while (getopt_long (argc
, argv
, "", options
, NULL
) >= 0);
425 if (optind
+ 1 != argc
)
427 fprintf (stderr
, "Missing test filename\n");
431 f
= fopen (argv
[optind
], "r");
434 fprintf (stderr
, "Couldn't open %s\n", argv
[optind
]);
438 while ((len
= getline (&line
, &line_len
, f
)) > 0)
440 char *pattern
, *flagstr
, *string
, *expect
, *matches
, *p
;
441 int cflags
= REG_EXTENDED
, eflags
= 0, try_bre_ere
= 0;
443 if (line
[len
- 1] == '\n')
444 line
[len
- 1] = '\0';
446 /* Skip comments and empty lines. */
447 if (*line
== '#' || *line
== '\0')
453 pattern
= strtok (line
, "\t");
457 if (strcmp (pattern
, "\"\"") == 0)
460 flagstr
= strtok (NULL
, "\t");
464 string
= strtok (NULL
, "\t");
468 if (strcmp (string
, "\"\"") == 0)
471 for (p
= flagstr
; *p
; ++p
)
477 cflags
&= ~REG_EXTENDED
;
492 cflags
|= REG_NEWLINE
;
495 eflags
|= REG_NOTBOL
;
498 eflags
|= REG_NOTEOL
;
511 replace_special_chars (pattern
);
512 glibc_re_syntax (pattern
);
514 replace_special_chars (string
);
516 expect
= strtok (NULL
, "\t");
520 replace_special_chars (expect
);
521 matches
= strtok (NULL
, "\t");
523 replace_special_chars (matches
);
526 if (setlocale (LC_ALL
, "C") == NULL
)
528 puts ("setlocale C failed");
531 if (test (pattern
, cflags
, string
, eflags
, expect
, matches
, "FAIL")
533 && test (pattern
, cflags
& ~REG_EXTENDED
, string
, eflags
,
534 expect
, matches
, "FAIL")))
538 if (setlocale (LC_ALL
, "cs_CZ.UTF-8") == NULL
)
540 puts ("setlocale cs_CZ.UTF-8 failed");
543 else if (test (pattern
, cflags
, string
, eflags
, expect
, matches
,
546 && test (pattern
, cflags
& ~REG_EXTENDED
, string
,
547 eflags
, expect
, matches
, "UTF-8 FAIL")))
549 else if (mb_tests (pattern
, cflags
, string
, eflags
, expect
, matches
)
551 && mb_tests (pattern
, cflags
& ~REG_EXTENDED
, string
,
552 eflags
, expect
, matches
)))