summaryrefslogtreecommitdiffstats
path: root/tools/regression/usr.bin/sed/multitest.t
blob: f3a42e26d86acbcbe6355736a4e077f7e7899280 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
#!/bin/sh -
#
# Copyright (c) 1992 Diomidis Spinellis.
# Copyright (c) 1992, 1993
#	The Regents of the University of California.  All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 4. Neither the name of the University nor the names of its contributors
#    may be used to endorse or promote products derived from this software
#    without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
#	@(#)sed.test	8.1 (Berkeley) 6/6/93
#
#	$FreeBSD$
#

# sed Regression Tests
#
# The directory regress.test.out contains the expected test results
#
# These are the regression tests created during the development of the
# BSD sed.  The reference file naming scheme used in this script can't
# handle gracefully the insertion of new tests between existing ones.
# Therefore, either use the new m4-based regress.t framework, or add
# tests after the last existing test.

main()
{
	cd `dirname $0`
	REGRESS=regress.multitest.out
	DICT=/usr/share/dict/words

	awk 'END { for (i = 1; i < 15; i++) print "l1_" i}' </dev/null >lines1
	awk 'END { for (i = 1; i < 10; i++) print "l2_" i}' </dev/null >lines2

	echo "1..121"

	exec 4>&1 5>&2
	tests
	exec 1>&4 2>&5

	# Remove temporary files
	rm -f current.out lines[1-4] script[1-2]
}

tests()
{
	SED=sed
	MARK=0

	test_args
	test_addr
	test_group
	test_acid
	test_branch
	test_pattern
	test_print
	test_subst
	test_error
	# Handle the result of the last test
	result
}

# Display a test's result
result()
{
	if [ "$TODO" = '1' ] ; then
		TODO='TODO '
	else
		TODO=''
	fi
	if ! [ -r $REGRESS/${MARK}_${TESTNAME} ] ; then
		echo "Seeding $REGRESS/${MARK}_${TESTNAME} with current result" 1>&2
		cp current.out $REGRESS/${MARK}_${TESTNAME}
	fi
	if diff -c $REGRESS/${MARK}_${TESTNAME} current.out ; then
		echo "ok $MARK $TESTNAME # $TODO$COMMENT"
	else
		echo "not ok $MARK $TESTNAME # $TODO$COMMENT"
	fi 1>&4 2>&5
}

# Mark the beginning of each test
mark()
{
	[ $MARK -gt 0 ] && result
	MARK=`expr $MARK + 1`
	TESTNAME=$1
	exec 1>&4 2>&5
	exec >"current.out"
}

test_args()
{
	COMMENT='Argument parsing - first type'
	mark '1.1'
	$SED 's/^/e1_/p' lines1
	mark '1.2' ; $SED -n 's/^/e1_/p' lines1
	mark '1.3'
	$SED 's/^/e1_/p' <lines1
	mark '1.4' ; $SED -n 's/^/e1_/p' <lines1
	COMMENT='Argument parsing - second type'
	mark '1.4.1'
	$SED -e '' <lines1
	echo 's/^/s1_/p' >script1
	echo 's/^/s2_/p' >script2
	mark '1.5'
	$SED -f script1 lines1
	mark '1.6'
	$SED -f script1 <lines1
	mark '1.7'
	$SED -e 's/^/e1_/p' lines1
	mark '1.8'
	$SED -e 's/^/e1_/p' <lines1
	mark '1.9' ; $SED -n -f script1 lines1
	mark '1.10' ; $SED -n -f script1 <lines1
	mark '1.11' ; $SED -n -e 's/^/e1_/p' lines1
	mark '1.12'
	$SED -n -e 's/^/e1_/p' <lines1
	mark '1.13'
	$SED -e 's/^/e1_/p' -e 's/^/e2_/p' lines1
	mark '1.14'
	$SED -f script1 -f script2 lines1
	mark '1.15'
	$SED -e 's/^/e1_/p' -f script1 lines1
	mark '1.16'
	$SED -e 's/^/e1_/p' lines1 lines1
	# POSIX D11.2:11251
	mark '1.17' ; $SED p <lines1 lines1
cat >script1 <<EOF
#n
# A comment

p
EOF
	mark '1.18' ; $SED -f script1 <lines1 lines1
}

