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 | s2=$seed |
---|
21 | mib=20 |
---|
22 | rmax=-1 |
---|
23 | rmin=-1 |
---|
24 | rtot=0 |
---|
25 | new_test "$mib MiB of zeroes, ratio $r" |
---|
26 | echo " expecting $expect" |
---|
27 | printf " got" |
---|
28 | for x in 0 1 2 3 4; do |
---|
29 | ret=`dd if=/dev/zero bs=1048576 count=$mib 2>/dev/null | $ZZUF -s $s2 -r $r | "$ZZERO"` |
---|
30 | if [ "$rmax" = -1 -o "$ret" -gt "$rmax" ]; then rmax=$ret; fi |
---|
31 | if [ "$rmin" = -1 -o "$ret" -lt "$rmin" ]; then rmin=$ret; fi |
---|
32 | rtot=`expr $rtot + $ret || true` |
---|
33 | printf " $ret" |
---|
34 | s2=`expr $s2 + 1` |
---|
35 | done |
---|
36 | echo "" |
---|
37 | rmean=`expr '(' $rtot + 2 ')' / 5 || true` |
---|
38 | delta=`expr $rmean - $expect || true` |
---|
39 | printf " min/avg/max $rmin/$rmean/$rmax .........." |
---|
40 | if [ "$delta" -gt -5 -a "$delta" -lt 5 ]; then |
---|
41 | pass_test " ok" |
---|
42 | elif [ $(($rmean * 8)) -lt $(($expect * 7)) \ |
---|
43 | -o $(($rmean * 7)) -gt $(($expect * 8)) ]; then |
---|
44 | fail_test " FAILED" |
---|
45 | else |
---|
46 | pass_test " ok" |
---|
47 | fi |
---|
48 | } |
---|
49 | |
---|
50 | start_test "zzuf -r test" |
---|
51 | |
---|
52 | # if X flips are performed on N bits set to 0, the average number of bits |
---|
53 | # set to 1 is: N / 2 * (1 - pow(1 - 2 / N, X) |
---|
54 | checkflip 0.000000001 0 |
---|
55 | checkflip 0.00000001 1 |
---|
56 | checkflip 0.0000001 16 |
---|
57 | checkflip 0.000001 167 |
---|
58 | checkflip 0.00001 1677 |
---|
59 | checkflip 0.0001 16775 |
---|
60 | checkflip 0.001 167604 |
---|
61 | checkflip 0.01 1661055 |
---|
62 | checkflip 0.1 15205967 |
---|
63 | |
---|
64 | stop_test |
---|
65 | |
---|