1 # Tests for the module zsh/mathfunc
4 if ( zmodload -i zsh/mathfunc ) >/dev/null 2>&1; then
5 zmodload -i zsh/mathfunc
7 ZTST_unimplemented="The module zsh/mathfunc is not available."
11 # -g makes pi available in later tests
13 (( pi = 4 * atan(1.0) ))
15 0:Basic operation with atan
19 (( result = atan(3,2) ))
21 0:atan with two arguments
24 print $(( atan(1,2,3) ))
25 1:atan can't take three arguments
26 ?(eval):1: wrong number of arguments: atan(1,2,3)
28 float r1=$(( rand48() ))
29 float r2=$(( rand48() ))
30 float r3=$(( rand48() ))
31 # Yes, this is a floating point equality test like they tell
32 # you not to do. As the pseudrandom sequence is deterministic,
33 # this is the right thing to do in this case.
34 if (( r1 == r2 )); then
35 print "Seed not updated correctly the first time"
37 print "First two random numbers differ, OK"
39 if (( r2 == r3 )); then
40 print "Seed not updated correctly the second time"
42 print "Second two random numbers differ, OK"
44 0:rand48 with default initialisation
45 F:This test fails if your math library doesn't have erand48().
46 >First two random numbers differ, OK
47 >Second two random numbers differ, OK
50 float r1=$(( rand48(seed) ))
51 float r2=$(( rand48(seed) ))
53 float r3=$(( rand48(seed) ))
54 float r4=$(( rand48(seed2) ))
55 # Yes, this is a floating point equality test like they tell
56 # you not to do. As the pseudrandom sequence is deterministic,
57 # this is the right thing to do in this case.
58 if (( r1 == r2 )); then
59 print "Seed not updated correctly the first time"
61 print "First two random numbers differ, OK"
63 if (( r2 == r3 )); then
64 print "Seed not updated correctly the second time"
66 print "Second two random numbers differ, OK"
68 if (( r3 == r4 )); then
69 print "Identical seeds generate identical numbers, OK"
71 print "Indeterminate result from identical seeds"
73 0:rand48 with pre-generated seed
74 F:This test fails if your math library doesn't have erand48().
75 >First two random numbers differ, OK
76 >Second two random numbers differ, OK
77 >Identical seeds generate identical numbers, OK
80 (( pitest = 4.0 * atan(1) ))
81 # This is a string test of the output to 5 digits.
82 if [[ $pi = $pitest ]]; then
83 print "OK, atan on an integer seemed to work"
85 print "BAD: got $pitest instead of $pi"
87 0:Conversion of arguments from integer
88 >OK, atan on an integer seemed to work
92 for str in 0 0.0 1 1.5 -1 -1.5; do
93 (( result = abs($str) ))
96 0:Use of abs on various numbers
104 print $(( sqrt(-1) ))
105 1:Non-negative argument checking for square roots.
106 ?(eval):1: math: argument to sqrt out of range
108 # Simple test that the pseudorandom number generators are producing
109 # something that could conceivably be pseudorandom numbers in a
110 # linear range. Not a detailed quantitative verification.
111 integer N=10000 isource ok=1
112 float -F f sum sumsq max max2 av sd
114 randoms=('f = RANDOM' 'f = rand48()')
115 zmodload -i zsh/mathfunc
116 for isource in 1 2; do
117 (( sum = sumsq = max = 0 ))
119 let $randoms[$isource]
120 (( f > max )) && (( max = f ))
121 (( sum += f, sumsq += f * f ))
124 (( sd = sqrt((sumsq - N * av * av) / (N-1)) ))
125 (( max2 = 0.5 * max ))
126 if (( av > max2 * 1.1 )) || (( av < max2 * 0.9 )); then
127 print "WARNING: average of random numbers is suspicious.
128 Was testing: $randoms[$isource]"
131 if (( sd < max / 4 )); then
132 print "WARNING: distribution of random numbers is suspicious.
133 Was testing: $randoms[$isource]"
138 0:Test random number generator distributions are not grossly broken