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