26763: fix problem on failed cd -s to relative path
[zsh.git] / Test / V03mathfunc.ztst
blob069059da4ff1b73d02f4055b47f2c999e7e459b9
1 # Tests for the module zsh/mathfunc
3 %prep
4   if ( zmodload -i zsh/mathfunc ) >/dev/null 2>&1; then
5     zmodload -i zsh/mathfunc
6   else
7     ZTST_unimplemented="The module zsh/mathfunc is not available."
8   fi
10 %test
11   # -g makes pi available in later tests
12   float -gF 5 pi
13   (( pi = 4 * atan(1.0) ))
14   print $pi
15 0:Basic operation with atan
16 >3.14159
18   float -F 5 result
19   (( result = atan(3,2) ))
20   print $result
21 0:atan with two arguments
22 >0.98279
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"
36   else
37     print "First two random numbers differ, OK"
38   fi
39   if (( r2 == r3 )); then
40     print "Seed not updated correctly the second time"
41   else
42     print "Second two random numbers differ, OK"
43   fi
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
49   seed=f45677a6cbe4
50   float r1=$(( rand48(seed) ))
51   float r2=$(( rand48(seed) ))
52   seed2=$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"
60   else
61     print "First two random numbers differ, OK"
62   fi
63   if (( r2 == r3 )); then
64     print "Seed not updated correctly the second time"
65   else
66     print "Second two random numbers differ, OK"
67   fi
68   if (( r3 == r4 )); then
69     print "Identical seeds generate identical numbers, OK"
70   else
71     print "Indeterminate result from identical seeds"
72   fi
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
79   float -F 5 pitest
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"
84   else
85     print "BAD: got $pitest instead of $pi"
86   fi
87 0:Conversion of arguments from integer
88 >OK, atan on an integer seemed to work
90   float -F 5 result
91   typeset str
92   for str in 0 0.0 1 1.5 -1 -1.5; do
93     (( result = abs($str) ))
94     print $result
95   done
96 0:Use of abs on various numbers
97 >0.00000
98 >0.00000
99 >1.00000
100 >1.50000
101 >1.00000
102 >1.50000
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
113   typeset -a randoms
114   randoms=('f = RANDOM' 'f = rand48()')
115   zmodload -i zsh/mathfunc
116   for isource in 1 2; do
117     (( sum = sumsq = max = 0 ))
118     repeat $N; do
119       let $randoms[$isource]
120       (( f > max )) && (( max = f ))
121       (( sum += f, sumsq += f * f ))
122     done
123     (( av = sum / N ))
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]"
129       (( ok = 0 ))
130     fi
131     if (( sd < max / 4 )); then
132       print "WARNING: distribution of random numbers is suspicious.
133   Was testing: $randoms[$isource]"
134       (( ok = 0 ))
135     fi
136   done
137   (( ok ))
138 0:Test random number generator distributions are not grossly broken