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, "^x\\|xy*z$", "\xc3\xb6xyyz", 2, 1},
47   {RE_SYNTAX_POSIX_BASIC, "^x\\\\y\\{6\\}z\\+", "x\\yyyyyyzz\xc3\xb6", 0, 1},
48   {RE_SYNTAX_POSIX_BASIC, "^x\\\\y\\{2,36\\}z\\+", "x\\yzz\xc3\xb6", -1, 1},
49   {RE_SYNTAX_POSIX_BASIC, "^x\\\\y\\{,3\\}z\\+", "x\\yyyzz\xc3\xb6", 0, 1},
50   /* FIXME.  This one should be optimizable, but is not ATM.  */
51   {RE_SYNTAX_POSIX_BASIC, "x[ABC]y", "axCy", 1, 0},
52   {RE_SYNTAX_POSIX_BASIC, "\\`x\\|z\\'", "x\xe2\x80\x94", 0, 1},
53   {RE_SYNTAX_POSIX_BASIC, "\\(xy\\)z\\1a\\1", "\xe2\x80\x94xyzxyaxy\xc3\x84", 3, 1},
54   {RE_SYNTAX_POSIX_BASIC, "xy\\?z", "\xc3\x84xz\xc3\xb6", 2, 1},
55   {RE_SYNTAX_POSIX_EXTENDED, "foo", "b\xc3\xa4rfoob\xc3\xa4z", 4, 1},
56   {RE_SYNTAX_POSIX_EXTENDED, "^x|xy*z$", "\xc3\xb6xyyz", 2, 1},
57   {RE_SYNTAX_POSIX_EXTENDED, "^x\\\\y{6}z+", "x\\yyyyyyzz\xc3\xb6", 0, 1},
58   {RE_SYNTAX_POSIX_EXTENDED, "^x\\\\y{2,36}z+", "x\\yzz\xc3\xb6", -1, 1},
59   {RE_SYNTAX_POSIX_EXTENDED, "^x\\\\y{,3}z+", "x\\yyyzz\xc3\xb6", 0, 1},
60   /* FIXME.  This one should be optimizable, but is not ATM.  */
61   {RE_SYNTAX_POSIX_EXTENDED, "x[ABC]y", "axCy", 1, 0},
62   {RE_SYNTAX_POSIX_EXTENDED, "\\`x|z\\'", "x\xe2\x80\x94", 0, 1},
63   {RE_SYNTAX_POSIX_EXTENDED, "(xy)z\\1a\\1", "\xe2\x80\x94xyzxyaxy\xc3\x84", 3, 1},
64   {RE_SYNTAX_POSIX_EXTENDED, "xy?z", "\xc3\x84xz\xc3\xb6", 2, 1},
65   /* Should not be optimized.  */
66   {RE_SYNTAX_POSIX_BASIC, "x.y", "ax\xe2\x80\x94yz", 1, 0},
67   {RE_SYNTAX_POSIX_BASIC, "x\xc3\x96*y", "ax\xc3\x96\xc3\x96yz", 1, 0},
68   {RE_SYNTAX_POSIX_BASIC, "x[\xc3\x84\xc3\xa4]y", "ax\xc3\xa4y", 1, 0},
69   {RE_SYNTAX_POSIX_BASIC, "x[A-Z,]y", "axCy", 1, 0},
70   {RE_SYNTAX_POSIX_BASIC, "x[^y]z", "ax\xe2\x80\x94z", 1, 0},
71   {RE_SYNTAX_POSIX_BASIC, "x[[:alnum:]]z", "ax\xc3\x96z", 1, 0},
72   {RE_SYNTAX_POSIX_BASIC, "\\<g", "\xe2\x80\x94g", 3, 0},
73   {RE_SYNTAX_POSIX_BASIC, "\\bg\\b", "\xe2\x80\x94g", 3, 0},
74   {RE_SYNTAX_POSIX_BASIC, "\\Bg\\B", "\xc3\xa4g\xc3\xa4", 2, 0},
75   {RE_SYNTAX_POSIX_BASIC, "a\\wz", "a\xc3\x84z", 0, 0},
76   {RE_SYNTAX_POSIX_BASIC, "x\\Wz", "\xc3\x96x\xe2\x80\x94z", 2, 0},
77   {RE_SYNTAX_POSIX_EXTENDED, "x.y", "ax\xe2\x80\x94yz", 1, 0},
78   {RE_SYNTAX_POSIX_EXTENDED, "x\xc3\x96*y", "ax\xc3\x96\xc3\x96yz", 1, 0},
79   {RE_SYNTAX_POSIX_EXTENDED, "x[\xc3\x84\xc3\xa4]y", "ax\xc3\xa4y", 1, 0},
80   {RE_SYNTAX_POSIX_EXTENDED, "x[A-Z,]y", "axCy", 1, 0},
81   {RE_SYNTAX_POSIX_EXTENDED, "x[^y]z", "ax\xe2\x80\x94z", 1, 0},
82   {RE_SYNTAX_POSIX_EXTENDED, "x[[:alnum:]]z", "ax\xc3\x96z", 1, 0},
83   {RE_SYNTAX_POSIX_EXTENDED, "\\<g", "\xe2\x80\x94g", 3, 0},
84   {RE_SYNTAX_POSIX_EXTENDED, "\\bg\\b", "\xe2\x80\x94g", 3, 0},
85   {RE_SYNTAX_POSIX_EXTENDED, "\\Bg\\B", "\xc3\xa4g\xc3\xa4", 2, 0},
86   {RE_SYNTAX_POSIX_EXTENDED, "a\\wz", "a\xc3\x84z", 0, 0},
87   {RE_SYNTAX_POSIX_EXTENDED, "x\\Wz", "\xc3\x96x\xe2\x80\x94z", 2, 0},
88 };
89
90 int
91 main (void)
92 {
93   struct re_pattern_buffer regbuf;
94   const char *err;
95   size_t i;
96   int ret = 0;
97
98   mtrace ();
99
100   setlocale (LC_ALL, "de_DE.UTF-8");
101   for (i = 0; i < sizeof (tests) / sizeof (tests[0]); ++i)
102     {
103       int res, optimized;
104       re_set_syntax (tests[i].syntax);
105       memset (&regbuf, '\0', sizeof (regbuf));
106       err = re_compile_pattern (tests[i].pattern, strlen (tests[i].pattern),
107                                 &regbuf);
108       if (err != NULL)
109         {
110           printf ("re_compile_pattern failed: %s\n", err);
111           ret = 1;
112           continue;
113         }
114
115       /* Check if re_search will be done as multi-byte or single-byte.  */
116       optimized = ((re_dfa_t *) regbuf.buffer)->mb_cur_max == 1;
117       if (optimized != tests[i].optimize)
118         {
119           printf ("pattern %zd %soptimized while it should%s be\n",
120                   i, optimized ? "" : "not ", tests[i].optimize ? "" : " not");
121           ret = 1;
122         }
123
124       res = re_search (&regbuf, tests[i].string, strlen (tests[i].string), 0,
125                        strlen (tests[i].string), NULL);
126       if (res != tests[i].res)
127         {
128           printf ("re_search %zd failed: %d\n", i, res);
129           ret = 1;
130           regfree (&regbuf);
131           continue;
132         }
133       regfree (&regbuf);
134     }
135
136   return ret;
137 }