1#!/bin/echo Run scripts/test.sh 2 3#testing "name" "command" "result" "infile" "stdin" 4 5testcmd 'as cat' '""' "one\ntwo\nthree" "" "one\ntwo\nthree" 6# This segfaults ubuntu 12.04's sed. No really. 7testcmd '- - twice' '"" - -' "hello\n" "" "hello\n" 8testcmd '-n' '-n ""' "" "" "one\ntwo\nthree" 9testcmd '-n p' '-n p' "one\ntwo\nthree" "" "one\ntwo\nthree" 10testcmd 'explicit pattern' '-e p -n' "one\ntwo\nthree" "" "one\ntwo\nthree" 11 12# Exploring the wonders of sed addressing modes 13testcmd '' '-n 1p' "one\n" "" "one\ntwo\nthree" 14testcmd '' '2p' "one\ntwo\ntwo\nthree" "" "one\ntwo\nthree" 15testcmd '' '-n 2p' "two\n" "" "one\ntwo\nthree" 16testcmd '-n $p' '-n \$p' "three" "" "one\ntwo\nthree" 17testcmd 'as cat #2' "-n '1,\$p'" "one\ntwo\nthree" "" "one\ntwo\nthree" 18testcmd 'no input means no last line' "'\$a boing'" "" "" "" 19testcmd '-n $,$p' '-n \$,\$p' 'three' '' 'one\ntwo\nthree' 20testcmd '' '-n 1,2p' "one\ntwo\n" "" "one\ntwo\nthree" 21testcmd '' '-n 2,3p' "two\nthree" "" "one\ntwo\nthree" 22testcmd '' '-n 2,1p' "two\n" "" "one\ntwo\nthree" 23testcmd '$ with 2 inputs' '-n \$p - input' "four\n" "four\n" "one\ntwo\nthree" 24testcmd '' '-n /two/p' "two\n" "" "one\ntwo\nthree" 25testcmd '' '-n 1,/two/p' 'one\ntwo\n' '' 'one\ntwo\nthree' 26testcmd '' '-n /one/,2p' 'one\ntwo\n' '' 'one\ntwo\nthree' 27testcmd '' '-n 1,/one/p' 'one\ntwo\nthree' '' 'one\ntwo\nthree' 28testcmd '' '-n /one/,1p' 'one\n' '' 'one\ntwo\nthree' 29testcmd '-n /two/,$p' '-n /two/,\$p' 'two\nthree' '' 'one\ntwo\nthree' 30 31# Fun with newlines! 32testcmd '' '-n 3p' "three" "" "one\ntwo\nthree" 33testcmd 'prodigal newline' "-n '1,\$p' - input" \ 34 "one\ntwo\nthree\nfour\n" "four\n" "one\ntwo\nthree" 35testcmd 'Newline only added if further output' "-n 3p - input" "three" \ 36 "four\n" "one\ntwo\nthree" 37 38# Fun with match delimiters and escapes 39testcmd 'match \t tab' "-n '/\t/p'" "\tx\n" "" "\tx\n" 40testcmd 'match t delim disables \t tab' "-n '\t\txtp'" "" "" "\tx\n" 41testcmd 'match t delim makes \t literal t' "-n '\t\txtp'" "tx\n" "" "tx\n" 42testcmd 'match n delim' "-n '\n\txnp'" "\tx\n" "" "\tx\n" 43testcmd 'match n delim disables \n newline' "-n '\n\nxnp'" "" "" "\nx\n" 44toyonly testcmd 'match \n literal n' "-n '\n\nxnp'" "nx\n" "" "nx\n" 45testcmd 'end match does not check starting match line' \ 46 "-n '/two/,/two/p'" "two\nthree" "" "one\ntwo\nthree" 47testcmd 'end match/start match mixing number/letter' \ 48 "-n '2,/two/p'" "two\nthree" "" "one\ntwo\nthree" 49testcmd 'num then regex' '-n 2,/d/p' 'b\nc\nd\n' '' 'a\nb\nc\nd\ne\nf\n' 50testcmd 'regex then num' '-n /b/,4p' 'b\nc\nd\n' '' 'a\nb\nc\nd\ne\nf\n' 51testcmd 'multiple regex address match' '-n /on/,/off/p' \ 52 'bone\nturtle\scoff\ntron\nlurid\noffer\n' "" \ 53 'zap\nbone\nturtle\scoff\nfred\ntron\nlurid\noffer\nbecause\n' 54testcmd 'regex address overlap' '-n /on/,/off/p' "on\nzap\noffon\n" "" \ 55 'on\nzap\noffon\nping\noff\n' 56testcmd 'getdelim with nested [:blah:]' '-n "sa\a[a[:space:]bc]*aXXagp"' \ 57 "ABXXCDXXEFXXGHXXIXX" "" "ABaaCDa EFaa aGHa a Ia " 58testcmd '[ in [[]' "'s@[[]@X@g'" "X" "" "[" 59testcmd '[[] with ] as delimiter' "'s][[]]X]g'" "X" "" "[" 60testcmd '[[] with [ as delimiter' "'s[\[\[][X['" "X" "" "[" 61 62# gGhHlnNpPqrstwxy:= 63# s///#comment 64# abcdDi 65 66testcmd 'prodigaler newline' '-e a\\ -e woo' 'one\nwoo\n' '' 'one' 67testcmd "aci" "-e '3a boom' -e '/hre/i bang' -e '3a whack' -e '3c bong'" \ 68 "one\ntwo\nbang\nbong\nboom\nwhack\nfour\n" "" \ 69 "one\ntwo\nthree\nfour\n" 70testcmd "b loop" "':woo;=;b woo' | head -n 5" '1\n1\n1\n1\n1\n' "" "X" 71testcmd "b skip" "-n '2b zap;d;:zap;p'" "two\n" "" "one\ntwo\nthree" 72testcmd "b end" "-n '2b;p'" "one\nthree" "" "one\ntwo\nthree" 73testcmd "c range" "'2,4c blah'" "one\nblah\nfive\nsix" "" \ 74 "one\ntwo\nthree\nfour\nfive\nsix" 75testcmd "c {range}" "-e '2,4{c blah' -e '}'" "one\nblah\nblah\nblah\nfive\nsix"\ 76 "" "one\ntwo\nthree\nfour\nfive\nsix" 77testcmd "c multiple continuation" "-e 'c\\' -e 'two\\' -e ''" "two\n\n" "" \ 78 "hello" 79testcmd 'multiline continuations' '-e c\\ -e line1\\ -e line2' 'line1\nline2\n'\ 80 '' 'one\n' 81toyonly testing "c empty continuation" "sed -e 'c\\'" "\n" "" "hello" 82testing "D further processing depends on whether line is blank" \ 83 "sed -e '/one/,/three/{' -e 'i meep' -e'N;2D;}'" \ 84 "meep\nmeep\ntwo\nthree\n" "" "one\ntwo\nthree\n" 85testing 'newline staying away' 'sed s/o/x/' 'xne\ntwx' '' 'one\ntwo' 86 87# Why on _earth_ is this not an error? There's a \ with no continuation! 88#testing 'sed what, _really_?' 'sed -e a\\ && echo yes really' \ 89# 'one\nyes really\n' '' 'one\n' 90 91# all the s/// test 92 93testing "match empty line" "sed -e 's/^\$/@/'" "@\n" "" "\n" 94 95testing '\1' "sed 's/t\\(w\\)o/za\\1py/'" "one\nzawpy\nthree" "" \ 96 "one\ntwo\nthree" 97testing '\1 p' "sed 's/t\\(w\\)o/za\\1py/p'" "one\nzawpy\nzawpy\nthree" \ 98 "" "one\ntwo\nthree" 99testing '\1 no newline' "sed 's/t\\(w\\)o/za\\1py/'" "one\nzawpy" "" \ 100 "one\ntwo" 101testing '\1 p no newline' "sed 's/t\\(w\\)o/za\\1py/p'" \ 102 "one\nzawpy\nzawpy" "" "one\ntwo" 103testing '-n s//\1/p' "sed -n 's/t\\(w\\)o/za\\1py/p'" "zawpy" "" "one\ntwo" 104testing '-n s//\1/p no newline' "sed -n 's/t\\(w\\)o/za\\1py/p'" "zawpy" \ 105 "" "one\ntwo" 106testing 'backref error' \ 107 "sed 's/w/ale \2 ha/' >/dev/null 2>/dev/null || echo no" \ 108 "no\n" "" "one\ntwo\nthree" 109testing 'empty match after nonempty match' "sed -e 's/a*/c/g'" 'cbcncgc' \ 110 '' 'baaang' 111testing 'empty match' "sed -e 's/[^ac]*/A/g'" 'AaAcA' '' 'abcde' 112testing 's///#comment' "sed -e 's/TWO/four/i#comment'" "one\nfour\nthree" \ 113 "" "one\ntwo\nthree" 114testing 's///num off end' 'sed -e s/e//2' 'e\n' '' 'e\n' 115 116testing 'N flushes pending a and advances match counter' \ 117 "sed -e 'a woo' -e 'N;\$p'" 'woo\none\ntwo\none\ntwo' "" 'one\ntwo' 118testing "delimiter in regex [char range] doesn't count" "sed -e 's/[/]//'" \ 119 "onetwo\n" "" 'one/two\n' 120testing "delete regex range start line after trigger" \ 121 "sed -e '/one/,/three/{' -e 'i meep' -e '1D;}'" \ 122 "meep\nmeep\ntwo\nmeep\nthree" "" "one\ntwo\nthree" 123testing "blank pattern repeats last pattern" \ 124 "sed -e '/^three/s//abc&def/'" \ 125 "one two three\nabcthreedef four five\nfive six seven\n" "" \ 126 "one two three\nthree four five\nfive six seven\n" 127 128testcmd "interleave -e and -f" "-e 'a abc' -f input -e 'a ghi'" \ 129 "hello\nabc\ndef\nghi\n" "a def" "hello\n" 130 131# Different ways of parsing line continuations 132 133testing "" "sed -e '1a\' -e 'huh'" "meep\nhuh\n" "" "meep" 134testing "" "sed -f input" "blah\nboom\n" '1a\\\nboom' 'blah' 135testing "" "sed -f - input" "blah\nboom\n" 'blah' '1a\\\nboom' 136testing "" "sed '1a\ 137hello'" "merp\nhello\n" "" "merp" 138 139testing "" "sed -e '/x/c\' -e 'y'" 'y\n' '' 'x\n' 140testing "" "sed -e 's/a[([]*b/X/'" 'X' '' 'a[(b' 141toyonly testing "" "sed 'y/a\\bc/de\f/'" "db\f" "" "abc" 142testing "[a-a] (for perl)" "sed '"'s/\([^a-zA-Z0-9.:_\-\/]\)/\\\1/g'"'" \ 143 'he\ llo' "" "he llo" 144 145# Debian bug https://bugs.debian.org/635570 added code to ensure a file 146# ends with a newline via "sed -e '$a\'". Apparently all a\ with no additional 147# pattern lines after it does (other than making posix throw up) is 148# flush the pending newline as _if_ it had added another line. *shrug* Ok? 149testing "trailing a\ (for debian)" "sed 'a\\'" "hello\n" "" "hello" 150 151# You have to match the first line of a range in order to activate 152# the range, numeric and ascii work the same way 153toyonly testing "skip start of range" "sed -e n -e '1,2s/b/c/'" "a\nb\n" "" "a\nb\n" 154testing "range +1" "sed -ne '/blah/,+1p'" "blah\n6\n" "" \ 155 "1\n2\n3\n4\n5\nblah\n6\n7\n8\n9\n" 156testing "range +0" "sed -ne '/blah/,+0p'" "blah\n" "" \ 157 "1\n2\n3\n4\n5\nblah\n6\n7\n8\n9\n" 158testing "range +3" "sed -ne '2,+3p'" "2\n3\n4\n5\n" "" \ 159 "1\n2\n3\n4\n5\nblah\n6\n7\n8\n9\n" 160 161testing "not -s" "sed -n 1p input -" "one" "one" "two" 162testing "-s" "sed -sn 1p input -" "one\ntwo" "one\n" "two" 163 164testing "bonus backslashes" \ 165 "sed -e 'a \l \x\' -e \"\$(echo -e 'ab\\\nc')\"" \ 166 "hello\nl x\nab\nc\n" "" "hello\n" 167 168# toybox saying "no }" here broke the AOSP build. 169testing "end b with }" "sed -n '/START/{:a;n;/END/q;p;ba}'" "b\nc\n" \ 170 "" "a\nSTART\nb\nc\nEND\nd" 171 172testcmd '-z' '-z "s/\n/-/g"' "a-b-c" "" "a\nb\nc" 173testcmd '-z N' '-z N' 'one\0two\0' '' 'one\0two\0' 174testcmd 'p noeol' '-z p' 'one\0one' '' 'one' 175testcmd '-z N noeol' '-z N' 'one\0two' '' 'one\0two' 176testcmd '-z S' "-z 'N;P'" 'one\0one\0two' '' 'one\0two' 177testcmd '-z D' "-z 'N;D'" 'two' '' 'one\0two' 178testcmd '-z G' "-z 'h;G'" 'one\0one' '' 'one' 179testcmd '-z H' "-z 'H;g'" '\0one' '' 'one' 180toyonly testcmd '-z x NOEOL' '-z ax' 'abc\0x\0def\0x\0' '' 'abc\0def' 181testcmd 's after NUL' 's/t/x/' 'one\0xwo' '' 'one\0two' 182testcmd '^ not trigger after NUL' 's/^t/x/' 'one\0two' '' 'one\0two' 183 184# toybox handling of empty capturing groups broke minjail. Check that we 185# correctly replace an empty capturing group with the empty string: 186testcmd '\n with empty capture' \ 187 '-E "s/(ARM_)?(NR_)([a-z]*) (.*)/\1\2\3/"' "NR_read" "" "NR_read foo" 188# ...but also that we report an error for a backreference to a group that 189# isn't in the pattern: 190testcmd '\n too high' '-E "s/(.*)/\2/p" 2>/dev/null || echo OK' "OK\n" "" "foo" 191 192toyonly testcmd 's///x' '"s/(hello )?(world)/\2/x"' "world" "" "hello world" 193 194# Performance test 195X=x; Y=20; while [ $Y -gt 0 ]; do X=$X$X; Y=$(($Y-1)); done 196testing 'megabyte s/x/y/g (20 sec timeout)' \ 197 "timeout 20 sed 's/x/y/g' | sha1sum" \ 198 '138c1fa7c3f64186203b0192fb4abdb33cb4e98a -\n' '' "$X\n" 199unset X Y 200 201testcmd "w doesn't blank" "-e 'w one' -e 'w one' -e d; cat one" \ 202 'hello\nhello\n' '' 'hello\n' 203 204testcmd 's i and I' 's/o/0/ig' "f00l F00L" "" "fool FOOL" 205 206testcmd 's l ignores posix' "-n 'N;l'" 'one\\ntwo$\n' '' 'one\ntwo\n' 207testcmd 's l loses missing newline' "-n 'N;l'" 'one\\ntwo$\n' '' 'one\ntwo' 208testcmd 's -z l' "-zn 'N;l'" 'one\\000two$\0' '' 'one\0two\0' 209testcmd 's -z l missing newline' "-zn 'N;l'" 'one\\000two$\0' '' 'one\0two' 210 211testcmd 'count match' '"s/./&X/4"' '0123X45\n' '' '012345\n' 212 213# -i with $ last line test 214 215#echo meep | sed/sed -e '1a\' -e 'huh' 216#echo blah | sed/sed -f <(echo -e "1a\\\\\nboom") 217#echo merp | sed/sed "1a\\ 218#hello" 219