xref: /original-bsd/lib/libc/db/test/run.test (revision c3e32dec)
1#!/bin/sh -
2#
3#	@(#)run.test	8.1 (Berkeley) 06/04/93
4#
5
6# db regression tests
7
8main()
9{
10	DICT=/usr/share/dict/words
11	PROG=obj/dbtest
12	TMP1=t1
13	TMP2=t2
14	TMP3=t3
15
16	test1
17	test2
18	test3
19	test4
20	test5
21	test6
22	test7
23	test8
24	test9
25	test10
26	test11
27	test12
28	test13
29	test20
30	rm -f $TMP1 $TMP2 $TMP3
31	exit 0
32}
33
34# Take the first hundred entries in the dictionary, and make them
35# be key/data pairs.
36test1()
37{
38	printf "Test 1: btree, hash: small key, small data pairs\n"
39	sed 200q $DICT > $TMP1
40	for type in btree hash; do
41		rm -f $TMP2 $TMP3
42		for i in `sed 200q $DICT`; do
43			printf "p\nk%s\nd%s\ng\nk%s\n" $i $i $i
44		done > $TMP2
45		$PROG -o $TMP3 $type $TMP2
46		if (cmp -s $TMP1 $TMP3) ; then :
47		else
48			printf "test1: type %s: failed\n" $type
49			exit 1
50		fi
51	done
52	printf "Test 1: recno: small key, small data pairs\n"
53	rm -f $TMP2 $TMP3
54	sed 200q $DICT |
55	awk '{
56		++i;
57		printf("p\nk%d\nd%s\ng\nk%d\n", i, $0, i);
58	}' > $TMP2
59	$PROG -o $TMP3 recno $TMP2
60	if (cmp -s $TMP1 $TMP3) ; then :
61	else
62		printf "test1: type recno: failed\n"
63		exit 1
64	fi
65}
66
67# Take the first 200 entries in the dictionary, and give them
68# each a medium size data entry.
69test2()
70{
71	printf "Test 2: btree, hash: small key, medium data pairs\n"
72	mdata=abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
73	echo $mdata |
74	awk '{ for (i = 1; i < 201; ++i) print $0 }' > $TMP1
75	for type in hash btree; do
76		rm -f $TMP2 $TMP3
77		for i in `sed 200q $DICT`; do
78			printf "p\nk%s\nd%s\ng\nk%s\n" $i $mdata $i
79		done > $TMP2
80		$PROG -o $TMP3 $type $TMP2
81		if (cmp -s $TMP1 $TMP3) ; then :
82		else
83			printf "test2: type %s: failed\n" $type
84			exit 1
85		fi
86	done
87	printf "Test 2: recno: small key, medium data pairs\n"
88	rm -f $TMP2 $TMP3
89	echo $mdata |
90	awk '{  for (i = 1; i < 201; ++i)
91		printf("p\nk%d\nd%s\ng\nk%d\n", i, $0, i);
92	}' > $TMP2
93	$PROG -o $TMP3 recno $TMP2
94	if (cmp -s $TMP1 $TMP3) ; then :
95	else
96		printf "test2: type recno: failed\n"
97		exit 1
98	fi
99}
100
101# Insert the programs in /bin with their paths as their keys.
102test3()
103{
104	printf "Test 3: hash: small key, big data pairs\n"
105	rm -f $TMP1
106	(find /bin -type f -print | xargs cat) > $TMP1
107	for type in hash; do
108		rm -f $TMP2 $TMP3
109		for i in `find /bin -type f -print`; do
110			printf "p\nk%s\nD%s\ng\nk%s\n" $i $i $i
111		done > $TMP2
112		$PROG -o $TMP3 $type $TMP2
113		if (cmp -s $TMP1 $TMP3) ; then :
114		else
115			printf "test3: %s: page size %d: failed\n" \
116			    $type $psize
117			exit 1
118		fi
119	done
120	printf "Test 3: btree: small key, big data pairs\n"
121	for psize in 512 16384 65536; do
122		printf "\tpage size %d\n" $psize
123		for type in btree; do
124			rm -f $TMP2 $TMP3
125			for i in `find /bin -type f -print`; do
126				printf "p\nk%s\nD%s\ng\nk%s\n" $i $i $i
127			done > $TMP2
128			$PROG -i psize=$psize -o $TMP3 $type $TMP2
129			if (cmp -s $TMP1 $TMP3) ; then :
130			else
131				printf "test3: %s: page size %d: failed\n" \
132				    $type $psize
133				exit 1
134			fi
135		done
136	done
137	printf "Test 3: recno: big data pairs\n"
138	rm -f $TMP2 $TMP3
139	find /bin -type f -print |
140	awk '{
141		++i;
142		printf("p\nk%d\nD%s\ng\nk%d\n", i, $0, i);
143	}' > $TMP2
144	for psize in 512 16384 65536; do
145		printf "\tpage size %d\n" $psize
146		$PROG -i psize=$psize -o $TMP3 recno $TMP2
147		if (cmp -s $TMP1 $TMP3) ; then :
148		else
149			printf "test3: recno: page size %d: failed\n" $psize
150			exit 1
151		fi
152	done
153}
154
155# Do random recno entries.
156test4()
157{
158	printf "Test 4: recno: random entries\n"
159	echo "abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg" |
160	awk '{
161		for (i = 37; i <= 37 + 88 * 17; i += 17)
162			printf("input key %d: %.*s\n", i, i % 41, $0);
163		for (i = 1; i <= 15; ++i)
164			printf("input key %d: %.*s\n", i, i % 41, $0);
165		for (i = 19234; i <= 19234 + 61 * 27; i += 27)
166			printf("input key %d: %.*s\n", i, i % 41, $0);
167		exit
168	}' > $TMP1
169	rm -f TMP2 $TMP3
170	cat $TMP1 |
171	awk 'BEGIN {
172			i = 37;
173			incr = 17;
174		}
175		{
176			printf("p\nk%d\nd%s\n", i, $0);
177			if (i == 19234 + 61 * 27)
178				exit;
179			if (i == 37 + 88 * 17) {
180				i = 1;
181				incr = 1;
182			} else if (i == 15) {
183				i = 19234;
184				incr = 27;
185			} else
186				i += incr;
187		}
188		END {
189			for (i = 37; i <= 37 + 88 * 17; i += 17)
190				printf("g\nk%d\n", i);
191			for (i = 1; i <= 15; ++i)
192				printf("g\nk%d\n", i);
193			for (i = 19234; i <= 19234 + 61 * 27; i += 27)
194				printf("g\nk%d\n", i);
195		}' > $TMP2
196	$PROG -o $TMP3 recno $TMP2
197	if (cmp -s $TMP1 $TMP3) ; then :
198	else
199		printf "test4: type recno: failed\n"
200		exit 1
201	fi
202}
203
204# Do reverse order recno entries.
205test5()
206{
207	printf "Test 5: recno: reverse order entries\n"
208	echo "abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg" |
209	awk ' {
210		for (i = 1500; i; --i)
211			printf("input key %d: %.*s\n", i, i % 34, $0);
212		exit;
213	}' > $TMP1
214	rm -f TMP2 $TMP3
215	cat $TMP1 |
216	awk 'BEGIN {
217			i = 1500;
218		}
219		{
220			printf("p\nk%d\nd%s\n", i, $0);
221			--i;
222		}
223		END {
224			for (i = 1500; i; --i)
225				printf("g\nk%d\n", i);
226		}' > $TMP2
227	$PROG -o $TMP3 recno $TMP2
228	if (cmp -s $TMP1 $TMP3) ; then :
229	else
230		printf "test5: type recno: failed\n"
231		exit 1
232	fi
233}
234
235# Do alternating order recno entries.
236test6()
237{
238	printf "Test 6: recno: alternating order entries\n"
239	echo "abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg" |
240	awk ' {
241		for (i = 1; i < 1200; i += 2)
242			printf("input key %d: %.*s\n", i, i % 34, $0);
243		for (i = 2; i < 1200; i += 2)
244			printf("input key %d: %.*s\n", i, i % 34, $0);
245		exit;
246	}' > $TMP1
247	rm -f TMP2 $TMP3
248	cat $TMP1 |
249	awk 'BEGIN {
250			i = 1;
251			even = 0;
252		}
253		{
254			printf("p\nk%d\nd%s\n", i, $0);
255			i += 2;
256			if (i >= 1200) {
257				if (even == 1)
258					exit;
259				even = 1;
260				i = 2;
261			}
262		}
263		END {
264			for (i = 1; i < 1200; ++i)
265				printf("g\nk%d\n", i);
266		}' > $TMP2
267	$PROG -o $TMP3 recno $TMP2
268	sort -o $TMP1 $TMP1
269	sort -o $TMP3 $TMP3
270	if (cmp -s $TMP1 $TMP3) ; then :
271	else
272		printf "test6: type recno: failed\n"
273		exit 1
274	fi
275}
276
277# Delete cursor record
278test7()
279{
280	printf "Test 7: btree, recno: delete cursor record\n"
281	echo "abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg" |
282	awk '{
283		for (i = 1; i <= 120; ++i)
284			printf("%05d: input key %d: %s\n", i, i, $0);
285		printf("%05d: input key %d: %s\n", 120, 120, $0);
286		printf("get failed, no such key\n");
287		printf("%05d: input key %d: %s\n", 1, 1, $0);
288		printf("%05d: input key %d: %s\n", 2, 2, $0);
289		exit;
290	}' > $TMP1
291	rm -f TMP2 $TMP3
292
293	for type in btree recno; do
294		cat $TMP1 |
295		awk '{
296			if (i == 120)
297				exit;
298			printf("p\nk%d\nd%s\n", ++i, $0);
299		}
300		END {
301			printf("fR_NEXT\n");
302			for (i = 1; i <= 120; ++i)
303				printf("s\n");
304			printf("fR_CURSOR\ns\nk120\n");
305			printf("r\nk120\n");
306			printf("fR_NEXT\ns\n");
307			printf("fR_CURSOR\ns\nk1\n");
308			printf("r\nk1\n");
309			printf("fR_FIRST\ns\n");
310		}' > $TMP2
311		$PROG -o $TMP3 recno $TMP2
312		if (cmp -s $TMP1 $TMP3) ; then :
313		else
314			printf "test7: type $type: failed\n"
315			exit 1
316		fi
317	done
318}
319
320# Make sure that overflow pages are reused.
321test8()
322{
323	printf "Test 8: btree, hash: repeated small key, big data pairs\n"
324	rm -f $TMP1
325	awk 'BEGIN {
326		for (i = 1; i <= 10; ++i) {
327			printf("p\nkkey1\nD/bin/sh\n");
328			printf("p\nkkey2\nD/bin/csh\n");
329			if (i % 8 == 0) {
330				printf("c\nkkey2\nD/bin/csh\n");
331				printf("c\nkkey1\nD/bin/sh\n");
332				printf("e\t%d of 10 (comparison)\r\n", i);
333			} else
334				printf("e\t%d of 10             \r\n", i);
335			printf("r\nkkey1\nr\nkkey2\n");
336		}
337		printf("e\n");
338		printf("eend of test8 run\n");
339	}' > $TMP1
340	$PROG btree $TMP1
341	$PROG hash $TMP1
342	# No explicit test for success.
343}
344
345# Test btree duplicate keys
346test9()
347{
348	printf "Test 9: btree: duplicate keys\n"
349	echo "abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg" |
350	awk '{
351		for (i = 1; i <= 543; ++i)
352			printf("%05d: input key %d: %s\n", i, i, $0);
353		exit;
354	}' > $TMP1
355	rm -f TMP2 $TMP3
356
357	for type in btree; do
358		cat $TMP1 |
359		awk '{
360			if (i++ % 2)
361				printf("p\nkduplicatekey\nd%s\n", $0);
362			else
363				printf("p\nkunique%dkey\nd%s\n", i, $0);
364		}
365		END {
366				printf("o\n");
367		}' > $TMP2
368		$PROG -iflags=1 -o $TMP3 $type $TMP2
369		sort -o $TMP3 $TMP3
370		if (cmp -s $TMP1 $TMP3) ; then :
371		else
372			printf "test9: type $type: failed\n"
373			exit 1
374		fi
375	done
376}
377
378# Test use of cursor flags without initialization
379test10()
380{
381	printf "Test 10: btree, recno: test cursor flag use\n"
382	echo "abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg" |
383	awk '{
384		for (i = 1; i <= 20; ++i)
385			printf("%05d: input key %d: %s\n", i, i, $0);
386		exit;
387	}' > $TMP1
388	rm -f TMP2 $TMP3
389
390	# Test that R_CURSOR doesn't succeed before cursor initialized
391	for type in btree recno; do
392		cat $TMP1 |
393		awk '{
394			if (i == 10)
395				exit;
396			printf("p\nk%d\nd%s\n", ++i, $0);
397		}
398		END {
399			printf("fR_CURSOR\nr\nk1\n");
400			printf("eR_CURSOR SHOULD HAVE FAILED\n");
401		}' > $TMP2
402		$PROG -o $TMP3 $type $TMP2 > /dev/null 2>&1
403		if [ -s $TMP3 ] ; then
404			printf "Test 10: delete: R_CURSOR SHOULD HAVE FAILED\n"
405			exit 1
406		fi
407	done
408	for type in btree recno; do
409		cat $TMP1 |
410		awk '{
411			if (i == 10)
412				exit;
413			printf("p\nk%d\nd%s\n", ++i, $0);
414		}
415		END {
416			printf("fR_CURSOR\np\nk1\ndsome data\n");
417			printf("eR_CURSOR SHOULD HAVE FAILED\n");
418		}' > $TMP2
419		$PROG -o $TMP3 $type $TMP2 > /dev/null 2>&1
420		if [ -s $TMP3 ] ; then
421			printf "Test 10: put: R_CURSOR SHOULD HAVE FAILED\n"
422			exit 1
423		fi
424	done
425}
426
427# Test insert in reverse order.
428test11()
429{
430	printf "Test 11: recno: reverse order insert\n"
431	echo "abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg" |
432	awk '{
433		for (i = 1; i <= 779; ++i)
434			printf("%05d: input key %d: %s\n", i, i, $0);
435		exit;
436	}' > $TMP1
437	rm -f TMP2 $TMP3
438
439	for type in recno; do
440		cat $TMP1 |
441		awk '{
442			if (i == 0) {
443				i = 1;
444				printf("p\nk1\nd%s\n", $0);
445				printf("%s\n", "fR_IBEFORE");
446			} else
447				printf("p\nk1\nd%s\n", $0);
448		}
449		END {
450				printf("or\n");
451		}' > $TMP2
452		$PROG -o $TMP3 $type $TMP2
453		if (cmp -s $TMP1 $TMP3) ; then :
454		else
455			printf "test11: type $type: failed\n"
456			exit 1
457		fi
458	done
459}
460
461# Take the first 20000 entries in the dictionary, reverse them, and give
462# them each a small size data entry.  Use a small page size to make sure
463# the btree split code gets hammered.
464test12()
465{
466	printf "Test 12: btree: lots of keys, small page size\n"
467	mdata=abcdefghijklmnopqrstuvwxy
468	echo $mdata |
469	awk '{ for (i = 1; i < 20001; ++i) print $0 }' > $TMP1
470	for type in btree; do
471		rm -f $TMP2 $TMP3
472		for i in `sed 20000q $DICT | rev`; do
473			printf "p\nk%s\nd%s\ng\nk%s\n" $i $mdata $i
474		done > $TMP2
475		$PROG -i psize=512 -o $TMP3 $type $TMP2
476		if (cmp -s $TMP1 $TMP3) ; then :
477		else
478			printf "test12: type %s: failed\n" $type
479			exit 1
480		fi
481	done
482}
483
484# Test different byte orders.
485test13()
486{
487	printf "Test 13: btree, hash: differing byte orders\n"
488	sed 50q $DICT > $TMP1
489	for order in 1234 4321; do
490		for type in btree hash; do
491			rm -f byte.file $TMP2 $TMP3
492			for i in `sed 50q $DICT`; do
493				printf "p\nk%s\nd%s\ng\nk%s\n" $i $i $i
494			done > $TMP2
495			$PROG -ilorder=$order -f byte.file -o $TMP3 $type $TMP2
496			if (cmp -s $TMP1 $TMP3) ; then :
497			else
498				printf "test13: %s/%s put failed\n" $type $order
499				exit 1
500			fi
501			for i in `sed 50q $DICT`; do
502				printf "g\nk%s\n" $i
503			done > $TMP2
504			$PROG -ilorder=$order -f byte.file -o $TMP3 $type $TMP2
505			if (cmp -s $TMP1 $TMP3) ; then :
506			else
507				printf "test13: %s/%s get failed\n" $type $order
508				exit 1
509			fi
510		done
511	done
512	rm -f byte.file
513}
514
515# Try a variety of bucketsizes and fill factors for hashing
516test20()
517{
518	printf\
519    "Test 20: hash: bucketsize, fill factor; nelem 25000 cachesize 65536\n"
520	awk 'BEGIN {
521		for (i = 1; i <= 10000; ++i)
522			printf("%.*s\n", i % 34,
523		    "abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg");
524	}' > $TMP1
525	sed 10000q $DICT |
526	awk '{
527		++i;
528		printf("p\nk%s\nd%.*s\n", $0, i % 34,
529		    "abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg");
530	}' > $TMP2
531	sed 10000q $DICT |
532	awk '{
533		++i;
534		printf("g\nk%s\n", $0);
535	}' >> $TMP2
536	bsize=256
537	for ffactor in 11 14 21; do
538		printf "\tbucketsize %d, fill factor %d\n" $bsize $ffactor
539		$PROG -o$TMP3 \
540		    -ibsize=$bsize,ffactor=$ffactor,nelem=25000,cachesize=65536\
541		    hash $TMP2
542		if (cmp -s $TMP1 $TMP3) ; then :
543		else
544			printf "test20: type hash:\
545bsize=$bsize ffactor=$ffactor nelem=25000 cachesize=65536 failed\n"
546			exit 1
547		fi
548	done
549	bsize=512
550	for ffactor in 21 28 43; do
551		printf "\tbucketsize %d, fill factor %d\n" $bsize $ffactor
552		$PROG -o$TMP3 \
553		    -ibsize=$bsize,ffactor=$ffactor,nelem=25000,cachesize=65536\
554		    hash $TMP2
555		if (cmp -s $TMP1 $TMP3) ; then :
556		else
557			printf "test20: type hash:\
558bsize=$bsize ffactor=$ffactor nelem=25000 cachesize=65536 failed\n"
559			exit 1
560		fi
561	done
562	bsize=1024
563	for ffactor in 43 57 85; do
564		printf "\tbucketsize %d, fill factor %d\n" $bsize $ffactor
565		$PROG -o$TMP3 \
566		    -ibsize=$bsize,ffactor=$ffactor,nelem=25000,cachesize=65536\
567		    hash $TMP2
568		if (cmp -s $TMP1 $TMP3) ; then :
569		else
570			printf "test20: type hash:\
571bsize=$bsize ffactor=$ffactor nelem=25000 cachesize=65536 failed\n"
572			exit 1
573		fi
574	done
575	bsize=2048
576	for ffactor in 85 114 171; do
577		printf "\tbucketsize %d, fill factor %d\n" $bsize $ffactor
578		$PROG -o$TMP3 \
579		    -ibsize=$bsize,ffactor=$ffactor,nelem=25000,cachesize=65536\
580		    hash $TMP2
581		if (cmp -s $TMP1 $TMP3) ; then :
582		else
583			printf "test20: type hash:\
584bsize=$bsize ffactor=$ffactor nelem=25000 cachesize=65536 failed\n"
585			exit 1
586		fi
587	done
588	bsize=4096
589	for ffactor in 171 228 341; do
590		printf "\tbucketsize %d, fill factor %d\n" $bsize $ffactor
591		$PROG -o$TMP3 \
592		    -ibsize=$bsize,ffactor=$ffactor,nelem=25000,cachesize=65536\
593		    hash $TMP2
594		if (cmp -s $TMP1 $TMP3) ; then :
595		else
596			printf "test20: type hash:\
597bsize=$bsize ffactor=$ffactor nelem=25000 cachesize=65536 failed\n"
598			exit 1
599		fi
600	done
601	bsize=8192
602	for ffactor in 341 455 683; do
603		printf "\tbucketsize %d, fill factor %d\n" $bsize $ffactor
604		$PROG -o$TMP3 \
605		    -ibsize=$bsize,ffactor=$ffactor,nelem=25000,cachesize=65536\
606		    hash $TMP2
607		if (cmp -s $TMP1 $TMP3) ; then :
608		else
609			printf "test20: type hash:\
610bsize=$bsize ffactor=$ffactor nelem=25000 cachesize=65536 failed\n"
611			exit 1
612		fi
613	done
614}
615
616main
617