#!/bin/sh - # $OpenBSD: sedtest.sh,v 1.5 2010/07/03 02:34:36 phessler Exp $ # # Copyright (c) 1992 Diomidis Spinellis. # Copyright (c) 1992, 1993 # The Regents of the University of California. All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # 3. Neither the name of the University nor the names of its contributors # may be used to endorse or promote products derived from this software # without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF # SUCH DAMAGE. # # from: @(#)sed.test 8.1 (Berkeley) 6/6/93 # # sed Regression Tests # # The following files are created: # lines[1-4], script1, script2 # Test results are stored in sed.out main() { TEST=${1-sed} TESTLOG=${2-sed.out} DICT=${3-/usr/share/dict/words} ulimit -n 256 rm -f lines1 lines2 i=1 while [ $i -lt 15 ]; do echo "l1_$i" >> lines1 if [ $i -lt 10 ]; then echo "l2_$i" >> lines2 fi i=$((i + 1)) done tests $TEST $TESTLOG } tests() { SED=$1 LOG=$2 rm -f $LOG MARK=100 exec 3>&0 4>&1 5>&2 exec 0/dev/null 2>/dev/null test_error exec 0>&3 1>&4 2>&5 exec 4>&1 5>&2 test_args test_addr echo Testing commands test_group test_acid test_branch test_pattern test_print test_subst exec 1>&4 2>&5 } mark() { exec 2>&1 >>$LOG test $MARK -ne 100 && echo "" MARK=$((MARK + 1)) echo "Test $1:$MARK" | sed 's/./=/g' echo "Test $1:$MARK" echo "Test $1:$MARK" | sed 's/./=/g' } test_args() { mark '1.1' echo Testing argument parsing echo First type $SED 's/^/e1_/p' lines1 mark '1.2' ; $SED -n 's/^/e1_/p' lines1 mark '1.3' $SED 's/^/e1_/p' script1 echo 's/^/s2_/p' >script2 mark '1.5' $SED -f script1 lines1 mark '1.6' $SED -f script1 script1 <lines3 # GNU and SunOS sed behave differently here mark '7.1' $SED -n l lines3 mark '7.2' ; $SED -e '/l2_/=' lines1 lines2 rm -f lines4 mark '7.3' ; $SED -e '3,12w lines4' lines1 echo w results cat lines4 mark '7.4' ; $SED -e '4r lines2' lines1 mark '7.5' ; $SED -e '5r /dev/dds' lines1 mark '7.6' ; $SED -e '6r /dev/null' lines1 mark '7.7' sed '200q' $DICT | sed 's$.*$s/^/&/w tmpdir/&$' >script1 rm -rf tmpdir mkdir tmpdir $SED -f script1 lines1 cat tmpdir/* rm -rf tmpdir mark '7.8' echo line1 > lines3 echo "" >> lines3 $SED -n -e '$p' lines3 /dev/null } test_subst() { echo Testing substitution commands mark '8.1' ; $SED -e 's/./X/g' lines1 mark '8.2' ; $SED -e 's,.,X,g' lines1 # GNU and SunOS sed thinks we are escaping . as wildcard, not as separator # mark '8.3' ; $SED -e 's.\..X.g' lines1 # POSIX does not say that this should work # mark '8.4' ; $SED -e 's/[/]/Q/' lines1 mark '8.4' ; $SED -e 's/[\/]/Q/' lines1 mark '8.5' ; $SED -e 's_\__X_' lines1 mark '8.6' ; $SED -e 's/./(&)/g' lines1 mark '8.7' ; $SED -e 's/./(\&)/g' lines1 mark '8.8' ; $SED -e 's/\(.\)\(.\)\(.\)/x\3x\2x\1/g' lines1 mark '8.9' ; $SED -e 's/_/u0\ u1\ u2/g' lines1 mark '8.10' $SED -e 's/./X/4' lines1 rm -f lines4 mark '8.11' ; $SED -e 's/1/X/w lines4' lines1 echo s wfile results cat lines4 mark '8.12' ; $SED -e 's/[123]/X/g' lines1 mark '8.13' ; $SED -e 'y/0123456789/9876543210/' lines1 mark '8.14' ; $SED -e 'y10\123456789198765432\101' lines1 mark '8.15' ; $SED -e '1N;2y/\n/X/' lines1 mark '8.16' echo 'eeefff' | $SED -e 'p' -e 's/e/X/p' -e ':x' -e 's//Y/p' -e '/f/bx' echo '[ as an s delimiter and its escapes' mark '8.17' ; $SED -e 's[_[X[' lines1 # This is a matter of interpretation # POSIX 1003.1, 2004 says "Within the BRE and the replacement, # the BRE delimiter itself can be used as a *literal* character # if it is preceded by a backslash mark '8.18' ; sed 's/l/[/' lines1 | $SED -e 's[\[.[X[' mark '8.19' ; sed 's/l/[/' lines1 | $SED -e 's[\[.[X\[[' echo '\ in y command' mark '8.20' printf 'a\\b(c\n' | $SED 'y%ABCDEFGHIJKLMNOPQRSTUVWXYZ, /\\()"%abcdefghijklmnopqrstuvwxyz,------%' } test_error() { set -x $SED -x && exit 1 $SED -f && exit 1 $SED -e && exit 1 $SED -f /dev/dds && exit 1 $SED p /dev/dds && exit 1 $SED -f /bin/sh && exit 1 $SED '{' && exit 1 $SED '{' && exit 1 $SED '/hello/' && exit 1 $SED '1,/hello/' && exit 1 $SED -e '-5p' && exit 1 $SED '/jj' && exit 1 $SED 'a hello' && exit 1 $SED 'a \ hello' && exit 1 $SED 'b foo' && exit 1 $SED 'd hello' && exit 1 $SED 's/aa' && exit 1 $SED 's/aa/' && exit 1 $SED 's/a/b' && exit 1 $SED 's/a/b/c/d' && exit 1 $SED 's/a/b/ 1 2' && exit 1 $SED 's/a/b/ 1 g' && exit 1 $SED 's/a/b/w' && exit 1 $SED 'y/aa' && exit 1 $SED 'y/aa/b/' && exit 1 $SED 'y/aa/' && exit 1 $SED 'y/a/b' && exit 1 $SED 'y/a/b/c/d' && exit 1 $SED '!' && exit 1 $SED supercalifrangolisticexprialidociussupercalifrangolisticexcius set +x } main "$@"