test_addr()
{
	COMMENT='Address ranges'
	mark '2.1' ; $SED -n -e '4p' lines1
	mark '2.2' ; $SED -n -e '20p' lines1 lines2
	mark '2.3' ; $SED -n -e '$p' lines1
	mark '2.4' ; $SED -n -e '$p' lines1 lines2
	mark '2.5' ; $SED -n -e '$a\
hello' /dev/null
	mark '2.6' ; $SED -n -e '$p' lines1 /dev/null lines2
	# Should not print anything
	mark '2.7' ; $SED -n -e '20p' lines1
	mark '2.8' ; $SED -n -e '/NOTFOUND/p' lines1
	mark '2.9' ; $SED -n '/l1_7/p' lines1
	mark '2.10' ; $SED -n ' /l1_7/ p' lines1
	mark '2.11' ; $SED -n '\_l1\_7_p' lines1
	mark '2.12' ; $SED -n '1,4p' lines1
	mark '2.13' ; $SED -n '1,$p' lines1 lines2
	mark '2.14' ; $SED -n '1,/l2_9/p' lines1 lines2
	mark '2.15' ; $SED -n '/4/,$p' lines1 lines2
	mark '2.16' ; $SED -n '/4/,20p' lines1 lines2
	mark '2.17' ; $SED -n '/4/,/10/p' lines1 lines2
	mark '2.18' ; $SED -n '/l2_3/,/l1_8/p' lines1 lines2
	mark '2.19' ; $SED -n '12,3p' lines1 lines2
	mark '2.20' ; $SED -n '/l1_7/,3p' lines1 lines2
	mark '2.21' ; $SED -n '13,+4p' lines1 lines2
	mark '2.22' ; $SED -n '/l1_6/,+2p' lines1 lines2
}

test_group()
{
	COMMENT='Brace and other grouping'
	mark '3.1' ; $SED -e '
4,12 {
	s/^/^/
	s/$/$/
	s/_/T/
}' lines1
	mark '3.2' ; $SED -e '
4,12 {
	s/^/^/
	/6/,/10/ {
		s/$/$/
		/8/ s/_/T/
	}
}' lines1
	mark '3.3' ; $SED -e '
4,12 !{
	s/^/^/
	/6/,/10/ !{
		s/$/$/
		/8/ !s/_/T/
	}
}' lines1
	mark '3.4' ; $SED -e '4,12!s/^/^/' lines1
}

test_acid()
{
	COMMENT='Commands a c d and i'
	mark '4.1' ; $SED -n -e '
s/^/before_i/p
20i\
inserted
s/^/after_i/p
' lines1 lines2
	mark '4.2' ; $SED -n -e '
5,12s/^/5-12/
s/^/before_a/p
/5-12/a\
appended
s/^/after_a/p
' lines1 lines2
	mark '4.3'
	$SED -n -e '
s/^/^/p
/l1_/a\
appended
8,10N
s/$/$/p
' lines1 lines2
	mark '4.4' ; $SED -n -e '
c\
hello
' lines1
	mark '4.5' ; $SED -n -e '
8c\
hello
' lines1
	mark '4.6' ; $SED -n -e '
3,14c\
hello
' lines1
# SunOS and GNU sed behave differently.   We follow POSIX
	mark '4.7' ; $SED -n -e '
8,3c\
hello
' lines1
	mark '4.8' ; $SED d <lines1
}

