| 1 | #!/bin/sh |
|---|
| 2 | |
|---|
| 3 | set -e |
|---|
| 4 | |
|---|
| 5 | checkutils() |
|---|
| 6 | { |
|---|
| 7 | r=$1 |
|---|
| 8 | for type in 00 ff text random; do |
|---|
| 9 | file="$DIR/file-$type" |
|---|
| 10 | ZZOPTS="-s $seed -r $r" |
|---|
| 11 | case $file in |
|---|
| 12 | *text*) ZZOPTS="$ZZOPTS -P '\\n' -R '\\000'" ;; |
|---|
| 13 | esac |
|---|
| 14 | echo "*** file $file, ratio $r ***" |
|---|
| 15 | REFMD5="" |
|---|
| 16 | if [ $r = 0.0 -a $type = 00 ]; then |
|---|
| 17 | check="bb7df04e1b0a2570657527a7e108ae23" |
|---|
| 18 | echo "*** should be $check ***" |
|---|
| 19 | check "$ZZOPTS" "< $file" "zzuf" "$check" |
|---|
| 20 | else |
|---|
| 21 | check "$ZZOPTS" "< $file" "zzuf" |
|---|
| 22 | fi |
|---|
| 23 | for n in 1 2 3; do |
|---|
| 24 | check "$ZZOPTS" "$ZZCAT $n $file" "zzcat $n" |
|---|
| 25 | done |
|---|
| 26 | if [ "$STATIC_CAT" = "" ]; then |
|---|
| 27 | check "$ZZOPTS" "cat $file" "cat" |
|---|
| 28 | check "$ZZOPTS" "-i cat < $file" "|cat" |
|---|
| 29 | fi |
|---|
| 30 | if [ "$STATIC_DD" = "" ]; then |
|---|
| 31 | check "$ZZOPTS" "dd bs=65536 if=$file" "dd(bs=65536)" |
|---|
| 32 | check "$ZZOPTS" "dd bs=1111 if=$file" "dd(bs=1111)" |
|---|
| 33 | check "$ZZOPTS" "dd bs=1024 if=$file" "dd(bs=1024)" |
|---|
| 34 | check "$ZZOPTS" "dd bs=1 if=$file" "dd(bs=1)" |
|---|
| 35 | fi |
|---|
| 36 | case $file in |
|---|
| 37 | *text*) |
|---|
| 38 | # We don't include grep or sed when the input is not text, because |
|---|
| 39 | # they put a newline at the end of their input if it was not there |
|---|
| 40 | # initially. (Linux sed doesn't, but OS X sed does.) |
|---|
| 41 | check "$ZZOPTS" "head -n 9999 $file" "head -n 9999" |
|---|
| 42 | check "$ZZOPTS" "tail -n 9999 $file" "tail -n 9999" |
|---|
| 43 | check "$ZZOPTS" "tail -n +1 $file" "tail -n +1" |
|---|
| 44 | if grep -a '' /dev/null >/dev/null 2>&1; then |
|---|
| 45 | check "$ZZOPTS" "grep -a '' $file" "grep -a ''" |
|---|
| 46 | fi |
|---|
| 47 | check "$ZZOPTS" "sed -e n $file" "sed -e n" |
|---|
| 48 | #check "$ZZOPTS" "cut -b1- $file" "cut -b1-" |
|---|
| 49 | check "$ZZOPTS" "-i head -n 9999 < $file" "|head -n 9999" |
|---|
| 50 | check "$ZZOPTS" "-i tail -n 9999 < $file" "|tail -n 9999" |
|---|
| 51 | check "$ZZOPTS" "-i tail -n +1 < $file" "|tail -n +1" |
|---|
| 52 | if grep -a '' /dev/null >/dev/null 2>&1; then |
|---|
| 53 | check "$ZZOPTS" "-i grep -a '' < $file" "|grep -a ''" |
|---|
| 54 | fi |
|---|
| 55 | check "$ZZOPTS" "-i sed -e n < $file" "|sed -e n" |
|---|
| 56 | #check "$ZZOPTS" "-i cut -b1- < $file" "|cut -b1-" |
|---|
| 57 | ;; |
|---|
| 58 | esac |
|---|
| 59 | done |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | check() |
|---|
| 63 | { |
|---|
| 64 | ZZOPTS="$1" |
|---|
| 65 | CMD="$2" |
|---|
| 66 | ALIAS="$3" |
|---|
| 67 | CHECK="$4" |
|---|
| 68 | printf " $(echo "$ALIAS .............." | cut -b1-18) " |
|---|
| 69 | MD5="$(eval "$ZZUF -m $ZZOPTS $CMD" 2>/dev/null | cut -f2 -d' ')" |
|---|
| 70 | if [ -n "$CHECK" ]; then |
|---|
| 71 | REFMD5="$CHECK" |
|---|
| 72 | fi |
|---|
| 73 | if [ -z "$REFMD5" ]; then |
|---|
| 74 | REFMD5="$MD5" |
|---|
| 75 | echo "$MD5" |
|---|
| 76 | else |
|---|
| 77 | TESTED=$(($TESTED + 1)) |
|---|
| 78 | if [ "$MD5" != "$REFMD5" ]; then |
|---|
| 79 | FAILED=$(($FAILED + 1)) |
|---|
| 80 | echo "$MD5 FAILED" |
|---|
| 81 | else |
|---|
| 82 | echo 'ok' |
|---|
| 83 | fi |
|---|
| 84 | fi |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | DIR="$(dirname "$0")" |
|---|
| 88 | ZZUF="$DIR/../src/zzuf" |
|---|
| 89 | ZZCAT="$DIR/zzcat" |
|---|
| 90 | if [ ! -f "$ZZCAT" ]; then |
|---|
| 91 | echo "error: test/zzcat is missing" |
|---|
| 92 | exit 1 |
|---|
| 93 | fi |
|---|
| 94 | if file /bin/cat | grep -q 'statically linked'; then |
|---|
| 95 | STATIC_CAT=1 |
|---|
| 96 | fi |
|---|
| 97 | if file /bin/dd | grep -q 'statically linked'; then |
|---|
| 98 | STATIC_DD=1 |
|---|
| 99 | fi |
|---|
| 100 | FAILED=0 |
|---|
| 101 | TESTED=0 |
|---|
| 102 | |
|---|
| 103 | if [ -z "$1" ]; then |
|---|
| 104 | seed=$(date | $ZZUF -m 2>/dev/null | cut -f2 -d' ' | tr -d abcdef | cut -b1-8) |
|---|
| 105 | else |
|---|
| 106 | seed="$1" |
|---|
| 107 | fi |
|---|
| 108 | |
|---|
| 109 | echo "*** running zzuf utils test suite with seed $seed ***" |
|---|
| 110 | |
|---|
| 111 | checkutils 0.0 |
|---|
| 112 | checkutils 0.000000001 |
|---|
| 113 | checkutils 0.0000001 |
|---|
| 114 | checkutils 0.00001 |
|---|
| 115 | checkutils 0.001 |
|---|
| 116 | checkutils 0.1 |
|---|
| 117 | checkutils 10.0 |
|---|
| 118 | |
|---|
| 119 | if [ "$FAILED" != 0 ]; then |
|---|
| 120 | echo "*** $FAILED tests failed out of $TESTED ***" |
|---|
| 121 | exit 1 |
|---|
| 122 | fi |
|---|
| 123 | echo "*** all $TESTED tests OK ***" |
|---|
| 124 | |
|---|
| 125 | exit 0 |
|---|
| 126 | |
|---|