ntpd: fix build failure if !NTPD_SERVER. Closes 4994
[oweals/busybox.git] / testsuite / sed.tests
index a9d9ada311e2e1b0f4d5d4d10feedcba4034ecbd..ba163e9e92ecad29f2b341b4386213b923484d82 100755 (executable)
@@ -2,11 +2,11 @@
 
 # SUSv3 compliant sed tests.
 # Copyright 2005 by Rob Landley <rob@landley.net>
-# Licensed under GPL v2, see file LICENSE for details.
+# Licensed under GPLv2, see file LICENSE in this source tree.
 
-. testing.sh
+. ./testing.sh
 
-# testing "description" "arguments" "result" "infile" "stdin"
+# testing "description" "commands" "result" "infile" "stdin"
 
 # Corner cases
 testing "sed no files (stdin)" 'sed ""' "hello\n" "" "hello\n"
@@ -80,10 +80,18 @@ test x"$SKIP_KNOWN_BUGS" = x"" && {
 # Query: how does this interact with no newline at EOF?
 testing "sed n (flushes pattern space, terminates early)" "sed -e 'n;p'" \
        "a\nb\nb\nc\n" "" "a\nb\nc\n"
-# N does _not_ flush pattern space, therefore c is still in there @ script end.
-testing "sed N (doesn't flush pattern space when terminating)" "sed -e 'N;p'" \
-       "a\nb\na\nb\nc\n" "" "a\nb\nc\n"
 }
+# non-GNU sed: N does _not_ flush pattern space, therefore c is eaten @ script end
+# GNU sed: N flushes pattern space, therefore c is printed too @ script end
+testing "sed N (flushes pattern space (GNU behavior))" "sed -e 'N;p'" \
+       "a\nb\na\nb\nc\n" "" "a\nb\nc\n"
+
+testing "sed N test2" "sed ':a;N;s/\n/ /;ta'" \
+       "a b c\n" "" "a\nb\nc\n"
+
+testing "sed N test3" "sed 'N;s/\n/ /'" \
+       "a b\nc\n" "" "a\nb\nc\n"
+
 testing "sed address match newline" 'sed "/b/N;/b\\nc/i woo"' \
        "a\nwoo\nb\nc\nd\n" "" "a\nb\nc\nd\n"
 
@@ -110,7 +118,7 @@ testing "sed embedded NUL" "sed -e 's/woo/bang/'" "\0bang\0woo\0" "" \
 testing "sed embedded NUL g" "sed -e 's/woo/bang/g'" "bang\0bang\0" "" \
        "woo\0woo\0"
 test x"$SKIP_KNOWN_BUGS" = x"" && {
-echo -e "/woo/a he\0llo" > sed.commands
+$ECHO -e "/woo/a he\0llo" > sed.commands
 testing "sed NUL in command" "sed -f sed.commands" "woo\nhe\0llo\n" "" "woo"
 rm sed.commands
 }
@@ -153,7 +161,7 @@ testing "sed clusternewline" \
        "one\none\n111\n222\ntwo\ntwo" "one" "two"
 }
 testing "sed subst+write" \
-       "sed -e 's/i/z/' -e 'woutputw' input -; echo -n X; cat outputw" \
+       "sed -e 's/i/z/' -e 'woutputw' input -; $ECHO -n X; cat outputw" \
        "thzngy\nagaznXthzngy\nagazn" "thingy" "again"
 rm outputw
 testing "sed trailing NUL" \
@@ -217,7 +225,7 @@ testing "sed s/xxx/[/" "sed -e 's/xxx/[/'" "[\n" "" "xxx\n"
 #testing "sed -g (exhaustive)" "sed -e 's/[[:space:]]*/,/g'" ",1,2,3,4,5," \
 #      "" "12345"
 
-# testing "description" "arguments" "result" "infile" "stdin"
+# testing "description" "commands" "result" "infile" "stdin"
 
 testing "sed n command must reset 'substituted' bit" \
        "sed 's/1/x/;T;n;: next;s/3/y/;t quit;n;b next;: quit;q'" \
@@ -241,4 +249,52 @@ testing "sed 2d;2,1p (gnu compat)" \
        "third\n" "" \
        "first\nsecond\nthird\nfourth\n"
 
+# Regex means: "match / at BOL or nothing, then one or more not-slashes".
+# The bug was that second slash in /usr/lib was treated as "at BOL" too.
+testing "sed beginning (^) matches only once" \
+       "sed 's,\(^/\|\)[^/][^/]*,>\0<,g'" \
+       ">/usr</>lib<\n" "" \
+       "/usr/lib\n"
+
+testing "sed c" \
+       "sed 'crepl'" \
+       "repl\nrepl\n" "" \
+       "first\nsecond\n"
+
+testing "sed nested {}s" \
+       "sed '/asd/ { p; /s/ { s/s/c/ }; p; q }'" \
+       "qwe\nasd\nacd\nacd\n" "" \
+       "qwe\nasd\nzxc\n"
+
+testing "sed a cmd ended by double backslash" \
+       "sed -e '/| one /a \\
+       | three \\\\' -e '/| one-/a \\
+       | three-* \\\\'" \
+'      | one \\
+       | three \\
+       | two \\
+' '' \
+'      | one \\
+       | two \\
+'
+
+# first three lines are deleted; 4th line is matched and printed by "2,3" and by "4" ranges
+testing "sed with N skipping lines past ranges on next cmds" \
+       "sed -n '1{N;N;d};1p;2,3p;3p;4p'" \
+       "4\n4\n" "" "1\n2\n3\n4\n"
+
+testing "sed -i with address modifies all files, not only first" \
+       "cp input input2; sed -i -e '1s/foo/bar/' input input2 && cat input input2; rm input2" \
+       "bar\nbar\n" "foo\n" ""
+
+testing "sed understands \r" \
+       "sed 's/r/\r/'" \
+       "\rrr\n" "" "rrr\n"
+
+testing "sed -i finishes ranges correctly" \
+       "sed '1,2d' -i input; echo \$?; cat input" \
+       "0\n3\n4\n" "1\n2\n3\n4\n" ""
+
+# testing "description" "commands" "result" "infile" "stdin"
+
 exit $FAILCOUNT