test_branch()
{
	COMMENT='Labels and branching'
	mark '5.1' ; $SED -n -e '
b label4
:label3
s/^/label3_/p
b end
:label4
2,12b label1
b label2
:label1
s/^/label1_/p
b
:label2
s/^/label2_/p
b label3
:end
' lines1
	mark '5.2'
	$SED -n -e '
s/l1_/l2_/
t ok
b
:ok
s/^/tested /p
' lines1 lines2
# SunOS and GNU sed behave as follows: lines 9-$ aren't printed at all
	mark '5.3' ; $SED -n -e '
5,8b inside
1,5 {
	s/^/^/p
	:inside
	s/$/$/p
}
' lines1
# Check that t clears the substitution done flag
	mark '5.4' ; $SED -n -e '
1,8s/^/^/
t l1
:l1
t l2
s/$/$/p
b
:l2
s/^/ERROR/
' lines1
# Check that reading a line clears the substitution done flag
	mark '5.5'
	$SED -n -e '
t l2
1,8s/^/^/p
2,7N
b
:l2
s/^/ERROR/p
' lines1
	mark '5.6' ; $SED 5q lines1
	mark '5.7' ; $SED -e '
5i\
hello
5q' lines1
# Branch across block boundary
	mark '5.8' ; $SED -e '
{
:b
}
s/l/m/
tb' lines1
}

test_pattern()
{
COMMENT='Pattern space commands'
# Check that the pattern space is deleted
	mark '6.1' ; $SED -n -e '
c\
changed
p
' lines1
	mark '6.2' ; $SED -n -e '
4d
p
' lines1
	mark '6.3'
	$SED -e 'N;N;N;D' lines1
	mark '6.4' ; $SED -e '
2h
3H
4g
5G
6x
6p
6x
6p
' lines1
	mark '6.5' ; $SED -e '4n' lines1
	mark '6.6' ; $SED -n -e '4n' lines1
}

test_print()
{
	COMMENT='Print and file routines'
	awk 'END {for (i = 1; i < 256; i++) printf("%c", i);print "\n"}' \
		</dev/null >lines3
	# GNU and SunOS sed behave differently here
	mark '7.1'
	$SED -n l lines3
	mark '7.2' ; $SED -e '/l2_/=' lines1 lines2
	rm -f lines4
	mark '7.3' ; $SED -e '3,12w lines4' lines1
	COMMENT='w results'
	cat lines4
	mark '7.4' ; $SED -e '4r lines2' lines1
	mark '7.5' ; $SED -e '5r /dev/dds' lines1
	mark '7.6' ; $SED -e '6r /dev/null' lines1
	mark '7.7'
	sed '200q' $DICT | sed 's$.*$s/^/&/w tmpdir/&$' >script1
	rm -rf tmpdir
	mkdir tmpdir
	$SED -f script1 lines1
	cat tmpdir/*
	rm -rf tmpdir
	mark '7.8'
	echo line1 > lines3
	echo "" >> lines3
	TODO=1
	$SED -n -e '$p' lines3 /dev/null
		
}

test_subst()
{
	COMMENT='Substitution commands'
	mark '8.1' ; $SED -e 's/./X/g' lines1
	mark '8.2' ; $SED -e 's,.,X,g' lines1
# SunOS sed thinks we are escaping . as wildcard, not as separator
	mark '8.3'
	$SED -e 's.\..X.g' lines1
	mark '8.4' ; $SED -e 's/[\/]/Q/' lines1
	mark '8.5' ; $SED -e 's_\__X_' lines1
	mark '8.6' ; $SED -e 's/./(&)/g' lines1
	mark '8.7' ; $SED -e 's/./(\&)/g' lines1
	mark '8.8' ; $SED -e 's/\(.\)\(.\)\(.\)/x\3x\2x\1/g' lines1
	mark '8.9' ; $SED -e 's/_/u0\
u1\
u2/g' lines1
	mark '8.10'
	$SED -e 's/./X/4' lines1
	rm -f lines4
	mark '8.11' ; $SED -e 's/1/X/w lines4' lines1
	COMMENT='s wfile results'
	cat lines4
	mark '8.12' ; $SED -e 's/[123]/X/g' lines1
	mark '8.13' ; $SED -e 'y/0123456789/9876543210/' lines1
	mark '8.14' ; 
	$SED -e 'y10\123456789198765432\101' lines1
	mark '8.15' ; $SED -e '1N;2y/\n/X/' lines1
	mark '8.16'
	echo 'eeefff' | $SED -e '
		p
		s/e/X/p
		:x
		s//Y/p 
		# Establish limit counter in the hold space
		# GNU sed version 3.02 enters into an infinite loop here
		x 
		/.\{10\}/ {
			s/.*/ERROR/
			b
		}
		s/.*/&./
		x 
		/f/bx
	'
	# POSIX does not say that this should work,
	# but it does for GNU, BSD, and SunOS
	mark '8.17' ; $SED -e 's/[/]/Q/' lines1
}

