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