[PATCH9 BigInt v1.60 fix for "\n"
[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;
e745a66c 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);
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";
dccbb853 33 plan tests => 141;
07d34614 34 if ($] < 5.006)
35 {
36 for (1..141) { skip (1,'Not supported on older Perls'); }
37 exit;
38 }
ee15d750 39 }
40
41package Math::BigInt::Test;
42
43use Math::BigInt;
44use vars qw/@ISA/;
45@ISA = qw/Math::BigInt/; # child of MBI
46use overload;
47
48package Math::BigFloat::Test;
49
50use Math::BigFloat;
51use vars qw/@ISA/;
52@ISA = qw/Math::BigFloat/; # child of MBI
53use overload;
54
55package main;
56
57use Math::BigInt;
58use Math::BigFloat;
59
60my ($x,$y,$z,$u);
e745a66c 61my $version = '1.46'; # adjust manually to match latest release
ee15d750 62
63###############################################################################
64# check whether op's accept normal strings, even when inherited by subclasses
65
66# do one positive and one negative test to avoid false positives by "accident"
67
68my ($func,@args,$ans,$rc,$class,$try);
69while (<DATA>)
70 {
d614cd8b 71 chomp;
ee15d750 72 next if /^#/; # skip comments
73 if (s/^&//)
74 {
75 $func = $_;
76 }
77 else
78 {
79 @args = split(/:/,$_,99);
80 $ans = pop @args;
81 foreach $class (qw/
82 Math::BigInt Math::BigFloat Math::BigInt::Test Math::BigFloat::Test/)
83 {
dccbb853 84 $try = "'$args[0]'"; # quote it
85 $try = $args[0] if $args[0] =~ /'/; # already quoted
86 $try = '' if $args[0] eq ''; # undef, no argument
87 $try = "$class\->$func($try);";
ee15d750 88 $rc = eval $try;
89 print "# Tried: '$try'\n" if !ok ($rc, $ans);
90 }
91 }
92
93 }
94
dccbb853 95$class = 'Math::BigInt';
96
97# test whether use Math::BigInt qw/version/ works
98$try = "use $class ($version.'1');";
99$try .= ' $x = $class->new(123); $x = "$x";';
100eval $try;
101ok_undef ( $_ ); # should result in error!
102
103# test whether fallback to calc works
104$try = "use $class ($version,'lib','foo, bar , ');";
b3abae2a 105$try .= "$class\->config()->{lib};";
dccbb853 106$ans = eval $try;
107ok ( $ans, "Math::BigInt::Calc");
108
109# test whether constant works or not, also test for qw($version)
110# bgcd() is present in subclass, too
111$try = "use Math::BigInt ($version,'bgcd',':constant');";
112$try .= ' $x = 2**150; bgcd($x); $x = "$x";';
113$ans = eval $try;
114ok ( $ans, "1427247692705959881058285969449495136382746624");
115
116# test wether Math::BigInt::Scalar via use works (w/ dff. spellings of calc)
117$try = "use $class ($version,'lib','Scalar');";
118$try .= ' $x = 2**10; $x = "$x";';
119$ans = eval $try; ok ( $ans, "1024");
120$try = "use $class ($version,'LiB','$class\::Scalar');";
121$try .= ' $x = 2**10; $x = "$x";';
122$ans = eval $try; ok ( $ans, "1024");
123
124# test wether calc => undef (array element not existing) works
125# no longer supported
126#$try = "use $class ($version,'LIB');";
127#$try = "require $class; $class\::import($version,'CALC');";
128#$try .= " \$x = $class\->new(2)**10; \$x = ".'"$x";';
129#print "$try\n";
130#$ans = eval $try; ok ( $ans, 1024);
131
ee15d750 132# all done
133
134###############################################################################
135# Perl 5.005 does not like ok ($x,undef)
136
137sub ok_undef
138 {
139 my $x = shift;
140
141 ok (1,1) and return if !defined $x;
142 ok ($x,'undef');
143 }
144
145__END__
146&is_zero
1471:0
1480:1
149&is_one
1501:1
1510:0
152&is_positive
1531:1
154-1:0
155&is_negative
1561:0
157-1:1
158&is_nan
159abc:1
1601:0
161&is_inf
162inf:1
1630:0
164&bstr
1655:5
16610:10
167abc:NaN
dccbb853 168'+inf':inf
169'-inf':-inf
ee15d750 170&bsstr
1711:1e+0
1720:0e+1
1732:2e+0
174200:2e+2
175&babs
176-1:1
1771:1
178&bnot
179-2:1
1801:-2
dccbb853 181&bzero
182:0
183&bnan
184:NaN
185abc:NaN
186&bone
187:1
188'+':1
189'-':-1
190&binf
191:inf
192'+':inf
193'-':-inf