1 | #!/bin/sh |
---|
2 | # |
---|
3 | # check-zzuf-r-ratio - zzuf RNG statistic tests |
---|
4 | # Copyright (c) 2008-2010 Sam Hocevar <sam@hocevar.net> |
---|
5 | # All Rights Reserved |
---|
6 | # |
---|
7 | # This program is free software. It comes without any warranty, to |
---|
8 | # the extent permitted by applicable law. You can redistribute it |
---|
9 | # and/or modify it under the terms of the Do What The Fuck You Want |
---|
10 | # To Public License, Version 2, as published by Sam Hocevar. See |
---|
11 | # http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
12 | # |
---|
13 | |
---|
14 | . "$(dirname "$0")/functions.inc" |
---|
15 | |
---|
16 | checkflip() |
---|
17 | { |
---|
18 | r=$1 |
---|
19 | expect=$2 |
---|
20 | mib=20 |
---|
21 | try=3 |
---|
22 | s2=$seed |
---|
23 | new_test "$mib MiB of zeroes, ratio $r" |
---|
24 | echo " expecting $expect" |
---|
25 | checkflip_internal $1 $2 $mib $try $s2 |
---|
26 | } |
---|
27 | |
---|
28 | checkflip_internal() |
---|
29 | { |
---|
30 | r=$1 |
---|
31 | expect=$2 |
---|
32 | mib=$3 |
---|
33 | try=$4 |
---|
34 | s2=$5 |
---|
35 | rmax=-1 |
---|
36 | rmin=-1 |
---|
37 | rtot=0 |
---|
38 | printf " got" |
---|
39 | for x in 0 1 2 3 4; do |
---|
40 | ret=`dd if=/dev/zero bs=1048576 count=$mib 2>/dev/null | $ZZUF -s $s2 -r $r | "$ZZERO"` |
---|
41 | if [ "$rmax" = -1 -o "$ret" -gt "$rmax" ]; then rmax=$ret; fi |
---|
42 | if [ "$rmin" = -1 -o "$ret" -lt "$rmin" ]; then rmin=$ret; fi |
---|
43 | rtot=`expr $rtot + $ret || true` |
---|
44 | printf " $ret" |
---|
45 | s2=`expr $s2 + 1` |
---|
46 | done |
---|
47 | echo "" |
---|
48 | rmean=`expr '(' $rtot + 2 ')' / 5 || true` |
---|
49 | delta=`expr $rmean - $expect || true` |
---|
50 | printf " min/avg/max $rmin/$rmean/$rmax .........." |
---|
51 | if [ "$delta" -gt -5 -a "$delta" -lt 5 ]; then |
---|
52 | pass_test " ok" |
---|
53 | elif [ $(($rmean * 8)) -lt $(($expect * 7)) \ |
---|
54 | -o $(($rmean * 7)) -gt $(($expect * 8)) ]; then |
---|
55 | if [ $try -gt 0 ]; then |
---|
56 | # Hack: if we failed with that seed, just try another one. |
---|
57 | # Kinda defeats the purpose of the test, but well, that's |
---|
58 | # how randomness works, you cannot win each time. |
---|
59 | echo " trying again" |
---|
60 | checkflip_internal $1 $2 $3 $(($3 - 1)) "$3$s2" |
---|
61 | else |
---|
62 | fail_test " FAILED" |
---|
63 | fi |
---|
64 | else |
---|
65 | pass_test " ok" |
---|
66 | fi |
---|
67 | } |
---|
68 | |
---|
69 | start_test "zzuf -r test" |
---|
70 | |
---|
71 | # if X flips are performed on N bits set to 0, the average number of bits |
---|
72 | # set to 1 is: N / 2 * (1 - pow(1 - 2 / N, X) |
---|
73 | checkflip 0.000000001 0 |
---|
74 | checkflip 0.00000001 1 |
---|
75 | checkflip 0.0000001 16 |
---|
76 | checkflip 0.000001 167 |
---|
77 | checkflip 0.00001 1677 |
---|
78 | checkflip 0.0001 16775 |
---|
79 | checkflip 0.001 167604 |
---|
80 | checkflip 0.01 1661055 |
---|
81 | checkflip 0.1 15205967 |
---|
82 | |
---|
83 | stop_test |
---|
84 | |
---|