Commit | Line | Data |
89d0527d |
1 | #!./perl |
2 | |
3 | # $RCSfile$ |
4 | # |
5aabfad6 |
5 | # Regression tests for the Math::Complex pacakge |
6 | # -- Raphael Manfredi, September 1996 |
5cd24f17 |
7 | # -- Jarkko Hietaniemi, March-April 1997 |
5aabfad6 |
8 | |
89d0527d |
9 | BEGIN { |
10 | chdir 't' if -d 't'; |
11 | @INC = '../lib'; |
12 | } |
5aabfad6 |
13 | |
89d0527d |
14 | use Math::Complex; |
15 | |
16 | $test = 0; |
17 | $| = 1; |
fe82cf6e |
18 | @script = (); |
5aabfad6 |
19 | my $eps = 1e-11; |
89d0527d |
20 | |
21 | while (<DATA>) { |
0c721ce2 |
22 | s/^\s+//; |
23 | next if $_ eq '' || /^\#/; |
24 | chomp; |
25 | $test_set = 0; # Assume not a test over a set of values |
89d0527d |
26 | if (/^&(.*)/) { |
27 | $op = $1; |
28 | next; |
29 | } |
30 | elsif (/^\{(.*)\}/) { |
31 | set($1, \@set, \@val); |
32 | next; |
33 | } |
34 | elsif (s/^\|//) { |
0c721ce2 |
35 | $test_set = 1; # Requests we loop over the set... |
89d0527d |
36 | } |
37 | my @args = split(/:/); |
0c721ce2 |
38 | if ($test_set == 1) { |
89d0527d |
39 | my $i; |
40 | for ($i = 0; $i < @set; $i++) { |
0c721ce2 |
41 | # complex number |
42 | $target = $set[$i]; |
43 | # textual value as found in set definition |
44 | $zvalue = $val[$i]; |
89d0527d |
45 | test($zvalue, $target, @args); |
46 | } |
47 | } else { |
48 | test($op, undef, @args); |
49 | } |
50 | } |
51 | |
5cd24f17 |
52 | # test the divbyzeros |
53 | |
54 | test_dbz( |
55 | 'i/0', |
56 | # 'tan(pi/2)', # may succeed thanks to floating point inaccuracies |
57 | # 'sec(pi/2)', # may succeed thanks to floating point inaccuracies |
58 | 'csc(0)', |
59 | 'cot(0)', |
60 | 'atan(i)', |
61 | 'asec(0)', |
62 | 'acsc(0)', |
63 | 'acot(i)', |
64 | # 'tanh(pi/2)', # may succeed thanks to floating point inaccuracies |
65 | # 'sech(pi/2)', # may succeed thanks to floating point inaccuracies |
66 | 'csch(0)', |
67 | 'coth(0)', |
68 | 'atanh(1)', |
69 | 'asech(0)', |
70 | 'acsch(0)', |
71 | 'acoth(1)' |
72 | ); |
73 | |
74 | sub test_dbz { |
75 | for my $op (@_) { |
76 | $test++; |
77 | |
78 | push(@script, qq(eval '$op';)); |
79 | push(@script, qq(print 'not ' unless (\$@ =~ /Division by zero/);)); |
80 | push(@script, qq(print "ok $test\n";)); |
81 | } |
82 | } |
83 | |
89d0527d |
84 | print "1..$test\n"; |
fe82cf6e |
85 | eval join '', @script; |
89d0527d |
86 | die $@ if $@; |
87 | |
88 | sub test { |
89 | my ($op, $z, @args) = @_; |
90 | $test++; |
91 | my $i; |
92 | for ($i = 0; $i < @args; $i++) { |
93 | $val = value($args[$i]); |
fe82cf6e |
94 | push @script, "\$z$i = $val;\n"; |
89d0527d |
95 | } |
96 | if (defined $z) { |
97 | $args = "'$op'"; # Really the value |
0c721ce2 |
98 | $try = "abs(\$z0 - \$z1) <= $eps ? \$z1 : \$z0"; |
fe82cf6e |
99 | push @script, "\$res = $try; "; |
100 | push @script, "check($test, $args[0], \$res, \$z$#args, $args);\n"; |
89d0527d |
101 | } else { |
102 | my ($try, $args); |
103 | if (@args == 2) { |
104 | $try = "$op \$z0"; |
105 | $args = "'$args[0]'"; |
106 | } else { |
107 | $try = ($op =~ /^\w/) ? "$op(\$z0, \$z1)" : "\$z0 $op \$z1"; |
108 | $args = "'$args[0]', '$args[1]'"; |
109 | } |
fe82cf6e |
110 | push @script, "\$res = $try; "; |
111 | push @script, "check($test, '$try', \$res, \$z$#args, $args);\n"; |
89d0527d |
112 | } |
113 | } |
114 | |
115 | sub set { |
116 | my ($set, $setref, $valref) = @_; |
117 | @{$setref} = (); |
118 | @{$valref} = (); |
119 | my @set = split(/;\s*/, $set); |
120 | my @res; |
121 | my $i; |
122 | for ($i = 0; $i < @set; $i++) { |
123 | push(@{$valref}, $set[$i]); |
124 | my $val = value($set[$i]); |
fe82cf6e |
125 | push @script, "\$s$i = $val;\n"; |
126 | push @{$setref}, "\$s$i"; |
89d0527d |
127 | } |
128 | } |
129 | |
130 | sub value { |
131 | local ($_) = @_; |
132 | if (/^\s*\((.*),(.*)\)/) { |
133 | return "cplx($1,$2)"; |
134 | } |
135 | elsif (/^\s*\[(.*),(.*)\]/) { |
136 | return "cplxe($1,$2)"; |
137 | } |
138 | elsif (/^\s*'(.*)'/) { |
139 | my $ex = $1; |
140 | $ex =~ s/\bz\b/$target/g; |
141 | $ex =~ s/\br\b/abs($target)/g; |
142 | $ex =~ s/\bt\b/arg($target)/g; |
143 | $ex =~ s/\ba\b/Re($target)/g; |
144 | $ex =~ s/\bb\b/Im($target)/g; |
145 | return $ex; |
146 | } |
147 | elsif (/^\s*"(.*)"/) { |
148 | return "\"$1\""; |
149 | } |
150 | return $_; |
151 | } |
152 | |
153 | sub check { |
154 | my ($test, $try, $got, $expected, @z) = @_; |
0c721ce2 |
155 | |
156 | # print "# @_\n"; |
157 | |
158 | if ("$got" eq "$expected" |
159 | || |
160 | ($expected =~ /^-?\d/ && $got == $expected) |
161 | || |
162 | (abs($got - $expected) < $eps) |
163 | ) { |
89d0527d |
164 | print "ok $test\n"; |
165 | } else { |
166 | print "not ok $test\n"; |
167 | my $args = (@z == 1) ? "z = $z[0]" : "z0 = $z[0], z1 = $z[1]"; |
168 | print "# '$try' expected: '$expected' got: '$got' for $args\n"; |
169 | } |
170 | } |
171 | __END__ |
172 | &+ |
173 | (3,4):(3,4):(6,8) |
174 | (-3,4):(3,-4):(0,0) |
175 | (3,4):-3:(0,4) |
176 | 1:(4,2):(5,2) |
177 | [2,0]:[2,pi]:(0,0) |
178 | |
179 | &++ |
180 | (2,1):(3,1) |
181 | |
182 | &- |
183 | (2,3):(-2,-3) |
184 | [2,pi/2]:[2,-(pi)/2] |
185 | 2:[2,0]:(0,0) |
186 | [3,0]:2:(1,0) |
187 | 3:(4,5):(-1,-5) |
188 | (4,5):3:(1,5) |
189 | |
190 | &-- |
191 | (1,2):(0,2) |
192 | [2,pi]:[3,pi] |
193 | |
194 | &* |
195 | (0,1):(0,1):(-1,0) |
196 | (4,5):(1,0):(4,5) |
197 | [2,2*pi/3]:(1,0):[2,2*pi/3] |
198 | 2:(0,1):(0,2) |
199 | (0,1):3:(0,3) |
200 | (0,1):(4,1):(-1,4) |
201 | (2,1):(4,-1):(9,2) |
202 | |
203 | &/ |
204 | (3,4):(3,4):(1,0) |
205 | (4,-5):1:(4,-5) |
206 | 1:(0,1):(0,-1) |
207 | (0,6):(0,2):(3,0) |
208 | (9,2):(4,-1):(2,1) |
209 | [4,pi]:[2,pi/2]:[2,pi/2] |
210 | [2,pi/2]:[4,pi]:[0.5,-(pi)/2] |
211 | |
0c721ce2 |
212 | &Re |
213 | (3,4):3 |
214 | (-3,4):-3 |
215 | [1,pi/2]:0 |
216 | |
217 | &Im |
218 | (3,4):4 |
219 | (3,-4):-4 |
220 | [1,pi/2]:1 |
221 | |
89d0527d |
222 | &abs |
223 | (3,4):5 |
224 | (-3,4):5 |
225 | |
0c721ce2 |
226 | &arg |
227 | [2,0]:0 |
228 | [-2,0]:pi |
229 | |
89d0527d |
230 | &~ |
231 | (4,5):(4,-5) |
232 | (-3,4):(-3,-4) |
233 | [2,pi/2]:[2,-(pi)/2] |
234 | |
235 | &< |
236 | (3,4):(1,2):0 |
237 | (3,4):(3,2):0 |
238 | (3,4):(3,8):1 |
239 | (4,4):(5,129):1 |
240 | |
241 | &== |
242 | (3,4):(4,5):0 |
243 | (3,4):(3,5):0 |
244 | (3,4):(2,4):0 |
245 | (3,4):(3,4):1 |
246 | |
247 | &sqrt |
0c721ce2 |
248 | -9:(0,3) |
89d0527d |
249 | (-100,0):(0,10) |
250 | (16,-30):(5,-3) |
251 | |
252 | &stringify_cartesian |
253 | (-100,0):"-100" |
254 | (0,1):"i" |
255 | (4,-3):"4-3i" |
256 | (4,0):"4" |
257 | (-4,0):"-4" |
258 | (-2,4):"-2+4i" |
259 | (-2,-1):"-2-i" |
260 | |
261 | &stringify_polar |
262 | [-1, 0]:"[1,pi]" |
263 | [1, pi/3]:"[1,pi/3]" |
264 | [6, -2*pi/3]:"[6,-2pi/3]" |
265 | [0.5, -9*pi/11]:"[0.5,-9pi/11]" |
266 | |
267 | { (4,3); [3,2]; (-3,4); (0,2); [2,1] } |
268 | |
269 | |'z + ~z':'2*Re(z)' |
270 | |'z - ~z':'2*i*Im(z)' |
271 | |'z * ~z':'abs(z) * abs(z)' |
272 | |
0c721ce2 |
273 | { (2,3); [3,2]; (-3,2); (0,2); 3; 1.2; -3; (-3, 0); (-2, -1); [2,1] } |
89d0527d |
274 | |
0c721ce2 |
275 | |'(root(z, 4))[1] ** 4':'z' |
276 | |'(root(z, 5))[3] ** 5':'z' |
277 | |'(root(z, 8))[7] ** 8':'z' |
89d0527d |
278 | |'abs(z)':'r' |
0c721ce2 |
279 | |'acot(z)':'acotan(z)' |
280 | |'acsc(z)':'acosec(z)' |
281 | |'acsc(z)':'asin(1 / z)' |
282 | |'asec(z)':'acos(1 / z)' |
89d0527d |
283 | |'cbrt(z)':'cbrt(r) * exp(i * t/3)' |
89d0527d |
284 | |'cos(acos(z))':'z' |
89d0527d |
285 | |'cos(z) ** 2 + sin(z) ** 2':1 |
89d0527d |
286 | |'cos(z)':'cosh(i*z)' |
0c721ce2 |
287 | |'cosh(z) ** 2 - sinh(z) ** 2':1 |
288 | |'cot(acot(z))':'z' |
289 | |'cot(z)':'1 / tan(z)' |
290 | |'cot(z)':'cotan(z)' |
291 | |'csc(acsc(z))':'z' |
292 | |'csc(z)':'1 / sin(z)' |
293 | |'csc(z)':'cosec(z)' |
89d0527d |
294 | |'exp(log(z))':'z' |
0c721ce2 |
295 | |'exp(z)':'exp(a) * exp(i * b)' |
296 | |'ln(z)':'log(z)' |
297 | |'log(exp(z))':'z' |
298 | |'log(z)':'log(r) + i*t' |
89d0527d |
299 | |'log10(z)':'log(z) / log(10)' |
89d0527d |
300 | |'logn(z, 2)':'log(z) / log(2)' |
0c721ce2 |
301 | |'logn(z, 3)':'log(z) / log(3)' |
302 | |'sec(asec(z))':'z' |
303 | |'sec(z)':'1 / cos(z)' |
304 | |'sin(asin(z))':'z' |
305 | |'sin(i * z)':'i * sinh(z)' |
306 | |'sqrt(z) * sqrt(z)':'z' |
307 | |'sqrt(z)':'sqrt(r) * exp(i * t/2)' |
308 | |'tan(atan(z))':'z' |
309 | |'z**z':'exp(z * log(z))' |
89d0527d |
310 | |
0c721ce2 |
311 | { (1,1); [1,0.5]; (-2, -1); 2; -3; (-1,0.5); (0,0.5); 0.5; (2, 0); (-1, -2) } |
89d0527d |
312 | |
89d0527d |
313 | |'cosh(acosh(z))':'z' |
0c721ce2 |
314 | |'coth(acoth(z))':'z' |
315 | |'coth(z)':'1 / tanh(z)' |
316 | |'coth(z)':'cotanh(z)' |
317 | |'csch(acsch(z))':'z' |
318 | |'csch(z)':'1 / sinh(z)' |
319 | |'csch(z)':'cosech(z)' |
320 | |'sech(asech(z))':'z' |
321 | |'sech(z)':'1 / cosh(z)' |
322 | |'sinh(asinh(z))':'z' |
89d0527d |
323 | |'tanh(atanh(z))':'z' |
89d0527d |
324 | |
0c721ce2 |
325 | { (0.2,-0.4); [1,0.5]; -1.2; (-1,0.5); 0.5; (1.1, 0) } |
89d0527d |
326 | |
89d0527d |
327 | |'acos(cos(z)) ** 2':'z * z' |
89d0527d |
328 | |'acosh(cosh(z)) ** 2':'z * z' |
0c721ce2 |
329 | |'acoth(z)':'acotanh(z)' |
330 | |'acoth(z)':'atanh(1 / z)' |
331 | |'acsch(z)':'acosech(z)' |
332 | |'acsch(z)':'asinh(1 / z)' |
333 | |'asech(z)':'acosh(1 / z)' |
334 | |'asin(sin(z))':'z' |
335 | |'asinh(sinh(z))':'z' |
336 | |'atan(tan(z))':'z' |
89d0527d |
337 | |'atanh(tanh(z))':'z' |
338 | |
0c721ce2 |
339 | &sin |
340 | ( 2, 3):( 9.15449914691143, -4.16890695996656) |
341 | (-2, 3):( -9.15449914691143, -4.16890695996656) |
342 | (-2,-3):( -9.15449914691143, 4.16890695996656) |
343 | ( 2,-3):( 9.15449914691143, 4.16890695996656) |
344 | |
345 | &cos |
346 | ( 2, 3):( -4.18962569096881, -9.10922789375534) |
347 | (-2, 3):( -4.18962569096881, 9.10922789375534) |
348 | (-2,-3):( -4.18962569096881, -9.10922789375534) |
349 | ( 2,-3):( -4.18962569096881, 9.10922789375534) |
350 | |
351 | &tan |
352 | ( 2, 3):( -0.00376402564150, 1.00323862735361) |
353 | (-2, 3):( 0.00376402564150, 1.00323862735361) |
354 | (-2,-3):( 0.00376402564150, -1.00323862735361) |
355 | ( 2,-3):( -0.00376402564150, -1.00323862735361) |
356 | |
357 | &sec |
358 | ( 2, 3):( -0.04167496441114, 0.09061113719624) |
359 | (-2, 3):( -0.04167496441114, -0.09061113719624) |
360 | (-2,-3):( -0.04167496441114, 0.09061113719624) |
361 | ( 2,-3):( -0.04167496441114, -0.09061113719624) |
362 | |
363 | &csc |
364 | ( 2, 3):( 0.09047320975321, 0.04120098628857) |
365 | (-2, 3):( -0.09047320975321, 0.04120098628857) |
366 | (-2,-3):( -0.09047320975321, -0.04120098628857) |
367 | ( 2,-3):( 0.09047320975321, -0.04120098628857) |
368 | |
369 | &cot |
370 | ( 2, 3):( -0.00373971037634, -0.99675779656936) |
371 | (-2, 3):( 0.00373971037634, -0.99675779656936) |
372 | (-2,-3):( 0.00373971037634, 0.99675779656936) |
373 | ( 2,-3):( -0.00373971037634, 0.99675779656936) |
374 | |
375 | &asin |
376 | ( 2, 3):( 0.57065278432110, 1.98338702991654) |
377 | (-2, 3):( -0.57065278432110, 1.98338702991654) |
378 | (-2,-3):( -0.57065278432110, -1.98338702991654) |
379 | ( 2,-3):( 0.57065278432110, -1.98338702991654) |
380 | |
381 | &acos |
382 | ( 2, 3):( 1.00014354247380, -1.98338702991654) |
383 | (-2, 3):( 2.14144911111600, -1.98338702991654) |
384 | (-2,-3):( 2.14144911111600, 1.98338702991654) |
385 | ( 2,-3):( 1.00014354247380, 1.98338702991654) |
386 | |
387 | &atan |
388 | ( 2, 3):( 1.40992104959658, 0.22907268296854) |
389 | (-2, 3):( -1.40992104959658, 0.22907268296854) |
390 | (-2,-3):( -1.40992104959658, -0.22907268296854) |
391 | ( 2,-3):( 1.40992104959658, -0.22907268296854) |
392 | |
393 | &asec |
394 | ( 2, 3):( 1.42041072246703, 0.23133469857397) |
395 | (-2, 3):( 1.72118193112276, 0.23133469857397) |
396 | (-2,-3):( 1.72118193112276, -0.23133469857397) |
397 | ( 2,-3):( 1.42041072246703, -0.23133469857397) |
398 | |
399 | &acsc |
400 | ( 2, 3):( 0.15038560432786, -0.23133469857397) |
401 | (-2, 3):( -0.15038560432786, -0.23133469857397) |
402 | (-2,-3):( -0.15038560432786, 0.23133469857397) |
403 | ( 2,-3):( 0.15038560432786, 0.23133469857397) |
404 | |
405 | &acot |
406 | ( 2, 3):( 0.16087527719832, -0.22907268296854) |
407 | (-2, 3):( -0.16087527719832, -0.22907268296854) |
408 | (-2,-3):( -0.16087527719832, 0.22907268296854) |
409 | ( 2,-3):( 0.16087527719832, 0.22907268296854) |
410 | |
411 | &sinh |
412 | ( 2, 3):( -3.59056458998578, 0.53092108624852) |
413 | (-2, 3):( 3.59056458998578, 0.53092108624852) |
414 | (-2,-3):( 3.59056458998578, -0.53092108624852) |
415 | ( 2,-3):( -3.59056458998578, -0.53092108624852) |
416 | |
417 | &cosh |
418 | ( 2, 3):( -3.72454550491532, 0.51182256998738) |
419 | (-2, 3):( -3.72454550491532, -0.51182256998738) |
420 | (-2,-3):( -3.72454550491532, 0.51182256998738) |
421 | ( 2,-3):( -3.72454550491532, -0.51182256998738) |
422 | |
423 | &tanh |
424 | ( 2, 3):( 0.96538587902213, -0.00988437503832) |
425 | (-2, 3):( -0.96538587902213, -0.00988437503832) |
426 | (-2,-3):( -0.96538587902213, 0.00988437503832) |
427 | ( 2,-3):( 0.96538587902213, 0.00988437503832) |
428 | |
429 | &sech |
430 | ( 2, 3):( -0.26351297515839, -0.03621163655877) |
431 | (-2, 3):( -0.26351297515839, 0.03621163655877) |
432 | (-2,-3):( -0.26351297515839, -0.03621163655877) |
433 | ( 2,-3):( -0.26351297515839, 0.03621163655877) |
434 | |
435 | &csch |
436 | ( 2, 3):( -0.27254866146294, -0.04030057885689) |
437 | (-2, 3):( 0.27254866146294, -0.04030057885689) |
438 | (-2,-3):( 0.27254866146294, 0.04030057885689) |
439 | ( 2,-3):( -0.27254866146294, 0.04030057885689) |
440 | |
441 | &coth |
442 | ( 2, 3):( 1.03574663776500, 0.01060478347034) |
443 | (-2, 3):( -1.03574663776500, 0.01060478347034) |
444 | (-2,-3):( -1.03574663776500, -0.01060478347034) |
445 | ( 2,-3):( 1.03574663776500, -0.01060478347034) |
446 | |
447 | &asinh |
448 | ( 2, 3):( 1.96863792579310, 0.96465850440760) |
449 | (-2, 3):( -1.96863792579310, 0.96465850440761) |
450 | (-2,-3):( -1.96863792579310, -0.96465850440761) |
451 | ( 2,-3):( 1.96863792579310, -0.96465850440760) |
452 | |
453 | &acosh |
454 | ( 2, 3):( 1.98338702991654, 1.00014354247380) |
455 | (-2, 3):( -1.98338702991653, -2.14144911111600) |
456 | (-2,-3):( -1.98338702991653, 2.14144911111600) |
457 | ( 2,-3):( 1.98338702991654, -1.00014354247380) |
458 | |
459 | &atanh |
460 | ( 2, 3):( 0.14694666622553, 1.33897252229449) |
461 | (-2, 3):( -0.14694666622553, 1.33897252229449) |
462 | (-2,-3):( -0.14694666622553, -1.33897252229449) |
463 | ( 2,-3):( 0.14694666622553, -1.33897252229449) |
464 | |
465 | &asech |
466 | ( 2, 3):( 0.23133469857397, -1.42041072246703) |
467 | (-2, 3):( -0.23133469857397, 1.72118193112276) |
468 | (-2,-3):( -0.23133469857397, -1.72118193112276) |
469 | ( 2,-3):( 0.23133469857397, 1.42041072246703) |
470 | |
471 | &acsch |
472 | ( 2, 3):( 0.15735549884499, -0.22996290237721) |
473 | (-2, 3):( -0.15735549884499, -0.22996290237721) |
474 | (-2,-3):( -0.15735549884499, 0.22996290237721) |
475 | ( 2,-3):( 0.15735549884499, 0.22996290237721) |
476 | |
477 | &acoth |
478 | ( 2, 3):( 0.14694666622553, -0.23182380450040) |
479 | (-2, 3):( -0.14694666622553, -0.23182380450040) |
480 | (-2,-3):( -0.14694666622553, 0.23182380450040) |
481 | ( 2,-3):( 0.14694666622553, 0.23182380450040) |
482 | |
483 | # eof |