multi_test.sh revision 167552
1#!/bin/sh -
2#
3# Copyright (c) 1992 Diomidis Spinellis.
4# Copyright (c) 1992, 1993
5#	The Regents of the University of California.  All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15# 4. Neither the name of the University nor the names of its contributors
16#    may be used to endorse or promote products derived from this software
17#    without specific prior written permission.
18#
19# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29# SUCH DAMAGE.
30#
31#	@(#)sed.test	8.1 (Berkeley) 6/6/93
32#
33#	$FreeBSD: head/tools/regression/usr.bin/sed/multitest.t 167552 2007-03-14 13:05:45Z dds $
34#
35
36# sed Regression Tests
37#
38# The directory regress.test.out contains the expected test results
39#
40# These are the regression tests created during the development of the
41# BSD sed.  The reference file naming scheme used in this script can't
42# handle gracefully the insertion of new tests between existing ones.
43# Therefore, either use the new m4-based regress.t framework, or add
44# tests after the last existing test.
45
46main()
47{
48	REGRESS=regress.multitest.out
49	DICT=/usr/share/dict/words
50
51	#test_error | more
52
53	awk 'END { for (i = 1; i < 15; i++) print "l1_" i}' </dev/null >lines1
54	awk 'END { for (i = 1; i < 10; i++) print "l2_" i}' </dev/null >lines2
55
56	echo "1..90"
57
58	exec 4>&1 5>&2
59	tests
60	exec 1>&4 2>&5
61
62	# Remove temporary files
63	rm -f current.out lines[1-4] script[1-2]
64}
65
66tests()
67{
68	SED=sed
69	MARK=0
70
71	test_args
72	test_addr
73	test_group
74	test_acid
75	test_branch
76	test_pattern
77	test_print
78	test_subst
79	# Handle the result of the last test
80	result
81}
82
83# Display a test's result
84result()
85{
86	if [ "$TODO" = '1' ] ; then
87		TODO='TODO '
88	else
89		TODO=''
90	fi
91	if diff -c $REGRESS/${MARK}_${TESTNAME} current.out ; then
92		echo "ok $MARK $TESTNAME # $TODO$COMMENT"
93	else
94		echo "not ok $MARK $TESTNAME # $TODO$COMMENT"
95	fi 1>&4 2>&5
96}
97
98# Mark the beginning of each test
99mark()
100{
101	[ $MARK -gt 0 ] && result
102	MARK=`expr $MARK + 1`
103	TESTNAME=$1
104	exec 1>&4 2>&5
105	exec >"current.out"
106}
107
108test_args()
109{
110	COMMENT='Argument parsing - first type'
111	mark '1.1'
112	$SED 's/^/e1_/p' lines1
113	mark '1.2' ; $SED -n 's/^/e1_/p' lines1
114	mark '1.3'
115	$SED 's/^/e1_/p' <lines1
116	mark '1.4' ; $SED -n 's/^/e1_/p' <lines1
117	COMMENT='Argument parsing - second type'
118	mark '1.4.1'
119	$SED -e '' <lines1
120	echo 's/^/s1_/p' >script1
121	echo 's/^/s2_/p' >script2
122	mark '1.5'
123	$SED -f script1 lines1
124	mark '1.6'
125	$SED -f script1 <lines1
126	mark '1.7'
127	$SED -e 's/^/e1_/p' lines1
128	mark '1.8'
129	$SED -e 's/^/e1_/p' <lines1
130	mark '1.9' ; $SED -n -f script1 lines1
131	mark '1.10' ; $SED -n -f script1 <lines1
132	mark '1.11' ; $SED -n -e 's/^/e1_/p' lines1
133	mark '1.12'
134	$SED -n -e 's/^/e1_/p' <lines1
135	mark '1.13'
136	$SED -e 's/^/e1_/p' -e 's/^/e2_/p' lines1
137	mark '1.14'
138	$SED -f script1 -f script2 lines1
139	mark '1.15'
140	$SED -e 's/^/e1_/p' -f script1 lines1
141	mark '1.16'
142	$SED -e 's/^/e1_/p' lines1 lines1
143	# POSIX D11.2:11251
144	mark '1.17' ; $SED p <lines1 lines1
145cat >script1 <<EOF
146#n
147# A comment
148
149p
150EOF
151	mark '1.18' ; $SED -f script1 <lines1 lines1
152}
153
154test_addr()
155{
156	COMMENT='Address ranges'
157	mark '2.1' ; $SED -n -e '4p' lines1
158	mark '2.2' ; $SED -n -e '20p' lines1 lines2
159	mark '2.3' ; $SED -n -e '$p' lines1
160	mark '2.4' ; $SED -n -e '$p' lines1 lines2
161	mark '2.5' ; $SED -n -e '$a\
162hello' /dev/null
163	mark '2.6' ; $SED -n -e '$p' lines1 /dev/null lines2
164	# Should not print anything
165	mark '2.7' ; $SED -n -e '20p' lines1
166	mark '2.8' ; $SED -n -e '/NOTFOUND/p' lines1
167	mark '2.9' ; $SED -n '/l1_7/p' lines1
168	mark '2.10' ; $SED -n ' /l1_7/ p' lines1
169	mark '2.11' ; $SED -n '\_l1\_7_p' lines1
170	mark '2.12' ; $SED -n '1,4p' lines1
171	mark '2.13' ; $SED -n '1,$p' lines1 lines2
172	mark '2.14' ; $SED -n '1,/l2_9/p' lines1 lines2
173	mark '2.15' ; $SED -n '/4/,$p' lines1 lines2
174	mark '2.16' ; $SED -n '/4/,20p' lines1 lines2
175	mark '2.17' ; $SED -n '/4/,/10/p' lines1 lines2
176	mark '2.18' ; $SED -n '/l2_3/,/l1_8/p' lines1 lines2
177	mark '2.19' ; $SED -n '12,3p' lines1 lines2
178	mark '2.20' ; $SED -n '/l1_7/,3p' lines1 lines2
179}
180
181test_group()
182{
183	COMMENT='Brace and other grouping'
184	mark '3.1' ; $SED -e '
1854,12 {
186	s/^/^/
187	s/$/$/
188	s/_/T/
189}' lines1
190	mark '3.2' ; $SED -e '
1914,12 {
192	s/^/^/
193	/6/,/10/ {
194		s/$/$/
195		/8/ s/_/T/
196	}
197}' lines1
198	mark '3.3' ; $SED -e '
1994,12 !{
200	s/^/^/
201	/6/,/10/ !{
202		s/$/$/
203		/8/ !s/_/T/
204	}
205}' lines1
206	mark '3.4' ; $SED -e '4,12!s/^/^/' lines1
207}
208
209test_acid()
210{
211	COMMENT='Commands a c d and i'
212	mark '4.1' ; $SED -n -e '
213s/^/before_i/p
21420i\
215inserted
216s/^/after_i/p
217' lines1 lines2
218	mark '4.2' ; $SED -n -e '
2195,12s/^/5-12/
220s/^/before_a/p
221/5-12/a\
222appended
223s/^/after_a/p
224' lines1 lines2
225	mark '4.3'
226	$SED -n -e '
227s/^/^/p
228/l1_/a\
229appended
2308,10N
231s/$/$/p
232' lines1 lines2
233	mark '4.4' ; $SED -n -e '
234c\
235hello
236' lines1
237	mark '4.5' ; $SED -n -e '
2388c\
239hello
240' lines1
241	mark '4.6' ; $SED -n -e '
2423,14c\
243hello
244' lines1
245# SunOS and GNU sed behave differently.   We follow POSIX
246	mark '4.7' ; $SED -n -e '
2478,3c\
248hello
249' lines1
250	mark '4.8' ; $SED d <lines1
251}
252
253test_branch()
254{
255	COMMENT='Labels and branching'
256	mark '5.1' ; $SED -n -e '
257b label4
258:label3
259s/^/label3_/p
260b end
261:label4
2622,12b label1
263b label2
264:label1
265s/^/label1_/p
266b
267:label2
268s/^/label2_/p
269b label3
270:end
271' lines1
272	mark '5.2'
273	$SED -n -e '
274s/l1_/l2_/
275t ok
276b
277:ok
278s/^/tested /p
279' lines1 lines2
280# SunOS and GNU sed behave differently here.  Clarification needed.
281	mark '5.3' ; $SED -n -e '
2825,8b inside
2831,5 {
284	s/^/^/p
285	:inside
286	s/$/$/p
287}
288' lines1
289# Check that t clears the substitution done flag
290	mark '5.4' ; $SED -n -e '
2911,8s/^/^/
292t l1
293:l1
294t l2
295s/$/$/p
296b
297:l2
298s/^/ERROR/
299' lines1
300# Check that reading a line clears the substitution done flag
301	mark '5.5'
302	$SED -n -e '
303t l2
3041,8s/^/^/p
3052,7N
306b
307:l2
308s/^/ERROR/p
309' lines1
310	mark '5.6' ; $SED 5q lines1
311	mark '5.7' ; $SED -e '
3125i\
313hello
3145q' lines1
315# Branch across block boundary
316	mark '5.8' ; $SED -e '
317{
318:b
319}
320s/l/m/
321tb' lines1
322}
323
324test_pattern()
325{
326COMMENT='Pattern space commands'
327# Check that the pattern space is deleted
328	mark '6.1' ; $SED -n -e '
329c\
330changed
331p
332' lines1
333	mark '6.2' ; $SED -n -e '
3344d
335p
336' lines1
337	mark '6.3'
338	$SED -e 'N;N;N;D' lines1
339	mark '6.4' ; $SED -e '
3402h
3413H
3424g
3435G
3446x
3456p
3466x
3476p
348' lines1
349	mark '6.5' ; $SED -e '4n' lines1
350	mark '6.6' ; $SED -n -e '4n' lines1
351}
352
353test_print()
354{
355	COMMENT='Print and file routines'
356	awk 'END {for (i = 1; i < 256; i++) printf("%c", i);print "\n"}' \
357		</dev/null >lines3
358	# GNU and SunOS sed behave differently here
359	mark '7.1'
360	$SED -n l lines3
361	mark '7.2' ; $SED -e '/l2_/=' lines1 lines2
362	rm -f lines4
363	mark '7.3' ; $SED -e '3,12w lines4' lines1
364	COMMENT='w results'
365	cat lines4
366	mark '7.4' ; $SED -e '4r lines2' lines1
367	mark '7.5' ; $SED -e '5r /dev/dds' lines1
368	mark '7.6' ; $SED -e '6r /dev/null' lines1
369	mark '7.7'
370	sed '200q' $DICT | sed 's$.*$s/^/&/w tmpdir/&$' >script1
371	rm -rf tmpdir
372	mkdir tmpdir
373	$SED -f script1 lines1
374	cat tmpdir/*
375	rm -rf tmpdir
376	mark '7.8'
377	echo line1 > lines3
378	echo "" >> lines3
379	TODO=1
380	$SED -n -e '$p' lines3 /dev/null
381		
382}
383
384test_subst()
385{
386	COMMENT='Substitution commands'
387	mark '8.1' ; $SED -e 's/./X/g' lines1
388	mark '8.2' ; $SED -e 's,.,X,g' lines1
389# SunOS sed thinks we are escaping . as wildcard, not as separator
390	mark '8.3'
391	$SED -e 's.\..X.g' lines1
392	mark '8.4' ; $SED -e 's/[\/]/Q/' lines1
393	mark '8.5' ; $SED -e 's_\__X_' lines1
394	mark '8.6' ; $SED -e 's/./(&)/g' lines1
395	mark '8.7' ; $SED -e 's/./(\&)/g' lines1
396	mark '8.8' ; $SED -e 's/\(.\)\(.\)\(.\)/x\3x\2x\1/g' lines1
397	mark '8.9' ; $SED -e 's/_/u0\
398u1\
399u2/g' lines1
400	mark '8.10'
401	$SED -e 's/./X/4' lines1
402	rm -f lines4
403	mark '8.11' ; $SED -e 's/1/X/w lines4' lines1
404	COMMENT='s wfile results'
405	cat lines4
406	mark '8.12' ; $SED -e 's/[123]/X/g' lines1
407	mark '8.13' ; $SED -e 'y/0123456789/9876543210/' lines1
408	mark '8.14' ; 
409	$SED -e 'y10\123456789198765432\101' lines1
410	mark '8.15' ; $SED -e '1N;2y/\n/X/' lines1
411	mark '8.16'
412	echo 'eeefff' | $SED -e '
413		p
414		s/e/X/p
415		:x
416		s//Y/p 
417		# Establish limit counter in the hold space
418		# GNU sed version 3.02 enters into an infinite loop here
419		x 
420		/.\{10\}/ {
421			s/.*/ERROR/
422			b
423		}
424		s/.*/&./
425		x 
426		/f/bx
427	'
428	# POSIX does not say that this should work,
429	# but it does for GNU, BSD, and SunOS
430	mark '8.17' ; $SED -e 's/[/]/Q/' lines1
431}
432
433test_error()
434{
435	exec 0>&3 4>&1 5>&2
436	exec 0</dev/null
437	exec 2>&1
438	set -x
439	$TEST -x && exit 1
440	$TEST -f && exit 1
441	$TEST -e && exit 1
442	$TEST -f /dev/dds && exit 1
443	$TEST p /dev/dds && exit 1
444	$TEST -f /bin/sh && exit 1
445	$TEST '{' && exit 1
446	$TEST '{' && exit 1
447	$TEST '/hello/' && exit 1
448	$TEST '1,/hello/' && exit 1
449	$TEST -e '-5p' && exit 1
450	$TEST '/jj' && exit 1
451	$TEST 'a hello' && exit 1
452	$TEST 'a \ hello' && exit 1
453	$TEST 'b foo' && exit 1
454	$TEST 'd hello' && exit 1
455	$TEST 's/aa' && exit 1
456	$TEST 's/aa/' && exit 1
457	$TEST 's/a/b' && exit 1
458	$TEST 's/a/b/c/d' && exit 1
459	$TEST 's/a/b/ 1 2' && exit 1
460	$TEST 's/a/b/ 1 g' && exit 1
461	$TEST 's/a/b/w' && exit 1
462	$TEST 'y/aa' && exit 1
463	$TEST 'y/aa/b/' && exit 1
464	$TEST 'y/aa/' && exit 1
465	$TEST 'y/a/b' && exit 1
466	$TEST 'y/a/b/c/d' && exit 1
467	$TEST '!' && exit 1
468	$TEST supercalifrangolisticexprialidociussupercalifrangolisticexcius
469	set +x
470	exec 0>&3 1>&4 2>&5
471}
472
473main
474