xref: /csrg-svn/lib/libcompat/regexp/timer.c (revision 41355)
1*41355Sbostic /*
2*41355Sbostic  * Simple timing program for regcomp().
3*41355Sbostic  *
4*41355Sbostic  *	Copyright (c) 1986 by University of Toronto.
5*41355Sbostic  *	Written by Henry Spencer.  Not derived from licensed software.
6*41355Sbostic  *
7*41355Sbostic  *	Permission is granted to anyone to use this software for any
8*41355Sbostic  *	purpose on any computer system, and to redistribute it freely,
9*41355Sbostic  *	subject to the following restrictions:
10*41355Sbostic  *
11*41355Sbostic  *	1. The author is not responsible for the consequences of use of
12*41355Sbostic  *		this software, no matter how awful, even if they arise
13*41355Sbostic  *		from defects in it.
14*41355Sbostic  *
15*41355Sbostic  *	2. The origin of this software must not be misrepresented, either
16*41355Sbostic  *		by explicit claim or by omission.
17*41355Sbostic  *
18*41355Sbostic  *	3. Altered versions must be plainly marked as such, and must not
19*41355Sbostic  *		be misrepresented as being the original software.
20*41355Sbostic  *
21*41355Sbostic  * Usage: timer ncomp nexec nsub
22*41355Sbostic  *	or
23*41355Sbostic  *	timer ncomp nexec nsub regexp string [ answer [ sub ] ]
24*41355Sbostic  *
25*41355Sbostic  * The second form is for timing repetitions of a single test case.
26*41355Sbostic  * The first form's test data is a compiled-in copy of the "tests" file.
27*41355Sbostic  * Ncomp, nexec, nsub are how many times to do each regcomp, regexec,
28*41355Sbostic  * and regsub.  The way to time an operation individually is to do something
29*41355Sbostic  * like "timer 1 50 1".
30*41355Sbostic  */
31*41355Sbostic #include <stdio.h>
32*41355Sbostic 
33*41355Sbostic struct try {
34*41355Sbostic 	char *re, *str, *ans, *src, *dst;
35*41355Sbostic } tests[] = {
36*41355Sbostic #include "timer.t.h"
37*41355Sbostic { NULL, NULL, NULL, NULL, NULL }
38*41355Sbostic };
39*41355Sbostic 
40*41355Sbostic #include <regexp.h>
41*41355Sbostic 
42*41355Sbostic int errreport = 0;		/* Report errors via errseen? */
43*41355Sbostic char *errseen = NULL;		/* Error message. */
44*41355Sbostic 
45*41355Sbostic char *progname;
46*41355Sbostic 
47*41355Sbostic /* ARGSUSED */
main(argc,argv)48*41355Sbostic main(argc, argv)
49*41355Sbostic int argc;
50*41355Sbostic char *argv[];
51*41355Sbostic {
52*41355Sbostic 	int ncomp, nexec, nsub;
53*41355Sbostic 	struct try one;
54*41355Sbostic 	char dummy[512];
55*41355Sbostic 
56*41355Sbostic 	if (argc < 4) {
57*41355Sbostic 		ncomp = 1;
58*41355Sbostic 		nexec = 1;
59*41355Sbostic 		nsub = 1;
60*41355Sbostic 	} else {
61*41355Sbostic 		ncomp = atoi(argv[1]);
62*41355Sbostic 		nexec = atoi(argv[2]);
63*41355Sbostic 		nsub = atoi(argv[3]);
64*41355Sbostic 	}
65*41355Sbostic 
66*41355Sbostic 	progname = argv[0];
67*41355Sbostic 	if (argc > 5) {
68*41355Sbostic 		one.re = argv[4];
69*41355Sbostic 		one.str = argv[5];
70*41355Sbostic 		if (argc > 6)
71*41355Sbostic 			one.ans = argv[6];
72*41355Sbostic 		else
73*41355Sbostic 			one.ans = "y";
74*41355Sbostic 		if (argc > 7) {
75*41355Sbostic 			one.src = argv[7];
76*41355Sbostic 			one.dst = "xxx";
77*41355Sbostic 		} else {
78*41355Sbostic 			one.src = "x";
79*41355Sbostic 			one.dst = "x";
80*41355Sbostic 		}
81*41355Sbostic 		errreport = 1;
82*41355Sbostic 		try(one, ncomp, nexec, nsub);
83*41355Sbostic 	} else
84*41355Sbostic 		multiple(ncomp, nexec, nsub);
85*41355Sbostic 	exit(0);
86*41355Sbostic }
87*41355Sbostic 
88*41355Sbostic void
regerror(s)89*41355Sbostic regerror(s)
90*41355Sbostic char *s;
91*41355Sbostic {
92*41355Sbostic 	if (errreport)
93*41355Sbostic 		errseen = s;
94*41355Sbostic 	else
95*41355Sbostic 		error(s, "");
96*41355Sbostic }
97*41355Sbostic 
98*41355Sbostic #ifndef ERRAVAIL
error(s1,s2)99*41355Sbostic error(s1, s2)
100*41355Sbostic char *s1;
101*41355Sbostic char *s2;
102*41355Sbostic {
103*41355Sbostic 	fprintf(stderr, "regexp: ");
104*41355Sbostic 	fprintf(stderr, s1, s2);
105*41355Sbostic 	fprintf(stderr, "\n");
106*41355Sbostic 	exit(1);
107*41355Sbostic }
108*41355Sbostic #endif
109*41355Sbostic 
110*41355Sbostic int lineno = 0;
111*41355Sbostic 
multiple(ncomp,nexec,nsub)112*41355Sbostic multiple(ncomp, nexec, nsub)
113*41355Sbostic int ncomp, nexec, nsub;
114*41355Sbostic {
115*41355Sbostic 	register int i;
116*41355Sbostic 	extern char *strchr();
117*41355Sbostic 
118*41355Sbostic 	errreport = 1;
119*41355Sbostic 	for (i = 0; tests[i].re != NULL; i++) {
120*41355Sbostic 		lineno++;
121*41355Sbostic 		try(tests[i], ncomp, nexec, nsub);
122*41355Sbostic 	}
123*41355Sbostic }
124*41355Sbostic 
125*41355Sbostic try(fields, ncomp, nexec, nsub)
126*41355Sbostic struct try fields;
127*41355Sbostic int ncomp, nexec, nsub;
128*41355Sbostic {
129*41355Sbostic 	regexp *r;
130*41355Sbostic 	char dbuf[BUFSIZ];
131*41355Sbostic 	register int i;
132*41355Sbostic 
133*41355Sbostic 	errseen = NULL;
134*41355Sbostic 	r = regcomp(fields.re);
135*41355Sbostic 	if (r == NULL) {
136*41355Sbostic 		if (*fields.ans != 'c')
137*41355Sbostic 			complain("regcomp failure in `%s'", fields.re);
138*41355Sbostic 		return;
139*41355Sbostic 	}
140*41355Sbostic 	if (*fields.ans == 'c') {
141*41355Sbostic 		complain("unexpected regcomp success in `%s'", fields.re);
142*41355Sbostic 		free((char *)r);
143*41355Sbostic 		return;
144*41355Sbostic 	}
145*41355Sbostic 	for (i = ncomp-1; i > 0; i--) {
146*41355Sbostic 		free((char *)r);
147*41355Sbostic 		r = regcomp(fields.re);
148*41355Sbostic 	}
149*41355Sbostic 	if (!regexec(r, fields.str)) {
150*41355Sbostic 		if (*fields.ans != 'n')
151*41355Sbostic 			complain("regexec failure in `%s'", "");
152*41355Sbostic 		free((char *)r);
153*41355Sbostic 		return;
154*41355Sbostic 	}
155*41355Sbostic 	if (*fields.ans == 'n') {
156*41355Sbostic 		complain("unexpected regexec success", "");
157*41355Sbostic 		free((char *)r);
158*41355Sbostic 		return;
159*41355Sbostic 	}
160*41355Sbostic 	for (i = nexec-1; i > 0; i--)
161*41355Sbostic 		(void) regexec(r, fields.str);
162*41355Sbostic 	errseen = NULL;
163*41355Sbostic 	for (i = nsub; i > 0; i--)
164*41355Sbostic 		regsub(r, fields.src, dbuf);
165*41355Sbostic 	if (errseen != NULL) {
166*41355Sbostic 		complain("regsub complaint", "");
167*41355Sbostic 		free((char *)r);
168*41355Sbostic 		return;
169*41355Sbostic 	}
170*41355Sbostic 	if (strcmp(dbuf, fields.dst) != 0)
171*41355Sbostic 		complain("regsub result `%s' wrong", dbuf);
172*41355Sbostic 	free((char *)r);
173*41355Sbostic }
174*41355Sbostic 
complain(s1,s2)175*41355Sbostic complain(s1, s2)
176*41355Sbostic char *s1;
177*41355Sbostic char *s2;
178*41355Sbostic {
179*41355Sbostic 	fprintf(stderr, "try: %d: ", lineno);
180*41355Sbostic 	fprintf(stderr, s1, s2);
181*41355Sbostic 	fprintf(stderr, " (%s)\n", (errseen != NULL) ? errseen : "");
182*41355Sbostic }
183