1# Tests for the module zsh/mathfunc
2
3%prep
4  if ! zmodload zsh/mathfunc 2>/dev/null; then
5    ZTST_unimplemented="The module zsh/mathfunc is not available."
6  fi
7
8%test
9  # -g makes pi available in later tests
10  float -gF 5 pi
11  (( pi = 4 * atan(1.0) ))
12  print $pi
130:Basic operation with atan
14>3.14159
15
16  float -F 5 result
17  (( result = atan(3,2) ))
18  print $result
190:atan with two arguments
20>0.98279
21
22  print $(( atan(1,2,3) ))
231:atan can't take three arguments
24?(eval):1: wrong number of arguments: atan(1,2,3)
25
26  float r1=$(( rand48() ))
27  float r2=$(( rand48() ))
28  float r3=$(( rand48() ))
29  # Yes, this is a floating point equality test like they tell
30  # you not to do.  As the pseudrandom sequence is deterministic,
31  # this is the right thing to do in this case.
32  if (( r1 == r2 )); then
33    print "Seed not updated correctly the first time"
34  else
35    print "First two random numbers differ, OK"
36  fi
37  if (( r2 == r3 )); then
38    print "Seed not updated correctly the second time"
39  else
40    print "Second two random numbers differ, OK"
41  fi
420:rand48 with default initialisation
43F:This test fails if your math library doesn't have erand48().
44>First two random numbers differ, OK
45>Second two random numbers differ, OK
46
47  seed=f45677a6cbe4
48  float r1=$(( rand48(seed) ))
49  float r2=$(( rand48(seed) ))
50  seed2=$seed
51  float r3=$(( rand48(seed) ))
52  float r4=$(( rand48(seed2) ))
53  # Yes, this is a floating point equality test like they tell
54  # you not to do.  As the pseudrandom sequence is deterministic,
55  # this is the right thing to do in this case.
56  if (( r1 == r2 )); then
57    print "Seed not updated correctly the first time"
58  else
59    print "First two random numbers differ, OK"
60  fi
61  if (( r2 == r3 )); then
62    print "Seed not updated correctly the second time"
63  else
64    print "Second two random numbers differ, OK"
65  fi
66  if (( r3 == r4 )); then
67    print "Identical seeds generate identical numbers, OK"
68  else
69    print "Indeterminate result from identical seeds"
70  fi
710:rand48 with pre-generated seed
72F:This test fails if your math library doesn't have erand48().
73>First two random numbers differ, OK
74>Second two random numbers differ, OK
75>Identical seeds generate identical numbers, OK
76
77  float -F 5 pitest
78  (( pitest = 4.0 * atan(1) ))
79  # This is a string test of the output to 5 digits.
80  if [[ $pi = $pitest ]]; then
81    print "OK, atan on an integer seemed to work"
82  else
83    print "BAD: got $pitest instead of $pi"
84  fi
850:Conversion of arguments from integer
86>OK, atan on an integer seemed to work
87
88  float -F 5 result
89  typeset str
90  for str in 0 0.0 1 1.5 -1 -1.5; do
91    (( result = abs($str) ))
92    print $result
93  done
940:Use of abs on various numbers
95>0.00000
96>0.00000
97>1.00000
98>1.50000
99>1.00000
100>1.50000
101
102   print $(( sqrt(-1) ))
1030:Non-negative argument checking for square roots.
104>NaN
105
106# Simple test that the pseudorandom number generators are producing
107# something that could conceivably be pseudorandom numbers in a
108# linear range.  Not a detailed quantitative verification.
109  integer N=10000 isource ok=1
110  float -F f sum sumsq max max2 av sd
111  typeset -a randoms
112  randoms=('f = RANDOM' 'f = rand48()')
113  for isource in 1 2; do
114    (( sum = sumsq = max = 0 ))
115    repeat $N; do
116      let $randoms[$isource]
117      (( f > max )) && (( max = f ))
118      (( sum += f, sumsq += f * f ))
119    done
120    (( av = sum / N ))
121    (( sd = sqrt((sumsq - N * av * av) / (N-1)) ))
122    (( max2 = 0.5 * max ))
123    if (( av > max2 * 1.1 )) || (( av < max2 * 0.9 )); then
124      print "WARNING: average of random numbers is suspicious.
125  Was testing: $randoms[$isource]"
126      (( ok = 0 ))
127    fi
128    if (( sd < max / 4 )); then
129      print "WARNING: distribution of random numbers is suspicious.
130  Was testing: $randoms[$isource]"
131      (( ok = 0 ))
132    fi
133  done
134  (( ok ))
1350:Test random number generator distributions are not grossly broken
136
137  float -F 5 g l
138  (( g = gamma(2), l = lgamma(2) ))
139  print $g, $l
1400:Test Gamma function gamma and lgamma
141>1.00000, 0.00000
142
143  float -F 5 a b c
144  (( a = log2(0.5), b = log2(1.5), c = log2(99) ))
145  print -r - "$a, $b, $c"
1460:log2
147>-1.00000, 0.58496, 6.62936
148