Built win.arm64 against r3658
[kbuild-mirror.git] / src / oldsed / testsuite / tst-boost.c
blob117ac9b44b9db9ac12583211ea4079778bf1baf8
1 /* Regular expression tests.
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.
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA. */
21 #include "config.h"
23 #include <sys/types.h>
24 #ifdef HAVE_MCHECK_H
25 #include <mcheck.h>
26 #endif
27 #include <regex.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
32 void
33 frob_escapes (char *src, int pattern)
35 char *dst;
37 for (dst = src; *src != '\0'; dst++, src++)
39 if (*src == '\\')
41 switch (src[1])
43 case 't':
44 src++;
45 *dst = '\t';
46 continue;
47 case 'n':
48 src++;
49 *dst = '\n';
50 continue;
51 case 'r':
52 src++;
53 *dst = '\r';
54 continue;
55 case '\\':
56 case '^':
57 case '{':
58 case '|':
59 case '}':
60 if (!pattern)
62 src++;
63 *dst = *src;
64 continue;
66 break;
69 if (src != dst)
70 *dst = *src;
72 *dst = '\0';
75 int
76 main (int argc, char **argv)
78 int ret = 0, n;
79 char *line = NULL;
80 size_t line_len = 0;
81 ssize_t len;
82 FILE *f;
83 char *pattern, *string;
84 int flags = REG_EXTENDED;
85 int eflags = 0;
86 regex_t re;
87 regmatch_t rm[20];
89 #ifdef HAVE_MCHECK_H
90 mtrace ();
91 #endif
93 if (argc < 2)
95 fprintf (stderr, "Missing test filename\n");
96 return 1;
99 f = fopen (argv[1], "r");
100 if (f == NULL)
102 fprintf (stderr, "Couldn't open %s\n", argv[1]);
103 return 1;
106 while ((len = getline (&line, &line_len, f)) > 0)
108 char *p, *q;
109 int i;
111 if (line[len - 1] == '\n')
112 line[--len] = '\0';
114 puts (line);
116 if (line[0] == ';')
117 continue;
119 if (line[0] == '\0')
120 continue;
122 if (line[0] == '-')
124 if (strstr (line, "REG_BASIC"))
125 flags = 0;
126 else
127 flags = REG_EXTENDED;
128 if (strstr (line, "REG_ICASE"))
129 flags |= REG_ICASE;
130 if (strstr (line, "REG_NEWLINE"))
131 flags |= REG_NEWLINE;
132 eflags = 0;
133 if (strstr (line, "REG_NOTBOL"))
134 eflags |= REG_NOTBOL;
135 if (strstr (line, "REG_NOTEOL"))
136 eflags |= REG_NOTEOL;
137 continue;
140 pattern = line + strspn (line, " \t");
141 if (*pattern == '\0')
142 continue;
143 p = pattern + strcspn (pattern, " \t");
144 if (*p == '\0')
145 continue;
146 *p++ = '\0';
148 string = p + strspn (p, " \t");
149 if (*string == '\0')
150 continue;
151 if (*string == '"')
153 string++;
154 p = strchr (string, '"');
155 if (p == NULL)
156 continue;
157 *p++ = '\0';
159 else
161 p = string + strcspn (string, " \t");
162 if (*string == '!')
163 string = NULL;
164 else if (*p == '\0')
165 continue;
166 else
167 *p++ = '\0';
170 frob_escapes (pattern, 1);
171 if (string != NULL)
172 frob_escapes (string, 0);
174 n = regcomp (&re, pattern, flags);
175 if (n != 0)
177 if (string != NULL)
179 char buf[500];
180 regerror (n, &re, buf, sizeof (buf));
181 printf ("FAIL regcomp unexpectedly failed: %s\n",
182 buf);
183 ret = 1;
185 continue;
187 else if (string == NULL)
189 regfree (&re);
190 puts ("FAIL regcomp unpexpectedly succeeded");
191 ret = 1;
192 continue;
195 if (regexec (&re, string, 20, rm, eflags))
197 for (i = 0; i < 20; ++i)
199 rm[i].rm_so = -1;
200 rm[i].rm_eo = -1;
204 regfree (&re);
206 for (i = 0; i < 20 && *p != '\0'; ++i)
208 int rm_so, rm_eo;
210 rm_so = strtol (p, &q, 10);
211 if (p == q)
212 break;
213 p = q;
215 rm_eo = strtol (p, &q, 10);
216 if (p == q)
217 break;
218 p = q;
220 if (rm[i].rm_so != rm_so || rm[i].rm_eo != rm_eo)
222 printf ("FAIL rm[%d] %d..%d != expected %d..%d\n",
223 i, rm[i].rm_so, rm[i].rm_eo, rm_so, rm_eo);
224 ret = 1;
225 break;
230 free (line);
231 fclose (f);
232 return ret;