xref: /netbsd-src/usr.bin/sed/TEST/sed.test (revision ae9172d6cd9432a6a1a56760d86b32c57a66c39c)
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. All advertising materials mentioning features or use of this software
16#    must display the following acknowledgement:
17#	This product includes software developed by the University of
18#	California, Berkeley and its contributors.
19# 4. Neither the name of the University nor the names of its contributors
20#    may be used to endorse or promote products derived from this software
21#    without specific prior written permission.
22#
23# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33# SUCH DAMAGE.
34#
35#	from: @(#)sed.test	8.1 (Berkeley) 6/6/93
36#	$Id: sed.test,v 1.2 1994/02/03 23:45:04 cgd Exp $
37#
38
39# sed Regression Tests
40#
41# The following files are created:
42# lines[1-4], script1, script2
43# Two directories *.out contain the test results
44
45main()
46{
47	BASE=/usr/old/bin/sed
48	BASELOG=sed.out
49	TEST=../obj/sed
50	TESTLOG=nsed.out
51	DICT=/usr/share/dict/words
52
53	test_error | more
54
55	awk 'END { for (i = 1; i < 15; i++) print "l1_" i}' </dev/null >lines1
56	awk 'END { for (i = 1; i < 10; i++) print "l2_" i}' </dev/null >lines2
57
58	exec 4>&1 5>&2
59
60	# Set these flags to get messages about known problems
61	BSD=1
62	GNU=0
63	SUN=0
64	tests $BASE $BASELOG
65
66	BSD=0
67	GNU=0
68	SUN=0
69	tests $TEST $TESTLOG
70	exec 1>&4 2>&5
71	diff -c $BASELOG $TESTLOG | more
72}
73
74tests()
75{
76	SED=$1
77	DIR=$2
78	rm -rf $DIR
79	mkdir $DIR
80	MARK=100
81
82	test_args
83	test_addr
84	echo Testing commands
85	test_group
86	test_acid
87	test_branch
88	test_pattern
89	test_print
90	test_subst
91}
92
93mark()
94{
95	MARK=`expr $MARK + 1`
96	exec 1>&4 2>&5
97	exec >"$DIR/${MARK}_$1"
98	echo "Test $1:$MARK"
99	# Uncomment this line to match tests with sed error messages
100	echo "Test $1:$MARK" >&5
101}
102
103test_args()
104{
105	mark '1.1'
106	echo Testing argument parsing
107	echo First type
108	if [ $SUN -eq 1 ] ; then
109		echo SunOS sed prints only with -n
110	else
111		$SED 's/^/e1_/p' lines1
112	fi
113	mark '1.2' ; $SED -n 's/^/e1_/p' lines1
114	mark '1.3'
115	if [ $SUN -eq 1 ] ; then
116		echo SunOS sed prints only with -n
117	else
118		$SED 's/^/e1_/p' <lines1
119	fi
120	mark '1.4' ; $SED -n 's/^/e1_/p' <lines1
121	echo Second type
122	mark '1.4.1'
123	if [ $SUN -eq 1 ] ; then
124		echo SunOS sed fails this
125	fi
126	$SED -e '' <lines1
127	echo 's/^/s1_/p' >script1
128	echo 's/^/s2_/p' >script2
129	mark '1.5'
130	if [ $SUN -eq 1 ] ; then
131		echo SunOS sed prints only with -n
132	else
133		$SED -f script1 lines1
134	fi
135	mark '1.6'
136	if [ $SUN -eq 1 ] ; then
137		echo SunOS sed prints only with -n
138	else
139		$SED -f script1 <lines1
140	fi
141	mark '1.7'
142	if [ $SUN -eq 1 ] ; then
143		echo SunOS sed prints only with -n
144	else
145		$SED -e 's/^/e1_/p' lines1
146	fi
147	mark '1.8'
148	if [ $SUN -eq 1 ] ; then
149		echo SunOS sed prints only with -n
150	else
151		$SED -e 's/^/e1_/p' <lines1
152	fi
153	mark '1.9' ; $SED -n -f script1 lines1
154	mark '1.10' ; $SED -n -f script1 <lines1
155	mark '1.11' ; $SED -n -e 's/^/e1_/p' lines1
156	mark '1.12'
157	if [ $SUN -eq 1 ] ; then
158		echo SunOS sed prints only with -n
159	else
160		$SED -n -e 's/^/e1_/p' <lines1
161	fi
162	mark '1.13'
163	if [ $SUN -eq 1 ] ; then
164		echo SunOS sed prints only with -n
165	else
166		$SED -e 's/^/e1_/p' -e 's/^/e2_/p' lines1
167	fi
168	mark '1.14'
169	if [ $SUN -eq 1 ] ; then
170		echo SunOS sed prints only with -n
171	else
172		$SED -f script1 -f script2 lines1
173	fi
174	mark '1.15'
175	if [ $GNU -eq 1 -o $SUN -eq 1 ] ; then
176		echo GNU and SunOS sed fail this following older POSIX draft
177	else
178		$SED -e 's/^/e1_/p' -f script1 lines1
179	fi
180	mark '1.16'
181	if [ $SUN -eq 1 ] ; then
182		echo SunOS sed prints only with -n
183	else
184		$SED -e 's/^/e1_/p' lines1 lines1
185	fi
186	# POSIX D11.2:11251
187	mark '1.17' ; $SED p <lines1 lines1
188cat >script1 <<EOF
189#n
190# A comment
191
192p
193EOF
194	mark '1.18' ; $SED -f script1 <lines1 lines1
195}
196
197test_addr()
198{
199	echo Testing address ranges
200	mark '2.1' ; $SED -n -e '4p' lines1
201	mark '2.2' ; $SED -n -e '20p' lines1 lines2
202	mark '2.3' ; $SED -n -e '$p' lines1
203	mark '2.4' ; $SED -n -e '$p' lines1 lines2
204	mark '2.5' ; $SED -n -e '$a\
205hello' /dev/null
206	mark '2.6' ; $SED -n -e '$p' lines1 /dev/null lines2
207	# Should not print anything
208	mark '2.7' ; $SED -n -e '20p' lines1
209	mark '2.8' ; $SED -n -e '0p' lines1
210	mark '2.9' ; $SED -n '/l1_7/p' lines1
211	mark '2.10' ; $SED -n ' /l1_7/ p' lines1
212	mark '2.11'
213	if [ $BSD -eq 1 ] ; then
214		echo BSD sed fails this test
215	fi
216	if [ $GNU -eq 1 ] ; then
217		echo GNU sed fails this
218	fi
219	$SED -n '\_l1\_7_p' lines1
220	mark '2.12' ; $SED -n '1,4p' lines1
221	mark '2.13' ; $SED -n '1,$p' lines1 lines2
222	mark '2.14' ; $SED -n '1,/l2_9/p' lines1 lines2
223	mark '2.15' ; $SED -n '/4/,$p' lines1 lines2
224	mark '2.16' ; $SED -n '/4/,20p' lines1 lines2
225	mark '2.17' ; $SED -n '/4/,/10/p' lines1 lines2
226	mark '2.18' ; $SED -n '/l2_3/,/l1_8/p' lines1 lines2
227	mark '2.19'
228	if [ $GNU -eq 1 ] ; then
229		echo GNU sed fails this
230	fi
231	$SED -n '12,3p' lines1 lines2
232	mark '2.20'
233	if [ $GNU -eq 1 ] ; then
234		echo GNU sed fails this
235	fi
236	$SED -n '/l1_7/,3p' lines1 lines2
237}
238
239test_group()
240{
241	echo Brace and other grouping
242	mark '3.1' ; $SED -e '
2434,12 {
244	s/^/^/
245	s/$/$/
246	s/_/T/
247}' lines1
248	mark '3.2' ; $SED -e '
2494,12 {
250	s/^/^/
251	/6/,/10/ {
252		s/$/$/
253		/8/ s/_/T/
254	}
255}' lines1
256	mark '3.3' ; $SED -e '
2574,12 !{
258	s/^/^/
259	/6/,/10/ !{
260		s/$/$/
261		/8/ !s/_/T/
262	}
263}' lines1
264	mark '3.4' ; $SED -e '4,12!s/^/^/' lines1
265}
266
267test_acid()
268{
269	echo Testing a c d and i commands
270	mark '4.1' ; $SED -n -e '
271s/^/before_i/p
27220i\
273inserted
274s/^/after_i/p
275' lines1 lines2
276	mark '4.2' ; $SED -n -e '
2775,12s/^/5-12/
278s/^/before_a/p
279/5-12/a\
280appended
281s/^/after_a/p
282' lines1 lines2
283	mark '4.3'
284	if [ $GNU -eq 1 ] ; then
285		echo GNU sed fails this
286	fi
287	$SED -n -e '
288s/^/^/p
289/l1_/a\
290appended
2918,10N
292s/$/$/p
293' lines1 lines2
294	mark '4.4' ; $SED -n -e '
295c\
296hello
297' lines1
298	mark '4.5' ; $SED -n -e '
2998c\
300hello
301' lines1
302	mark '4.6' ; $SED -n -e '
3033,14c\
304hello
305' lines1
306# SunOS and GNU sed behave differently.   We follow POSIX
307#	mark '4.7' ; $SED -n -e '
308#8,3c\
309#hello
310#' lines1
311	mark '4.8' ; $SED d <lines1
312}
313
314test_branch()
315{
316	echo Testing labels and branching
317	mark '5.1' ; $SED -n -e '
318b label4
319:label3
320s/^/label3_/p
321b end
322:label4
3232,12b label1
324b label2
325:label1
326s/^/label1_/p
327b
328:label2
329s/^/label2_/p
330b label3
331:end
332' lines1
333	mark '5.2'
334	if [ $BSD -eq 1 ] ; then
335		echo BSD sed fails this test
336	fi
337	$SED -n -e '
338s/l1_/l2_/
339t ok
340b
341:ok
342s/^/tested /p
343' lines1 lines2
344# SunOS sed behaves differently here.  Clarification needed.
345#	mark '5.3' ; $SED -n -e '
346#5,8b inside
347#1,5 {
348#	s/^/^/p
349#	:inside
350#	s/$/$/p
351#}
352#' lines1
353# Check that t clears the substitution done flag
354	mark '5.4' ; $SED -n -e '
3551,8s/^/^/
356t l1
357:l1
358t l2
359s/$/$/p
360b
361:l2
362s/^/ERROR/
363' lines1
364# Check that reading a line clears the substitution done flag
365	mark '5.5'
366	if [ $BSD -eq 1 ] ; then
367		echo BSD sed fails this test
368	fi
369	$SED -n -e '
370t l2
3711,8s/^/^/p
3722,7N
373b
374:l2
375s/^/ERROR/p
376' lines1
377	mark '5.6' ; $SED 5q lines1
378	mark '5.7' ; $SED -e '
3795i\
380hello
3815q' lines1
382# Branch across block boundary
383	mark '5.8' ; $SED -e '
384{
385:b
386}
387s/l/m/
388tb' lines1
389}
390
391test_pattern()
392{
393echo Pattern space commands
394# Check that the pattern space is deleted
395	mark '6.1' ; $SED -n -e '
396c\
397changed
398p
399' lines1
400	mark '6.2' ; $SED -n -e '
4014d
402p
403' lines1
404# SunOS sed refused to print here
405#	mark '6.3' ; $SED -e '
406#N
407#N
408#N
409#D
410#P
411#4p
412#' lines1
413	mark '6.4' ; $SED -e '
4142h
4153H
4164g
4175G
4186x
4196p
4206x
4216p
422' lines1
423	mark '6.5' ; $SED -e '4n' lines1
424	mark '6.6' ; $SED -n -e '4n' lines1
425}
426
427test_print()
428{
429	echo Testing print and file routines
430	awk 'END {for (i = 1; i < 256; i++) printf("%c", i);print "\n"}' \
431		</dev/null >lines3
432	# GNU and SunOS sed behave differently here
433	mark '7.1'
434	if [ $BSD -eq 1 ] ; then
435		echo 'BSD sed drops core on this one; TEST SKIPPED'
436	else
437		$SED -n l lines3
438	fi
439	mark '7.2' ; $SED -e '/l2_/=' lines1 lines2
440	rm -f lines4
441	mark '7.3' ; $SED -e '3,12w lines4' lines1
442	echo w results
443	cat lines4
444	mark '7.4' ; $SED -e '4r lines2' lines1
445	mark '7.5' ; $SED -e '5r /dev/dds' lines1
446	mark '7.6' ; $SED -e '6r /dev/null' lines1
447	mark '7.7'
448	if [ $BSD -eq 1 -o $GNU -eq 1 -o $SUN -eq 1 ] ; then
449		echo BSD, GNU and SunOS cannot pass this one
450	else
451		sed '200q' $DICT | sed 's$.*$s/^/&/w tmpdir/&$' >script1
452		rm -rf tmpdir
453		mkdir tmpdir
454		$SED -f script1 lines1
455		cat tmpdir/*
456		rm -rf tmpdir
457	fi
458	mark '7.8'
459	if [ $BSD -eq 1 ] ; then
460		echo BSD sed cannot pass 7.7
461	else
462		echo line1 > lines3
463		echo "" >> lines3
464		$SED -n -e '$p' lines3 /dev/null
465	fi
466
467}
468
469test_subst()
470{
471	echo Testing substitution commands
472	mark '8.1' ; $SED -e 's/./X/g' lines1
473	mark '8.2' ; $SED -e 's,.,X,g' lines1
474# GNU and SunOS sed thinks we are escaping . as wildcard, not as separator
475#	mark '8.3' ; $SED -e 's.\..X.g' lines1
476# POSIX does not say that this should work
477#	mark '8.4' ; $SED -e 's/[/]/Q/' lines1
478	mark '8.4' ; $SED -e 's/[\/]/Q/' lines1
479	mark '8.5' ; $SED -e 's_\__X_' lines1
480	mark '8.6' ; $SED -e 's/./(&)/g' lines1
481	mark '8.7' ; $SED -e 's/./(\&)/g' lines1
482	mark '8.8' ; $SED -e 's/\(.\)\(.\)\(.\)/x\3x\2x\1/g' lines1
483	mark '8.9' ; $SED -e 's/_/u0\
484u1\
485u2/g' lines1
486	mark '8.10'
487	if [ $BSD -eq 1 -o $GNU -eq 1 ] ; then
488		echo 'BSD/GNU sed do not understand digit flags on s commands'
489	fi
490	$SED -e 's/./X/4' lines1
491	rm -f lines4
492	mark '8.11' ; $SED -e 's/1/X/w lines4' lines1
493	echo s wfile results
494	cat lines4
495	mark '8.12' ; $SED -e 's/[123]/X/g' lines1
496	mark '8.13' ; $SED -e 'y/0123456789/9876543210/' lines1
497	mark '8.14' ;
498	if [ $BSD -eq 1 -o $GNU -eq 1 -o $SUN -eq 1 ] ; then
499		echo BSD/GNU/SUN sed fail this test
500	else
501		$SED -e 'y10\123456789198765432\101' lines1
502	fi
503	mark '8.15' ; $SED -e '1N;2y/\n/X/' lines1
504	mark '8.16'
505	if [ $BSD -eq 1 ] ; then
506		echo 'BSD sed does not handle branch defined REs'
507	else
508		echo 'eeefff' | $SED -e 'p' -e 's/e/X/p' -e ':x' \
509		    -e 's//Y/p' -e '/f/bx'
510	fi
511}
512
513test_error()
514{
515	exec 0>&3 4>&1 5>&2
516	exec 0</dev/null
517	exec 2>&1
518	set -x
519	$TEST -x && exit 1
520	$TEST -f && exit 1
521	$TEST -e && exit 1
522	$TEST -f /dev/dds && exit 1
523	$TEST p /dev/dds && exit 1
524	$TEST -f /bin/sh && exit 1
525	$TEST '{' && exit 1
526	$TEST '{' && exit 1
527	$TEST '/hello/' && exit 1
528	$TEST '1,/hello/' && exit 1
529	$TEST -e '-5p' && exit 1
530	$TEST '/jj' && exit 1
531	$TEST 'a hello' && exit 1
532	$TEST 'a \ hello' && exit 1
533	$TEST 'b foo' && exit 1
534	$TEST 'd hello' && exit 1
535	$TEST 's/aa' && exit 1
536	$TEST 's/aa/' && exit 1
537	$TEST 's/a/b' && exit 1
538	$TEST 's/a/b/c/d' && exit 1
539	$TEST 's/a/b/ 1 2' && exit 1
540	$TEST 's/a/b/ 1 g' && exit 1
541	$TEST 's/a/b/w' && exit 1
542	$TEST 'y/aa' && exit 1
543	$TEST 'y/aa/b/' && exit 1
544	$TEST 'y/aa/' && exit 1
545	$TEST 'y/a/b' && exit 1
546	$TEST 'y/a/b/c/d' && exit 1
547	$TEST '!' && exit 1
548	$TEST supercalifrangolisticexprialidociussupercalifrangolisticexcius
549	set +x
550	exec 0>&3 1>&4 2>&5
551}
552
553main
554