1 /* Test RE_HAT_LISTS_NOT_NEWLINE and RE_DOT_NEWLINE.
2    Copyright (C) 2007-2022 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
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.
9 
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.
14 
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/>.  */
18 
19 #include <regex.h>
20 #include <stdio.h>
21 #include <string.h>
22 
23 #include <support/test-driver.h>
24 #include <support/check.h>
25 
26 struct tests
27 {
28   const char *regex;
29   const char *string;
30   reg_syntax_t syntax;
31   int retval;
32 };
33 static const struct tests tests[] = {
34 #define EGREP RE_SYNTAX_EGREP
35 #define EGREP_NL (RE_SYNTAX_EGREP | RE_DOT_NEWLINE) & ~RE_HAT_LISTS_NOT_NEWLINE
36   { "a.b", "a\nb", EGREP, 0 },
37   { "a.b", "a\nb", EGREP_NL, 0 },
38   { "a[^x]b", "a\nb", EGREP, 0 },
39   { "a[^x]b", "a\nb", EGREP_NL, 0 },
40   /* While \S and \W are internally handled as [^[:space:]] and [^[:alnum:]_],
41      RE_HAT_LISTS_NOT_NEWLINE did not make any difference, so ensure
42      it doesn't change.  */
43   { "a\\Sb", "a\nb", EGREP, -1 },
44   { "a\\Sb", "a\nb", EGREP_NL, -1 },
45   { "a\\Wb", "a\nb", EGREP, 0 },
46   { "a\\Wb", "a\nb", EGREP_NL, 0 }
47 };
48 static const size_t tests_size = sizeof (tests) / sizeof (tests[0]);
49 
50 static int
do_test(void)51 do_test (void)
52 {
53   struct re_pattern_buffer r;
54 
55   for (size_t i = 0; i < tests_size; i++)
56     {
57       re_set_syntax (tests[i].syntax);
58       memset (&r, 0, sizeof (r));
59       const char *re = re_compile_pattern (tests[i].regex,
60 					   strlen (tests[i].regex), &r);
61       TEST_VERIFY (re == NULL);
62       if (re != NULL)
63         continue;
64 
65       size_t len = strlen (tests[i].string);
66       int rv = re_search (&r, tests[i].string, len, 0, len, NULL);
67       TEST_VERIFY (rv == tests[i].retval);
68       if (test_verbose > 0)
69         printf ("info: i=%zu rv=%d expected=%d\n", i, rv, tests[i].retval);
70 
71       regfree (&r);
72     }
73 
74   return 0;
75 }
76 
77 #include <support/test-driver.c>
78