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