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
23 #include <sys/types.h>
33 main (int argc
, char **argv
)
40 char *pattern
= NULL
, *string
= NULL
;
42 size_t pattern_alloced
= 0, string_alloced
= 0;
44 int pattern_valid
= 0, rm_valid
= 0;
53 fprintf (stderr
, "Missing test filename\n");
57 f
= fopen (argv
[1], "r");
60 fprintf (stderr
, "Couldn't open %s\n", argv
[1]);
64 if ((len
= getline (&line
, &line_len
, f
)) <= 0
65 || strncmp (line
, "# PCRE", 6) != 0)
67 fprintf (stderr
, "Not a PCRE test file\n");
75 while ((len
= getline (&line
, &line_len
, f
)) > 0)
82 if (line
[len
- 1] == '\n')
100 p
= strrchr (line
+ 1, '/');
106 printf ("%lu: Invalid pattern line: %s\n", linenum
, line
);
111 if (p
[1] == 'i' && p
[2] == '\0')
113 else if (p
[1] != '\0')
115 printf ("%lu: Invalid pattern line: %s\n", linenum
, line
);
120 if (pattern_alloced
< (size_t) (p
- line
))
122 pattern
= realloc (pattern
, p
- line
);
125 printf ("%lu: Cannot record pattern: %m\n", linenum
);
129 pattern_alloced
= p
- line
;
132 memcpy (pattern
, line
+ 1, p
- line
- 1);
133 pattern
[p
- line
- 1] = '\0';
138 if (strncmp (line
, " ", 4) == 0)
145 printf ("%lu: No previous valid pattern %s\n", linenum
, line
);
149 if (string_alloced
< (size_t) (len
- 3))
151 string
= realloc (string
, len
- 3);
154 printf ("%lu: Cannot record search string: %m\n", linenum
);
158 string_alloced
= len
- 3;
161 memcpy (string
, line
+ 4, len
- 3);
163 n
= regcomp (&re
, pattern
,
164 REG_EXTENDED
| (ignorecase
? REG_ICASE
: 0));
168 regerror (n
, &re
, buf
, sizeof (buf
));
169 printf ("%lu: regcomp failed for %s: %s\n",
170 linenum
, pattern
, buf
);
175 if (regexec (&re
, string
, 20, rm
, 0))
188 printf ("%lu: No preceeding pattern or search string\n", linenum
);
193 if (strcmp (line
, "No match") == 0)
195 if (rm
[0].rm_so
!= -1 || rm
[0].rm_eo
!= -1)
197 printf ("%lu: /%s/ on %s unexpectedly matched %d..%d\n",
198 linenum
, pattern
, string
, rm
[0].rm_so
, rm
[0].rm_eo
);
209 num
= strtoul (p
, &p
, 10);
210 if (num
>= 20 || *p
!= ':' || p
[1] != ' ')
212 printf ("%lu: Invalid line %s\n", linenum
, line
);
217 if (rm
[num
].rm_so
== -1 || rm
[num
].rm_eo
== -1)
219 if (strcmp (p
+ 2, "<unset>") != 0)
221 printf ("%lu: /%s/ on %s unexpectedly failed to match register %ld %d..%d\n",
222 linenum
, pattern
, string
, num
,
223 rm
[num
].rm_so
, rm
[num
].rm_eo
);
229 if (rm
[num
].rm_eo
< rm
[num
].rm_so
230 || rm
[num
].rm_eo
- rm
[num
].rm_so
!= len
- (p
+ 2 - line
)
231 || strncmp (p
+ 2, string
+ rm
[num
].rm_so
,
232 rm
[num
].rm_eo
- rm
[num
].rm_so
) != 0)
234 printf ("%lu: /%s/ on %s unexpectedly failed to match %s for register %ld %d..%d\n",
235 linenum
, pattern
, string
, p
+ 2, num
,
236 rm
[num
].rm_so
, rm
[num
].rm_eo
);