xref: /netbsd-src/tests/usr.bin/sed/t_sed.sh (revision 09bc0ce2d8f42fd4aa07ddbfca78176d9ffd5979)
1*09bc0ce2Sgutteridge# $NetBSD: t_sed.sh,v 1.11 2023/05/06 02:12:11 gutteridge Exp $
20498995fSjruoho#
30498995fSjruoho# Copyright (c) 2012 The NetBSD Foundation, Inc.
40498995fSjruoho# All rights reserved.
50498995fSjruoho#
60498995fSjruoho# This code is derived from software contributed to The NetBSD Foundation
7a837eb4fSdholland# by Jukka Ruohonen and David A. Holland.
80498995fSjruoho#
90498995fSjruoho# Redistribution and use in source and binary forms, with or without
100498995fSjruoho# modification, are permitted provided that the following conditions
110498995fSjruoho# are met:
120498995fSjruoho# 1. Redistributions of source code must retain the above copyright
130498995fSjruoho#    notice, this list of conditions and the following disclaimer.
140498995fSjruoho# 2. Redistributions in binary form must reproduce the above copyright
150498995fSjruoho#    notice, this list of conditions and the following disclaimer in the
160498995fSjruoho#    documentation and/or other materials provided with the distribution.
170498995fSjruoho#
180498995fSjruoho# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
190498995fSjruoho# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
200498995fSjruoho# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
210498995fSjruoho# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
220498995fSjruoho# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
230498995fSjruoho# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
240498995fSjruoho# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
250498995fSjruoho# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
260498995fSjruoho# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
270498995fSjruoho# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
280498995fSjruoho# POSSIBILITY OF SUCH DAMAGE.
290498995fSjruoho#
300498995fSjruoho
311e806afdSjruohoatf_test_case c2048
321e806afdSjruohoc2048_head() {
331e806afdSjruoho	atf_set "descr" "Test that sed(1) does not fail when the " \
34f12d9a9cSgutteridge			"2048th character is a backslash (PR bin/25899)"
351e806afdSjruoho}
361e806afdSjruoho
371e806afdSjruohoc2048_body() {
381e806afdSjruoho
39d477a9c1Sjmmv	atf_check -s exit:0 -o inline:'foo\n' -e empty \
40d477a9c1Sjmmv		-x "echo foo | sed -f $(atf_get_srcdir)/d_c2048.in"
411e806afdSjruoho}
421e806afdSjruoho
430498995fSjruohoatf_test_case emptybackref
440498995fSjruohoemptybackref_head() {
453ab762d4Schristos	atf_set "descr" "Test that sed(1) handles empty back references"
460498995fSjruoho}
470498995fSjruoho
480498995fSjruohoemptybackref_body() {
490498995fSjruoho
500498995fSjruoho	atf_check -o inline:"foo1bar1\n" \
510498995fSjruoho		-x "echo foo1bar1 | sed -ne '/foo\(.*\)bar\1/p'"
520498995fSjruoho
530498995fSjruoho	atf_check -o inline:"foobar\n" \
540498995fSjruoho		-x "echo foobar | sed -ne '/foo\(.*\)bar\1/p'"
550498995fSjruoho}
560498995fSjruoho
579e07384eSjruohoatf_test_case longlines
589e07384eSjruoholonglines_head() {
599e07384eSjruoho	atf_set "descr" "Test that sed(1) handles " \
609e07384eSjruoho			"long lines correctly (PR bin/42261)"
619e07384eSjruoho}
629e07384eSjruoho
639e07384eSjruoholonglines_body() {
649e07384eSjruoho
659e07384eSjruoho	str=$(awk 'BEGIN {while(x<2043){printf "x";x++}}')
669e07384eSjruoho	echo $str > input
679e07384eSjruoho
689e07384eSjruoho	atf_check -o save:output -x "echo x | sed s,x,${str},g"
699e07384eSjruoho	atf_check -s exit:0 -o empty -e empty -x "diff input output"
709e07384eSjruoho}
719e07384eSjruoho
72a837eb4fSdhollandatf_test_case rangeselection
73a837eb4fSdhollandrangeselection_head() {
74a837eb4fSdholland	atf_set "descr" "Test that sed(1) handles " \
75a837eb4fSdholland			"range selection correctly"
76a837eb4fSdholland}
77a837eb4fSdholland
78a837eb4fSdhollandrangeselection_body() {
79a837eb4fSdholland	# basic cases
80a837eb4fSdholland	atf_check -o inline:"D\n" \
81a837eb4fSdholland		-x "printf 'A\nB\nC\nD\n' | sed '1,3d'"
82a837eb4fSdholland	atf_check -o inline:"A\n" \
83a837eb4fSdholland		-x "printf 'A\nB\nC\nD\n' | sed '2,4d'"
84f12d9a9cSgutteridge	# two non-overlapping ranges
85a837eb4fSdholland	atf_check -o inline:"C\n" \
86a837eb4fSdholland		-x "printf 'A\nB\nC\nD\nE\n' | sed '1,2d;4,5d'"
87a837eb4fSdholland	# overlapping ranges; the first prevents the second from being entered
88a837eb4fSdholland	atf_check -o inline:"D\nE\n" \
89a837eb4fSdholland		-x "printf 'A\nB\nC\nD\nE\n' | sed '1,3d;3,5d'"
90a837eb4fSdholland	# the 'n' command can also prevent ranges from being entered
91a837eb4fSdholland	atf_check -o inline:"B\nB\nC\nD\n" \
92a837eb4fSdholland		-x "printf 'A\nB\nC\nD\n' | sed '1,3s/A/B/;1,3n;1,3s/B/C/'"
93a837eb4fSdholland	atf_check -o inline:"B\nC\nC\nD\n" \
94a837eb4fSdholland		-x "printf 'A\nB\nC\nD\n' | sed '1,3s/A/B/;1,3n;2,3s/B/C/'"
95a837eb4fSdholland
96a837eb4fSdholland	# basic cases using regexps
97a837eb4fSdholland	atf_check -o inline:"D\n" \
98a837eb4fSdholland		-x "printf 'A\nB\nC\nD\n' | sed '/A/,/C/d'"
99a837eb4fSdholland	atf_check -o inline:"A\n" \
100a837eb4fSdholland		-x "printf 'A\nB\nC\nD\n' | sed '/B/,/D/d'"
101f12d9a9cSgutteridge	# two non-overlapping ranges
102a837eb4fSdholland	atf_check -o inline:"C\n" \
103a837eb4fSdholland		-x "printf 'A\nB\nC\nD\nE\n' | sed '/A/,/B/d;/D/,/E/d'"
104a837eb4fSdholland	# two overlapping ranges; the first blocks the second as above
105a837eb4fSdholland	atf_check -o inline:"D\nE\n" \
106a837eb4fSdholland		-x "printf 'A\nB\nC\nD\nE\n' | sed '/A/,/C/d;/C/,/E/d'"
107a837eb4fSdholland	# the 'n' command makes some lines invisible to downstreap regexps
108a837eb4fSdholland	atf_check -o inline:"B\nC\nC\nD\n" \
109a837eb4fSdholland		-x "printf 'A\nB\nC\nD\n' | sed '/A/,/C/s/A/B/;1,3n;/B/,/C/s/B/C/'"
110a837eb4fSdholland
111a837eb4fSdholland	# a range ends at the *first* matching end line
112a837eb4fSdholland	atf_check -o inline:"D\nC\n" \
113a837eb4fSdholland		-x "printf 'A\nB\nC\nD\nC\n' | sed '/A/,/C/d'"
114a837eb4fSdholland	# another matching start line within the range has no effect
115a837eb4fSdholland	atf_check -o inline:"D\nC\n" \
116a837eb4fSdholland		-x "printf 'A\nB\nA\nC\nD\nC\n' | sed '/A/,/C/d'"
117a837eb4fSdholland}
118a837eb4fSdholland
119a34d3aeaSchristosatf_test_case preserve_leading_ws_ia
120a34d3aeaSchristospreserve_leading_ws_ia_head() {
121a34d3aeaSchristos	atf_set "descr" "Test that sed(1) preserves leading whitespace " \
122a34d3aeaSchristos			"in insert and append (PR bin/49872)"
123a34d3aeaSchristos}
124a34d3aeaSchristos
125a34d3aeaSchristospreserve_leading_ws_ia_body() {
126a34d3aeaSchristos	atf_check -o inline:"    1 2 3\n4 5 6\n    7 8 9\n\n" \
127a34d3aeaSchristos		-x 'echo | sed -e "/^$/i\\
128a34d3aeaSchristos    1 2 3\\
129a34d3aeaSchristos4 5 6\\
130a34d3aeaSchristos    7 8 9"'
131a34d3aeaSchristos}
132a34d3aeaSchristos
1336b700166Schristosatf_test_case escapes_in_subst
1346b700166Schristosescapes_in_subst_head() {
1356b700166Schristos	atf_set "descr" "Test that sed(1) expands \x \d \o escapes " \
136369abe06Sandvar		"in substitution strings"
1376b700166Schristos}
1386b700166Schristos
1396b700166Schristosescapes_in_subst_body() {
1406b700166Schristos	atf_check -o inline:"fooXbar\n" \
1416b700166Schristos		-x 'echo "foo bar" | sed -e "s/ /\x58/"'
1426b700166Schristos	atf_check -o inline:"fooXbar\n" \
1436b700166Schristos		-x 'echo "foo bar" | sed -e "s/ /\o130/"'
1446b700166Schristos	atf_check -o inline:"fooXbar\n" \
1456b700166Schristos		-x 'echo "foo bar" | sed -e "s/ /\d88/"'
1466b700166Schristos}
1476b700166Schristos
1486b700166Schristosatf_test_case escapes_in_re
1496b700166Schristosescapes_in_re_head() {
1506b700166Schristos	atf_set "descr" "Test that sed(1) expands \x \d \o escapes " \
1516b700166Schristos		"in regex strings"
1526b700166Schristos}
1536b700166Schristos
1546b700166Schristosescapes_in_re_body() {
1556b700166Schristos	atf_check -o inline:"foo bar\n" \
1566b700166Schristos		-x 'echo "fooXbar" | sed -e "s/\x58/ /"'
1576b700166Schristos	atf_check -o inline:"foo bar\n" \
1586b700166Schristos		-x 'echo "fooXbar" | sed -e "s/\o130/ /"'
1596b700166Schristos	atf_check -o inline:"foo bar\n" \
1606b700166Schristos		-x 'echo "fooXbar" | sed -e "s/\d88/ /"'
1616b700166Schristos}
1626b700166Schristos
1636b700166Schristosatf_test_case escapes_in_re_bracket
1646b700166Schristosescapes_in_re_bracket_head() {
1656b700166Schristos	atf_set "descr" "Test that sed(1) does not expand \x \d \o escapes " \
1666b700166Schristos		"in regex strings inside braces"
1676b700166Schristos}
1686b700166Schristos
1696b700166Schristosescapes_in_re_bracket_body() {
1706b700166Schristos	atf_check -o inline:"foo    bar\n" \
1716b700166Schristos		-x 'echo "foo\\x58bar" | sed -e "s/[\x58]/ /g"'
1726b700166Schristos	atf_check -o inline:"f       bar\n" \
1736b700166Schristos		-x 'echo "fooo\\130bar" | sed -e "s/[\o130]/ /g"'
1746b700166Schristos	atf_check -o inline:"foo    bar\n" \
1756b700166Schristos		-x 'echo "foo\\d88bar" | sed -e "s/[\d88]/ /g"'
1766b700166Schristos}
177f12d9a9cSgutteridge
178*09bc0ce2Sgutteridgeatf_test_case relative_addressing
179*09bc0ce2Sgutteridgerelative_addressing_head() {
180*09bc0ce2Sgutteridge	atf_set "descr" "Test that sed(1) handles relative addressing " \
181*09bc0ce2Sgutteridge		"properly (PR bin/49109)"
182*09bc0ce2Sgutteridge}
183*09bc0ce2Sgutteridge
184*09bc0ce2Sgutteridgerelative_addressing_body() {
185*09bc0ce2Sgutteridge	atf_check -o match:"3" -x 'seq 1 4 | sed -n "1,+2p" | wc -l'
186*09bc0ce2Sgutteridge}
187*09bc0ce2Sgutteridge
1880498995fSjruohoatf_init_test_cases() {
1891e806afdSjruoho	atf_add_test_case c2048
1900498995fSjruoho	atf_add_test_case emptybackref
1919e07384eSjruoho	atf_add_test_case longlines
192a837eb4fSdholland	atf_add_test_case rangeselection
193a34d3aeaSchristos	atf_add_test_case preserve_leading_ws_ia
1946b700166Schristos	atf_add_test_case escapes_in_subst
1956b700166Schristos	atf_add_test_case escapes_in_re
1966b700166Schristos	atf_add_test_case escapes_in_re_bracket
197*09bc0ce2Sgutteridge	atf_add_test_case relative_addressing
1980498995fSjruoho}
199