t_sed.sh revision 1.13 1 1.13 gutterid # $NetBSD: t_sed.sh,v 1.13 2025/06/02 01:32:47 gutteridge Exp $
2 1.1 jruoho #
3 1.1 jruoho # Copyright (c) 2012 The NetBSD Foundation, Inc.
4 1.1 jruoho # All rights reserved.
5 1.1 jruoho #
6 1.1 jruoho # This code is derived from software contributed to The NetBSD Foundation
7 1.2 dholland # by Jukka Ruohonen and David A. Holland.
8 1.1 jruoho #
9 1.1 jruoho # Redistribution and use in source and binary forms, with or without
10 1.1 jruoho # modification, are permitted provided that the following conditions
11 1.1 jruoho # are met:
12 1.1 jruoho # 1. Redistributions of source code must retain the above copyright
13 1.1 jruoho # notice, this list of conditions and the following disclaimer.
14 1.1 jruoho # 2. Redistributions in binary form must reproduce the above copyright
15 1.1 jruoho # notice, this list of conditions and the following disclaimer in the
16 1.1 jruoho # documentation and/or other materials provided with the distribution.
17 1.1 jruoho #
18 1.1 jruoho # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 1.1 jruoho # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 1.1 jruoho # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 1.1 jruoho # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 1.1 jruoho # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 1.1 jruoho # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 1.1 jruoho # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 1.1 jruoho # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 1.1 jruoho # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 1.1 jruoho # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 1.1 jruoho # POSSIBILITY OF SUCH DAMAGE.
29 1.1 jruoho #
30 1.1 jruoho
31 1.4 jruoho atf_test_case c2048
32 1.4 jruoho c2048_head() {
33 1.4 jruoho atf_set "descr" "Test that sed(1) does not fail when the " \
34 1.10 gutterid "2048th character is a backslash (PR bin/25899)"
35 1.4 jruoho }
36 1.4 jruoho
37 1.4 jruoho c2048_body() {
38 1.4 jruoho
39 1.5 jmmv atf_check -s exit:0 -o inline:'foo\n' -e empty \
40 1.5 jmmv -x "echo foo | sed -f $(atf_get_srcdir)/d_c2048.in"
41 1.4 jruoho }
42 1.4 jruoho
43 1.1 jruoho atf_test_case emptybackref
44 1.1 jruoho emptybackref_head() {
45 1.8 christos atf_set "descr" "Test that sed(1) handles empty back references"
46 1.1 jruoho }
47 1.1 jruoho
48 1.1 jruoho emptybackref_body() {
49 1.1 jruoho
50 1.1 jruoho atf_check -o inline:"foo1bar1\n" \
51 1.1 jruoho -x "echo foo1bar1 | sed -ne '/foo\(.*\)bar\1/p'"
52 1.1 jruoho
53 1.1 jruoho atf_check -o inline:"foobar\n" \
54 1.1 jruoho -x "echo foobar | sed -ne '/foo\(.*\)bar\1/p'"
55 1.1 jruoho }
56 1.1 jruoho
57 1.3 jruoho atf_test_case longlines
58 1.3 jruoho longlines_head() {
59 1.3 jruoho atf_set "descr" "Test that sed(1) handles " \
60 1.3 jruoho "long lines correctly (PR bin/42261)"
61 1.3 jruoho }
62 1.3 jruoho
63 1.3 jruoho longlines_body() {
64 1.3 jruoho
65 1.3 jruoho str=$(awk 'BEGIN {while(x<2043){printf "x";x++}}')
66 1.3 jruoho echo $str > input
67 1.3 jruoho
68 1.3 jruoho atf_check -o save:output -x "echo x | sed s,x,${str},g"
69 1.3 jruoho atf_check -s exit:0 -o empty -e empty -x "diff input output"
70 1.3 jruoho }
71 1.3 jruoho
72 1.2 dholland atf_test_case rangeselection
73 1.2 dholland rangeselection_head() {
74 1.2 dholland atf_set "descr" "Test that sed(1) handles " \
75 1.2 dholland "range selection correctly"
76 1.2 dholland }
77 1.2 dholland
78 1.2 dholland rangeselection_body() {
79 1.2 dholland # basic cases
80 1.2 dholland atf_check -o inline:"D\n" \
81 1.2 dholland -x "printf 'A\nB\nC\nD\n' | sed '1,3d'"
82 1.2 dholland atf_check -o inline:"A\n" \
83 1.2 dholland -x "printf 'A\nB\nC\nD\n' | sed '2,4d'"
84 1.10 gutterid # two non-overlapping ranges
85 1.2 dholland atf_check -o inline:"C\n" \
86 1.2 dholland -x "printf 'A\nB\nC\nD\nE\n' | sed '1,2d;4,5d'"
87 1.2 dholland # overlapping ranges; the first prevents the second from being entered
88 1.2 dholland atf_check -o inline:"D\nE\n" \
89 1.2 dholland -x "printf 'A\nB\nC\nD\nE\n' | sed '1,3d;3,5d'"
90 1.2 dholland # the 'n' command can also prevent ranges from being entered
91 1.2 dholland atf_check -o inline:"B\nB\nC\nD\n" \
92 1.2 dholland -x "printf 'A\nB\nC\nD\n' | sed '1,3s/A/B/;1,3n;1,3s/B/C/'"
93 1.2 dholland atf_check -o inline:"B\nC\nC\nD\n" \
94 1.2 dholland -x "printf 'A\nB\nC\nD\n' | sed '1,3s/A/B/;1,3n;2,3s/B/C/'"
95 1.2 dholland
96 1.2 dholland # basic cases using regexps
97 1.2 dholland atf_check -o inline:"D\n" \
98 1.2 dholland -x "printf 'A\nB\nC\nD\n' | sed '/A/,/C/d'"
99 1.2 dholland atf_check -o inline:"A\n" \
100 1.2 dholland -x "printf 'A\nB\nC\nD\n' | sed '/B/,/D/d'"
101 1.10 gutterid # two non-overlapping ranges
102 1.2 dholland atf_check -o inline:"C\n" \
103 1.2 dholland -x "printf 'A\nB\nC\nD\nE\n' | sed '/A/,/B/d;/D/,/E/d'"
104 1.2 dholland # two overlapping ranges; the first blocks the second as above
105 1.2 dholland atf_check -o inline:"D\nE\n" \
106 1.2 dholland -x "printf 'A\nB\nC\nD\nE\n' | sed '/A/,/C/d;/C/,/E/d'"
107 1.2 dholland # the 'n' command makes some lines invisible to downstreap regexps
108 1.2 dholland atf_check -o inline:"B\nC\nC\nD\n" \
109 1.2 dholland -x "printf 'A\nB\nC\nD\n' | sed '/A/,/C/s/A/B/;1,3n;/B/,/C/s/B/C/'"
110 1.2 dholland
111 1.2 dholland # a range ends at the *first* matching end line
112 1.2 dholland atf_check -o inline:"D\nC\n" \
113 1.2 dholland -x "printf 'A\nB\nC\nD\nC\n' | sed '/A/,/C/d'"
114 1.2 dholland # another matching start line within the range has no effect
115 1.2 dholland atf_check -o inline:"D\nC\n" \
116 1.2 dholland -x "printf 'A\nB\nA\nC\nD\nC\n' | sed '/A/,/C/d'"
117 1.2 dholland }
118 1.2 dholland
119 1.6 christos atf_test_case preserve_leading_ws_ia
120 1.6 christos preserve_leading_ws_ia_head() {
121 1.6 christos atf_set "descr" "Test that sed(1) preserves leading whitespace " \
122 1.6 christos "in insert and append (PR bin/49872)"
123 1.6 christos }
124 1.6 christos
125 1.6 christos preserve_leading_ws_ia_body() {
126 1.6 christos atf_check -o inline:" 1 2 3\n4 5 6\n 7 8 9\n\n" \
127 1.6 christos -x 'echo | sed -e "/^$/i\\
128 1.6 christos 1 2 3\\
129 1.6 christos 4 5 6\\
130 1.6 christos 7 8 9"'
131 1.6 christos }
132 1.6 christos
133 1.7 christos atf_test_case escapes_in_subst
134 1.7 christos escapes_in_subst_head() {
135 1.7 christos atf_set "descr" "Test that sed(1) expands \x \d \o escapes " \
136 1.9 andvar "in substitution strings"
137 1.7 christos }
138 1.7 christos
139 1.7 christos escapes_in_subst_body() {
140 1.7 christos atf_check -o inline:"fooXbar\n" \
141 1.7 christos -x 'echo "foo bar" | sed -e "s/ /\x58/"'
142 1.7 christos atf_check -o inline:"fooXbar\n" \
143 1.7 christos -x 'echo "foo bar" | sed -e "s/ /\o130/"'
144 1.7 christos atf_check -o inline:"fooXbar\n" \
145 1.7 christos -x 'echo "foo bar" | sed -e "s/ /\d88/"'
146 1.7 christos }
147 1.7 christos
148 1.13 gutterid atf_test_case subst_escapes
149 1.13 gutterid subst_escapes_head() {
150 1.13 gutterid atf_set "descr" "Test \[dox]number escapes in subst part of 's' command"
151 1.13 gutterid }
152 1.13 gutterid
153 1.13 gutterid subst_escapes_body() {
154 1.13 gutterid atf_expect_fail "PR bin/59453: sed misparses \[dox]number escapes"
155 1.13 gutterid
156 1.13 gutterid atf_check -o inline:"#8ball\n" \
157 1.13 gutterid -x "echo | sed -e 's/^/\d358ball'"
158 1.13 gutterid
159 1.13 gutterid atf_check -o inline:"#3duh\n" \
160 1.13 gutterid -x "echo | sed -e 's/^/\o0433duh'"
161 1.13 gutterid
162 1.13 gutterid atf_check -o inline:"#duh\n" \
163 1.13 gutterid -x "echo | sed -e 's/^/\x23duh'"
164 1.13 gutterid }
165 1.13 gutterid
166 1.7 christos atf_test_case escapes_in_re
167 1.7 christos escapes_in_re_head() {
168 1.7 christos atf_set "descr" "Test that sed(1) expands \x \d \o escapes " \
169 1.7 christos "in regex strings"
170 1.7 christos }
171 1.7 christos
172 1.7 christos escapes_in_re_body() {
173 1.7 christos atf_check -o inline:"foo bar\n" \
174 1.7 christos -x 'echo "fooXbar" | sed -e "s/\x58/ /"'
175 1.7 christos atf_check -o inline:"foo bar\n" \
176 1.7 christos -x 'echo "fooXbar" | sed -e "s/\o130/ /"'
177 1.7 christos atf_check -o inline:"foo bar\n" \
178 1.7 christos -x 'echo "fooXbar" | sed -e "s/\d88/ /"'
179 1.7 christos }
180 1.7 christos
181 1.7 christos atf_test_case escapes_in_re_bracket
182 1.7 christos escapes_in_re_bracket_head() {
183 1.7 christos atf_set "descr" "Test that sed(1) does not expand \x \d \o escapes " \
184 1.7 christos "in regex strings inside braces"
185 1.7 christos }
186 1.7 christos
187 1.7 christos escapes_in_re_bracket_body() {
188 1.7 christos atf_check -o inline:"foo bar\n" \
189 1.7 christos -x 'echo "foo\\x58bar" | sed -e "s/[\x58]/ /g"'
190 1.7 christos atf_check -o inline:"f bar\n" \
191 1.7 christos -x 'echo "fooo\\130bar" | sed -e "s/[\o130]/ /g"'
192 1.7 christos atf_check -o inline:"foo bar\n" \
193 1.7 christos -x 'echo "foo\\d88bar" | sed -e "s/[\d88]/ /g"'
194 1.7 christos }
195 1.10 gutterid
196 1.11 gutterid atf_test_case relative_addressing
197 1.11 gutterid relative_addressing_head() {
198 1.11 gutterid atf_set "descr" "Test that sed(1) handles relative addressing " \
199 1.11 gutterid "properly (PR bin/49109)"
200 1.11 gutterid }
201 1.11 gutterid
202 1.11 gutterid relative_addressing_body() {
203 1.11 gutterid atf_check -o match:"3" -x 'seq 1 4 | sed -n "1,+2p" | wc -l'
204 1.11 gutterid }
205 1.11 gutterid
206 1.1 jruoho atf_init_test_cases() {
207 1.4 jruoho atf_add_test_case c2048
208 1.1 jruoho atf_add_test_case emptybackref
209 1.3 jruoho atf_add_test_case longlines
210 1.2 dholland atf_add_test_case rangeselection
211 1.6 christos atf_add_test_case preserve_leading_ws_ia
212 1.7 christos atf_add_test_case escapes_in_subst
213 1.13 gutterid atf_add_test_case subst_escapes
214 1.7 christos atf_add_test_case escapes_in_re
215 1.7 christos atf_add_test_case escapes_in_re_bracket
216 1.11 gutterid atf_add_test_case relative_addressing
217 1.1 jruoho }
218