📄 stests
字号:
# @(#)stests 8.1 (Berkeley) 6/6/93#Latest version. My sort passes all tests because I wrote it.#We differ only on 25E and 25H.#(I found at least one bug in constructing test 25, and was driven#to rewrite field parsing to clarify it.)##In 25E, -k2.3,2.1b, the fields are not necessarily out of order.#Even if they were, it would be legal (11752-3), although certainly#justification for warning.##On 25H, your answer is as defensible as mine. (Our suggestion#*1 backs mine.)# Tests for the Unix sort utility# Test Posix features except for locale.# Test some nonstandard features if present.# Other tests should be made for files too big to fit in memory.# Initialize switches for nonstandard features.# Use parenthesized settings for supported features.o=: # officially obsolescent features: +1 -2, misplaced -o (o=)g=: # -g numeric sort including e-format numbers (g=)M=: # -M sort by month names (M=)s=: # -s stable, do not compare raw bytes on equal keys (s=)y= # -y user-specified memory size (y=-y10000)# Detect what features are supported, assuming bad options cause# errors. Set switches accordingly.echo obsolescent and nonstandard features recognized, if any:if sort +0 </dev/null 2>/dev/null; then o= echo ' +1 -2'; fiif sort /dev/null -o xx 2>/dev/null; then o= echo ' displaced -o'; fiif sort -g </dev/null 2>/dev/null; then g= echo ' -g g-format numbers'; fiif sort -M </dev/null 2>/dev/null; then M= echo ' -M months'; fiif sort -s </dev/null 2>/dev/null; then s= echo ' -s stable'; fiif sort -y10000 </dev/null 2>/dev/null; then y=-y10000 echo ' -y space'; fiif sort -z10000 </dev/null 2>/dev/null; then echo ' -z size (not exercised)'; fiif sort -T. </dev/null 2>/dev/null; then echo ' -T tempdir (not exercised)'; fiexport TEST # major sequence number of testtrap "rm -f in in1 out xx -k xsort linecount fields; exit" 0 1 2 13 15# xsort testno options# Sort file "in" with specified options.# Compare with file "out" if that is supplied,# otherwise make plausibility checks on output# "sum" must be dumb; insensitive to the# order of lines within a file.# System V sum is suitable; sum -5 is the v10 equivalent.PATH=.:$PATHexport PATHcat <<'!' >xsort; chmod +x xsort X=$1; shift if sort "$@" in >xx && sort -c "$@" xx then if test -f out then cmp xx out >/dev/null && exit 0 echo $TEST$X comparison failed else test "`cksum -o2 <in`" = "`cksum -o2 <xx`" && exit 0 echo $TEST$X checksum failed fi else echo $TEST$X failed fi exit 1!# linecount testno file count# declares the given "testno" to be in error if number of# lines in "file" differs from "count"cat <<'!' >linecount; chmod +x linecountawk 'END{ if(NR!='$3') print "'$TEST$1' failed" }' $2!rm -f out#---------------------------------------------------------------TEST=01; echo $TEST # -c status, checksum # obsolescent features go togethercat <<! >inba!rm -f out -osort -c in 2>/dev/null && echo ${TEST}A failedxsort B || '"cksum"' is probably unsuitable - see comments$o sort +0 in -o in || echo ${TEST}c failed#---------------------------------------------------------------TEST=02; echo $TEST # output from -ccat <<! >inxy!sort -cr in >out 2>xx && echo ${TEST}A failedtest -s out && echo ${TEST}B failedtest -s xx && echo option -c is noisy "(probably legal)"test -s xx || echo option -c is quiet "(legal, not classical)"#---------------------------------------------------------------TEST=03; echo $TEST # -ncat <<! >in-99.0-99.1-.0002-1020010.000000000000000000000000000000000001103xx!cat <<! >out-99.1-99.0-10-.0002x23x100010.000000000000000000000000000000000001!xsort "" -n#---------------------------------------------------------------TEST=04; echo $TEST # -b without fields, piping, -c status returncat <<! >in b a!cp in outxsort A -bcat in | sort | cat >xxcmp xx out >/dev/null || echo ${TEST}B failedsort in | sort -cr 2>/dev/null && echo ${TEST}C failed#---------------------------------------------------------------TEST=05; echo $TEST # fields, reverse fields, -c status returncat <<! >inb b pa b qx a!cat <<! >outx aa b qb b p!$o xsort A +1 -2$o xsort B +1 -2 +2rxsort C -k 2,2xsort D -k 2,2 -k 3rxsort E -k 2,2.0xsort F -k 2,2 -k 1,1 -k 3sort -c -k 2 in 2>/dev/null && ${TEST}G failed#---------------------------------------------------------------TEST=06; echo $TEST # -tcat <<! >ina:a!!cp in out$o xsort A -t : -r +0$o xsort B -t : +0 -1xsort C -t : -r -k 1xsort D -t : -k 1,1#---------------------------------------------------------------TEST=07; echo $TEST # -t, character positions in fields # -t: as 1 arg is not strictly conforming, but classicalcat <<! >in: ab:bac!cat <<! >out:bac: ab!$o xsort A -b -t: +1.1 $o xsort B -t: +1.1rxsort C -b -t: -k 2.2xsort D -t: -k 2.2r#---------------------------------------------------------------TEST=08; echo $TEST # space and tab as -t characterscat <<! >in b c b c b c!cp in outxsort A -t ' ' -k2,2xsort B -t ' ' -k2.1,2.0cat <<! >out b c b c b c!xsort C -t ' ' -k2,2xsort D -t ' ' -k2.1,2.0cat <<! >out b c b c b c!xsort E -k2cat <<! >out b c b c b c!xsort F -k2b#---------------------------------------------------------------TEST=09; echo $TEST # alphabetic as -t charactercat <<! >inzXayXazXb!cp in outxsort "" -tX -k2 -k1r,1#---------------------------------------------------------------TEST=10; echo $TEST # -mcat <<! >inaababbcca!cat <<! >in1Zaaaacc!cat <<! >outZaaaaababacbccca!sort -m in in1 >xxcmp xx out >/dev/null || echo $TEST failed#---------------------------------------------------------------TEST=11; echo $TEST # multiple files, -o overwites input, -m, -mucat <<! >inabcd!sort -o xx in in in in in in in in in in in in in in in in inlinecount A xx 68sort -o in -mu in in in in in in in in in in in in in in in in inlinecount B in 4sort -o in -m in in in in in in in in in in in in in in in in incmp in xx >/dev/null || echo ${TEST}C failed#---------------------------------------------------------------TEST=12; echo $TEST # does -mu pick the first among equals?cat <<! >in3B3b3B2~3B24.14155.!cat <<! >out3B3B24.15!xsort A -mudf || echo "(other behavior is legal, not classical)"xsort B -mudf -k1 || echo "(other behavior is legal, not classical)"#---------------------------------------------------------------TEST=13; echo $TEST # long records (>8000 bytes, keys >16000), -rawk 'BEGIN { x="x" for(i=1; i<=12; i++) x = x x for(i=15; i<=25; i++) print x i}' >inawk 'BEGIN { x="x" for(i=1; i<=12; i++) x = x x for(i=25; i>=15; i--) print x i}' >outxsort A -rxsort B -k 1,1r -k 1#---------------------------------------------------------------TEST=14; echo $TEST "(3 long parts)"awk 'BEGIN { for(i=0; i<100000; i++) print rand() }' | grep -v e >inrm -f outxsort A; echo $TEST "(part A done)"xsort B -n; echo $TEST "(part B done)"# next test is unclean: xx is a hidden side-effect of xsortawk ' $0 < x { print "test '${TEST}C' failed"; exit } $0 "" != x { print >"out"; x = $0 }' xxxsort C -n -u#---------------------------------------------------------------TEST=15; echo $TEST "(long)" # force intermediate files if possibleawk 'BEGIN { for(i=0; i<20000; i++) print rand() }' >inrm -f outxsort A -r $ysort -r in | awk '$0 "x" != x { print ; x = $0 "x" }' >outxsort B -u -r $y#---------------------------------------------------------------TEST=16; echo $TEST # -nr, -nm, file name -awk 'BEGIN { for(i=-100; i<=100; i+=2) printf "%.10d\n", i }' >inawk 'BEGIN { for(i=-99; i<=100; i+=2) print i }' | sort -nr in - >xxawk '$0+0 != 101-NR { print "'${TEST}A' failed"; exit }' xxawk 'BEGIN { for(i=-99; i<=100; i+=2) print i }' | sort -mn in - >xxawk '$0+0 != -101+NR { print "'${TEST}B' failed"; exit }' xx#---------------------------------------------------------------TEST=17; echo $TEST # -d, fields without end, modifier overridecat <<! >ina-Ba+ba bA+ba b!cat <<! >outa ba bA+ba-Ba+b!$o xsort A -df +0 +0d xsort B -df -k 1 -k 1d#---------------------------------------------------------------TEST=18; echo $TEST # -u on key onlycat <<! >in12 y13 z12 x!cat <<! >out12 x12 y13 z!$o xsort A +0 -1xsort B -k 1,1sort -u -k 1,1 in >xx
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -