xref: /freebsd/contrib/netbsd-tests/usr.bin/sed/t_sed.sh (revision faf25f48d601ae39f5752602f3020e2e92605625)
1# $NetBSD: t_sed.sh,v 1.6 2016/04/05 00:48:53 christos Exp $
2#
3# Copyright (c) 2012 The NetBSD Foundation, Inc.
4# All rights reserved.
5#
6# This code is derived from software contributed to The NetBSD Foundation
7# by Jukka Ruohonen and David A. Holland.
8#
9# Redistribution and use in source and binary forms, with or without
10# modification, are permitted provided that the following conditions
11# are met:
12# 1. Redistributions of source code must retain the above copyright
13#    notice, this list of conditions and the following disclaimer.
14# 2. Redistributions in binary form must reproduce the above copyright
15#    notice, this list of conditions and the following disclaimer in the
16#    documentation and/or other materials provided with the distribution.
17#
18# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28# POSSIBILITY OF SUCH DAMAGE.
29#
30
31atf_test_case c2048
32c2048_head() {
33	atf_set "descr" "Test that sed(1) does not fail when the " \
34			"2048'th character is a backslash (PR bin/25899)"
35}
36
37c2048_body() {
38
39	atf_check -s exit:0 -o inline:'foo\n' -e empty \
40		-x "echo foo | sed -f $(atf_get_srcdir)/d_c2048.in"
41}
42
43atf_test_case emptybackref
44emptybackref_head() {
45	atf_set "descr" "Test that sed(1) handles " \
46			"empty back references (PR bin/28126)"
47}
48
49emptybackref_body() {
50
51	atf_check -o inline:"foo1bar1\n" \
52		-x "echo foo1bar1 | sed -ne '/foo\(.*\)bar\1/p'"
53
54	atf_expect_fail "PR bin/28126"
55
56	atf_check -o inline:"foobar\n" \
57		-x "echo foobar | sed -ne '/foo\(.*\)bar\1/p'"
58}
59
60atf_test_case longlines
61longlines_head() {
62	atf_set "descr" "Test that sed(1) handles " \
63			"long lines correctly (PR bin/42261)"
64}
65
66longlines_body() {
67
68	str=$(awk 'BEGIN {while(x<2043){printf "x";x++}}')
69	echo $str > input
70
71	atf_check -o save:output -x "echo x | sed s,x,${str},g"
72	atf_check -s exit:0 -o empty -e empty -x "diff input output"
73}
74
75atf_test_case rangeselection
76rangeselection_head() {
77	atf_set "descr" "Test that sed(1) handles " \
78			"range selection correctly"
79}
80
81rangeselection_body() {
82	# basic cases
83	atf_check -o inline:"D\n" \
84		-x "printf 'A\nB\nC\nD\n' | sed '1,3d'"
85	atf_check -o inline:"A\n" \
86		-x "printf 'A\nB\nC\nD\n' | sed '2,4d'"
87	# two nonoverlapping ranges
88	atf_check -o inline:"C\n" \
89		-x "printf 'A\nB\nC\nD\nE\n' | sed '1,2d;4,5d'"
90	# overlapping ranges; the first prevents the second from being entered
91	atf_check -o inline:"D\nE\n" \
92		-x "printf 'A\nB\nC\nD\nE\n' | sed '1,3d;3,5d'"
93	# the 'n' command can also prevent ranges from being entered
94	atf_check -o inline:"B\nB\nC\nD\n" \
95		-x "printf 'A\nB\nC\nD\n' | sed '1,3s/A/B/;1,3n;1,3s/B/C/'"
96	atf_check -o inline:"B\nC\nC\nD\n" \
97		-x "printf 'A\nB\nC\nD\n' | sed '1,3s/A/B/;1,3n;2,3s/B/C/'"
98
99	# basic cases using regexps
100	atf_check -o inline:"D\n" \
101		-x "printf 'A\nB\nC\nD\n' | sed '/A/,/C/d'"
102	atf_check -o inline:"A\n" \
103		-x "printf 'A\nB\nC\nD\n' | sed '/B/,/D/d'"
104	# two nonoverlapping ranges
105	atf_check -o inline:"C\n" \
106		-x "printf 'A\nB\nC\nD\nE\n' | sed '/A/,/B/d;/D/,/E/d'"
107	# two overlapping ranges; the first blocks the second as above
108	atf_check -o inline:"D\nE\n" \
109		-x "printf 'A\nB\nC\nD\nE\n' | sed '/A/,/C/d;/C/,/E/d'"
110	# the 'n' command makes some lines invisible to downstreap regexps
111	atf_check -o inline:"B\nC\nC\nD\n" \
112		-x "printf 'A\nB\nC\nD\n' | sed '/A/,/C/s/A/B/;1,3n;/B/,/C/s/B/C/'"
113
114	# a range ends at the *first* matching end line
115	atf_check -o inline:"D\nC\n" \
116		-x "printf 'A\nB\nC\nD\nC\n' | sed '/A/,/C/d'"
117	# another matching start line within the range has no effect
118	atf_check -o inline:"D\nC\n" \
119		-x "printf 'A\nB\nA\nC\nD\nC\n' | sed '/A/,/C/d'"
120}
121
122atf_test_case preserve_leading_ws_ia
123preserve_leading_ws_ia_head() {
124	atf_set "descr" "Test that sed(1) preserves leading whitespace " \
125			"in insert and append (PR bin/49872)"
126}
127
128preserve_leading_ws_ia_body() {
129	atf_check -o inline:"    1 2 3\n4 5 6\n    7 8 9\n\n" \
130		-x 'echo | sed -e "/^$/i\\
131    1 2 3\\
1324 5 6\\
133    7 8 9"'
134}
135
136atf_init_test_cases() {
137	atf_add_test_case c2048
138	atf_add_test_case emptybackref
139	atf_add_test_case longlines
140	atf_add_test_case rangeselection
141	atf_add_test_case preserve_leading_ws_ia
142}
143