155998Sbostic#!/bin/sh - 255998Sbostic# 355998Sbostic# Copyright (c) 1992 Diomidis Spinellis. 455998Sbostic# Copyright (c) 1992 The Regents of the University of California. 555998Sbostic# All rights reserved. 655998Sbostic# 755998Sbostic# %sccs.include.redist.sh% 855998Sbostic# 9*56006Sbostic# @(#)sed.test 5.2 (Berkeley) 08/24/92 1055998Sbostic# 1155998Sbostic 1255998Sbostic# sed Regression Tests 1355998Sbostic# 1455998Sbostic# The following files are created: 1555998Sbostic# lines[1-4], script1, script2 1655998Sbostic# Two directories *.out contain the test results 1755998Sbostic 1855998Sbosticmain() 1955998Sbostic{ 2055998Sbostic BASE=/usr/bin/sed 2155998Sbostic BASELOG=sed.out 2255998Sbostic TEST=../obj/sed 2355998Sbostic TESTLOG=nsed.out 2455998Sbostic DICT=/usr/share/dict/words 2555998Sbostic 2655998Sbostic awk 'END { for (i = 1; i < 15; i++) print "l1_" i}' </dev/null >lines1 2755998Sbostic awk 'END { for (i = 1; i < 10; i++) print "l2_" i}' </dev/null >lines2 2855998Sbostic 2955998Sbostic exec 4>&1 5>&2 3055998Sbostic 3155998Sbostic # Set these flags to get messages about known problems 3255998Sbostic BSD=1 3355998Sbostic GNU=0 3455998Sbostic SUN=0 3555998Sbostic tests $BASE $BASELOG 3655998Sbostic 3755998Sbostic BSD=0 3855998Sbostic GNU=0 3955998Sbostic SUN=0 4055998Sbostic tests $TEST $TESTLOG 4155998Sbostic exec 1>&4 2>&5 4255998Sbostic diff -c $BASELOG $TESTLOG | more 4355998Sbostic} 4455998Sbostic 4555998Sbostictests() 4655998Sbostic{ 4755998Sbostic SED=$1 4855998Sbostic DIR=$2 4955998Sbostic rm -rf $DIR 5055998Sbostic mkdir $DIR 5155998Sbostic MARK=100 5255998Sbostic 5355998Sbostic test_args 5455998Sbostic test_addr 5555998Sbostic echo Testing commands 5655998Sbostic test_group 57*56006Sbostic test_acid 5855998Sbostic test_branch 5955998Sbostic test_pattern 6055998Sbostic test_print 6155998Sbostic test_subst 6255998Sbostic} 6355998Sbostic 6455998Sbosticmark() 6555998Sbostic{ 6655998Sbostic MARK=`expr $MARK + 1` 6755998Sbostic exec 1>&4 2>&5 6855998Sbostic exec >"$DIR/${MARK}_$1" 6955998Sbostic echo "Test $1:$MARK" 7055998Sbostic # Uncomment this line to match tests with sed error messages 7155998Sbostic echo "Test $1:$MARK" > /dev/stderr 7255998Sbostic} 7355998Sbostic 7455998Sbostictest_args() 7555998Sbostic{ 7655998Sbostic mark '1.1' 7755998Sbostic echo Testing argument parsing 7855998Sbostic echo First type 7955998Sbostic if [ $SUN -eq 1 ] ; then 8055998Sbostic echo SunOS sed prints only with -n 8155998Sbostic else 8255998Sbostic $SED 's/^/e1_/p' lines1 8355998Sbostic fi 8455998Sbostic mark '1.2' ; $SED -n 's/^/e1_/p' lines1 8555998Sbostic mark '1.3' 8655998Sbostic if [ $SUN -eq 1 ] ; then 8755998Sbostic echo SunOS sed prints only with -n 8855998Sbostic else 8955998Sbostic $SED 's/^/e1_/p' <lines1 9055998Sbostic fi 9155998Sbostic mark '1.4' ; $SED -n 's/^/e1_/p' <lines1 9255998Sbostic echo Second type 9355998Sbostic mark '1.4.1' 9455998Sbostic if [ $SUN -eq 1 ] ; then 9555998Sbostic echo SunOS sed fails this 9655998Sbostic fi 9755998Sbostic $SED -e '' <lines1 9855998Sbostic echo 's/^/s1_/p' >script1 9955998Sbostic echo 's/^/s2_/p' >script2 10055998Sbostic mark '1.5' 10155998Sbostic if [ $SUN -eq 1 ] ; then 10255998Sbostic echo SunOS sed prints only with -n 10355998Sbostic else 10455998Sbostic $SED -f script1 lines1 10555998Sbostic fi 10655998Sbostic mark '1.6' 10755998Sbostic if [ $SUN -eq 1 ] ; then 10855998Sbostic echo SunOS sed prints only with -n 10955998Sbostic else 11055998Sbostic $SED -f script1 <lines1 11155998Sbostic fi 11255998Sbostic mark '1.7' 11355998Sbostic if [ $SUN -eq 1 ] ; then 11455998Sbostic echo SunOS sed prints only with -n 11555998Sbostic else 11655998Sbostic $SED -e 's/^/e1_/p' lines1 11755998Sbostic fi 11855998Sbostic mark '1.8' 11955998Sbostic if [ $SUN -eq 1 ] ; then 12055998Sbostic echo SunOS sed prints only with -n 12155998Sbostic else 12255998Sbostic $SED -e 's/^/e1_/p' <lines1 12355998Sbostic fi 12455998Sbostic mark '1.9' ; $SED -n -f script1 lines1 12555998Sbostic mark '1.10' ; $SED -n -f script1 <lines1 12655998Sbostic mark '1.11' ; $SED -n -e 's/^/e1_/p' lines1 12755998Sbostic mark '1.12' 12855998Sbostic if [ $SUN -eq 1 ] ; then 12955998Sbostic echo SunOS sed prints only with -n 13055998Sbostic else 13155998Sbostic $SED -n -e 's/^/e1_/p' <lines1 13255998Sbostic fi 13355998Sbostic mark '1.13' 13455998Sbostic if [ $SUN -eq 1 ] ; then 13555998Sbostic echo SunOS sed prints only with -n 13655998Sbostic else 13755998Sbostic $SED -e 's/^/e1_/p' -e 's/^/e2_/p' lines1 13855998Sbostic fi 13955998Sbostic mark '1.14' 14055998Sbostic if [ $SUN -eq 1 ] ; then 14155998Sbostic echo SunOS sed prints only with -n 14255998Sbostic else 14355998Sbostic $SED -f script1 -f script2 lines1 14455998Sbostic fi 14555998Sbostic mark '1.15' 14655998Sbostic if [ $GNU -eq 1 -o $SUN -eq 1 ] ; then 14755998Sbostic echo GNU and SunOS sed fail this following older POSIX draft 14855998Sbostic else 14955998Sbostic $SED -e 's/^/e1_/p' -f script1 lines1 15055998Sbostic fi 15155998Sbostic mark '1.16' 15255998Sbostic if [ $SUN -eq 1 ] ; then 15355998Sbostic echo SunOS sed prints only with -n 15455998Sbostic else 15555998Sbostic $SED -e 's/^/e1_/p' lines1 lines1 15655998Sbostic fi 15755998Sbostic # POSIX D11.2:11251 15855998Sbostic mark '1.17' ; $SED p <lines1 lines1 15955998Sbosticcat >script1 <<EOF 16055998Sbostic#n 16155998Sbostic# A comment 16255998Sbostic 16355998Sbosticp 16455998SbosticEOF 16555998Sbostic mark '1.18' ; $SED -f script1 <lines1 lines1 16655998Sbostic} 16755998Sbostic 16855998Sbostictest_addr() 16955998Sbostic{ 17055998Sbostic echo Testing address ranges 17155998Sbostic mark '2.1' ; $SED -n -e '4p' lines1 17255998Sbostic mark '2.2' ; $SED -n -e '20p' lines1 lines2 17355998Sbostic mark '2.3' ; $SED -n -e '$p' lines1 17455998Sbostic mark '2.4' ; $SED -n -e '$p' lines1 lines2 17555998Sbostic mark '2.5' ; $SED -n -e '$a\ 17655998Sbostichello' /dev/null 17755998Sbostic mark '2.6' ; $SED -n -e '$p' lines1 /dev/null lines2 17855998Sbostic # Should not print anything 17955998Sbostic mark '2.7' ; $SED -n -e '20p' lines1 18055998Sbostic mark '2.8' ; $SED -n -e '0p' lines1 18155998Sbostic mark '2.9' ; $SED -n '/l1_7/p' lines1 18255998Sbostic mark '2.10' ; $SED -n ' /l1_7/ p' lines1 18355998Sbostic mark '2.11' 18455998Sbostic if [ $BSD -eq 1 ] ; then 18555998Sbostic echo BSD sed fails this test 18655998Sbostic fi 18755998Sbostic if [ $GNU -eq 1 ] ; then 18855998Sbostic echo GNU sed fails this 18955998Sbostic fi 19055998Sbostic $SED -n '\_l1\_7_p' lines1 19155998Sbostic mark '2.12' ; $SED -n '1,4p' lines1 19255998Sbostic mark '2.13' ; $SED -n '1,$p' lines1 lines2 19355998Sbostic mark '2.14' ; $SED -n '1,/l2_9/p' lines1 lines2 19455998Sbostic mark '2.15' ; $SED -n '/4/,$p' lines1 lines2 19555998Sbostic mark '2.16' ; $SED -n '/4/,20p' lines1 lines2 19655998Sbostic mark '2.17' ; $SED -n '/4/,/10/p' lines1 lines2 19755998Sbostic mark '2.18' ; $SED -n '/l2_3/,/l1_8/p' lines1 lines2 19855998Sbostic mark '2.19' 19955998Sbostic if [ $GNU -eq 1 ] ; then 20055998Sbostic echo GNU sed fails this 20155998Sbostic fi 20255998Sbostic $SED -n '12,3p' lines1 lines2 20355998Sbostic mark '2.20' 20455998Sbostic if [ $GNU -eq 1 ] ; then 20555998Sbostic echo GNU sed fails this 20655998Sbostic fi 20755998Sbostic $SED -n '/l1_7/,3p' lines1 lines2 20855998Sbostic} 20955998Sbostic 21055998Sbostictest_group() 21155998Sbostic{ 21255998Sbostic echo Brace and other grouping 21355998Sbostic mark '3.1' ; $SED -e ' 21455998Sbostic4,12 { 21555998Sbostic s/^/^/ 21655998Sbostic s/$/$/ 21755998Sbostic s/_/T/ 21855998Sbostic}' lines1 21955998Sbostic mark '3.2' ; $SED -e ' 22055998Sbostic4,12 { 22155998Sbostic s/^/^/ 22255998Sbostic /6/,/10/ { 22355998Sbostic s/$/$/ 22455998Sbostic /8/ s/_/T/ 22555998Sbostic } 22655998Sbostic}' lines1 22755998Sbostic mark '3.3' ; $SED -e ' 22855998Sbostic4,12 !{ 22955998Sbostic s/^/^/ 23055998Sbostic /6/,/10/ !{ 23155998Sbostic s/$/$/ 23255998Sbostic /8/ !s/_/T/ 23355998Sbostic } 23455998Sbostic}' lines1 23555998Sbostic mark '3.4' ; $SED -e '4,12!s/^/^/' lines1 23655998Sbostic} 23755998Sbostic 238*56006Sbostictest_acid() 23955998Sbostic{ 240*56006Sbostic echo Testing a c d and i commands 24155998Sbostic mark '4.1' ; $SED -n -e ' 24255998Sbostics/^/before_i/p 24355998Sbostic20i\ 24455998Sbosticinserted 24555998Sbostics/^/after_i/p 24655998Sbostic' lines1 lines2 24755998Sbostic mark '4.2' ; $SED -n -e ' 24855998Sbostic5,12s/^/5-12/ 24955998Sbostics/^/before_a/p 25055998Sbostic/5-12/a\ 25155998Sbosticappended 25255998Sbostics/^/after_a/p 25355998Sbostic' lines1 lines2 25455998Sbostic mark '4.3' 25555998Sbostic if [ $GNU -eq 1 ] ; then 25655998Sbostic echo GNU sed fails this 25755998Sbostic fi 25855998Sbostic $SED -n -e ' 25955998Sbostics/^/^/p 26055998Sbostic/l1_/a\ 26155998Sbosticappended 26255998Sbostic8,10N 26355998Sbostics/$/$/p 26455998Sbostic' lines1 lines2 26555998Sbostic mark '4.4' ; $SED -n -e ' 26655998Sbosticc\ 26755998Sbostichello 26855998Sbostic' lines1 26955998Sbostic mark '4.5' ; $SED -n -e ' 27055998Sbostic8c\ 27155998Sbostichello 27255998Sbostic' lines1 27355998Sbostic mark '4.6' ; $SED -n -e ' 27455998Sbostic3,14c\ 27555998Sbostichello 27655998Sbostic' lines1 27755998Sbostic# SunOS and GNU sed behave differently. We follow POSIX 27855998Sbostic# mark '4.7' ; $SED -n -e ' 27955998Sbostic#8,3c\ 28055998Sbostic#hello 28155998Sbostic#' lines1 282*56006Sbostic mark '4.8' ; $SED d <lines1 28355998Sbostic} 28455998Sbostic 28555998Sbostictest_branch() 28655998Sbostic{ 28755998Sbostic echo Testing labels and branching 28855998Sbostic mark '5.1' ; $SED -n -e ' 28955998Sbosticb label4 29055998Sbostic:label3 29155998Sbostics/^/label3_/p 29255998Sbosticb end 29355998Sbostic:label4 29455998Sbostic2,12b label1 29555998Sbosticb label2 29655998Sbostic:label1 29755998Sbostics/^/label1_/p 29855998Sbosticb 29955998Sbostic:label2 30055998Sbostics/^/label2_/p 30155998Sbosticb label3 30255998Sbostic:end 30355998Sbostic' lines1 30455998Sbostic mark '5.2' 30555998Sbostic if [ $BSD -eq 1 ] ; then 30655998Sbostic echo BSD sed fails this test 30755998Sbostic fi 30855998Sbostic $SED -n -e ' 30955998Sbostics/l1_/l2_/ 31055998Sbostict ok 31155998Sbosticb 31255998Sbostic:ok 31355998Sbostics/^/tested /p 31455998Sbostic' lines1 lines2 31555998Sbostic# SunOS sed behaves differently here. Clarification needed. 31655998Sbostic# mark '5.3' ; $SED -n -e ' 31755998Sbostic#5,8b inside 31855998Sbostic#1,5 { 31955998Sbostic# s/^/^/p 32055998Sbostic# :inside 32155998Sbostic# s/$/$/p 32255998Sbostic#} 32355998Sbostic#' lines1 32455998Sbostic# Check that t clears the substitution done flag 32555998Sbostic mark '5.4' ; $SED -n -e ' 32655998Sbostic1,8s/^/^/ 32755998Sbostict l1 32855998Sbostic:l1 32955998Sbostict l2 33055998Sbostics/$/$/p 33155998Sbosticb 33255998Sbostic:l2 33355998Sbostics/^/ERROR/ 33455998Sbostic' lines1 33555998Sbostic# Check that reading a line clears the substitution done flag 33655998Sbostic mark '5.5' 33755998Sbostic if [ $BSD -eq 1 ] ; then 33855998Sbostic echo BSD sed fails this test 33955998Sbostic fi 34055998Sbostic $SED -n -e ' 34155998Sbostict l2 34255998Sbostic1,8s/^/^/p 34355998Sbostic2,7N 34455998Sbosticb 34555998Sbostic:l2 34655998Sbostics/^/ERROR/p 34755998Sbostic' lines1 34855998Sbostic mark '5.6' ; $SED 5q lines1 34955998Sbostic mark '5.7' ; $SED -e ' 35055998Sbostic5i\ 35155998Sbostichello 35255998Sbostic5q' lines1 35355998Sbostic} 35455998Sbostic 35555998Sbostictest_pattern() 35655998Sbostic{ 35755998Sbosticecho Pattern space commands 35855998Sbostic# Check that the pattern space is deleted 35955998Sbostic mark '6.1' ; $SED -n -e ' 36055998Sbosticc\ 36155998Sbosticchanged 36255998Sbosticp 36355998Sbostic' lines1 36455998Sbostic mark '6.2' ; $SED -n -e ' 36555998Sbostic4d 36655998Sbosticp 36755998Sbostic' lines1 36855998Sbostic# SunOS sed refused to print here 36955998Sbostic# mark '6.3' ; $SED -e ' 37055998Sbostic#N 37155998Sbostic#N 37255998Sbostic#N 37355998Sbostic#D 37455998Sbostic#P 37555998Sbostic#4p 37655998Sbostic#' lines1 37755998Sbostic mark '6.4' ; $SED -e ' 37855998Sbostic2h 37955998Sbostic3H 38055998Sbostic4g 38155998Sbostic5G 38255998Sbostic6x 38355998Sbostic6p 38455998Sbostic6x 38555998Sbostic6p 38655998Sbostic' lines1 38755998Sbostic mark '6.5' ; $SED -e '4n' lines1 38855998Sbostic mark '6.6' ; $SED -n -e '4n' lines1 38955998Sbostic} 39055998Sbostic 39155998Sbostictest_print() 39255998Sbostic{ 39355998Sbostic echo Testing print and file routines 39455998Sbostic awk 'END {for (i = 1; i < 256; i++) printf("%c", i);print "\n"}' \ 39555998Sbostic </dev/null >lines3 39655998Sbostic # GNU and SunOS sed behave differently here 39755998Sbostic mark '7.1' 39855998Sbostic if [ $BSD -eq 1 ] ; then 39955998Sbostic echo 'BSD sed drops core on this one; TEST SKIPPED' 40055998Sbostic else 40155998Sbostic $SED -n l lines3 40255998Sbostic fi 40355998Sbostic mark '7.2' ; $SED -e '/l2_/=' lines1 lines2 40455998Sbostic rm -f lines4 40555998Sbostic mark '7.3' ; $SED -e '3,12w lines4' lines1 40655998Sbostic echo w results 40755998Sbostic cat lines4 40855998Sbostic mark '7.4' ; $SED -e '4r lines2' lines1 40955998Sbostic mark '7.5' ; $SED -e '5r /dev/dds' lines1 41055998Sbostic mark '7.6' ; $SED -e '6r /dev/null' lines1 41155998Sbostic mark '7.7' 41255998Sbostic if [ $BSD -eq 1 -o $GNU -eq 1 -o $SUN -eq 1 ] ; then 41355998Sbostic echo BSD, GNU and SunOS cannot pass this one 41455998Sbostic else 41555998Sbostic sed '200q' $DICT | sed 's$.*$s/^/&/w tmpdir/&$' >script1 41655998Sbostic rm -rf tmpdir 41755998Sbostic mkdir tmpdir 41855998Sbostic $SED -f script1 lines1 41955998Sbostic cat tmpdir/* 42055998Sbostic rm -rf tmpdir 42155998Sbostic fi 42255998Sbostic mark '7.8' 42355998Sbostic if [ $BSD -eq 1 ] ; then 42455998Sbostic echo BSD sed cannot pass 7.7 42555998Sbostic else 42655998Sbostic echo line1 > lines3 42755998Sbostic echo "" >> lines3 42855998Sbostic $SED -n -e '$p' lines3 /dev/null 42955998Sbostic fi 43055998Sbostic 43155998Sbostic} 43255998Sbostic 43355998Sbostictest_subst() 43455998Sbostic{ 43555998Sbostic echo Testing substitution commands 43655998Sbostic mark '8.1' ; $SED -e 's/./X/g' lines1 43755998Sbostic mark '8.2' ; $SED -e 's,.,X,g' lines1 43855998Sbostic# GNU and SunOS sed thinks we are escaping . as wildcard, not as separator 43955998Sbostic# mark '8.3' ; $SED -e 's.\..X.g' lines1 44055998Sbostic# POSIX does not say that this should work 44155998Sbostic# mark '8.4' ; $SED -e 's/[/]/Q/' lines1 44255998Sbostic mark '8.4' ; $SED -e 's/[\/]/Q/' lines1 44355998Sbostic mark '8.5' ; $SED -e 's_\__X_' lines1 44455998Sbostic mark '8.6' ; $SED -e 's/./(&)/g' lines1 44555998Sbostic mark '8.7' ; $SED -e 's/./(\&)/g' lines1 44655998Sbostic mark '8.8' ; $SED -e 's/\(.\)\(.\)\(.\)/x\3x\2x\1/g' lines1 44755998Sbostic mark '8.9' ; $SED -e 's/_/u0\ 44855998Sbosticu1\ 44955998Sbosticu2/g' lines1 45055998Sbostic mark '8.10' 45155998Sbostic if [ $BSD -eq 1 -o $GNU -eq 1 ] ; then 45255998Sbostic echo 'BSD/GNU sed do not understand digit flags on s commands' 45355998Sbostic fi 45455998Sbostic $SED -e 's/./X/4' lines1 45555998Sbostic rm -f lines4 45655998Sbostic mark '8.11' ; $SED -e 's/1/X/w lines4' lines1 45755998Sbostic echo s wfile results 45855998Sbostic cat lines4 45955998Sbostic mark '8.12' ; $SED -e 's/[123]/X/g' lines1 46055998Sbostic mark '8.13' ; $SED -e 'y/0123456789/9876543210/' lines1 46155998Sbostic mark '8.14' ; 46255998Sbostic if [ $BSD -eq 1 -o $GNU -eq 1 -o $SUN -eq 1 ] ; then 46355998Sbostic echo BSD/GNU/SUN sed fail this test 46455998Sbostic else 46555998Sbostic $SED -e 'y10\123456789198765432\101' lines1 46655998Sbostic fi 46755998Sbostic mark '8.15' ; $SED -e '1N;2y/\n/X/' lines1 46855998Sbostic} 46955998Sbostic 47055998Sbosticmain 471