1#!/bin/sh -
2#	$NetBSD: sed.test,v 1.3 1997/01/09 20:21:37 tls 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. All advertising materials mentioning features or use of this software
17#    must display the following acknowledgement:
18#	This product includes software developed by the University of
19#	California, Berkeley and its contributors.
20# 4. Neither the name of the University nor the names of its contributors
21#    may be used to endorse or promote products derived from this software
22#    without specific prior written permission.
23#
24# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34# SUCH DAMAGE.
35#
36#	from: @(#)sed.test	8.1 (Berkeley) 6/6/93
37#	$NetBSD: sed.test,v 1.3 1997/01/09 20:21:37 tls Exp $
38#
39
40# sed Regression Tests
41
42# Modified by Paolo Bonzini to:
43# - not warn about buggy seds
44# - run tests once instead of comparing them to the system sed
45# - remove most uses of awk
46# - cleanup at exit
47# - comment tests that broke because of extensions
48
49main()
50{
51	TEST="${1-../sed/sed}"
52	TESTLOG="${2-sed.out}"
53	# DICT="${3-/usr/share/dict/words}"
54
55	: > lines1
56	: > lines2
57	for i in 1 2 3 4 5 6 7 8 9; do
58	  echo l1_$i >> lines1
59	  echo l2_$i >> lines2
60	done
61	for i in 10 11 12 13 14; do
62	  echo l1_$i >> lines1
63	done
64
65	# Set these flags to get messages about known problems
66	tests "$TEST" "$TESTLOG"
67
68	rm -f lines[1234] script[12]
69}
70
71tests()
72{
73	SED="$1"
74	LOG="$2"
75	MARK=100
76	rm -f "$LOG"
77
78	exec 3>&0 4>&1 5>&2
79	exec 0</dev/null 1>/dev/null 2>/dev/null
80	test_error
81	exec 0>&3 1>&4 2>&5
82
83	exec 4>&1 5>&2
84	test_args
85	test_addr
86	test_group
87	test_acid
88	test_branch
89	test_pattern
90	test_print
91	test_subst
92	exec 1>&4 2>&5
93}
94
95mark()
96{
97	exec 2>&1 >>$LOG
98	test $MARK = 100 || echo
99	MARK=`expr $MARK + 1`
100	echo "Test $1:$MARK" | sed 's/./=/g'
101	echo "Test $1:$MARK"
102	echo "Test $1:$MARK" | sed 's/./=/g'
103}
104
105test_args()
106{
107	mark '1.1'
108	echo Testing argument parsing
109	echo First type
110	$SED 's/^/e1_/p' lines1
111	mark '1.2' ; $SED -n 's/^/e1_/p' lines1
112	mark '1.3' ; $SED 's/^/e1_/p' <lines1
113	mark '1.4' ; $SED -n 's/^/e1_/p' <lines1
114	echo Second type
115	mark '1.4.1'
116	$SED -e '' <lines1
117	echo 's/^/s1_/p' >script1
118	echo 's/^/s2_/p' >script2
119	mark '1.5' ; $SED -f script1 lines1
120	mark '1.6' ; $SED -f script1 <lines1
121	mark '1.7' ; $SED -e 's/^/e1_/p' lines1
122	mark '1.8' ; $SED -e 's/^/e1_/p' <lines1
123	mark '1.9' ; $SED -n -f script1 lines1
124	mark '1.10' ; $SED -n -f script1 <lines1
125	mark '1.11' ; $SED -n -e 's/^/e1_/p' lines1
126	mark '1.12' ; $SED -n -e 's/^/e1_/p' <lines1
127	mark '1.13' ; $SED -e 's/^/e1_/p' -e 's/^/e2_/p' lines1
128	mark '1.14' ; $SED -f script1 -f script2 lines1
129	mark '1.15' ; $SED -e 's/^/e1_/p' -f script1 lines1
130	mark '1.16' ; $SED -e 's/^/e1_/p' lines1 lines1
131	# POSIX D11.2:11251
132	mark '1.17' ; $SED p <lines1 lines1
133cat >script1 <<EOF
134#n
135# A comment
136
137p
138EOF
139	mark '1.18' ; $SED -f script1 <lines1 lines1
140}
141
142test_addr()
143{
144	echo Testing address ranges
145	mark '2.1' ; $SED -n -e '4p' lines1
146	mark '2.2' ; $SED -n -e '20p' lines1 lines2
147	mark '2.3' ; $SED -n -e '$p' lines1
148	mark '2.4' ; $SED -n -e '$p' lines1 lines2
149	mark '2.5' ; $SED -n -e '$a\
150hello' /dev/null
151	mark '2.6' ; $SED -n -e '$p' lines1 /dev/null lines2
152	# Should not print anything
153	mark '2.7' ; $SED -n -e '20p' lines1
154	# Disabled because it is undefined behavior
155	# mark '2.8' ; $SED -n -e '0p' lines1
156	mark '2.9' ; $SED -n '/l1_7/p' lines1
157	mark '2.10' ; $SED -n ' /l1_7/ p' lines1
158	mark '2.11' ; $SED -n '\_l1\_7_p' lines1
159	mark '2.12' ; $SED -n '1,4p' lines1
160	mark '2.13' ; $SED -n '1,$p' lines1 lines2
161	mark '2.14' ; $SED -n '1,/l2_9/p' lines1 lines2
162	mark '2.15' ; $SED -n '/4/,$p' lines1 lines2
163	mark '2.16' ; $SED -n '/4/,20p' lines1 lines2
164	mark '2.17' ; $SED -n '/4/,/10/p' lines1 lines2
165	mark '2.18' ; $SED -n '/l2_3/,/l1_8/p' lines1 lines2
166	mark '2.19' ; $SED -n '12,3p' lines1 lines2
167	mark '2.20' ; $SED -n '/l1_7/,3p' lines1 lines2
168}
169
170test_group()
171{
172	echo Brace and other grouping
173	mark '3.1' ; $SED -e '
1744,12 {
175	s/^/^/
176	s/$/$/
177	s/_/T/
178}' lines1
179	mark '3.2' ; $SED -e '
1804,12 {
181	s/^/^/
182	/6/,/10/ {
183		s/$/$/
184		/8/ s/_/T/
185	}
186}' lines1
187	mark '3.3' ; $SED -e '
1884,12 !{
189	s/^/^/
190	/6/,/10/ !{
191		s/$/$/
192		/8/ !s/_/T/
193	}
194}' lines1
195	mark '3.4' ; $SED -e '4,12!s/^/^/' lines1
196}
197
198test_acid()
199{
200	echo Testing a c d and i commands
201	mark '4.1' ; $SED -n -e '
202s/^/before_i/p
20320i\
204inserted
205s/^/after_i/p
206' lines1 lines2
207	mark '4.2' ; $SED -n -e '
2085,12s/^/5-12/
209s/^/before_a/p
210/5-12/a\
211appended
212s/^/after_a/p
213' lines1 lines2
214	mark '4.3' ; $SED -n -e '
215s/^/^/p
216/l1_/a\
217appended
2188,10N
219s/$/$/p
220' lines1 lines2
221	mark '4.4' ; $SED -n -e '
222c\
223hello
224' lines1
225	mark '4.5' ; $SED -n -e '
2268c\
227hello
228' lines1
229	mark '4.6' ; $SED -n -e '
2303,14c\
231hello
232' lines1
233	mark '4.7' ; $SED -n -e '
2348,3c\
235hello
236' lines1
237	mark '4.8' ; $SED d <lines1
238}
239
240test_branch()
241{
242	echo Testing labels and branching
243	mark '5.1' ; $SED -n -e '
244b label4
245:label3
246s/^/label3_/p
247b end
248:label4
2492,12b label1
250b label2
251:label1
252s/^/label1_/p
253b
254:label2
255s/^/label2_/p
256b label3
257:end
258' lines1
259	mark '5.2' ; $SED -n -e '
260s/l1_/l2_/
261t ok
262b
263:ok
264s/^/tested /p
265' lines1 lines2
266	mark '5.3' ; $SED -n -e '
2675,8b inside
2681,5 {
269	s/^/^/p
270	:inside
271	s/$/$/p
272}
273' lines1
274# Check that t clears the substitution done flag
275	mark '5.4' ; $SED -n -e '
2761,8s/^/^/
277t l1
278:l1
279t l2
280s/$/$/p
281b
282:l2
283s/^/ERROR/
284' lines1
285# Check that reading a line clears the substitution done flag
286	mark '5.5' ; $SED -n -e '
287t l2
2881,8s/^/^/p
2892,7N
290b
291:l2
292s/^/ERROR/p
293' lines1
294	mark '5.6' ; $SED 5q lines1
295	mark '5.7' ; $SED -e '
2965i\
297hello
2985q' lines1
299# Branch across block boundary
300	mark '5.8' ; $SED -e '
301{
302:b
303}
304s/l/m/
305tb' lines1
306}
307
308test_pattern()
309{
310echo Pattern space commands
311# Check that the pattern space is deleted
312	mark '6.1' ; $SED -n -e '
313c\
314changed
315p
316' lines1
317	mark '6.2' ; $SED -n -e '
3184d
319p
320' lines1
321	mark '6.3' ; $SED -e '
322N
323N
324N
325D
326P
3274p
328' lines1
329	mark '6.4' ; $SED -e '
3302h
3313H
3324g
3335G
3346x
3356p
3366x
3376p
338' lines1
339	mark '6.5' ; $SED -e '4n' lines1
340	mark '6.6' ; $SED -n -e '4n' lines1
341}
342
343test_print()
344{
345	echo Testing print and file routines
346	awk 'END {for (i = 1; i < 256; i++) printf("%c", i);print "\n"}' \
347		</dev/null >lines3
348	mark '7.1' ; $SED -n l lines3
349	mark '7.2' ; $SED -e '/l2_/=' lines1 lines2
350	rm -f lines4
351	mark '7.3' ; $SED -e '3,12w lines4' lines1
352	echo w results
353	cat lines4
354	mark '7.4' ; $SED -e '4r lines2' lines1
355	mark '7.5' ; $SED -e '5r /dev/dds' lines1
356	mark '7.6' ; $SED -e '6r /dev/null' lines1
357	# mark '7.7'
358	# sed '200q' $DICT | sed 's$.*$s/^/&/w tmpdir/&$' >script1
359	# rm -rf tmpdir
360	# mkdir tmpdir
361	# $SED -f script1 lines1
362	# cat tmpdir/*
363	# rm -rf tmpdir
364	mark '7.8'
365	echo line1 > lines3
366	echo "" >> lines3
367	$SED -n -e '$p' lines3 /dev/null
368}
369
370test_subst()
371{
372	echo Testing substitution commands
373	mark '8.1' ; $SED -e 's/./X/g' lines1
374	mark '8.2' ; $SED -e 's,.,X,g' lines1
375	mark '8.3' ; $SED -e 's.\..X.g' lines1
376# POSIX does not say that this should work
377#	mark '8.4' ; $SED -e 's/[/]/Q/' lines1
378	mark '8.4' ; $SED -e 's/[\/]/Q/' lines1
379	mark '8.5' ; $SED -e 's_\__X_' lines1
380	mark '8.6' ; $SED -e 's/./(&)/g' lines1
381	mark '8.7' ; $SED -e 's/./(\&)/g' lines1
382	mark '8.8' ; $SED -e 's/\(.\)\(.\)\(.\)/x\3x\2x\1/g' lines1
383	mark '8.9' ; $SED -e 's/_/u0\
384u1\
385u2/g' lines1
386	mark '8.10' ; $SED -e 's/./X/4' lines1
387	rm -f lines4
388	mark '8.11' ; $SED -e 's/1/X/w lines4' lines1
389	echo s wfile results
390	cat lines4
391	mark '8.12' ; $SED -e 's/[123]/X/g' lines1
392	mark '8.13' ; $SED -e 'y/0123456789/9876543210/' lines1
393	mark '8.14' ; $SED -e 'y10\123456789198765432\101' lines1
394	mark '8.15' ; $SED -e '1N;2y/\n/X/' lines1
395	mark '8.16'
396	echo 'eeefff' | $SED -e 'p' -e 's/e/X/p' -e ':x' \
397	    -e 's//Y/p' -e '/f/bx'
398	mark '8.17' ; $SED -e 's&.&\&&g' lines1
399}
400
401test_error()
402{
403	$SED -x && exit 1
404	$SED -f && exit 1
405	$SED -e && exit 1
406	$SED -f /dev/dds && exit 1
407	$SED p /dev/dds && exit 1
408	$SED -f /bin/sh && exit 1
409	$SED '{' && exit 1
410	$SED '{' && exit 1
411	$SED '/hello/' && exit 1
412	$SED '1,/hello/' && exit 1
413	$SED -e '-5p' && exit 1
414	$SED '/jj' && exit 1
415	# $SED 'a hello' && exit 1
416	# $SED 'a \ hello' && exit 1
417	$SED 'b foo' && exit 1
418	$SED 'd hello' && exit 1
419	$SED 's/aa' && exit 1
420	$SED 's/aa/' && exit 1
421	$SED 's/a/b' && exit 1
422	$SED 's/a/b/c/d' && exit 1
423	$SED 's/a/b/ 1 2' && exit 1
424	# $SED 's/a/b/ 1 g' && exit 1
425	$SED 's/a/b/w' && exit 1
426	$SED 'y/aa' && exit 1
427	$SED 'y/aa/b/' && exit 1
428	$SED 'y/aa/' && exit 1
429	$SED 'y/a/b' && exit 1
430	$SED 'y/a/b/c/d' && exit 1
431	$SED '!' && exit 1
432	$SED supercalifrangolisticexprialidociussupercalifrangolisticexcius
433}
434
435main ${1+"$@"}
436