source: zzuf/trunk/test/testsuite.sh @ 1616

Last change on this file since 1616 was 1616, checked in by Sam Hocevar, 14 years ago
  • Documented stdin behaviour.
  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 4.1 KB
Line 
1#!/bin/sh
2
3set -e
4
5create()
6{
7    rm -f /tmp/zzuf-zero-$$
8    dd if=/dev/zero of=/tmp/zzuf-zero-$$ bs=1024 count=32 2>/dev/null
9    rm -f /tmp/zzuf-random-$$
10    dd if=/dev/urandom of=/tmp/zzuf-random-$$ bs=1024 count=32 2>/dev/null
11    rm -f /tmp/zzuf-text-$$
12    strings </dev/urandom | dd bs=1024 count=32 of=/tmp/zzuf-text-$$ 2>/dev/null
13    echo "" >> /tmp/zzuf-text-$$ # Make sure we have a newline at EOF
14}
15
16check()
17{
18    ZZOPTS="$1"
19    CMD="$2"
20    ALIAS="$3"
21    echo -n " $(echo "$ALIAS .............." | cut -b1-18) "
22    NEWMD5="$(eval "$ZZUF $ZZOPTS $CMD" 2>/dev/null | $MD5PROG | cut -b1-32)"
23    if [ -z "$MD5" ]; then
24        MD5="$NEWMD5"
25        echo "$NEWMD5"
26    elif [ "$NEWMD5" != "$MD5" ]; then
27        OK=0
28        echo "$NEWMD5 FAILED"
29    else
30        echo 'ok'
31    fi
32}
33
34cleanup() {
35    if [ "$FAILED" = 0 ]; then
36        rm -f /tmp/zzuf-zero-$$
37        rm -f /tmp/zzuf-random-$$
38        rm -f /tmp/zzuf-text-$$
39        echo "*** temporary files removed ***"
40    else
41        echo "*** files preserved ***"
42        echo " /tmp/zzuf-zero-$$"
43        echo " /tmp/zzuf-random-$$"
44        echo " /tmp/zzuf-text-$$"
45    fi
46}
47
48trap "echo ''; echo '*** ABORTED ***'; cleanup; exit 0" 1 2 15
49
50seed=$((0$1))
51ZZUF="$(dirname "$0")/../src/zzuf"
52FDCAT="$(dirname "$0")/fdcat"
53STREAMCAT="$(dirname "$0")/streamcat"
54if md5sum /dev/null >/dev/null 2>&1; then
55  MD5PROG=md5sum
56elif md5 /dev/null >/dev/null 2>&1; then
57  MD5PROG=md5
58else
59  echo "error: no md5 program found (tried: md5sum, md5)"
60  exit 1
61fi
62if [ ! -f "$FDCAT" -o ! -f "$STREAMCAT" ]; then
63  echo "error: test/fdcat or test/streamcat are missing"
64  exit 1
65fi
66if file /bin/cat | grep -q 'statically linked'; then
67  STATIC_CAT=1
68fi
69if file /bin/dd | grep -q 'statically linked'; then
70  STATIC_DD=1
71fi
72FAILED=0
73TESTED=0
74
75echo "*** running zzuf test suite ***"
76echo "*** creating test files ***"
77create
78echo "*** using seed $seed ***"
79
80for r in 0.0 0.00001 0.001 0.1 10.0; do
81    for file in /tmp/zzuf-zero-$$ /tmp/zzuf-text-$$ /tmp/zzuf-random-$$; do
82        ZZOPTS="-s $seed -r $r"
83        case $file in
84          *text*) ZZOPTS="$ZZOPTS -P '\n'" ;;
85        esac
86        echo "*** file $file, ratio $r ***"
87        OK=1
88        MD5=""
89        check "$ZZOPTS" "< $file" "zzuf"
90        check "$ZZOPTS" "$FDCAT $file" "fdcat"
91        check "$ZZOPTS" "$STREAMCAT $file" "streamcat"
92        if [ "$STATIC_CAT" = "" ]; then
93            check "$ZZOPTS" "cat $file" "cat"
94            check "$ZZOPTS" "-i cat < $file" "|cat"
95        fi
96        if [ "$STATIC_DD" = "" ]; then
97            check "$ZZOPTS" "dd bs=65536 if=$file" "dd(bs=65536)"
98            check "$ZZOPTS" "dd bs=1111 if=$file" "dd(bs=1111)"
99            check "$ZZOPTS" "dd bs=1024 if=$file" "dd(bs=1024)"
100            check "$ZZOPTS" "dd bs=1 if=$file" "dd(bs=1)"
101        fi
102        case $file in
103          *text*)
104            # We don't include grep or sed when the input is not text, because
105            # they put a newline at the end of their input if it was not there
106            # initially. (Linux sed doesn't, but OS X sed does.)
107            check "$ZZOPTS" "head -- -n 9999 $file" "head -n 9999"
108            check "$ZZOPTS" "tail -- -n 9999 $file" "tail -n 9999"
109            check "$ZZOPTS" "tail -- -n +1 $file" "tail -n +1"
110            check "$ZZOPTS" "grep -- -a '' $file" "grep -a ''"
111            check "$ZZOPTS" "sed -- -e n $file" "sed -e n"
112            #check "$ZZOPTS" "cut -- -b1- $file" "cut -b1-"
113            check "$ZZOPTS" "-i head -- -n 9999 < $file" "|head -n 9999"
114            check "$ZZOPTS" "-i tail -- -n 9999 < $file" "|tail -n 9999"
115            check "$ZZOPTS" "-i tail -- -n +1 < $file" "|tail -n +1"
116            check "$ZZOPTS" "-i grep -- -a '' < $file" "|grep -a ''"
117            check "$ZZOPTS" "-i sed -- -e n < $file" "|sed -e n"
118            #check "$ZZOPTS" "-i cut -- -b1- < $file" "|cut -b1-"
119            ;;
120        esac
121        if [ "$OK" != 1 ]; then
122            echo "*** FAILED ***"
123            FAILED=$(($FAILED + 1))
124        fi
125        TESTED=$(($TESTED + 1))
126    done
127done
128
129if [ "$FAILED" != 0 ]; then
130    echo "*** $FAILED tests failed out of $TESTED ***"
131    cleanup
132    exit 1
133fi
134echo "*** all $TESTED tests OK ***"
135
136cleanup
137exit 0
138
Note: See TracBrowser for help on using the repository browser.