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

Last change on this file since 2325 was 2325, checked in by Sam Hocevar, 15 years ago
  • Add statistical analysis to the testsuite to check our random number generator.
  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 4.9 KB
Line 
1#!/bin/sh
2
3set -e
4
5checkflip()
6{
7    r=$1
8    expect=$2
9    s2=$seed
10    mib=20
11    echo "*** $mib MiB of zeroes, ratio $r ***"
12    echo " expected ....... $expect"
13    rmax=-1
14    rmin=-1
15    rtot=0
16    for x in 0 1 2 3 4 5 6 7 8 9; do
17        ret=`dd if=/dev/zero bs=1048576 count=$mib 2>/dev/null | "$ZZUF" -s $s2 -r $r | "$ZZERO"`
18        if [ "$rmax" = -1 -o "$ret" -gt "$rmax" ]; then rmax=$ret; fi
19        if [ "$rmin" = -1 -o "$ret" -lt "$rmin" ]; then rmin=$ret; fi
20        rtot=`expr $rtot + $ret || true`
21        echo " try $x .......... $ret"
22        s2=`expr $s2 + 1`
23    done
24    rmean=`expr '(' $rtot + 5 ')' / 10 || true`
25    delta=`expr $rmean - $expect || true`
26    if [ "$delta" -gt -5 -a "$delta" -lt 5 ]; then
27        result="ok"
28    elif [ $(($rmean * 8)) -lt $(($expect * 7)) \
29               -o $(($rmean * 7)) -gt $(($expect * 8)) ]; then
30        result="FAILED"
31        FAILED=$(($FAILED + 1))
32    else
33        result="ok"
34    fi
35    TESTED=$(($TESTED + 1))
36    echo " min/avg/max $rmin/$rmean/$rmax .......... $result"
37}
38
39checkutils()
40{
41    r=$1
42    for type in 00 ff text random; do
43        file="$DIR/file-$type"
44        ZZOPTS="-s $seed -r $r"
45        case $file in
46          *text*) ZZOPTS="$ZZOPTS -P '\n'" ;;
47        esac
48        echo "*** file $file, ratio $r ***"
49        REFMD5=""
50        if [ $r = 0.0 -a $type = 00 ]; then
51            check="bb7df04e1b0a2570657527a7e108ae23"
52            echo "*** should be $check ***"
53            check "$ZZOPTS" "< $file" "zzuf" "$check"
54        else
55            check "$ZZOPTS" "< $file" "zzuf"
56        fi
57        for n in 1 2 3; do
58            check "$ZZOPTS" "$ZZCAT $n $file" "zzcat $n"
59        done
60        if [ "$STATIC_CAT" = "" ]; then
61            check "$ZZOPTS" "cat $file" "cat"
62            check "$ZZOPTS" "-i cat < $file" "|cat"
63        fi
64        if [ "$STATIC_DD" = "" ]; then
65            check "$ZZOPTS" "dd bs=65536 if=$file" "dd(bs=65536)"
66            check "$ZZOPTS" "dd bs=1111 if=$file" "dd(bs=1111)"
67            check "$ZZOPTS" "dd bs=1024 if=$file" "dd(bs=1024)"
68            check "$ZZOPTS" "dd bs=1 if=$file" "dd(bs=1)"
69        fi
70        case $file in
71          *text*)
72            # We don't include grep or sed when the input is not text, because
73            # they put a newline at the end of their input if it was not there
74            # initially. (Linux sed doesn't, but OS X sed does.)
75            check "$ZZOPTS" "head -- -n 9999 $file" "head -n 9999"
76            check "$ZZOPTS" "tail -- -n 9999 $file" "tail -n 9999"
77            check "$ZZOPTS" "tail -- -n +1 $file" "tail -n +1"
78            check "$ZZOPTS" "grep -- -a '' $file" "grep -a ''"
79            check "$ZZOPTS" "sed -- -e n $file" "sed -e n"
80            #check "$ZZOPTS" "cut -- -b1- $file" "cut -b1-"
81            check "$ZZOPTS" "-i head -- -n 9999 < $file" "|head -n 9999"
82            check "$ZZOPTS" "-i tail -- -n 9999 < $file" "|tail -n 9999"
83            check "$ZZOPTS" "-i tail -- -n +1 < $file" "|tail -n +1"
84            check "$ZZOPTS" "-i grep -- -a '' < $file" "|grep -a ''"
85            check "$ZZOPTS" "-i sed -- -e n < $file" "|sed -e n"
86            #check "$ZZOPTS" "-i cut -- -b1- < $file" "|cut -b1-"
87            ;;
88        esac
89    done
90}
91
92check()
93{
94    ZZOPTS="$1"
95    CMD="$2"
96    ALIAS="$3"
97    CHECK="$4"
98    echo -n " $(echo "$ALIAS .............." | cut -b1-18) "
99    MD5="$(eval "$ZZUF -m $ZZOPTS $CMD" 2>/dev/null | cut -f2 -d' ')"
100    if [ -n "$CHECK" ]; then
101        REFMD5="$CHECK"
102    fi
103    if [ -z "$REFMD5" ]; then
104        REFMD5="$MD5"
105        echo "$MD5"
106    else
107        TESTED=$(($TESTED + 1))
108        if [ "$MD5" != "$REFMD5" ]; then
109            FAILED=$(($FAILED + 1))
110            echo "$MD5 FAILED"
111        else
112            echo 'ok'
113        fi
114    fi
115}
116
117DIR="$(dirname "$0")"
118ZZUF="$DIR/../src/zzuf"
119ZZCAT="$DIR/zzcat"
120if [ ! -f "$ZZCAT" ]; then
121  echo "error: test/zzcat is missing"
122  exit 1
123fi
124ZZERO="$DIR/zzero"
125if [ ! -f "$ZZERO" ]; then
126  echo "error: test/zzero is missing"
127  exit 1
128fi
129if file /bin/cat | grep -q 'statically linked'; then
130  STATIC_CAT=1
131fi
132if file /bin/dd | grep -q 'statically linked'; then
133  STATIC_DD=1
134fi
135FAILED=0
136TESTED=0
137
138if [ -z "$1" ]; then
139  seed=$(date | $ZZUF -m 2>/dev/null | cut -f2 -d' ' | tr -d abcdef | cut -b1-8)
140else
141  seed="$1"
142fi
143
144echo "*** running zzuf test suite with seed $seed ***"
145
146echo ""
147echo "*** check #1: random number generator ***"
148# if X flips are performed on N bits set to 0, the average number of bits
149# set to 1 is: N / 2 * (1 - pow(1 - 2 / N, X)
150checkflip 0.000000001 0
151checkflip 0.00000001  1
152checkflip 0.0000001  16
153checkflip 0.000001  167
154checkflip 0.00001  1677
155checkflip 0.0001  16775
156checkflip 0.001  167604
157checkflip 0.01  1661055
158checkflip 0.1  15205967
159
160echo ""
161echo "*** check #2: libc functions coverage ***"
162checkutils 0.0
163checkutils 0.000000001
164checkutils 0.0000001
165checkutils 0.00001
166checkutils 0.001
167checkutils 0.1
168checkutils 10.0
169
170echo ""
171if [ "$FAILED" != 0 ]; then
172    echo "*** $FAILED tests failed out of $TESTED ***"
173    exit 1
174fi
175echo "*** all $TESTED tests OK ***"
176
177exit 0
178
Note: See TracBrowser for help on using the repository browser.