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