Commit | Line | Data |
ee15d750 |
1 | #!/usr/bin/perl -w |
2 | |
dccbb853 |
3 | # test calling conventions, and :constant overloading |
ee15d750 |
4 | |
5 | use strict; |
6 | use Test; |
7 | |
8 | BEGIN |
9 | { |
10 | $| = 1; |
11 | # chdir 't' if -d 't'; |
12 | unshift @INC, '../lib'; # for running manually |
dccbb853 |
13 | plan tests => 141; |
ee15d750 |
14 | } |
15 | |
16 | package Math::BigInt::Test; |
17 | |
18 | use Math::BigInt; |
19 | use vars qw/@ISA/; |
20 | @ISA = qw/Math::BigInt/; # child of MBI |
21 | use overload; |
22 | |
23 | package Math::BigFloat::Test; |
24 | |
25 | use Math::BigFloat; |
26 | use vars qw/@ISA/; |
27 | @ISA = qw/Math::BigFloat/; # child of MBI |
28 | use overload; |
29 | |
30 | package main; |
31 | |
32 | use Math::BigInt; |
33 | use Math::BigFloat; |
34 | |
35 | my ($x,$y,$z,$u); |
dccbb853 |
36 | my $version = '1.45'; # adjust manually to match latest release |
ee15d750 |
37 | |
38 | ############################################################################### |
39 | # check whether op's accept normal strings, even when inherited by subclasses |
40 | |
41 | # do one positive and one negative test to avoid false positives by "accident" |
42 | |
43 | my ($func,@args,$ans,$rc,$class,$try); |
44 | while (<DATA>) |
45 | { |
46 | chop; |
47 | next if /^#/; # skip comments |
48 | if (s/^&//) |
49 | { |
50 | $func = $_; |
51 | } |
52 | else |
53 | { |
54 | @args = split(/:/,$_,99); |
55 | $ans = pop @args; |
56 | foreach $class (qw/ |
57 | Math::BigInt Math::BigFloat Math::BigInt::Test Math::BigFloat::Test/) |
58 | { |
dccbb853 |
59 | $try = "'$args[0]'"; # quote it |
60 | $try = $args[0] if $args[0] =~ /'/; # already quoted |
61 | $try = '' if $args[0] eq ''; # undef, no argument |
62 | $try = "$class\->$func($try);"; |
ee15d750 |
63 | $rc = eval $try; |
64 | print "# Tried: '$try'\n" if !ok ($rc, $ans); |
65 | } |
66 | } |
67 | |
68 | } |
69 | |
dccbb853 |
70 | $class = 'Math::BigInt'; |
71 | |
72 | # test whether use Math::BigInt qw/version/ works |
73 | $try = "use $class ($version.'1');"; |
74 | $try .= ' $x = $class->new(123); $x = "$x";'; |
75 | eval $try; |
76 | ok_undef ( $_ ); # should result in error! |
77 | |
78 | # test whether fallback to calc works |
79 | $try = "use $class ($version,'lib','foo, bar , ');"; |
80 | $try .= "$class\->_core_lib();"; |
81 | $ans = eval $try; |
82 | ok ( $ans, "Math::BigInt::Calc"); |
83 | |
84 | # test whether constant works or not, also test for qw($version) |
85 | # bgcd() is present in subclass, too |
86 | $try = "use Math::BigInt ($version,'bgcd',':constant');"; |
87 | $try .= ' $x = 2**150; bgcd($x); $x = "$x";'; |
88 | $ans = eval $try; |
89 | ok ( $ans, "1427247692705959881058285969449495136382746624"); |
90 | |
91 | # test wether Math::BigInt::Scalar via use works (w/ dff. spellings of calc) |
92 | $try = "use $class ($version,'lib','Scalar');"; |
93 | $try .= ' $x = 2**10; $x = "$x";'; |
94 | $ans = eval $try; ok ( $ans, "1024"); |
95 | $try = "use $class ($version,'LiB','$class\::Scalar');"; |
96 | $try .= ' $x = 2**10; $x = "$x";'; |
97 | $ans = eval $try; ok ( $ans, "1024"); |
98 | |
99 | # test wether calc => undef (array element not existing) works |
100 | # no longer supported |
101 | #$try = "use $class ($version,'LIB');"; |
102 | #$try = "require $class; $class\::import($version,'CALC');"; |
103 | #$try .= " \$x = $class\->new(2)**10; \$x = ".'"$x";'; |
104 | #print "$try\n"; |
105 | #$ans = eval $try; ok ( $ans, 1024); |
106 | |
ee15d750 |
107 | # all done |
108 | |
109 | ############################################################################### |
110 | # Perl 5.005 does not like ok ($x,undef) |
111 | |
112 | sub ok_undef |
113 | { |
114 | my $x = shift; |
115 | |
116 | ok (1,1) and return if !defined $x; |
117 | ok ($x,'undef'); |
118 | } |
119 | |
120 | __END__ |
121 | &is_zero |
122 | 1:0 |
123 | 0:1 |
124 | &is_one |
125 | 1:1 |
126 | 0:0 |
127 | &is_positive |
128 | 1:1 |
129 | -1:0 |
130 | &is_negative |
131 | 1:0 |
132 | -1:1 |
133 | &is_nan |
134 | abc:1 |
135 | 1:0 |
136 | &is_inf |
137 | inf:1 |
138 | 0:0 |
139 | &bstr |
140 | 5:5 |
141 | 10:10 |
142 | abc:NaN |
dccbb853 |
143 | '+inf':inf |
144 | '-inf':-inf |
ee15d750 |
145 | &bsstr |
146 | 1:1e+0 |
147 | 0:0e+1 |
148 | 2:2e+0 |
149 | 200:2e+2 |
150 | &babs |
151 | -1:1 |
152 | 1:1 |
153 | &bnot |
154 | -2:1 |
155 | 1:-2 |
dccbb853 |
156 | &bzero |
157 | :0 |
158 | &bnan |
159 | :NaN |
160 | abc:NaN |
161 | &bone |
162 | :1 |
163 | '+':1 |
164 | '-':-1 |
165 | &binf |
166 | :inf |
167 | '+':inf |
168 | '-':-inf |