Home | History | Annotate | Line # | Download | only in sed
t_sed.sh revision 1.15
      1 # $NetBSD: t_sed.sh,v 1.15 2025/06/03 19:03:23 martin 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 
     31 atf_test_case c2048
     32 c2048_head() {
     33 	atf_set "descr" "Test that sed(1) does not fail when the " \
     34 			"2048th character is a backslash (PR bin/25899)"
     35 }
     36 
     37 c2048_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 
     43 atf_test_case emptybackref
     44 emptybackref_head() {
     45 	atf_set "descr" "Test that sed(1) handles empty back references"
     46 }
     47 
     48 emptybackref_body() {
     49 
     50 	atf_check -o inline:"foo1bar1\n" \
     51 		-x "echo foo1bar1 | sed -ne '/foo\(.*\)bar\1/p'"
     52 
     53 	atf_check -o inline:"foobar\n" \
     54 		-x "echo foobar | sed -ne '/foo\(.*\)bar\1/p'"
     55 }
     56 
     57 atf_test_case longlines
     58 longlines_head() {
     59 	atf_set "descr" "Test that sed(1) handles " \
     60 			"long lines correctly (PR bin/42261)"
     61 }
     62 
     63 longlines_body() {
     64 
     65 	str=$(awk 'BEGIN {while(x<2043){printf "x";x++}}')
     66 	echo $str > input
     67 
     68 	atf_check -o save:output -x "echo x | sed s,x,${str},g"
     69 	atf_check -s exit:0 -o empty -e empty -x "diff input output"
     70 }
     71 
     72 atf_test_case rangeselection
     73 rangeselection_head() {
     74 	atf_set "descr" "Test that sed(1) handles " \
     75 			"range selection correctly"
     76 }
     77 
     78 rangeselection_body() {
     79 	# basic cases
     80 	atf_check -o inline:"D\n" \
     81 		-x "printf 'A\nB\nC\nD\n' | sed '1,3d'"
     82 	atf_check -o inline:"A\n" \
     83 		-x "printf 'A\nB\nC\nD\n' | sed '2,4d'"
     84 	# two non-overlapping ranges
     85 	atf_check -o inline:"C\n" \
     86 		-x "printf 'A\nB\nC\nD\nE\n' | sed '1,2d;4,5d'"
     87 	# overlapping ranges; the first prevents the second from being entered
     88 	atf_check -o inline:"D\nE\n" \
     89 		-x "printf 'A\nB\nC\nD\nE\n' | sed '1,3d;3,5d'"
     90 	# the 'n' command can also prevent ranges from being entered
     91 	atf_check -o inline:"B\nB\nC\nD\n" \
     92 		-x "printf 'A\nB\nC\nD\n' | sed '1,3s/A/B/;1,3n;1,3s/B/C/'"
     93 	atf_check -o inline:"B\nC\nC\nD\n" \
     94 		-x "printf 'A\nB\nC\nD\n' | sed '1,3s/A/B/;1,3n;2,3s/B/C/'"
     95 
     96 	# basic cases using regexps
     97 	atf_check -o inline:"D\n" \
     98 		-x "printf 'A\nB\nC\nD\n' | sed '/A/,/C/d'"
     99 	atf_check -o inline:"A\n" \
    100 		-x "printf 'A\nB\nC\nD\n' | sed '/B/,/D/d'"
    101 	# two non-overlapping ranges
    102 	atf_check -o inline:"C\n" \
    103 		-x "printf 'A\nB\nC\nD\nE\n' | sed '/A/,/B/d;/D/,/E/d'"
    104 	# two overlapping ranges; the first blocks the second as above
    105 	atf_check -o inline:"D\nE\n" \
    106 		-x "printf 'A\nB\nC\nD\nE\n' | sed '/A/,/C/d;/C/,/E/d'"
    107 	# the 'n' command makes some lines invisible to downstreap regexps
    108 	atf_check -o inline:"B\nC\nC\nD\n" \
    109 		-x "printf 'A\nB\nC\nD\n' | sed '/A/,/C/s/A/B/;1,3n;/B/,/C/s/B/C/'"
    110 
    111 	# a range ends at the *first* matching end line
    112 	atf_check -o inline:"D\nC\n" \
    113 		-x "printf 'A\nB\nC\nD\nC\n' | sed '/A/,/C/d'"
    114 	# another matching start line within the range has no effect
    115 	atf_check -o inline:"D\nC\n" \
    116 		-x "printf 'A\nB\nA\nC\nD\nC\n' | sed '/A/,/C/d'"
    117 }
    118 
    119 atf_test_case preserve_leading_ws_ia
    120 preserve_leading_ws_ia_head() {
    121 	atf_set "descr" "Test that sed(1) preserves leading whitespace " \
    122 			"in insert and append (PR bin/49872)"
    123 }
    124 
    125 preserve_leading_ws_ia_body() {
    126 	atf_check -o inline:"    1 2 3\n4 5 6\n    7 8 9\n\n" \
    127 		-x 'echo | sed -e "/^$/i\\
    128     1 2 3\\
    129 4 5 6\\
    130     7 8 9"'
    131 }
    132 
    133 atf_test_case escapes_in_subst
    134 escapes_in_subst_head() {
    135 	atf_set "descr" "Test that sed(1) expands \x \d \o escapes " \
    136 		"in substitution strings"
    137 }
    138 
    139 escapes_in_subst_body() {
    140 	# basic tests
    141 	atf_check -o inline:"fooXbar\n" \
    142 		-x 'echo "foo bar" | sed -e "s/ /\x58/"'
    143 	atf_check -o inline:"fooXbar\n" \
    144 		-x 'echo "foo bar" | sed -e "s/ /\o130/"'
    145 	atf_check -o inline:"fooXbar\n" \
    146 		-x 'echo "foo bar" | sed -e "s/ /\d88/"'
    147 
    148 	# overflowing the escaped char value
    149 	# atf_expect_fail "PR bin/59453: sed misparses \[dox]number escapes"
    150 	atf_check -o inline:"#8ball\n" \
    151 		  -x "echo | sed -e 's/^/\d358ball/'"
    152 
    153 	atf_check -o inline:"#3duh\n" \
    154 		  -x "echo | sed -e 's/^/\o0433duh/'"
    155 
    156 	atf_check -o inline:"#duh\n" \
    157 		  -x "echo | sed -e 's/^/\x23duh/'"
    158 
    159 	# check that escapes end after 2 or 3 chars
    160 	atf_check -o inline:"00000000  09 38 62 61 6c 6c 0a                              |.8ball.|\n" \
    161 		  -x "echo | sed -e 's/^/\d0098ball/' | hexdump -C | head -1"
    162 
    163 	atf_check -o inline:"00000000  07 37 62 61 6c 6c 0a                              |.7ball.|\n" \
    164 		  -x "echo | sed -e 's/^/\o0077ball/' | hexdump -C | head -1"
    165 
    166 	atf_check -o inline:"00000000  01 38 62 61 6c 6c 0a                              |.8ball.|\n" \
    167 		  -x "echo | sed -e 's/^/\x018ball/' | hexdump -C | head -1"
    168 }
    169 
    170 atf_test_case escapes_in_re
    171 escapes_in_re_head() {
    172 	atf_set "descr" "Test that sed(1) expands \x \d \o escapes " \
    173 		"in regex strings"
    174 }
    175 
    176 escapes_in_re_body() {
    177 	atf_check -o inline:"foo bar\n" \
    178 		-x 'echo "fooXbar" | sed -e "s/\x58/ /"'
    179 	atf_check -o inline:"foo bar\n" \
    180 		-x 'echo "fooXbar" | sed -e "s/\o130/ /"'
    181 	atf_check -o inline:"foo bar\n" \
    182 		-x 'echo "fooXbar" | sed -e "s/\d88/ /"'
    183 }
    184 
    185 atf_test_case escapes_in_re_bracket
    186 escapes_in_re_bracket_head() {
    187 	atf_set "descr" "Test that sed(1) does not expand \x \d \o escapes " \
    188 		"in regex strings inside braces"
    189 }
    190 
    191 escapes_in_re_bracket_body() {
    192 	atf_check -o inline:"foo    bar\n" \
    193 		-x 'echo "foo\\x58bar" | sed -e "s/[\x58]/ /g"'
    194 	atf_check -o inline:"f       bar\n" \
    195 		-x 'echo "fooo\\130bar" | sed -e "s/[\o130]/ /g"'
    196 	atf_check -o inline:"foo    bar\n" \
    197 		-x 'echo "foo\\d88bar" | sed -e "s/[\d88]/ /g"'
    198 }
    199 
    200 atf_test_case relative_addressing
    201 relative_addressing_head() {
    202 	atf_set "descr" "Test that sed(1) handles relative addressing " \
    203 		"properly (PR bin/49109)"
    204 }
    205 
    206 relative_addressing_body() {
    207 	atf_check -o match:"3" -x 'seq 1 4 | sed -n "1,+2p" | wc -l'
    208 }
    209 
    210 atf_init_test_cases() {
    211 	atf_add_test_case c2048
    212 	atf_add_test_case emptybackref
    213 	atf_add_test_case longlines
    214 	atf_add_test_case rangeselection
    215 	atf_add_test_case preserve_leading_ws_ia
    216 	atf_add_test_case escapes_in_subst
    217 	atf_add_test_case escapes_in_re
    218 	atf_add_test_case escapes_in_re_bracket
    219 	atf_add_test_case relative_addressing
    220 }
    221