maint: update copyright
[gnulib.git] / tests / test-regex.c
1 /* Test regular expressions
2    Copyright 1996-2001, 2003-2014 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 #include <config.h>
18
19 #include "regex.h"
20
21 #include <locale.h>
22 #include <limits.h>
23 #include <string.h>
24 #if HAVE_DECL_ALARM
25 # include <unistd.h>
26 # include <signal.h>
27 #endif
28
29 #include "localcharset.h"
30
31 int
32 main (void)
33 {
34   int result = 0;
35   static struct re_pattern_buffer regex;
36   unsigned char folded_chars[UCHAR_MAX + 1];
37   int i;
38   const char *s;
39   struct re_registers regs;
40
41 #if HAVE_DECL_ALARM
42   /* Some builds of glibc go into an infinite loop on this test.  */
43   int alarm_value = 2;
44   signal (SIGALRM, SIG_DFL);
45   alarm (alarm_value);
46 #endif
47   if (setlocale (LC_ALL, "en_US.UTF-8"))
48     {
49       {
50         /* http://sourceware.org/ml/libc-hacker/2006-09/msg00008.html
51            This test needs valgrind to catch the bug on Debian
52            GNU/Linux 3.1 x86, but it might catch the bug better
53            on other platforms and it shouldn't hurt to try the
54            test here.  */
55         static char const pat[] = "insert into";
56         static char const data[] =
57           "\xFF\0\x12\xA2\xAA\xC4\xB1,K\x12\xC4\xB1*\xACK";
58         re_set_syntax (RE_SYNTAX_GREP | RE_HAT_LISTS_NOT_NEWLINE
59                        | RE_ICASE);
60         memset (&regex, 0, sizeof regex);
61         s = re_compile_pattern (pat, sizeof pat - 1, &regex);
62         if (s)
63           result |= 1;
64         else if (re_search (&regex, data, sizeof data - 1,
65                             0, sizeof data - 1, &regs)
66                  != -1)
67           result |= 1;
68       }
69
70       /* Check whether it's really a UTF-8 locale.
71          On mingw, the setlocale call succeeds but returns
72          "English_United States.1252", with locale_charset() returning
73          "CP1252".  */
74       if (strcmp (locale_charset (), "UTF-8") == 0)
75         {
76           /* This test is from glibc bug 15078.
77              The test case is from Andreas Schwab in
78              <http://www.sourceware.org/ml/libc-alpha/2013-01/msg00967.html>.
79           */
80           static char const pat[] = "[^x]x";
81           static char const data[] =
82             /* <U1000><U103B><U103D><U1014><U103A><U102F><U1015><U103A> */
83             "\xe1\x80\x80"
84             "\xe1\x80\xbb"
85             "\xe1\x80\xbd"
86             "\xe1\x80\x94"
87             "\xe1\x80\xba"
88             "\xe1\x80\xaf"
89             "\xe1\x80\x95"
90             "\xe1\x80\xba"
91             "x";
92           re_set_syntax (0);
93           memset (&regex, 0, sizeof regex);
94           s = re_compile_pattern (pat, sizeof pat - 1, &regex);
95           if (s)
96             result |= 1;
97           else
98             {
99               i = re_search (&regex, data, sizeof data - 1,
100                              0, sizeof data - 1, 0);
101               if (i != 0 && i != 21)
102                 result |= 1;
103             }
104         }
105
106       if (! setlocale (LC_ALL, "C"))
107         return 1;
108     }
109
110   /* This test is from glibc bug 3957, reported by Andrew Mackey.  */
111   re_set_syntax (RE_SYNTAX_EGREP | RE_HAT_LISTS_NOT_NEWLINE);
112   memset (&regex, 0, sizeof regex);
113   s = re_compile_pattern ("a[^x]b", 6, &regex);
114   if (s)
115     result |= 2;
116   /* This should fail, but succeeds for glibc-2.5.  */
117   else if (re_search (&regex, "a\nb", 3, 0, 3, &regs) != -1)
118     result |= 2;
119
120   /* This regular expression is from Spencer ere test number 75
121      in grep-2.3.  */
122   re_set_syntax (RE_SYNTAX_POSIX_EGREP);
123   memset (&regex, 0, sizeof regex);
124   for (i = 0; i <= UCHAR_MAX; i++)
125     folded_chars[i] = i;
126   regex.translate = folded_chars;
127   s = re_compile_pattern ("a[[:@:>@:]]b\n", 11, &regex);
128   /* This should fail with _Invalid character class name_ error.  */
129   if (!s)
130     result |= 4;
131
132   /* Ensure that [b-a] is diagnosed as invalid, when
133      using RE_NO_EMPTY_RANGES. */
134   re_set_syntax (RE_SYNTAX_POSIX_EGREP | RE_NO_EMPTY_RANGES);
135   memset (&regex, 0, sizeof regex);
136   s = re_compile_pattern ("a[b-a]", 6, &regex);
137   if (s == 0)
138     result |= 8;
139
140   /* This should succeed, but does not for glibc-2.1.3.  */
141   memset (&regex, 0, sizeof regex);
142   s = re_compile_pattern ("{1", 2, &regex);
143   if (s)
144     result |= 8;
145
146   /* The following example is derived from a problem report
147      against gawk from Jorge Stolfi <stolfi@ic.unicamp.br>.  */
148   memset (&regex, 0, sizeof regex);
149   s = re_compile_pattern ("[an\371]*n", 7, &regex);
150   if (s)
151     result |= 8;
152   /* This should match, but does not for glibc-2.2.1.  */
153   else if (re_match (&regex, "an", 2, 0, &regs) != 2)
154     result |= 8;
155
156   memset (&regex, 0, sizeof regex);
157   s = re_compile_pattern ("x", 1, &regex);
158   if (s)
159     result |= 8;
160   /* glibc-2.2.93 does not work with a negative RANGE argument.  */
161   else if (re_search (&regex, "wxy", 3, 2, -2, &regs) != 1)
162     result |= 8;
163
164   /* The version of regex.c in older versions of gnulib
165      ignored RE_ICASE.  Detect that problem too.  */
166   re_set_syntax (RE_SYNTAX_EMACS | RE_ICASE);
167   memset (&regex, 0, sizeof regex);
168   s = re_compile_pattern ("x", 1, &regex);
169   if (s)
170     result |= 16;
171   else if (re_search (&regex, "WXY", 3, 0, 3, &regs) < 0)
172     result |= 16;
173
174   /* Catch a bug reported by Vin Shelton in
175      http://lists.gnu.org/archive/html/bug-coreutils/2007-06/msg00089.html
176      */
177   re_set_syntax (RE_SYNTAX_POSIX_BASIC
178                  & ~RE_CONTEXT_INVALID_DUP
179                  & ~RE_NO_EMPTY_RANGES);
180   memset (&regex, 0, sizeof regex);
181   s = re_compile_pattern ("[[:alnum:]_-]\\\\+$", 16, &regex);
182   if (s)
183     result |= 32;
184
185   /* REG_STARTEND was added to glibc on 2004-01-15.
186      Reject older versions.  */
187   if (! REG_STARTEND)
188     result |= 64;
189
190 #if 0
191   /* It would be nice to reject hosts whose regoff_t values are too
192      narrow (including glibc on hosts with 64-bit ptrdiff_t and
193      32-bit int), but we should wait until glibc implements this
194      feature.  Otherwise, support for equivalence classes and
195      multibyte collation symbols would always be broken except
196      when compiling --without-included-regex.   */
197   if (sizeof (regoff_t) < sizeof (ptrdiff_t)
198       || sizeof (regoff_t) < sizeof (ssize_t))
199     result |= 64;
200 #endif
201
202   return result;
203 }