1/*
2 * test4600 - 4600 series of the regress.cal test suite
3 *
4 * Copyright (C) 1999  Ernest Bowen and Landon Curt Noll
5 *
6 * Primary author:  Ernest Bowen
7 *
8 * Calc is open software; you can redistribute it and/or modify it under
9 * the terms of the version 2.1 of the GNU Lesser General Public License
10 * as published by the Free Software Foundation.
11 *
12 * Calc is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU Lesser General
15 * Public License for more details.
16 *
17 * A copy of version 2.1 of the GNU Lesser General Public License is
18 * distributed with calc under the filename COPYING-LGPL.  You should have
19 * received a copy with calc; if not, write to Free Software Foundation, Inc.
20 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 * Under source code control:	1996/07/02 20:04:40
23 * File existed as early as:	1996
24 *
25 * Share and enjoy!  :-)	http://www.isthe.com/chongo/tech/comp/calc/
26 */
27
28
29defaultverbose = 1;	/* default verbose value */
30
31/*
32 * test globals
33 */
34global A, f, pos;
35
36define stest(str, verbose)
37{
38	local x;
39
40	/* setup */
41	if (isnull(verbose))
42		verbose = defaultverbose;
43	if (verbose > 0) {
44		print str:":",:;
45	}
46	x = rm("-f", "junk4600");
47
48	/*
49	 * do file operations
50	 */
51	f = fopen("junk4600", "wb");
52	if (iserror(f)) {
53		print 'failed';
54		print '**** fopen("junk4600", "wb") failed';
55		return 1;
56	}
57	if (iserror(fputs(f,
58	 "Fourscore and seven years ago our fathers brought forth\n",
59	 "on this continent a new nation, conceived in liberty and dedicated\n",
60	 "to the proposition that all men are created equal.\n"))) {
61		print 'failed';
62		print '**** fputs(f, "Fourscore ... failed';
63		return 1;
64	}
65	if (iserror(freopen(f, "rb"))) {
66		print 'failed';
67		print '**** iserror(freopen(f, "rb")) failed';
68		return 1;
69	}
70	if (iserror(rewind(f))) {
71		print 'failed';
72		print '**** iserror(rewind(f)) failed';
73		return 1;
74	}
75	if (search(f, "and") != 10) {
76		print 'failed';
77		print '**** search(f, "and") != 10 failed';
78		return 1;
79	}
80	if (ftell(f) != 13) {
81		print 'failed';
82		print '**** ftell(f) != 13 failed';
83		return 1;
84	}
85	if (search(f, "and") != 109) {
86		print 'failed';
87		print '**** search(f, "and") != 109 failed';
88		return 1;
89	}
90	if (ftell(f) != 112) {
91		print 'failed';
92		print '**** ftell(f) != 112 failed';
93		return 1;
94	}
95	if (!isnull(search(f, "and"))) {
96		print 'failed';
97		print '**** !isnull(search(f, "and")) failed';
98		return 1;
99	}
100	if (ftell(f) != 172) {
101		print 'failed';
102		print '**** ftell(f) != 172 failed';
103		return 1;
104	}
105	if (rsearch(f, "and") != 109) {
106		print 'failed';
107		print '**** rsearch(f, "and") != 109 failed';
108		return 1;
109	}
110	if (ftell(f) != 111) {
111		print 'failed';
112		print '**** ftell(f) != 111 failed';
113		return 1;
114	}
115	if (iserror(fseek(f, -4, 1))) {
116		print 'failed';
117		print '**** iserror(fseek(f, -4, 1)) failed';
118		return 1;
119	}
120	if (rsearch(f, "and") != 10) {
121		print 'failed';
122		print '**** rsearch(f, "and") != 10 failed';
123		return 1;
124	}
125	if (ftell(f) != 12) {
126		print 'failed';
127		print '**** ftell(f) != 12 failed';
128		return 1;
129	}
130	if (iserror(fseek(f, -4, 1))) {
131		print 'failed';
132		print '**** iserror(fseek(f, -4, 1)) failed';
133		return 1;
134	}
135	if (!isnull(rsearch(f, "and"))) {
136		print 'failed';
137		print '**** !isnull(rsearch(f, "and")) failed';
138		return 1;
139	}
140	if (ftell(f) != 0) {
141		print 'failed';
142		print '**** ftell(f) != 0 failed';
143		return 1;
144	}
145	if (iserror(fclose(f))) {
146		print 'failed';
147		print '**** iserror(fclose(f)) failed';
148		return 1;
149	}
150
151	/*
152	 * cleanup
153	 */
154	x = rm("junk4600");
155	if (verbose > 0) {
156		printf("passed\n");
157	}
158	return 0;
159}
160
161define ttest(str, m, n, verbose)
162{
163	local a, s, i, j;
164
165	if (isnull(verbose))
166		verbose = defaultverbose;
167	if (verbose > 0) {
168		print str:":",:;
169	}
170	i = rm("-f", "junk4600");
171	f = fopen("junk4600", "wb");
172
173	if (isnull(n))
174		n = 4;
175	if (isnull(m))
176		m = 4;
177
178	mat A[m];
179	mat pos[m + 1];
180
181	pos[0] = 0;
182	for (i = 0; i < m; i++) {
183		j = 1 + randbit(n);
184		a = "";
185		while (j-- > 0)
186			a = strcat(a, char(rand(32, 127)));
187		A[i] = a;
188		fputs(f, a);
189		pos[i+1] = ftell(f);
190		if (verbose > 1)
191			printf("A[%d] has length %d\n", i, strlen(a));
192	}
193	fflush(f);
194	if (verbose > 1)
195		printf("File has size %d\n", pos[i]);
196	freopen(f, "rb");
197	if (size(f) != pos[i]) {
198		print 'failed';
199		printf("**** Failure 1 for file size\n");
200		return 1;
201	}
202	for (i = 0; i < m; i++) {
203		rewind(f);
204		for (;;) {
205			j = search(f, A[i]);
206			if (isnull(j) || j > pos[i]) {
207				print 'failed';
208				printf("**** Failure 2 for i = %d\n", i);
209				return 1;
210			}
211			if (j == pos[i])
212				break;
213			fseek(f, j + 1, 0);
214
215		}
216		if (ftell(f) != pos[i + 1]) {
217			print 'failed';
218			printf("**** Failure 3 for i = %d\n", i);
219			return 1;
220		}
221	}
222	for (i = m - 1; i >= 0; i--) {
223		fseek(f, 0, 2);
224		for (;;) {
225			j = rsearch(f, A[i]);
226			if (isnull(j) || j < pos[i]) {
227				print 'failed';
228				printf("**** Failure 4 for i = %d\n", i);
229				return 1;
230			}
231			if (j == pos[i])
232				break;
233			fseek(f, -1, 1);
234		}
235		if (ftell(f) != pos[i + 1] - 1) {
236			print 'failed';
237			printf("**** Failure 5 for i = %d\n", i);
238			return 1;
239		}
240	}
241	if (iserror(fclose(f))) {
242		print 'failed';
243		printf("**** Failure 6 for i = %d\n", i);
244		return 1;
245	}
246	i = rm("junk4600");
247	if (verbose > 0) {
248		printf("passed\n");
249	}
250	return 0;
251}
252
253define sprint(x)
254{
255	local i, n;
256
257	n = strlen(x);
258	for (i = 1; i <= n; i++) print ord(substr(x, i, 1)),;
259	print;
260}
261
262define findline(f,s)
263{
264
265	if (!isfile(f))
266		quit "First argument to be a file";
267	if (!isstr(s))
268		quit "Second argument to be a string";
269	if (!isnull(search(f,s))) {
270		rsearch(f, "\n");
271		print fgetline(f);
272	}
273}
274
275define findlineold(f,s)
276{
277	local str;
278
279	if (!isfile(f))
280		quit "First argument to be a file";
281	if (!isstr(s))
282		quit "Second argument to be a string";
283
284	while (!isnull(str = fgetline(f)) && strpos(str, s) == 0);
285		print str;
286}
287
288/*
289 * test4600 - perform all of the above tests a bunch of times
290 */
291define test4600(v, tnum)
292{
293	local n;	/* test parameter */
294	local i;
295
296	/*
297	 * set test parameters
298	 */
299	srand(4600e4600);
300
301	/*
302	 * test a lot of stuff
303	 */
304	for (i=0; i < 10; ++i) {
305		err += ttest(strcat(str(tnum++),
306			     ": ttest(",str(i),",",str(i),")"), i, i, v);
307		err += stest(strcat(str(tnum++), ": stest()"), v);
308	}
309
310	/*
311	 * report results
312	 */
313	if (v > 1) {
314		if (err) {
315			print "****", err, "error(s) found in testall";
316		} else {
317			print "no errors in testall";
318		}
319	}
320	return tnum;
321}
322