Update.
[platform/upstream/glibc.git] / posix / bug-regex20.c
1 /* Test for UTF-8 regular expression optimizations.
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.
5
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.
10
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.
15
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
19    02111-1307 USA.  */
20
21 #include <sys/types.h>
22 #include <mcheck.h>
23 #include <regex.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <locale.h>
28
29 #define RE_NO_INTERNAL_PROTOTYPES 1
30 #include "regex_internal.h"
31
32 static struct
33 {
34   int syntax;
35   const char *pattern;
36   const char *string;
37   int res, optimize;
38 } tests[] = {
39   /* \xc3\x84           LATIN CAPITAL LETTER A WITH DIAERESIS
40      \xc3\x96           LATIN CAPITAL LETTER O WITH DIAERESIS
41      \xc3\xa4           LATIN SMALL LETTER A WITH DIAERESIS
42      \xc3\xb6           LATIN SMALL LETTER O WITH DIAERESIS
43      \xe2\x80\x94       EM DASH  */
44   /* Should be optimized.  */
45   {RE_SYNTAX_POSIX_BASIC, "foo", "b\xc3\xa4rfoob\xc3\xa4z", 4, 1},
46   {RE_SYNTAX_POSIX_BASIC, "b\xc3\xa4z", "b\xc3\xa4rfoob\xc3\xa4z", 7, 1},
47   {RE_SYNTAX_POSIX_BASIC, "b\xc3\xa4*z", "b\xc3\xa4rfoob\xc3\xa4z", 7, 1},
48   {RE_SYNTAX_POSIX_BASIC, "b\xc3\xa4*z", "b\xc3\xa4rfoobz", 7, 1},
49   {RE_SYNTAX_POSIX_BASIC, "b\xc3\xa4\\+z",
50    "b\xc3\xa4rfoob\xc3\xa4\xc3\xa4z", 7, 1},
51   {RE_SYNTAX_POSIX_BASIC, "b\xc3\xa4\\?z", "b\xc3\xa4rfoob\xc3\xa4z", 7, 1},
52   {RE_SYNTAX_POSIX_BASIC, "b\xc3\xa4\\{1,2\\}z",
53    "b\xc3\xa4rfoob\xc3\xa4z", 7, 1},
54   {RE_SYNTAX_POSIX_BASIC, "^x\\|xy*z$", "\xc3\xb6xyyz", 2, 1},
55   {RE_SYNTAX_POSIX_BASIC, "^x\\\\y\\{6\\}z\\+", "x\\yyyyyyzz\xc3\xb6", 0, 1},
56   {RE_SYNTAX_POSIX_BASIC, "^x\\\\y\\{2,36\\}z\\+", "x\\yzz\xc3\xb6", -1, 1},
57   {RE_SYNTAX_POSIX_BASIC, "^x\\\\y\\{,3\\}z\\+", "x\\yyyzz\xc3\xb6", 0, 1},
58   {RE_SYNTAX_POSIX_BASIC, "^x\\|x\xc3\xa4*z$",
59    "\xc3\xb6x\xc3\xa4\xc3\xa4z", 2, 1},
60   {RE_SYNTAX_POSIX_BASIC, "^x\\\\\xc3\x84\\{6\\}z\\+",
61    "x\\\xc3\x84\xc3\x84\xc3\x84\xc3\x84\xc3\x84\xc3\x84zz\xc3\xb6", 0, 1},
62   {RE_SYNTAX_POSIX_BASIC, "^x\\\\\xc3\x84\\{2,36\\}z\\+",
63    "x\\\xc3\x84zz\xc3\xb6", -1, 1},
64   {RE_SYNTAX_POSIX_BASIC, "^x\\\\\xc3\x84\\{,3\\}z\\+",
65    "x\\\xc3\x84\xc3\x84\xc3\x84zz\xc3\xb6", 0, 1},
66   {RE_SYNTAX_POSIX_BASIC, "x[C]y", "axCy", 1, 1},
67   {RE_SYNTAX_POSIX_BASIC, "x[ABC]y", "axCy", 1, 1},
68   {RE_SYNTAX_POSIX_BASIC, "\\`x\\|z\\'", "x\xe2\x80\x94", 0, 1},
69   {RE_SYNTAX_POSIX_BASIC, "\\(xy\\)z\\1a\\1", "\xe2\x80\x94xyzxyaxy\xc3\x84", 3, 1},
70   {RE_SYNTAX_POSIX_BASIC, "xy\\?z", "\xc3\x84xz\xc3\xb6", 2, 1},
71   {RE_SYNTAX_POSIX_BASIC, "\\`\xc3\x84\\|z\\'", "\xc3\x84\xe2\x80\x94", 0, 1},
72   {RE_SYNTAX_POSIX_BASIC, "\\(x\xc3\x84\\)z\\1\x61\\1",
73    "\xe2\x80\x94x\xc3\x84zx\xc3\x84\x61x\xc3\x84\xc3\x96", 3, 1},
74   {RE_SYNTAX_POSIX_BASIC, "x\xc3\x96\\?z", "\xc3\x84xz\xc3\xb6", 2, 1},
75   {RE_SYNTAX_POSIX_EXTENDED, "foo", "b\xc3\xa4rfoob\xc3\xa4z", 4, 1},
76   {RE_SYNTAX_POSIX_EXTENDED, "^x|xy*z$", "\xc3\xb6xyyz", 2, 1},
77   {RE_SYNTAX_POSIX_EXTENDED, "^x\\\\y{6}z+", "x\\yyyyyyzz\xc3\xb6", 0, 1},
78   {RE_SYNTAX_POSIX_EXTENDED, "^x\\\\y{2,36}z+", "x\\yzz\xc3\xb6", -1, 1},
79   {RE_SYNTAX_POSIX_EXTENDED, "^x\\\\y{,3}z+", "x\\yyyzz\xc3\xb6", 0, 1},
80   {RE_SYNTAX_POSIX_EXTENDED, "x[C]y", "axCy", 1, 1},
81   {RE_SYNTAX_POSIX_EXTENDED, "x[ABC]y", "axCy", 1, 1},
82   {RE_SYNTAX_POSIX_EXTENDED, "\\`x|z\\'", "x\xe2\x80\x94", 0, 1},
83   {RE_SYNTAX_POSIX_EXTENDED, "(xy)z\\1a\\1", "\xe2\x80\x94xyzxyaxy\xc3\x84", 3, 1},
84   {RE_SYNTAX_POSIX_EXTENDED, "xy?z", "\xc3\x84xz\xc3\xb6", 2, 1},
85   /* Should not be optimized.  */
86   {RE_SYNTAX_POSIX_BASIC, "x.y", "ax\xe2\x80\x94yz", 1, 0},
87   {RE_SYNTAX_POSIX_BASIC, "x[\xc3\x84\xc3\xa4]y", "ax\xc3\xa4y", 1, 0},
88   {RE_SYNTAX_POSIX_BASIC, "x[A-Z,]y", "axCy", 1, 0},
89   {RE_SYNTAX_POSIX_BASIC, "x[^y]z", "ax\xe2\x80\x94z", 1, 0},
90   {RE_SYNTAX_POSIX_BASIC, "x[[:alnum:]]z", "ax\xc3\x96z", 1, 0},
91   {RE_SYNTAX_POSIX_BASIC, "x[[=A=]]z", "axAz", 1, 0},
92   {RE_SYNTAX_POSIX_BASIC, "x[[=\xc3\x84=]]z", "ax\xc3\x84z", 1, 0},
93   {RE_SYNTAX_POSIX_BASIC, "\\<g", "\xe2\x80\x94g", 3, 0},
94   {RE_SYNTAX_POSIX_BASIC, "\\bg\\b", "\xe2\x80\x94g", 3, 0},
95   {RE_SYNTAX_POSIX_BASIC, "\\Bg\\B", "\xc3\xa4g\xc3\xa4", 2, 0},
96   {RE_SYNTAX_POSIX_BASIC, "a\\wz", "a\xc3\x84z", 0, 0},
97   {RE_SYNTAX_POSIX_BASIC, "x\\Wz", "\xc3\x96x\xe2\x80\x94z", 2, 0},
98   {RE_SYNTAX_POSIX_EXTENDED, "x.y", "ax\xe2\x80\x94yz", 1, 0},
99   {RE_SYNTAX_POSIX_EXTENDED, "x[\xc3\x84\xc3\xa4]y", "ax\xc3\xa4y", 1, 0},
100   {RE_SYNTAX_POSIX_EXTENDED, "x[A-Z,]y", "axCy", 1, 0},
101   {RE_SYNTAX_POSIX_EXTENDED, "x[^y]z", "ax\xe2\x80\x94z", 1, 0},
102   {RE_SYNTAX_POSIX_EXTENDED, "x[[:alnum:]]z", "ax\xc3\x96z", 1, 0},
103   {RE_SYNTAX_POSIX_EXTENDED, "x[[=A=]]z", "axAz", 1, 0},
104   {RE_SYNTAX_POSIX_EXTENDED, "x[[=\xc3\x84=]]z", "ax\xc3\x84z", 1, 0},
105   {RE_SYNTAX_POSIX_EXTENDED, "\\<g", "\xe2\x80\x94g", 3, 0},
106   {RE_SYNTAX_POSIX_EXTENDED, "\\bg\\b", "\xe2\x80\x94g", 3, 0},
107   {RE_SYNTAX_POSIX_EXTENDED, "\\Bg\\B", "\xc3\xa4g\xc3\xa4", 2, 0},
108   {RE_SYNTAX_POSIX_EXTENDED, "a\\wz", "a\xc3\x84z", 0, 0},
109   {RE_SYNTAX_POSIX_EXTENDED, "x\\Wz", "\xc3\x96x\xe2\x80\x94z", 2, 0},
110 };
111
112 int
113 main (void)
114 {
115   struct re_pattern_buffer regbuf;
116   const char *err;
117   size_t i;
118   int ret = 0;
119
120   mtrace ();
121
122   setlocale (LC_ALL, "de_DE.UTF-8");
123   for (i = 0; i < sizeof (tests) / sizeof (tests[0]); ++i)
124     {
125       int res, optimized;
126
127       re_set_syntax (tests[i].syntax);
128       memset (&regbuf, '\0', sizeof (regbuf));
129       err = re_compile_pattern (tests[i].pattern, strlen (tests[i].pattern),
130                                 &regbuf);
131       if (err != NULL)
132         {
133           printf ("re_compile_pattern failed: %s\n", err);
134           ret = 1;
135           continue;
136         }
137
138       /* Check if re_search will be done as multi-byte or single-byte.  */
139       optimized = ((re_dfa_t *) regbuf.buffer)->mb_cur_max == 1;
140       if (optimized != tests[i].optimize)
141         {
142           printf ("pattern %zd %soptimized while it should%s be\n",
143                   i, optimized ? "" : "not ", tests[i].optimize ? "" : " not");
144           ret = 1;
145         }
146
147       res = re_search (&regbuf, tests[i].string, strlen (tests[i].string), 0,
148                        strlen (tests[i].string), NULL);
149       if (res != tests[i].res)
150         {
151           printf ("re_search %zd failed: %d\n", i, res);
152           ret = 1;
153           regfree (&regbuf);
154           continue;
155         }
156       regfree (&regbuf);
157
158       re_set_syntax (tests[i].syntax | RE_ICASE);
159       memset (&regbuf, '\0', sizeof (regbuf));
160       err = re_compile_pattern (tests[i].pattern, strlen (tests[i].pattern),
161                                 &regbuf);
162       if (err != NULL)
163         {
164           printf ("re_compile_pattern failed: %s\n", err);
165           ret = 1;
166           continue;
167         }
168
169       /* Check if re_search will be done as multi-byte or single-byte.  */
170       optimized = ((re_dfa_t *) regbuf.buffer)->mb_cur_max == 1;
171       if (optimized)
172         {
173           printf ("pattern %zd optimized while it should not be when case insensitive\n",
174                   i);
175           ret = 1;
176         }
177
178       res = re_search (&regbuf, tests[i].string, strlen (tests[i].string), 0,
179                        strlen (tests[i].string), NULL);
180       if (res != tests[i].res)
181         {
182           printf ("re_search %zd failed: %d\n", i, res);
183           ret = 1;
184           regfree (&regbuf);
185           continue;
186         }
187       regfree (&regbuf);
188     }
189
190   return ret;
191 }