xref: /openbsd/regress/usr.bin/sed/substitute.sh (revision 771fbea0)
1#!/bin/sh
2#
3#	$OpenBSD: substitute.sh,v 1.4 2016/05/30 16:31:02 schwarze Exp $
4#
5# Copyright (c) 2011 Ingo Schwarze <schwarze@openbsd.org>
6#
7# Permission to use, copy, modify, and distribute this test suite for any
8# purpose with or without fee is hereby granted, provided that the above
9# copyright notice and this permission notice appear in all copies.
10#
11# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18
19# Test suite for the sed(1) -E substitute command, checking
20# the handling of multiple zero-length matches in particular.
21
22# error counter
23err=0
24
25# test function for one specific flag; arguments are:
26# input string
27# regular expression to replace
28# substitution flag
29# wanted output
30tf() {
31	in=$1
32	patt="s/$2/x/$3"
33	want=$4
34	hexwant=`echo $want | hexdump -C`
35	hexout=`echo "$in" | sed -E "$patt" | hexdump -C`
36	if [ "X$hexout" != "X$hexwant" ]; then
37		echo "patt: $patt input: \"$in\\\\n\""
38		echo "want:" $hexwant
39		echo "got: " $hexout
40	fi
41	[ -z "$in" ] && want=""
42	hexwant=`echo -n $want | hexdump -C`
43	hexout=`echo -n "$in" | sed -E "$patt" | hexdump -C`
44	if [ "X$hexout" != "X$hexwant" ]; then
45		echo "patt: $patt input: \"$in\\\\0\""
46		echo "want:" $hexwant
47		echo "got: " $hexout
48	fi
49}
50
51# test function for various flags; arguments are:
52# input string
53# regular expression to replace
54# wanted output for /g (global substitution)
55# wanted output for /1 (substitution of first match) and so on
56t() {
57	# global substitution
58	in=$1
59	expr=$2
60	want=$3
61	shift 3
62	tf "$in" "$expr" g "$want"
63
64	# substitution with specific index
65	num=1
66	while [ $# -gt 0 ]; do
67		want=$1
68		shift
69		tf "$in" "$expr" "$num" "$want"
70		num=$((num+1))
71	done
72
73	# substitution with excessive index
74	tf "$in" "$expr" "$num" "$in"
75}
76
77t '' ^ x x
78t '' '()' x x
79t '' '$' x x
80t '' '^|$' x x
81t a ^ xa xa
82t a '()' xax xa ax
83t a '$' ax ax
84t a '\<' xa xa
85t a '^|a' x x
86t a '^|$' xax xa ax
87t a '^|a|$' x x
88t a 'a|$' x x
89t a '\<|a' x x
90t ab ^ xab xab
91t ab '()' xaxbx xab axb abx
92t ab '$' abx abx
93t ab '\<' xab xab
94t ab '^|a' xb xb
95t ab '^|b' xax xab ax
96t ab '^|$' xabx xab abx
97t ab '^|a|$' xbx xb abx
98t ab '^|b|$' xax xab ax
99t ab '^|a|b|$' xx xb ax
100t ab '^|ab|$' x x
101t ab 'a|()' xbx xb abx
102t ab 'a|$' xbx xb abx
103t ab 'ab|$' x x
104t ab 'b|()' xax xab ax
105t ab 'b|$' ax ax
106t ab '\<|a' xb xb
107t ab '\<|b' xax xab ax
108t abc '^|b' xaxc xabc axc
109t abc '^|b|$' xaxcx xabc axc abcx
110t abc '^|bc|$' xax xabc ax
111t abc 'ab|()' xcx xc abcx
112t abc 'ab|$' xcx xc abcx
113t abc 'b|()' xaxcx xabc axc abcx
114t abc 'bc|()' xax xabc ax
115t abc 'b|$' axcx axc abcx
116t aa a xx xa ax
117t aa 'a|()' xx xa ax
118t aa 'a*' x x
119t a:a: '\<' xa:xa: xa:a: a:xa:
120t a:a: '\<..' xx xa: a:x
121
122exit $err
123