test_error()
{
	COMMENT='Error cases'
	mark '9.1' ; $SED -x 2>/dev/null ; echo $?
	mark '9.2' ; $SED -f 2>/dev/null ; echo $?
	mark '9.3' ; $SED -e 2>/dev/null ; echo $?
	mark '9.4' ; $SED -f /dev/xyzzyxyzy 2>/dev/null ; echo $?
	mark '9.5' ; $SED p /dev/xyzzyxyzy 2>/dev/null ; echo $?
	mark '9.6' ; $SED -f /bin/sh 2>/dev/null ; echo $?
	mark '9.7' ; $SED '{' 2>/dev/null ; echo $?
	mark '9.8' ; $SED '{' 2>/dev/null ; echo $?
	mark '9.9' ; $SED '/hello/' 2>/dev/null ; echo $?
	mark '9.10' ; $SED '1,/hello/' 2>/dev/null ; echo $?
	mark '9.11' ; $SED -e '-5p' 2>/dev/null ; echo $?
	mark '9.12' ; $SED '/jj' 2>/dev/null ; echo $?
	mark '9.13' ; $SED 'a hello' 2>/dev/null ; echo $?
	mark '9.14' ; $SED 'a \ hello' 2>/dev/null ; echo $?
	mark '9.15' ; $SED 'b foo' 2>/dev/null ; echo $?
	mark '9.16' ; $SED 'd hello' 2>/dev/null ; echo $?
	mark '9.17' ; $SED 's/aa' 2>/dev/null ; echo $?
	mark '9.18' ; $SED 's/aa/' 2>/dev/null ; echo $?
	mark '9.19' ; $SED 's/a/b' 2>/dev/null ; echo $?
	mark '9.20' ; $SED 's/a/b/c/d' 2>/dev/null ; echo $?
	mark '9.21' ; $SED 's/a/b/ 1 2' 2>/dev/null ; echo $?
	mark '9.22' ; $SED 's/a/b/ 1 g' 2>/dev/null ; echo $?
	mark '9.23' ; $SED 's/a/b/w' 2>/dev/null ; echo $?
	mark '9.24' ; $SED 'y/aa' 2>/dev/null ; echo $?
	mark '9.25' ; $SED 'y/aa/b/' 2>/dev/null ; echo $?
	mark '9.26' ; $SED 'y/aa/' 2>/dev/null ; echo $?
	mark '9.27' ; $SED 'y/a/b' 2>/dev/null ; echo $?
	mark '9.28' ; $SED 'y/a/b/c/d' 2>/dev/null ; echo $?
	mark '9.29' ; $SED '!' 2>/dev/null ; echo $?
	mark '9.30' ; $SED supercalifrangolisticexprialidociussupercalifrangolisticexcius 2>/dev/null ; echo $?
	mark '9.31' ; $SED '' /dev/null 2>/dev/null ; echo $?
}

main
OpenPOWER on IntegriCloud