Re: PERFORCE change 12943 for review
[p5sagit/p5-mst-13.2.git] / lib / Math / BigInt / t / calling.t
CommitLineData
ee15d750 1#!/usr/bin/perl -w
2
dccbb853 3# test calling conventions, and :constant overloading
ee15d750 4
5use strict;
6use Test;
7
8BEGIN
9 {
10 $| = 1;
11 # chdir 't' if -d 't';
12 unshift @INC, '../lib'; # for running manually
dccbb853 13 plan tests => 141;
ee15d750 14 }
15
16package Math::BigInt::Test;
17
18use Math::BigInt;
19use vars qw/@ISA/;
20@ISA = qw/Math::BigInt/; # child of MBI
21use overload;
22
23package Math::BigFloat::Test;
24
25use Math::BigFloat;
26use vars qw/@ISA/;
27@ISA = qw/Math::BigFloat/; # child of MBI
28use overload;
29
30package main;
31
32use Math::BigInt;
33use Math::BigFloat;
34
35my ($x,$y,$z,$u);
dccbb853 36my $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
43my ($func,@args,$ans,$rc,$class,$try);
44while (<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";';
75eval $try;
76ok_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;
82ok ( $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;
89ok ( $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
112sub 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
1221:0
1230:1
124&is_one
1251:1
1260:0
127&is_positive
1281:1
129-1:0
130&is_negative
1311:0
132-1:1
133&is_nan
134abc:1
1351:0
136&is_inf
137inf:1
1380:0
139&bstr
1405:5
14110:10
142abc:NaN
dccbb853 143'+inf':inf
144'-inf':-inf
ee15d750 145&bsstr
1461:1e+0
1470:0e+1
1482:2e+0
149200:2e+2
150&babs
151-1:1
1521:1
153&bnot
154-2:1
1551:-2
dccbb853 156&bzero
157:0
158&bnan
159:NaN
160abc:NaN
161&bone
162:1
163'+':1
164'-':-1
165&binf
166:inf
167'+':inf
168'-':-inf