]>
Commit | Line | Data |
---|---|---|
92040cbc | 1 | /* POSIX regex testsuite from IEEE 2003.2. |
04277e02 | 2 | Copyright (C) 1998-2019 Free Software Foundation, Inc. |
92040cbc UD |
3 | This file is part of the GNU C Library. |
4 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998. | |
5 | ||
6 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
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. | |
92040cbc UD |
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 | |
41bdb6e2 | 14 | Lesser General Public License for more details. |
92040cbc | 15 | |
41bdb6e2 | 16 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
17 | License along with the GNU C Library; if not, see |
18 | <http://www.gnu.org/licenses/>. */ | |
92040cbc UD |
19 | |
20 | #include <sys/types.h> | |
21 | #include <regex.h> | |
22 | #include <stdio.h> | |
ba9234d9 | 23 | #include <string.h> |
92040cbc UD |
24 | |
25 | /* Data structure to describe the tests. */ | |
26 | struct test | |
27 | { | |
28 | int start; | |
29 | int end; | |
30 | const char *reg; | |
31 | const char *str; | |
32 | int options; | |
33 | } tests[] = | |
34 | { | |
35 | #include "ptestcases.h" | |
36 | }; | |
37 | ||
38 | ||
39 | int | |
40 | main (int argc, char *argv[]) | |
41 | { | |
42 | size_t cnt; | |
43 | int errors = 0; | |
44 | ||
45 | for (cnt = 0; cnt < sizeof (tests) / sizeof (tests[0]); ++cnt) | |
46 | if (tests[cnt].str == NULL) | |
47 | { | |
48 | printf ("\n%s\n%.*s\n", tests[cnt].reg, | |
49 | (int) strlen (tests[cnt].reg), | |
50 | "-----------------------------------------------------"); | |
51 | } | |
813ec65a UD |
52 | else if (tests[cnt].reg == NULL) |
53 | printf ("!!! %s\n", tests[cnt].str); | |
92040cbc UD |
54 | else |
55 | { | |
56 | regex_t re; | |
57 | regmatch_t match[20]; | |
58 | int err; | |
59 | ||
60 | printf ("regexp: \"%s\", string: \"%s\" -> ", tests[cnt].reg, | |
61 | tests[cnt].str); | |
62 | ||
63 | /* Compile the expression. */ | |
64 | err = regcomp (&re, tests[cnt].reg, tests[cnt].options); | |
65 | if (err != 0) | |
66 | { | |
648c1337 UD |
67 | if (tests[cnt].start == -2) |
68 | puts ("compiling failed, OK"); | |
92040cbc UD |
69 | else |
70 | { | |
71 | char buf[100]; | |
72 | regerror (err, &re, buf, sizeof (buf)); | |
73 | printf ("FAIL: %s\n", buf); | |
74 | ++errors; | |
75 | } | |
76 | ||
77 | continue; | |
78 | } | |
648c1337 UD |
79 | else if (tests[cnt].start == -2) |
80 | { | |
81 | puts ("compiling suceeds, FAIL"); | |
82 | errors++; | |
83 | continue; | |
84 | } | |
92040cbc UD |
85 | |
86 | /* Run the actual test. */ | |
87 | err = regexec (&re, tests[cnt].str, 20, match, 0); | |
88 | ||
89 | if (err != 0) | |
90 | { | |
91 | if (tests[cnt].start == -1) | |
92 | puts ("no match, OK"); | |
93 | else | |
94 | { | |
95 | puts ("no match, FAIL"); | |
96 | ++errors; | |
97 | } | |
98 | } | |
99 | else | |
100 | { | |
101 | if (match[0].rm_so == 0 && tests[cnt].start == 0 | |
102 | && match[0].rm_eo == 0 && tests[cnt].end == 0) | |
103 | puts ("match, OK"); | |
104 | else if (match[0].rm_so + 1 == tests[cnt].start | |
105 | && match[0].rm_eo == tests[cnt].end) | |
106 | puts ("match, OK"); | |
107 | else | |
108 | { | |
109 | printf ("wrong match (%d to %d): FAIL\n", | |
110 | match[0].rm_so, match[0].rm_eo); | |
111 | ++errors; | |
112 | } | |
113 | } | |
114 | ||
115 | /* Free all resources. */ | |
116 | regfree (&re); | |
117 | } | |
118 | ||
ba9234d9 | 119 | printf ("\n%Zu tests, %d errors\n", cnt, errors); |
92040cbc | 120 | |
97fd3a30 | 121 | return errors != 0; |
92040cbc | 122 | } |