1/*
2 * dotest - test truth statements found in line tests of dotest_testline file
3 *
4 * This file was created by Ernest Bowen <ebowen at une dot edu dot au>
5 * and modified by Landon Curt Noll.
6 *
7 * This dotest_code has been placed in the public domain.  Please do not
8 * copyright this dotest_code.
9 *
10 * ERNEST BOWEN AND LANDON CURT NOLL DISCLAIMS ALL WARRANTIES WITH REGARD TO
11 * THIS         SOFTWARE,  INCLUDING  ALL IMPLIED WARRANTIES OF MER-
12 * CHANTABILITY AND FITNESS.  IN NO EVENT SHALL         LANDON  CURT
13 * NOLL	 BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM  LOSS  OF
15 * USE,	 DATA  OR  PROFITS, WHETHER IN AN ACTION OF CONTRACT,
16 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR  IN
17 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 *
19 * This file is not covered under version 2.1 of the GNU LGPL.
20 *
21 * Under source dotest_code control:	2006/03/08 05:54:09
22 * File existed as early as:	2006
23 */
24
25
26/*
27 * dotest - perform tests from dotest_testline file
28 *
29 * given:
30 *	dotest_file	filename containing single test lines
31 *	dotest_code	regress.cal test number to use (def: 0)
32 *	dotest_maxcond	max error conditions allowed (def: <0 ==> 2^31-1)
33 *
34 * returns:
35 *	number of line test failures
36 *
37 * NOTE: All variables used by the dotest() function start with "dotest_".
38 *	 The dotest_file and dotest_read should not use any variable
39 *	 that starts with "dotest_".
40 */
41define dotest(dotest_file, dotest_code = 0, dotest_maxcond = -1)
42{
43    local dotest_f_file;	/* open file containing test lines */
44    local dotest_testline;	/* test line */
45    local dotest_testeval;	/* eval value from dotest_testline test line */
46    local dotest_tmperrcnt;	/* temp error count after line test */
47    local dotest_errcnt;	/* total number of errors */
48    local dotest_failcnt;	/* number of line tests failed */
49    local dotest_testnum;	/* number of test lines evaluated */
50    local dotest_linenum;	/* test line number */
51    local dotest_old_errmax;	/* value of errmax() prior to calling */
52    local dotest_old_errcount;	/* value of errcount() prior to calling */
53
54    /*
55     * preserve calling stats
56     */
57    dotest_old_errmax = errmax();
58    dotest_old_errcount = errcount(0);
59
60    /*
61     * initialize test accounting
62     */
63    dotest_errcnt = errcount();
64    dotest_failcnt = 0;
65    dotest_testnum = 0;
66    dotest_linenum = 0;
67
68    /*
69     * setup error accounting for dotest
70     */
71    if (dotest_maxcond >= 0 && dotest_maxcond < 2147483647) {
72	    errmax(dotest_maxcond + dotest_old_errcount + 1),;
73    } else {
74	    errmax(2147483647),;
75    }
76
77    /*
78     * open the test line file
79     */
80    printf("%d-: opening line file: %d", dotest_code, dotest_file);
81    dotest_f_file = fpathopen(dotest_file, "r");
82    if (!isfile(dotest_f_file)) {
83	printf("**** Unable to file or open file \"%s\"\n",
84		dotest_file);
85	quit;
86    }
87    printf('%d: testing "%s"\n', dotest_code, dotest_file);
88
89    /*
90     * perform dotest_testline test on each line of the file
91     */
92    for (;;) {
93
94	/* get the next test line */
95	dotest_testline = fgets(dotest_f_file);
96	++dotest_linenum;
97	if (iserror(dotest_testline)) {
98	    quit "**** Error while reading file";
99	} else if (isnull(dotest_testline)) {
100	    /* EOF - end of test file */
101	    break;
102	}
103
104	/* skip empty lines */
105	if (dotest_testline == "\n") {
106	    continue;
107	}
108
109	/* evaluate the test line */
110	dotest_testeval = eval(dotest_testline);
111
112	/* ignore white space or comment lines */
113	if (isnull(dotest_testeval)) {
114	    continue;
115	}
116
117	/* look for test line parse errors */
118	if (iserror(dotest_testeval)) {
119	    printf("**** evaluation error: ");
120	    ++dotest_failcnt;
121
122	/* look for test line dotest_failcnt */
123	} else if (dotest_testeval != 1) {
124	    printf("**** did not return 1: ");
125	    ++dotest_failcnt;
126	}
127
128	/* show the test line we just performed */
129	printf("%d-%d: %s", dotest_code, dotest_linenum, dotest_testline);
130
131	/* error accounting */
132	dotest_tmperrcnt = errcount() - dotest_errcnt;
133	if (dotest_tmperrcnt > 0) {
134
135	    /* report any other errors */
136	    if (dotest_tmperrcnt > 1) {
137		printf("%d-%d: NOTE: %d error conditions(s): %s\n",
138		    dotest_code, dotest_linenum, dotest_tmperrcnt);
139	    }
140
141	    /* report the calc error string */
142	    printf("%d-%d: NOTE: last error string: %s\n",
143	        dotest_code, dotest_linenum, strerror());
144
145	    /* new error count level */
146	    dotest_errcnt = errcount();
147	    if (dotest_maxcond >= 0 &&
148	        dotest_old_errcount-dotest_errcnt > dotest_maxcond) {
149		printf("%d-%d: total error conditions: %d > %d\n",
150		       dotest_code, dotest_linenum,
151		       dotest_maxcond, dotest_old_errcount-dotest_errcnt);
152	    }
153	}
154    }
155
156    /*
157     * test the close of the line file
158     */
159    printf("%d-: detected %d error condition(s), many of which may be OK\n",
160	   dotest_code, dotest_old_errcount-dotest_errcnt);
161    printf("%d-: closing line file: %d\n", dotest_code, dotest_file);
162    fclose(dotest_f_file);
163
164    /*
165     * test line file accounting
166     */
167    if (dotest_failcnt > 0) {
168	printf("**** %d-: %d test failure(s) in %d line(s)\n",
169	    dotest_code, dotest_failcnt, dotest_linenum);
170    } else {
171	printf("%d-: no failure(s) in %d line(s)\n",
172	    dotest_code, dotest_linenum);
173    }
174
175    /*
176     * prepare to return to the caller environment
177     *
178     * We increase the caller's error count by the number
179     * of line tests that failed, not the number of internal
180     * errors that were noted.
181     */
182    errmax(dotest_old_errmax),;
183    errcount(dotest_old_errcount + dotest_failcnt),;
184
185    /*
186     * All Done!!! -- Jessica Noll, Age 2
187     */
188    return dotest_failcnt;
189}
190