1#!/bin/sh
2#
3# cmptest @(#)cmptest.sh	1.5 20/08/25 Copyright 2015-2020 J. Schilling
4#
5# Usage: cmptest	---> runs 1000 test loops
6#	 cmptest #	---> runs # test loops
7#
8# A file is generated, then modified.
9#
10
11# Read test core functions
12. ../../common/test-common
13
14[ "$NO_RANDOM" = TRUE ] && exit
15
16cmd=admin		# for ../../common/optv
17ocmd=${admin}		# for ../../common/optv
18g=foo
19s=s.$g
20p=p.$g
21z=z.$g
22
23remove $z $s $p $g
24
25: ${AWK=/usr/bin/nawk}
26#$AWK 'BEGIN {print rand()}' < /dev/null > /dev/null 2> /dev/null || AWK=/usr/bin/nawk
27$AWK 'BEGIN {print rand()}' < /dev/null > /dev/null 2> /dev/null || AWK=/usr/bin/gawk
28$AWK 'BEGIN {print rand()}' < /dev/null > /dev/null 2> /dev/null || AWK=/usr/bin/awk
29$AWK 'BEGIN {print rand()}' < /dev/null > /dev/null 2> /dev/null || AWK=nawk
30$AWK 'BEGIN {print rand()}' < /dev/null > /dev/null 2> /dev/null || AWK=gawk
31$AWK 'BEGIN {print rand()}' < /dev/null > /dev/null 2> /dev/null || AWK=awk
32
33trap 'cleanup; exit' EXIT INT HUP
34
35cleanup() {
36	rm -f saved_orig changed patch_file expected original original.* failure xo xm xof xmf xef
37}
38
39rrand() {
40	$AWK '
41	function random(low, range) {
42		return int(range * rand()) + low
43	}
44
45	BEGIN {
46		base = ARGV[1]
47		max = ARGV[2]
48		seed = ARGV[3]
49
50		srand()		# Initialze with current time
51		s = srand()	# get previous seed
52		s = s + seed	# Current time + seed
53		srand(s)	# Better new seed
54
55		x = random(base, max)
56		print x
57	}
58	' "$@"
59}
60
61makefile() {
62	$AWK '
63	BEGIN {
64		nflines = ARGV[1]
65		for (i = 1; i <= nflines; i++) {
66			printf("This is original line %d\n", i);
67		}
68	}
69	' "$@"
70}
71
72changefile() {
73	$AWK '
74	function random(n) {
75		return int(n * rand())
76	}
77
78	BEGIN {
79		nflines = ARGV[1]
80		chlines = ARGV[2]
81		seed = chlines
82
83		ARGV[1] = ""	# Do not use as filename
84		ARGV[2] = ""	# Do not use as filename
85
86		srand()		# Initialze with current time
87		s = srand()	# get previous seed
88		s = s + seed	# Current time + seed
89		srand(s)	# Better new seed
90
91		for (i = 1; i <= nflines; i++) {
92			change_line[i] = 0
93		}
94
95		while (chlines > 0) {
96			i = random(nflines)
97			if (change_line[i] == 0) {
98				chlines--
99				change_line[i] = 1
100			}
101		}
102		i = 1;
103	}
104
105	{
106		if (change_line[i] != 0) {
107			what = random(3)
108			if (what == 0) {
109				print "Modified, was: " $0
110			} else if (what == 1) {
111				print $0
112				print "New line, inserted after: " $0
113			}
114		} else {
115			print $0
116		}
117		i++
118		next
119	}
120	' "$@"
121}
122
123nlines=$$	# seed startup helper, awk would get seed based on time_t
124maxlines=5000	# Longest file for our tests
125maxch=4		# Max. 25% of all lines are changed
126
127idx=0
128maxidx=${1:-1000}
129while [ $idx -le $maxidx ]
130do
131	nlines=`rrand 10 $maxlines $nlines`
132
133	max_changes=`expr $nlines / $maxch`
134	changes=`rrand 1 $max_changes $nlines`
135
136	makefile $nlines > original
137	cp original saved_orig
138
139	echo Test $idx: testing nlines=$nlines changes=$changes
140
141	changefile $nlines $changes original > changed
142	cp saved_orig original
143
144	echo testing file...
145	remove $s foo
146			${admin} -fy -ioriginal $s
147	[ $? -eq 0 ] && ${get} -e $s			&& cp changed foo
148	[ $? -eq 0 ] && ${delta} -ycomment $s
149	[ $? -eq 0 ] && ${get} $s
150	ret=$?
151	if [ $ret -ne 0 ]; then
152		echo Test $idx: sccs test returned $ret
153		diff original expected > failure
154		trap 0
155		exit 1
156	fi
157
158	diff changed foo > failure
159	ret=$?
160	if [ $ret -ne 0 ]; then
161		echo Test $idx: diff returned $ret
162		[ $ret -eq 1 ] && trap 0
163		exit 1
164	fi
165	idx=`expr $idx + 1`
166done
167idx=`expr $idx - 1`
168echo Test succeeded after $idx runs...
169
170remove $z $s $p $g
171