xref: /csrg-svn/usr.bin/sed/TEST/sed.test (revision 55998)
1*55998Sbostic#!/bin/sh -
2*55998Sbostic#
3*55998Sbostic# Copyright (c) 1992 Diomidis Spinellis.
4*55998Sbostic# Copyright (c) 1992 The Regents of the University of California.
5*55998Sbostic# All rights reserved.
6*55998Sbostic#
7*55998Sbostic# %sccs.include.redist.sh%
8*55998Sbostic#
9*55998Sbostic#	@(#)sed.test	5.1 (Berkeley) 08/23/92
10*55998Sbostic#
11*55998Sbostic
12*55998Sbostic# sed Regression Tests
13*55998Sbostic#
14*55998Sbostic# The following files are created:
15*55998Sbostic# lines[1-4], script1, script2
16*55998Sbostic# Two directories *.out contain the test results
17*55998Sbostic
18*55998Sbosticmain()
19*55998Sbostic{
20*55998Sbostic	BASE=/usr/bin/sed
21*55998Sbostic	BASELOG=sed.out
22*55998Sbostic	TEST=../obj/sed
23*55998Sbostic	TESTLOG=nsed.out
24*55998Sbostic	DICT=/usr/share/dict/words
25*55998Sbostic
26*55998Sbostic	awk 'END { for (i = 1; i < 15; i++) print "l1_" i}' </dev/null >lines1
27*55998Sbostic	awk 'END { for (i = 1; i < 10; i++) print "l2_" i}' </dev/null >lines2
28*55998Sbostic
29*55998Sbostic	exec 4>&1 5>&2
30*55998Sbostic
31*55998Sbostic	# Set these flags to get messages about known problems
32*55998Sbostic	BSD=1
33*55998Sbostic	GNU=0
34*55998Sbostic	SUN=0
35*55998Sbostic	tests $BASE $BASELOG
36*55998Sbostic
37*55998Sbostic	BSD=0
38*55998Sbostic	GNU=0
39*55998Sbostic	SUN=0
40*55998Sbostic	tests $TEST $TESTLOG
41*55998Sbostic	exec 1>&4 2>&5
42*55998Sbostic	diff -c $BASELOG $TESTLOG | more
43*55998Sbostic}
44*55998Sbostic
45*55998Sbostictests()
46*55998Sbostic{
47*55998Sbostic	SED=$1
48*55998Sbostic	DIR=$2
49*55998Sbostic	rm -rf $DIR
50*55998Sbostic	mkdir $DIR
51*55998Sbostic	MARK=100
52*55998Sbostic
53*55998Sbostic	test_args
54*55998Sbostic	test_addr
55*55998Sbostic	echo Testing commands
56*55998Sbostic	test_group
57*55998Sbostic	test_aci
58*55998Sbostic	test_branch
59*55998Sbostic	test_pattern
60*55998Sbostic	test_print
61*55998Sbostic	test_subst
62*55998Sbostic}
63*55998Sbostic
64*55998Sbosticmark()
65*55998Sbostic{
66*55998Sbostic	MARK=`expr $MARK + 1`
67*55998Sbostic	exec 1>&4 2>&5
68*55998Sbostic	exec >"$DIR/${MARK}_$1"
69*55998Sbostic	echo "Test $1:$MARK"
70*55998Sbostic	# Uncomment this line to match tests with sed error messages
71*55998Sbostic	echo "Test $1:$MARK" > /dev/stderr
72*55998Sbostic}
73*55998Sbostic
74*55998Sbostictest_args()
75*55998Sbostic{
76*55998Sbostic	mark '1.1'
77*55998Sbostic	echo Testing argument parsing
78*55998Sbostic	echo First type
79*55998Sbostic	if [ $SUN -eq 1 ] ; then
80*55998Sbostic		echo SunOS sed prints only with -n
81*55998Sbostic	else
82*55998Sbostic		$SED 's/^/e1_/p' lines1
83*55998Sbostic	fi
84*55998Sbostic	mark '1.2' ; $SED -n 's/^/e1_/p' lines1
85*55998Sbostic	mark '1.3'
86*55998Sbostic	if [ $SUN -eq 1 ] ; then
87*55998Sbostic		echo SunOS sed prints only with -n
88*55998Sbostic	else
89*55998Sbostic		$SED 's/^/e1_/p' <lines1
90*55998Sbostic	fi
91*55998Sbostic	mark '1.4' ; $SED -n 's/^/e1_/p' <lines1
92*55998Sbostic	echo Second type
93*55998Sbostic	mark '1.4.1'
94*55998Sbostic	if [ $SUN -eq 1 ] ; then
95*55998Sbostic		echo SunOS sed fails this
96*55998Sbostic	fi
97*55998Sbostic	$SED -e '' <lines1
98*55998Sbostic	echo 's/^/s1_/p' >script1
99*55998Sbostic	echo 's/^/s2_/p' >script2
100*55998Sbostic	mark '1.5'
101*55998Sbostic	if [ $SUN -eq 1 ] ; then
102*55998Sbostic		echo SunOS sed prints only with -n
103*55998Sbostic	else
104*55998Sbostic		$SED -f script1 lines1
105*55998Sbostic	fi
106*55998Sbostic	mark '1.6'
107*55998Sbostic	if [ $SUN -eq 1 ] ; then
108*55998Sbostic		echo SunOS sed prints only with -n
109*55998Sbostic	else
110*55998Sbostic		$SED -f script1 <lines1
111*55998Sbostic	fi
112*55998Sbostic	mark '1.7'
113*55998Sbostic	if [ $SUN -eq 1 ] ; then
114*55998Sbostic		echo SunOS sed prints only with -n
115*55998Sbostic	else
116*55998Sbostic		$SED -e 's/^/e1_/p' lines1
117*55998Sbostic	fi
118*55998Sbostic	mark '1.8'
119*55998Sbostic	if [ $SUN -eq 1 ] ; then
120*55998Sbostic		echo SunOS sed prints only with -n
121*55998Sbostic	else
122*55998Sbostic		$SED -e 's/^/e1_/p' <lines1
123*55998Sbostic	fi
124*55998Sbostic	mark '1.9' ; $SED -n -f script1 lines1
125*55998Sbostic	mark '1.10' ; $SED -n -f script1 <lines1
126*55998Sbostic	mark '1.11' ; $SED -n -e 's/^/e1_/p' lines1
127*55998Sbostic	mark '1.12'
128*55998Sbostic	if [ $SUN -eq 1 ] ; then
129*55998Sbostic		echo SunOS sed prints only with -n
130*55998Sbostic	else
131*55998Sbostic		$SED -n -e 's/^/e1_/p' <lines1
132*55998Sbostic	fi
133*55998Sbostic	mark '1.13'
134*55998Sbostic	if [ $SUN -eq 1 ] ; then
135*55998Sbostic		echo SunOS sed prints only with -n
136*55998Sbostic	else
137*55998Sbostic		$SED -e 's/^/e1_/p' -e 's/^/e2_/p' lines1
138*55998Sbostic	fi
139*55998Sbostic	mark '1.14'
140*55998Sbostic	if [ $SUN -eq 1 ] ; then
141*55998Sbostic		echo SunOS sed prints only with -n
142*55998Sbostic	else
143*55998Sbostic		$SED -f script1 -f script2 lines1
144*55998Sbostic	fi
145*55998Sbostic	mark '1.15'
146*55998Sbostic	if [ $GNU -eq 1 -o $SUN -eq 1 ] ; then
147*55998Sbostic		echo GNU and SunOS sed fail this following older POSIX draft
148*55998Sbostic	else
149*55998Sbostic		$SED -e 's/^/e1_/p' -f script1 lines1
150*55998Sbostic	fi
151*55998Sbostic	mark '1.16'
152*55998Sbostic	if [ $SUN -eq 1 ] ; then
153*55998Sbostic		echo SunOS sed prints only with -n
154*55998Sbostic	else
155*55998Sbostic		$SED -e 's/^/e1_/p' lines1 lines1
156*55998Sbostic	fi
157*55998Sbostic	# POSIX D11.2:11251
158*55998Sbostic	mark '1.17' ; $SED p <lines1 lines1
159*55998Sbosticcat >script1 <<EOF
160*55998Sbostic#n
161*55998Sbostic# A comment
162*55998Sbostic
163*55998Sbosticp
164*55998SbosticEOF
165*55998Sbostic	mark '1.18' ; $SED -f script1 <lines1 lines1
166*55998Sbostic}
167*55998Sbostic
168*55998Sbostictest_addr()
169*55998Sbostic{
170*55998Sbostic	echo Testing address ranges
171*55998Sbostic	mark '2.1' ; $SED -n -e '4p' lines1
172*55998Sbostic	mark '2.2' ; $SED -n -e '20p' lines1 lines2
173*55998Sbostic	mark '2.3' ; $SED -n -e '$p' lines1
174*55998Sbostic	mark '2.4' ; $SED -n -e '$p' lines1 lines2
175*55998Sbostic	mark '2.5' ; $SED -n -e '$a\
176*55998Sbostichello' /dev/null
177*55998Sbostic	mark '2.6' ; $SED -n -e '$p' lines1 /dev/null lines2
178*55998Sbostic	# Should not print anything
179*55998Sbostic	mark '2.7' ; $SED -n -e '20p' lines1
180*55998Sbostic	mark '2.8' ; $SED -n -e '0p' lines1
181*55998Sbostic	mark '2.9' ; $SED -n '/l1_7/p' lines1
182*55998Sbostic	mark '2.10' ; $SED -n ' /l1_7/ p' lines1
183*55998Sbostic	mark '2.11'
184*55998Sbostic	if [ $BSD -eq 1 ] ; then
185*55998Sbostic		echo BSD sed fails this test
186*55998Sbostic	fi
187*55998Sbostic	if [ $GNU -eq 1 ] ; then
188*55998Sbostic		echo GNU sed fails this
189*55998Sbostic	fi
190*55998Sbostic	$SED -n '\_l1\_7_p' lines1
191*55998Sbostic	mark '2.12' ; $SED -n '1,4p' lines1
192*55998Sbostic	mark '2.13' ; $SED -n '1,$p' lines1 lines2
193*55998Sbostic	mark '2.14' ; $SED -n '1,/l2_9/p' lines1 lines2
194*55998Sbostic	mark '2.15' ; $SED -n '/4/,$p' lines1 lines2
195*55998Sbostic	mark '2.16' ; $SED -n '/4/,20p' lines1 lines2
196*55998Sbostic	mark '2.17' ; $SED -n '/4/,/10/p' lines1 lines2
197*55998Sbostic	mark '2.18' ; $SED -n '/l2_3/,/l1_8/p' lines1 lines2
198*55998Sbostic	mark '2.19'
199*55998Sbostic	if [ $GNU -eq 1 ] ; then
200*55998Sbostic		echo GNU sed fails this
201*55998Sbostic	fi
202*55998Sbostic	$SED -n '12,3p' lines1 lines2
203*55998Sbostic	mark '2.20'
204*55998Sbostic	if [ $GNU -eq 1 ] ; then
205*55998Sbostic		echo GNU sed fails this
206*55998Sbostic	fi
207*55998Sbostic	$SED -n '/l1_7/,3p' lines1 lines2
208*55998Sbostic}
209*55998Sbostic
210*55998Sbostictest_group()
211*55998Sbostic{
212*55998Sbostic	echo Brace and other grouping
213*55998Sbostic	mark '3.1' ; $SED -e '
214*55998Sbostic4,12 {
215*55998Sbostic	s/^/^/
216*55998Sbostic	s/$/$/
217*55998Sbostic	s/_/T/
218*55998Sbostic}' lines1
219*55998Sbostic	mark '3.2' ; $SED -e '
220*55998Sbostic4,12 {
221*55998Sbostic	s/^/^/
222*55998Sbostic	/6/,/10/ {
223*55998Sbostic		s/$/$/
224*55998Sbostic		/8/ s/_/T/
225*55998Sbostic	}
226*55998Sbostic}' lines1
227*55998Sbostic	mark '3.3' ; $SED -e '
228*55998Sbostic4,12 !{
229*55998Sbostic	s/^/^/
230*55998Sbostic	/6/,/10/ !{
231*55998Sbostic		s/$/$/
232*55998Sbostic		/8/ !s/_/T/
233*55998Sbostic	}
234*55998Sbostic}' lines1
235*55998Sbostic	mark '3.4' ; $SED -e '4,12!s/^/^/' lines1
236*55998Sbostic}
237*55998Sbostic
238*55998Sbostictest_aci()
239*55998Sbostic{
240*55998Sbostic	echo Testing a c and i commands
241*55998Sbostic	mark '4.1' ; $SED -n -e '
242*55998Sbostics/^/before_i/p
243*55998Sbostic20i\
244*55998Sbosticinserted
245*55998Sbostics/^/after_i/p
246*55998Sbostic' lines1 lines2
247*55998Sbostic	mark '4.2' ; $SED -n -e '
248*55998Sbostic5,12s/^/5-12/
249*55998Sbostics/^/before_a/p
250*55998Sbostic/5-12/a\
251*55998Sbosticappended
252*55998Sbostics/^/after_a/p
253*55998Sbostic' lines1 lines2
254*55998Sbostic	mark '4.3'
255*55998Sbostic	if [ $GNU -eq 1 ] ; then
256*55998Sbostic		echo GNU sed fails this
257*55998Sbostic	fi
258*55998Sbostic	$SED -n -e '
259*55998Sbostics/^/^/p
260*55998Sbostic/l1_/a\
261*55998Sbosticappended
262*55998Sbostic8,10N
263*55998Sbostics/$/$/p
264*55998Sbostic' lines1 lines2
265*55998Sbostic	mark '4.4' ; $SED -n -e '
266*55998Sbosticc\
267*55998Sbostichello
268*55998Sbostic' lines1
269*55998Sbostic	mark '4.5' ; $SED -n -e '
270*55998Sbostic8c\
271*55998Sbostichello
272*55998Sbostic' lines1
273*55998Sbostic	mark '4.6' ; $SED -n -e '
274*55998Sbostic3,14c\
275*55998Sbostichello
276*55998Sbostic' lines1
277*55998Sbostic# SunOS and GNU sed behave differently.   We follow POSIX
278*55998Sbostic#	mark '4.7' ; $SED -n -e '
279*55998Sbostic#8,3c\
280*55998Sbostic#hello
281*55998Sbostic#' lines1
282*55998Sbostic}
283*55998Sbostic
284*55998Sbostictest_branch()
285*55998Sbostic{
286*55998Sbostic	echo Testing labels and branching
287*55998Sbostic	mark '5.1' ; $SED -n -e '
288*55998Sbosticb label4
289*55998Sbostic:label3
290*55998Sbostics/^/label3_/p
291*55998Sbosticb end
292*55998Sbostic:label4
293*55998Sbostic2,12b label1
294*55998Sbosticb label2
295*55998Sbostic:label1
296*55998Sbostics/^/label1_/p
297*55998Sbosticb
298*55998Sbostic:label2
299*55998Sbostics/^/label2_/p
300*55998Sbosticb label3
301*55998Sbostic:end
302*55998Sbostic' lines1
303*55998Sbostic	mark '5.2'
304*55998Sbostic	if [ $BSD -eq 1 ] ; then
305*55998Sbostic		echo BSD sed fails this test
306*55998Sbostic	fi
307*55998Sbostic	$SED -n -e '
308*55998Sbostics/l1_/l2_/
309*55998Sbostict ok
310*55998Sbosticb
311*55998Sbostic:ok
312*55998Sbostics/^/tested /p
313*55998Sbostic' lines1 lines2
314*55998Sbostic# SunOS sed behaves differently here.  Clarification needed.
315*55998Sbostic#	mark '5.3' ; $SED -n -e '
316*55998Sbostic#5,8b inside
317*55998Sbostic#1,5 {
318*55998Sbostic#	s/^/^/p
319*55998Sbostic#	:inside
320*55998Sbostic#	s/$/$/p
321*55998Sbostic#}
322*55998Sbostic#' lines1
323*55998Sbostic# Check that t clears the substitution done flag
324*55998Sbostic	mark '5.4' ; $SED -n -e '
325*55998Sbostic1,8s/^/^/
326*55998Sbostict l1
327*55998Sbostic:l1
328*55998Sbostict l2
329*55998Sbostics/$/$/p
330*55998Sbosticb
331*55998Sbostic:l2
332*55998Sbostics/^/ERROR/
333*55998Sbostic' lines1
334*55998Sbostic# Check that reading a line clears the substitution done flag
335*55998Sbostic	mark '5.5'
336*55998Sbostic	if [ $BSD -eq 1 ] ; then
337*55998Sbostic		echo BSD sed fails this test
338*55998Sbostic	fi
339*55998Sbostic	$SED -n -e '
340*55998Sbostict l2
341*55998Sbostic1,8s/^/^/p
342*55998Sbostic2,7N
343*55998Sbosticb
344*55998Sbostic:l2
345*55998Sbostics/^/ERROR/p
346*55998Sbostic' lines1
347*55998Sbostic	mark '5.6' ; $SED 5q lines1
348*55998Sbostic	mark '5.7' ; $SED -e '
349*55998Sbostic5i\
350*55998Sbostichello
351*55998Sbostic5q' lines1
352*55998Sbostic}
353*55998Sbostic
354*55998Sbostictest_pattern()
355*55998Sbostic{
356*55998Sbosticecho Pattern space commands
357*55998Sbostic# Check that the pattern space is deleted
358*55998Sbostic	mark '6.1' ; $SED -n -e '
359*55998Sbosticc\
360*55998Sbosticchanged
361*55998Sbosticp
362*55998Sbostic' lines1
363*55998Sbostic	mark '6.2' ; $SED -n -e '
364*55998Sbostic4d
365*55998Sbosticp
366*55998Sbostic' lines1
367*55998Sbostic# SunOS sed refused to print here
368*55998Sbostic#	mark '6.3' ; $SED -e '
369*55998Sbostic#N
370*55998Sbostic#N
371*55998Sbostic#N
372*55998Sbostic#D
373*55998Sbostic#P
374*55998Sbostic#4p
375*55998Sbostic#' lines1
376*55998Sbostic	mark '6.4' ; $SED -e '
377*55998Sbostic2h
378*55998Sbostic3H
379*55998Sbostic4g
380*55998Sbostic5G
381*55998Sbostic6x
382*55998Sbostic6p
383*55998Sbostic6x
384*55998Sbostic6p
385*55998Sbostic' lines1
386*55998Sbostic	mark '6.5' ; $SED -e '4n' lines1
387*55998Sbostic	mark '6.6' ; $SED -n -e '4n' lines1
388*55998Sbostic}
389*55998Sbostic
390*55998Sbostictest_print()
391*55998Sbostic{
392*55998Sbostic	echo Testing print and file routines
393*55998Sbostic	awk 'END {for (i = 1; i < 256; i++) printf("%c", i);print "\n"}' \
394*55998Sbostic		</dev/null >lines3
395*55998Sbostic	# GNU and SunOS sed behave differently here
396*55998Sbostic	mark '7.1'
397*55998Sbostic	if [ $BSD -eq 1 ] ; then
398*55998Sbostic		echo 'BSD sed drops core on this one; TEST SKIPPED'
399*55998Sbostic	else
400*55998Sbostic		$SED -n l lines3
401*55998Sbostic	fi
402*55998Sbostic	mark '7.2' ; $SED -e '/l2_/=' lines1 lines2
403*55998Sbostic	rm -f lines4
404*55998Sbostic	mark '7.3' ; $SED -e '3,12w lines4' lines1
405*55998Sbostic	echo w results
406*55998Sbostic	cat lines4
407*55998Sbostic	mark '7.4' ; $SED -e '4r lines2' lines1
408*55998Sbostic	mark '7.5' ; $SED -e '5r /dev/dds' lines1
409*55998Sbostic	mark '7.6' ; $SED -e '6r /dev/null' lines1
410*55998Sbostic	mark '7.7'
411*55998Sbostic	if [ $BSD -eq 1 -o $GNU -eq 1 -o $SUN -eq 1 ] ; then
412*55998Sbostic		echo BSD, GNU and SunOS cannot pass this one
413*55998Sbostic	else
414*55998Sbostic		sed '200q' $DICT | sed 's$.*$s/^/&/w tmpdir/&$' >script1
415*55998Sbostic		rm -rf tmpdir
416*55998Sbostic		mkdir tmpdir
417*55998Sbostic		$SED -f script1 lines1
418*55998Sbostic		cat tmpdir/*
419*55998Sbostic		rm -rf tmpdir
420*55998Sbostic	fi
421*55998Sbostic	mark '7.8'
422*55998Sbostic	if [ $BSD -eq 1 ] ; then
423*55998Sbostic		echo BSD sed cannot pass 7.7
424*55998Sbostic	else
425*55998Sbostic		echo line1 > lines3
426*55998Sbostic		echo "" >> lines3
427*55998Sbostic		$SED -n -e '$p' lines3 /dev/null
428*55998Sbostic	fi
429*55998Sbostic
430*55998Sbostic}
431*55998Sbostic
432*55998Sbostictest_subst()
433*55998Sbostic{
434*55998Sbostic	echo Testing substitution commands
435*55998Sbostic	mark '8.1' ; $SED -e 's/./X/g' lines1
436*55998Sbostic	mark '8.2' ; $SED -e 's,.,X,g' lines1
437*55998Sbostic# GNU and SunOS sed thinks we are escaping . as wildcard, not as separator
438*55998Sbostic#	mark '8.3' ; $SED -e 's.\..X.g' lines1
439*55998Sbostic# POSIX does not say that this should work
440*55998Sbostic#	mark '8.4' ; $SED -e 's/[/]/Q/' lines1
441*55998Sbostic	mark '8.4' ; $SED -e 's/[\/]/Q/' lines1
442*55998Sbostic	mark '8.5' ; $SED -e 's_\__X_' lines1
443*55998Sbostic	mark '8.6' ; $SED -e 's/./(&)/g' lines1
444*55998Sbostic	mark '8.7' ; $SED -e 's/./(\&)/g' lines1
445*55998Sbostic	mark '8.8' ; $SED -e 's/\(.\)\(.\)\(.\)/x\3x\2x\1/g' lines1
446*55998Sbostic	mark '8.9' ; $SED -e 's/_/u0\
447*55998Sbosticu1\
448*55998Sbosticu2/g' lines1
449*55998Sbostic	mark '8.10'
450*55998Sbostic	if [ $BSD -eq 1 -o $GNU -eq 1 ] ; then
451*55998Sbostic		echo 'BSD/GNU sed do not understand digit flags on s commands'
452*55998Sbostic	fi
453*55998Sbostic	$SED -e 's/./X/4' lines1
454*55998Sbostic	rm -f lines4
455*55998Sbostic	mark '8.11' ; $SED -e 's/1/X/w lines4' lines1
456*55998Sbostic	echo s wfile results
457*55998Sbostic	cat lines4
458*55998Sbostic	mark '8.12' ; $SED -e 's/[123]/X/g' lines1
459*55998Sbostic	mark '8.13' ; $SED -e 'y/0123456789/9876543210/' lines1
460*55998Sbostic	mark '8.14' ;
461*55998Sbostic	if [ $BSD -eq 1 -o $GNU -eq 1 -o $SUN -eq 1 ] ; then
462*55998Sbostic		echo BSD/GNU/SUN sed fail this test
463*55998Sbostic	else
464*55998Sbostic		$SED -e 'y10\123456789198765432\101' lines1
465*55998Sbostic	fi
466*55998Sbostic	mark '8.15' ; $SED -e '1N;2y/\n/X/' lines1
467*55998Sbostic}
468*55998Sbostic
469*55998Sbosticmain
470