1 /***********************************************************
2
3 Copyright 1995 by Tom Lord
4
5 All Rights Reserved
6
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the name of the copyright holder not be
12 used in advertising or publicity pertaining to distribution of the
13 software without specific, written prior permission.
14
15 Tom Lord DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17 EVENT SHALL TOM LORD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18 CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
19 USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
20 OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
21 PERFORMANCE OF THIS SOFTWARE.
22
23 ******************************************************************/
24
25
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <sys/types.h>
32 #include <regex.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36
37
38
39 struct a_test
40 {
41 int expected;
42 const char * pattern;
43 const unsigned char * data;
44 };
45
46 static const struct a_test the_tests[] =
47 {
48 #include "testcases.h"
49 {-1, 0, 0}
50 };
51
52
53
54
55 static int
run_a_test(int id,const struct a_test * t)56 run_a_test (int id, const struct a_test * t)
57 {
58 static const char * last_pattern = 0;
59 static regex_t r;
60 int err;
61 char errmsg[100];
62 int x;
63 regmatch_t regs[10];
64
65 if (!last_pattern || strcmp (last_pattern, t->pattern))
66 {
67 if (last_pattern)
68 regfree (&r);
69 last_pattern = t->pattern;
70 err = regcomp (&r, t->pattern, REG_EXTENDED);
71 if (err)
72 {
73 if (t->expected == 2)
74 {
75 puts (" OK.");
76 return 0;
77 }
78 if (last_pattern)
79 regfree (&r);
80 last_pattern = NULL;
81 regerror (err, &r, errmsg, 100);
82 printf (" FAIL: %s.\n", errmsg);
83 return 1;
84 }
85 else if (t->expected == 2)
86 {
87 printf ("test %d\n", id);
88 printf ("pattern \"%s\" successfull compilation not expected\n",
89 t->pattern);
90 return 1;
91 }
92 }
93
94 for (x = 0; x < 10; ++x)
95 regs[x].rm_so = regs[x].rm_eo = -1;
96
97 err = regexec (&r, t->data, 10, regs, 0);
98
99 if (err != t->expected)
100 {
101 printf ("test %d\n", id);
102 printf ("pattern \"%s\" data \"%s\" wanted %d got %d\n",
103 t->pattern, t->data, t->expected, err);
104 for (x = 0; x < 10; ++x)
105 if (regs[x].rm_so != -1)
106 printf ("reg %d == (%d, %d) %.*s\n",
107 x,
108 regs[x].rm_so,
109 regs[x].rm_eo,
110 regs[x].rm_eo - regs[x].rm_so,
111 t->data + regs[x].rm_so);
112 return 1;
113 }
114 puts (" OK.");
115 return 0;
116 }
117
118
119
120 int
main(int argc,char * argv[])121 main (int argc, char * argv[])
122 {
123 int x;
124 int lo;
125 int hi;
126 int res = 0;
127
128 lo = 0;
129 hi = (sizeof (the_tests) / sizeof (the_tests[0])) - 1;
130
131 if (argc > 1)
132 {
133 lo = atoi (argv[1]);
134 hi = lo + 1;
135
136 if (argc > 2)
137 hi = atoi (argv[2]);
138 }
139
140 for (x = lo; x < hi; ++x)
141 {
142 printf ("#%d:", x);
143 res |= run_a_test (x, &the_tests[x]);
144 }
145 return res != 0;
146 }
147