1#!/bin/sh - 2# $OpenBSD: sedtest.sh,v 1.8 2018/12/07 15:30:31 schwarze Exp $ 3# 4# Copyright (c) 1992 Diomidis Spinellis. 5# Copyright (c) 1992, 1993 6# The Regents of the University of California. All rights reserved. 7# 8# Redistribution and use in source and binary forms, with or without 9# modification, are permitted provided that the following conditions 10# are met: 11# 1. Redistributions of source code must retain the above copyright 12# notice, this list of conditions and the following disclaimer. 13# 2. Redistributions in binary form must reproduce the above copyright 14# notice, this list of conditions and the following disclaimer in the 15# documentation and/or other materials provided with the distribution. 16# 3. Neither the name of the University nor the names of its contributors 17# may be used to endorse or promote products derived from this software 18# without specific prior written permission. 19# 20# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23# ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30# SUCH DAMAGE. 31# 32# from: @(#)sed.test 8.1 (Berkeley) 6/6/93 33# 34 35# sed Regression Tests 36# 37# The following files are created: 38# lines[1-4], script1, script2 39# Test results are stored in sed.out 40 41main() 42{ 43 TEST=${1-sed} 44 TESTLOG=${2-sed.out} 45 DICT=${3-/usr/share/dict/words} 46 47 ulimit -n 256 48 rm -f lines1 lines2 49 i=1 50 while [ $i -lt 15 ]; do 51 echo "l1_$i" >> lines1 52 if [ $i -lt 10 ]; then 53 echo "l2_$i" >> lines2 54 fi 55 i=$((i + 1)) 56 done 57 58 tests $TEST $TESTLOG 59} 60 61tests() 62{ 63 SED=$1 64 LOG=$2 65 rm -f $LOG 66 MARK=100 67 68 exec 3>&0 4>&1 5>&2 69 exec 0</dev/null 1>/dev/null 2>/dev/null 70 test_error 71 exec 0>&3 1>&4 2>&5 72 73 exec 4>&1 5>&2 74 test_args 75 test_addr 76 echo Testing commands 77 test_group 78 test_acid 79 test_branch 80 test_pattern 81 test_print 82 test_subst 83 exec 1>&4 2>&5 84} 85 86mark() 87{ 88 exec 2>&1 >>$LOG 89 test $MARK -ne 100 && echo "" 90 MARK=$((MARK + 1)) 91 echo "Test $1:$MARK" | sed 's/./=/g' 92 echo "Test $1:$MARK" 93 echo "Test $1:$MARK" | sed 's/./=/g' 94} 95 96test_args() 97{ 98 mark '1.1' 99 echo Testing argument parsing 100 echo First type 101 $SED 's/^/e1_/p' lines1 102 mark '1.2' ; $SED -n 's/^/e1_/p' lines1 103 mark '1.3' 104 $SED 's/^/e1_/p' <lines1 105 mark '1.4' ; $SED -n 's/^/e1_/p' <lines1 106 echo Second type 107 mark '1.4.1' 108 $SED -e '' <lines1 109 echo 's/^/s1_/p' >script1 110 echo 's/^/s2_/p' >script2 111 mark '1.5' 112 $SED -f script1 lines1 113 mark '1.6' 114 $SED -f script1 <lines1 115 mark '1.7' 116 $SED -e 's/^/e1_/p' lines1 117 mark '1.8' 118 $SED -e 's/^/e1_/p' <lines1 119 mark '1.9' ; $SED -n -f script1 lines1 120 mark '1.10' ; $SED -n -f script1 <lines1 121 mark '1.11' ; $SED -n -e 's/^/e1_/p' lines1 122 mark '1.12' 123 $SED -n -e 's/^/e1_/p' <lines1 124 mark '1.13' 125 $SED -e 's/^/e1_/p' -e 's/^/e2_/p' lines1 126 mark '1.14' 127 $SED -f script1 -f script2 lines1 128 mark '1.15' 129 $SED -e 's/^/e1_/p' -f script1 lines1 130 mark '1.16' 131 $SED -e 's/^/e1_/p' lines1 lines1 132 # POSIX D11.2:11251 133 mark '1.17' ; $SED p <lines1 lines1 134cat >script1 <<EOF 135#n 136# A comment 137 138p 139EOF 140 mark '1.18' ; $SED -f script1 <lines1 lines1 141} 142 143test_addr() 144{ 145 echo Testing address ranges 146 mark '2.1' ; $SED -n -e '4p' lines1 147 mark '2.2' ; $SED -n -e '20p' lines1 lines2 148 mark '2.3' ; $SED -n -e '$p' lines1 149 mark '2.4' ; $SED -n -e '$p' lines1 lines2 150 mark '2.5' ; $SED -n -e '$a\ 151hello' /dev/null 152 mark '2.6' ; $SED -n -e '$p' lines1 /dev/null lines2 153 # Should not print anything 154 mark '2.7' ; $SED -n -e '20p' lines1 155 mark '2.8' ; $SED -n -e '0p' lines1 156 mark '2.9' ; $SED -n '/l1_7/p' lines1 157 mark '2.10' ; $SED -n ' /l1_7/ p' lines1 158 mark '2.11' 159 $SED -n '\_l1\_7_p' lines1 160 mark '2.12' ; $SED -n '1,4p' lines1 161 mark '2.13' ; $SED -n '1,$p' lines1 lines2 162 mark '2.14' ; $SED -n '1,/l2_9/p' lines1 lines2 163 mark '2.15' ; $SED -n '/4/,$p' lines1 lines2 164 mark '2.16' ; $SED -n '/4/,20p' lines1 lines2 165 mark '2.17' ; $SED -n '/4/,/10/p' lines1 lines2 166 mark '2.18' ; $SED -n '/l2_3/,/l1_8/p' lines1 lines2 167 mark '2.19' 168 $SED -n '12,3p' lines1 lines2 169 mark '2.20' 170 $SED -n '/l1_7/,3p' lines1 lines2 171} 172 173test_group() 174{ 175 echo Brace and other grouping 176 mark '3.1' ; $SED -e ' 1774,12 { 178 s/^/^/ 179 s/$/$/ 180 s/_/T/ 181}' lines1 182 mark '3.2' ; $SED -e ' 1834,12 { 184 s/^/^/ 185 /6/,/10/ { 186 s/$/$/ 187 /8/ s/_/T/ 188 } 189}' lines1 190 mark '3.3' ; $SED -e ' 1914,12 !{ 192 s/^/^/ 193 /6/,/10/ !{ 194 s/$/$/ 195 /8/ !s/_/T/ 196 } 197}' lines1 198 mark '3.4' ; $SED -e '4,12!s/^/^/' lines1 199} 200 201test_acid() 202{ 203 echo Testing a c d and i commands 204 mark '4.1' ; $SED -n -e ' 205s/^/before_i/p 20620i\ 207inserted 208s/^/after_i/p 209' lines1 lines2 210 mark '4.2' ; $SED -n -e ' 2115,12s/^/5-12/ 212s/^/before_a/p 213/5-12/a\ 214appended 215s/^/after_a/p 216' lines1 lines2 217 mark '4.3' 218 $SED -n -e ' 219s/^/^/p 220/l1_/a\ 221appended 2228,10N 223s/$/$/p 224' lines1 lines2 225 mark '4.4' ; $SED -n -e ' 226c\ 227hello 228' lines1 229 mark '4.5' ; $SED -n -e ' 2308c\ 231hello 232' lines1 233 mark '4.6' ; $SED -n -e ' 2343,14c\ 235hello 236' lines1 237# SunOS and GNU sed behave differently. We follow POSIX 238# mark '4.7' ; $SED -n -e ' 239#8,3c\ 240#hello 241#' lines1 242 mark '4.8' ; $SED d <lines1 243} 244 245test_branch() 246{ 247 echo Testing labels and branching 248 mark '5.1' ; $SED -n -e ' 249b label4 250:label3 251s/^/label3_/p 252b end 253:label4 2542,12b label1 255b label2 256:label1 257s/^/label1_/p 258b 259:label2 260s/^/label2_/p 261b label3 262:end 263' lines1 264 mark '5.2' 265 $SED -n -e ' 266s/l1_/l2_/ 267t ok 268b 269:ok 270s/^/tested /p 271' lines1 lines2 272# SunOS sed behaves differently here. Clarification needed. 273# mark '5.3' ; $SED -n -e ' 274#5,8b inside 275#1,5 { 276# s/^/^/p 277# :inside 278# s/$/$/p 279#} 280#' lines1 281# Check that t clears the substitution done flag 282 mark '5.4' ; $SED -n -e ' 2831,8s/^/^/ 284t l1 285:l1 286t l2 287s/$/$/p 288b 289:l2 290s/^/ERROR/ 291' lines1 292# Check that reading a line clears the substitution done flag 293 mark '5.5' 294 $SED -n -e ' 295t l2 2961,8s/^/^/p 2972,7N 298b 299:l2 300s/^/ERROR/p 301' lines1 302 mark '5.6' ; $SED 5q lines1 303 mark '5.7' ; $SED -e ' 3045i\ 305hello 3065q' lines1 307# Branch across block boundary 308 mark '5.8' ; $SED -e ' 309{ 310:b 311} 312s/l/m/ 313tb' lines1 314# Check that branch commands can be followed by a semicolon and another 315# command. POSIX does not require that this works, but GNU sed allows it. 316# Since POSIX does not require that label names can contain semicolons, 317# the extension is viable. 318 mark '5.9' ; echo "A\nB" | $SED '1b;=' 319 mark '5.10' ; echo "A\nB" | $SED 's/A/C/;t;=' 320 mark '5.11' ; echo "A\nB" | $SED '1bL;=;:L' 321} 322 323test_pattern() 324{ 325echo Pattern space commands 326# Check that the pattern space is deleted 327 mark '6.1' ; $SED -n -e ' 328c\ 329changed 330p 331' lines1 332 mark '6.2' ; $SED -n -e ' 3334d 334p 335' lines1 336# SunOS sed refused to print here 337# mark '6.3' ; $SED -e ' 338#N 339#N 340#N 341#D 342#P 343#4p 344#' lines1 345 mark '6.4' ; $SED -e ' 3462h 3473H 3484g 3495G 3506x 3516p 3526x 3536p 354' lines1 355 mark '6.5' ; $SED -e '4n' lines1 356 mark '6.6' ; $SED -n -e '4n' lines1 357} 358 359test_print() 360{ 361 echo Testing print and file routines 362 awk 'END {for (i = 1; i < 256; i++) printf("%c", i);print "\n"}' \ 363 </dev/null >lines3 364 # GNU and SunOS sed behave differently here 365 mark '7.1' 366 $SED -n l lines3 367 mark '7.2' ; $SED -e '/l2_/=' lines1 lines2 368 rm -f lines4 369 mark '7.3' ; $SED -e '3,12w lines4' lines1 370 echo w results 371 cat lines4 372 mark '7.4' ; $SED -e '4r lines2' lines1 373 mark '7.5' ; $SED -e '5r /dev/dds' lines1 374 mark '7.6' ; $SED -e '6r /dev/null' lines1 375 mark '7.7' 376 sed '200q' $DICT | sed 's$.*$s/^/&/w tmpdir/&$' >script1 377 rm -rf tmpdir 378 mkdir tmpdir 379 $SED -f script1 lines1 380 cat tmpdir/* 381 rm -rf tmpdir 382 mark '7.8' 383 echo line1 > lines3 384 echo "" >> lines3 385 $SED -n -e '$p' lines3 /dev/null 386} 387 388test_subst() 389{ 390 echo Testing substitution commands 391 mark '8.1' ; $SED -e 's/./X/g' lines1 392 mark '8.2' ; $SED -e 's,.,X,g' lines1 393# GNU and SunOS sed thinks we are escaping . as wildcard, not as separator 394# mark '8.3' ; $SED -e 's.\..X.g' lines1 395# POSIX does not say that this should work 396# mark '8.4' ; $SED -e 's/[/]/Q/' lines1 397 mark '8.4' ; $SED -e 's/[\/]/Q/' lines1 398 mark '8.5' ; $SED -e 's_\__X_' lines1 399 mark '8.6' ; $SED -e 's/./(&)/g' lines1 400 mark '8.7' ; $SED -e 's/./(\&)/g' lines1 401 mark '8.8' ; $SED -e 's/\(.\)\(.\)\(.\)/x\3x\2x\1/g' lines1 402 mark '8.9' ; $SED -e 's/_/u0\ 403u1\ 404u2/g' lines1 405 mark '8.10' 406 $SED -e 's/./X/4' lines1 407 rm -f lines4 408 mark '8.11' ; $SED -e 's/1/X/w lines4' lines1 409 echo s wfile results 410 cat lines4 411 mark '8.12' ; $SED -e 's/[123]/X/g' lines1 412 mark '8.13' ; $SED -e 'y/0123456789/9876543210/' lines1 413 mark '8.14' ; 414 $SED -e 'y10\123456789198765432\101' lines1 415 mark '8.15' ; $SED -e '1N;2y/\n/X/' lines1 416 mark '8.16' 417 echo 'eeefff' | $SED -e 'p' -e 's/e/X/p' -e ':x' -e 's//Y/p' -e '/f/bx' 418 echo 'various special characters as delimiters' 419# POSIX says "Within the BRE and the replacement, the BRE delimiter itself 420# can be used as a literal character if it is preceded by a <backslash>". 421# That is an ambiguous statement. We interpret it in the sense that the 422# character is passed on literally to the RE engine, without the backslash. 423 mark '8.17' ; $SED -e 's[_[X[' lines1 424 mark '8.18' ; $SED -e 's$1\$$\$R$' lines1 425 mark '8.19' ; $SED -Ee 's(\(3|5)(\(\1)(' lines1 426 mark '8.20' ; $SED -e 's*_1\**\*R*' lines1 427 mark '8.21' ; $SED -Ee 's+_1\++\+R+' lines1 428 mark '8.22' ; $SED -e 's.1\..R\..g' lines1 429 mark '8.23' ; sed 's/_/\//' lines1 | $SED -e 's/\/1/R/' 430 mark '8.24' ; $SED -Ee 's?_1\??\?R?' lines1 431 mark '8.25' ; $SED -e 's[\[2-4][\[R][' lines1 432 mark '8.26' ; $SED -e 's^\^l^R\^^' lines1 433 mark '8.27' ; $SED -Ee 's{1\{2}{\{R}{' lines1 434 mark '8.28' ; $SED -Ee 's|2\|4|\|R|' lines1 435 echo '\ in y command' 436 mark '8.29' 437 printf 'a\\b(c\n' | 438 $SED 'y%ABCDEFGHIJKLMNOPQRSTUVWXYZ, /\\()"%abcdefghijklmnopqrstuvwxyz,------%' 439 mark '8.30' 440# Test if an unmatched line is only printed once. 441 printf 'z\n' | $SED -e 's/^a*/b/2p' 442} 443 444test_error() 445{ 446 set -x 447 $SED -x && exit 1 448 $SED -f && exit 1 449 $SED -e && exit 1 450 $SED -f /dev/dds && exit 1 451 $SED p /dev/dds && exit 1 452 $SED -f /bin/sh && exit 1 453 $SED '{' && exit 1 454 $SED '{' && exit 1 455 $SED '/hello/' && exit 1 456 $SED '1,/hello/' && exit 1 457 $SED -e '-5p' && exit 1 458 $SED '/jj' && exit 1 459 $SED 'a hello' && exit 1 460 $SED 'a \ hello' && exit 1 461 $SED 'b foo' && exit 1 462 $SED 'd hello' && exit 1 463 $SED 's/aa' && exit 1 464 $SED 's/aa/' && exit 1 465 $SED 's/a/b' && exit 1 466 $SED 's/a/b/c/d' && exit 1 467 $SED 's/a/b/ 1 2' && exit 1 468 $SED 's/a/b/ 1 g' && exit 1 469 $SED 's/a/b/w' && exit 1 470 $SED 'y/aa' && exit 1 471 $SED 'y/aa/b/' && exit 1 472 $SED 'y/aa/' && exit 1 473 $SED 'y/a/b' && exit 1 474 $SED 'y/a/b/c/d' && exit 1 475 $SED '!' && exit 1 476 $SED supercalifrangolisticexprialidociussupercalifrangolisticexcius 477 set +x 478} 479 480main "$@" 481