xref: /freebsd/usr.bin/sed/tests/multi_test.sh (revision 257e70f1d5ee61037c8c59b116538d3b6b1427a2)
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# 3. 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 Regression Tests
32#
33# The directory regress.test.out contains the expected test results
34#
35# These are the regression tests mostly created during the development
36# of the BSD sed.  Each test should have a unique mark name, which is
37# used for naming the corresponding file in regress.multitest.out.
38
39SRCDIR=$(dirname $0)
40
41main()
42{
43	REGRESS=${SRCDIR}/regress.multitest.out
44	DICT=/usr/share/dict/words
45
46	awk 'END { for (i = 1; i < 15; i++) print "l1_" i}' </dev/null >lines1
47	awk 'END { for (i = 1; i < 10; i++) print "l2_" i}' </dev/null >lines2
48
49	echo "1..130"
50
51	exec 4>&1 5>&2
52	tests
53	exec 1>&4 2>&5
54
55	# Remove temporary files
56	rm -f current.out lines[1-4] script[1-2]
57}
58
59tests()
60{
61	SED=sed
62	MARK=0
63
64	test_args
65	test_addr
66	test_group
67	test_acid
68	test_branch
69	test_pattern
70	test_print
71	test_subst
72	test_error
73	# Handle the result of the last test
74	result
75}
76
77# Display a test's result
78result()
79{
80	if [ "$TODO" = '1' ] ; then
81		TODO='TODO '
82	else
83		TODO=''
84	fi
85	if ! [ -r $REGRESS/${TESTNAME} ] ; then
86		echo "Seeding $REGRESS/${TESTNAME} with current result" 1>&2
87		cp current.out $REGRESS/${TESTNAME}
88	fi
89	if diff -c $REGRESS/${TESTNAME} current.out ; then
90		echo "ok $MARK $TESTNAME # $TODO$OCOMMENT"
91	else
92		echo "not ok $MARK $TESTNAME # $TODO$OCOMMENT"
93	fi 1>&4 2>&5
94}
95
96# Mark the beginning of each test
97mark()
98{
99	[ $MARK -gt 0 ] && result
100	OCOMMENT=$COMMENT
101	MARK=`expr $MARK + 1`
102	TESTNAME=$1
103	exec 1>&4 2>&5
104	exec >"current.out"
105}
106
107test_args()
108{
109	COMMENT='Argument parsing - first type'
110	mark '1.1'
111	$SED 's/^/e1_/p' lines1
112	mark '1.2' ; $SED -n 's/^/e1_/p' lines1
113	mark '1.3'
114	$SED 's/^/e1_/p' <lines1
115	mark '1.4' ; $SED -n 's/^/e1_/p' <lines1
116	COMMENT='Argument parsing - second type'
117	mark '1.4.1'
118	$SED -e '' <lines1
119	echo 's/^/s1_/p' >script1
120	echo 's/^/s2_/p' >script2
121	mark '1.5'
122	$SED -f script1 lines1
123	mark '1.6'
124	$SED -f script1 <lines1
125	mark '1.7'
126	$SED -e 's/^/e1_/p' lines1
127	mark '1.8'
128	$SED -e 's/^/e1_/p' <lines1
129	mark '1.9' ; $SED -n -f script1 lines1
130	mark '1.10' ; $SED -n -f script1 <lines1
131	mark '1.11' ; $SED -n -e 's/^/e1_/p' lines1
132	mark '1.12'
133	$SED -n -e 's/^/e1_/p' <lines1
134	mark '1.13'
135	$SED -e 's/^/e1_/p' -e 's/^/e2_/p' lines1
136	mark '1.14'
137	$SED -f script1 -f script2 lines1
138	mark '1.15'
139	$SED -e 's/^/e1_/p' -f script1 lines1
140	mark '1.16'
141	$SED -e 's/^/e1_/p' lines1 lines1
142	# POSIX D11.2:11251
143	mark '1.17' ; $SED p <lines1 lines1
144cat >script1 <<EOF
145#n
146# A comment
147
148p
149EOF
150	mark '1.18' ; $SED -f script1 <lines1 lines1
151}
152
153test_addr()
154{
155	COMMENT='Address ranges'
156	mark '2.1' ; $SED -n -e '4p' lines1
157	mark '2.2' ; $SED -n -e '20p' lines1 lines2
158	mark '2.3' ; $SED -n -e '$p' lines1
159	mark '2.4' ; $SED -n -e '$p' lines1 lines2
160	mark '2.5' ; $SED -n -e '$a\
161hello' /dev/null
162	mark '2.6' ; $SED -n -e '$p' lines1 /dev/null lines2
163	# Should not print anything
164	mark '2.7' ; $SED -n -e '20p' lines1
165	mark '2.8' ; $SED -n -e '/NOTFOUND/p' lines1
166	mark '2.9' ; $SED -n '/l1_7/p' lines1
167	mark '2.10' ; $SED -n ' /l1_7/ p' lines1
168	mark '2.11' ; $SED -n '\_l1\_7_p' lines1
169	mark '2.12' ; $SED -n '1,4p' lines1
170	mark '2.13' ; $SED -n '1,$p' lines1 lines2
171	mark '2.14' ; $SED -n '1,/l2_9/p' lines1 lines2
172	mark '2.15' ; $SED -n '/4/,$p' lines1 lines2
173	mark '2.16' ; $SED -n '/4/,20p' lines1 lines2
174	mark '2.17' ; $SED -n '/4/,/10/p' lines1 lines2
175	mark '2.18' ; $SED -n '/l2_3/,/l1_8/p' lines1 lines2
176	mark '2.19' ; $SED -n '12,3p' lines1 lines2
177	mark '2.20' ; $SED -n '/l1_7/,3p' lines1 lines2
178	mark '2.21' ; $SED -n '13,+4p' lines1 lines2
179	mark '2.22' ; $SED -n '/l1_6/,+2p' lines1 lines2
180	# For PR bin/192108
181	mark '2.23'; $SED -n '12,+1p' lines1
182}
183
184test_group()
185{
186	COMMENT='Brace and other grouping'
187	mark '3.1' ; $SED -e '
1884,12 {
189	s/^/^/
190	s/$/$/
191	s/_/T/
192}' lines1
193	mark '3.2' ; $SED -e '
1944,12 {
195	s/^/^/
196	/6/,/10/ {
197		s/$/$/
198		/8/ s/_/T/
199	}
200}' lines1
201	mark '3.3' ; $SED -e '
2024,12 !{
203	s/^/^/
204	/6/,/10/ !{
205		s/$/$/
206		/8/ !s/_/T/
207	}
208}' lines1
209	mark '3.4' ; $SED -e '4,12!s/^/^/' lines1
210}
211
212test_acid()
213{
214	COMMENT='Commands a c d and i'
215	mark '4.1' ; $SED -n -e '
216s/^/before_i/p
21720i\
218inserted
219s/^/after_i/p
220' lines1 lines2
221	mark '4.2' ; $SED -n -e '
2225,12s/^/5-12/
223s/^/before_a/p
224/5-12/a\
225appended
226s/^/after_a/p
227' lines1 lines2
228	mark '4.3'
229	$SED -n -e '
230s/^/^/p
231/l1_/a\
232appended
2338,10N
234s/$/$/p
235' lines1 lines2
236	mark '4.4' ; $SED -n -e '
237c\
238hello
239' lines1
240	mark '4.5' ; $SED -n -e '
2418c\
242hello
243' lines1
244	mark '4.6' ; $SED -n -e '
2453,14c\
246hello
247' lines1
248# SunOS and GNU sed behave differently.   We follow POSIX
249	mark '4.7' ; $SED -n -e '
2508,3c\
251hello
252' lines1
253	mark '4.8' ; $SED d <lines1
254}
255
256test_branch()
257{
258	COMMENT='Labels and branching'
259	mark '5.1' ; $SED -n -e '
260b label4
261:label3
262s/^/label3_/p
263b end
264:label4
2652,12b label1
266b label2
267:label1
268s/^/label1_/p
269b
270:label2
271s/^/label2_/p
272b label3
273:end
274' lines1
275	mark '5.2'
276	$SED -n -e '
277s/l1_/l2_/
278t ok
279b
280:ok
281s/^/tested /p
282' lines1 lines2
283# SunOS and GNU sed behave as follows: lines 9-$ aren't printed at all
284	mark '5.3' ; $SED -n -e '
2855,8b inside
2861,5 {
287	s/^/^/p
288	:inside
289	s/$/$/p
290}
291' lines1
292# Check that t clears the substitution done flag
293	mark '5.4' ; $SED -n -e '
2941,8s/^/^/
295t l1
296:l1
297t l2
298s/$/$/p
299b
300:l2
301s/^/ERROR/
302' lines1
303# Check that reading a line clears the substitution done flag
304	mark '5.5'
305	$SED -n -e '
306t l2
3071,8s/^/^/p
3082,7N
309b
310:l2
311s/^/ERROR/p
312' lines1
313	mark '5.6' ; $SED 5q lines1
314	mark '5.7' ; $SED -e '
3155i\
316hello
3175q' lines1
318# Branch across block boundary
319	mark '5.8' ; $SED -e '
320{
321:b
322}
323s/l/m/
324tb' lines1
325}
326
327test_pattern()
328{
329COMMENT='Pattern space commands'
330# Check that the pattern space is deleted
331	mark '6.1' ; $SED -n -e '
332c\
333changed
334p
335' lines1
336	mark '6.2' ; $SED -n -e '
3374d
338p
339' lines1
340	mark '6.3'
341	$SED -e 'N;N;N;D' lines1
342	mark '6.4' ; $SED -e '
3432h
3443H
3454g
3465G
3476x
3486p
3496x
3506p
351' lines1
352	mark '6.5' ; $SED -e '4n' lines1
353	mark '6.6' ; $SED -n -e '4n' lines1
354}
355
356test_print()
357{
358	COMMENT='Print and file routines'
359	awk 'END {for (i = 1; i < 256; i++) printf("%c", i);print "\n"}' \
360		</dev/null >lines3
361	# GNU and SunOS sed behave differently here
362	mark '7.1'
363	$SED -n l lines3
364	mark '7.2' ; $SED -e '/l2_/=' lines1 lines2
365	rm -f lines4
366	mark '7.3' ; $SED -e '3,12w lines4' lines1
367	COMMENT='w results'
368	cat lines4
369	mark '7.4' ; $SED -e '4r lines2' lines1
370	mark '7.5' ; $SED -e '5r /dev/dds' lines1
371	mark '7.6' ; $SED -e '6r /dev/null' lines1
372	mark '7.7'
373	sed '200q' $DICT | sed 's$.*$s/^/&/w tmpdir/&$' >script1
374	rm -rf tmpdir
375	mkdir tmpdir
376	$SED -f script1 lines1
377	cat tmpdir/*
378	rm -rf tmpdir
379	mark '7.8'
380	echo line1 > lines3
381	echo "" >> lines3
382	$SED -n -e '$p' lines3 /dev/null
383
384}
385
386test_subst()
387{
388	COMMENT='Substitution commands'
389	mark '8.1' ; $SED -e 's/./X/g' lines1
390	mark '8.2' ; $SED -e 's,.,X,g' lines1
391# SunOS sed thinks we are escaping . as wildcard, not as separator
392	mark '8.3'
393	$SED -e 's.\..X.g' lines1
394	mark '8.4' ; $SED -e 's/[\/]/Q/' lines1
395	mark '8.5' ; $SED -e 's_\__X_' lines1
396	mark '8.6' ; $SED -e 's/./(&)/g' lines1
397	mark '8.7' ; $SED -e 's/./(\&)/g' lines1
398	mark '8.8' ; $SED -e 's/\(.\)\(.\)\(.\)/x\3x\2x\1/g' lines1
399	mark '8.9' ; $SED -e 's/_/u0\
400u1\
401u2/g' lines1
402	mark '8.10'
403	$SED -e 's/./X/4' lines1
404	rm -f lines4
405	mark '8.11' ; $SED -e 's/1/X/w lines4' lines1
406	COMMENT='s wfile results'
407	cat lines4
408	mark '8.12' ; $SED -e 's/[123]/X/g' lines1
409	mark '8.13' ; $SED -e 'y/0123456789/9876543210/' lines1
410	mark '8.14' ;
411	$SED -e 'y10\123456789198765432\101' lines1
412	mark '8.15' ; $SED -e '1N;2y/\n/X/' lines1
413	mark '8.16'
414	echo 'eeefff' | $SED -e '
415		p
416		s/e/X/p
417		:x
418		s//Y/p
419		# Establish limit counter in the hold space
420		# GNU sed version 3.02 enters into an infinite loop here
421		x
422		/.\{10\}/ {
423			s/.*/ERROR/
424			b
425		}
426		s/.*/&./
427		x
428		/f/bx
429	'
430	# POSIX does not say that this should work,
431	# but it does for GNU, BSD, and SunOS
432	mark '8.17' ; $SED -e 's/[/]/Q/' lines1
433
434	COMMENT='[ as an s delimiter and its escapes'
435	mark '8.18' ; $SED -e 's[_[X[' lines1
436	# This is a matter of interpretation
437	# POSIX 1003.1, 2004 says "Within the BRE and the replacement,
438	# the BRE delimiter itself can be used as a *literal* character
439	# if it is preceded by a backslash"
440	# SunOS 5.1 /usr/bin/sed and Mac OS X follow the literal POSIX
441	# interpretation.
442	# GNU sed version 4.1.5 treats \[ as the beginning of a character
443	# set specification (both with --posix and without).
444	mark '8.19' ; sed 's/l/[/' lines1 | $SED -e 's[\[.[X['
445	mark '8.20' ; sed 's/l/[/' lines1 | $SED -e 's[\[.[X\[['
446	COMMENT='\ in y command'
447	mark '8.21'
448	echo 'a\b(c' |
449	$SED 'y%ABCDEFGHIJKLMNOPQRSTUVWXYZ, /\\()"%abcdefghijklmnopqrstuvwxyz,------%'
450	COMMENT='\n in a character class and a BRE'
451	mark '8.22' ; (echo 1; echo 2) | $SED -n '1{;N;s/[\n]/X/;p;}'
452	mark '8.23' ; (echo 1; echo 2) | $SED -n '1{;N;s/\n/X/;p;}'
453}
454
455test_error()
456{
457	COMMENT='Error cases'
458	mark '9.1' ; $SED -x 2>/dev/null ; echo $?
459	mark '9.2' ; $SED -f 2>/dev/null ; echo $?
460	mark '9.3' ; $SED -e 2>/dev/null ; echo $?
461	mark '9.4' ; $SED -f /dev/xyzzyxyzy 2>/dev/null ; echo $?
462	mark '9.5' ; $SED p /dev/xyzzyxyzy 2>/dev/null ; echo $?
463	mark '9.6' ; $SED -f /bin/sh 2>/dev/null ; echo $?
464	mark '9.7' ; $SED '{' 2>/dev/null ; echo $?
465	mark '9.8' ; $SED '{' 2>/dev/null ; echo $?
466	mark '9.9' ; $SED '/hello/' 2>/dev/null ; echo $?
467	mark '9.10' ; $SED '1,/hello/' 2>/dev/null ; echo $?
468	mark '9.11' ; $SED -e '-5p' 2>/dev/null ; echo $?
469	mark '9.12' ; $SED '/jj' 2>/dev/null ; echo $?
470	mark '9.13' ; $SED 'a hello' 2>/dev/null ; echo $?
471	mark '9.14' ; $SED 'a \ hello' 2>/dev/null ; echo $?
472	mark '9.15' ; $SED 'b foo' 2>/dev/null ; echo $?
473	mark '9.16' ; $SED 'd hello' 2>/dev/null ; echo $?
474	mark '9.17' ; $SED 's/aa' 2>/dev/null ; echo $?
475	mark '9.18' ; $SED 's/aa/' 2>/dev/null ; echo $?
476	mark '9.19' ; $SED 's/a/b' 2>/dev/null ; echo $?
477	mark '9.20' ; $SED 's/a/b/c/d' 2>/dev/null ; echo $?
478	mark '9.21' ; $SED 's/a/b/ 1 2' 2>/dev/null ; echo $?
479	mark '9.22' ; $SED 's/a/b/ 1 g' 2>/dev/null ; echo $?
480	mark '9.23' ; $SED 's/a/b/w' 2>/dev/null ; echo $?
481	mark '9.24' ; $SED 'y/aa' 2>/dev/null ; echo $?
482	mark '9.25' ; $SED 'y/aa/b/' 2>/dev/null ; echo $?
483	mark '9.26' ; $SED 'y/aa/' 2>/dev/null ; echo $?
484	mark '9.27' ; $SED 'y/a/b' 2>/dev/null ; echo $?
485	mark '9.28' ; $SED 'y/a/b/c/d' 2>/dev/null ; echo $?
486	mark '9.29' ; $SED '!' 2>/dev/null ; echo $?
487	mark '9.30' ; $SED supercalifrangolisticexprialidociussupercalifrangolisticexcius 2>/dev/null ; echo $?
488	mark '9.31' ; $SED '' /dev/null 2>/dev/null ; echo $?
489}
490
491main
492