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