Commit | Line | Data |
184f15d5 |
1 | #!/usr/bin/perl -w |
2 | |
3 | use strict; |
4 | use Test; |
5 | |
6 | BEGIN |
7 | { |
8 | $| = 1; |
9 | chdir 't' if -d 't'; |
10 | unshift @INC, '../lib'; # for running manually |
6de7f0cc |
11 | plan tests => 83; |
184f15d5 |
12 | } |
13 | |
14 | # testing of Math::BigRat |
15 | |
16 | use Math::BigRat; |
6de7f0cc |
17 | use Math::BigInt; |
18 | use Math::BigFloat; |
19 | |
20 | # shortcuts |
21 | my $cr = 'Math::BigRat'; |
22 | my $mbi = 'Math::BigInt'; |
23 | my $mbf = 'Math::BigFloat'; |
184f15d5 |
24 | |
25 | my ($x,$y,$z); |
26 | |
27 | $x = Math::BigRat->new(1234); ok ($x,1234); |
8f675a64 |
28 | ok ($x->isa('Math::BigRat')); |
29 | ok (!$x->isa('Math::BigFloat')); |
30 | ok (!$x->isa('Math::BigInt')); |
31 | |
32 | ############################################################################## |
6de7f0cc |
33 | # new and bnorm() |
8f675a64 |
34 | |
6de7f0cc |
35 | foreach my $func (qw/new bnorm/) |
36 | { |
37 | $x = $cr->$func(1234); ok ($x,1234); |
38 | |
39 | $x = $cr->$func('1234/1'); ok ($x,1234); |
40 | $x = $cr->$func('1234/2'); ok ($x,617); |
41 | |
42 | $x = $cr->$func('100/1.0'); ok ($x,100); |
43 | $x = $cr->$func('10.0/1.0'); ok ($x,10); |
44 | $x = $cr->$func('0.1/10'); ok ($x,'1/100'); |
45 | $x = $cr->$func('0.1/0.1'); ok ($x,'1'); |
46 | $x = $cr->$func('1e2/10'); ok ($x,10); |
47 | $x = $cr->$func('1e2/1e1'); ok ($x,10); |
48 | $x = $cr->$func('1 / 3'); ok ($x,'1/3'); |
49 | $x = $cr->$func('-1 / 3'); ok ($x,'-1/3'); |
50 | $x = $cr->$func('NaN'); ok ($x,'NaN'); |
51 | $x = $cr->$func('inf'); ok ($x,'inf'); |
52 | $x = $cr->$func('-inf'); ok ($x,'-inf'); |
53 | $x = $cr->$func('1/'); ok ($x,'NaN'); |
54 | |
55 | # input ala '1+1/3' isn't parsed ok yet |
56 | $x = $cr->$func('1+1/3'); ok ($x,'NaN'); |
57 | |
58 | ############################################################################ |
59 | # other classes as input |
60 | |
61 | $x = $cr->$func($mbi->new(1231)); ok ($x,'1231'); |
62 | $x = $cr->$func($mbf->new(1232)); ok ($x,'1232'); |
63 | $x = $cr->$func($mbf->new(1232.3)); ok ($x,'12323/10'); |
64 | } |
8f675a64 |
65 | |
66 | ############################################################################## |
67 | # mixed arguments |
68 | |
69 | ok (Math::BigRat->new('3/7')->badd(1),'10/7'); |
70 | ok (Math::BigRat->new('3/10')->badd(1.1),'7/5'); |
71 | ok (Math::BigRat->new('3/7')->badd(Math::BigInt->new(1)),'10/7'); |
72 | ok (Math::BigRat->new('3/10')->badd(Math::BigFloat->new('1.1')),'7/5'); |
73 | |
74 | ok (Math::BigRat->new('3/7')->bsub(1),'-4/7'); |
75 | ok (Math::BigRat->new('3/10')->bsub(1.1),'-4/5'); |
76 | ok (Math::BigRat->new('3/7')->bsub(Math::BigInt->new(1)),'-4/7'); |
77 | ok (Math::BigRat->new('3/10')->bsub(Math::BigFloat->new('1.1')),'-4/5'); |
78 | |
79 | ok (Math::BigRat->new('3/7')->bmul(1),'3/7'); |
80 | ok (Math::BigRat->new('3/10')->bmul(1.1),'33/100'); |
81 | ok (Math::BigRat->new('3/7')->bmul(Math::BigInt->new(1)),'3/7'); |
82 | ok (Math::BigRat->new('3/10')->bmul(Math::BigFloat->new('1.1')),'33/100'); |
83 | |
84 | ok (Math::BigRat->new('3/7')->bdiv(1),'3/7'); |
85 | ok (Math::BigRat->new('3/10')->bdiv(1.1),'3/11'); |
86 | ok (Math::BigRat->new('3/7')->bdiv(Math::BigInt->new(1)),'3/7'); |
87 | ok (Math::BigRat->new('3/10')->bdiv(Math::BigFloat->new('1.1')),'3/11'); |
88 | |
89 | ############################################################################## |
184f15d5 |
90 | $x = Math::BigRat->new('1/4'); $y = Math::BigRat->new('1/3'); |
91 | ok ($x + $y, '7/12'); |
92 | ok ($x * $y, '1/12'); |
93 | ok ($x / $y, '3/4'); |
94 | |
95 | $x = Math::BigRat->new('7/5'); $x *= '3/2'; |
96 | ok ($x,'21/10'); |
97 | $x -= '0.1'; |
98 | ok ($x,'2'); # not 21/10 |
99 | |
100 | $x = Math::BigRat->new('2/3'); $y = Math::BigRat->new('3/2'); |
101 | ok ($x > $y,''); |
102 | ok ($x < $y,1); |
103 | ok ($x == $y,''); |
104 | |
105 | $x = Math::BigRat->new('-2/3'); $y = Math::BigRat->new('3/2'); |
106 | ok ($x > $y,''); |
107 | ok ($x < $y,'1'); |
108 | ok ($x == $y,''); |
109 | |
110 | $x = Math::BigRat->new('-2/3'); $y = Math::BigRat->new('-2/3'); |
111 | ok ($x > $y,''); |
112 | ok ($x < $y,''); |
113 | ok ($x == $y,'1'); |
114 | |
115 | $x = Math::BigRat->new('-2/3'); $y = Math::BigRat->new('-1/3'); |
116 | ok ($x > $y,''); |
117 | ok ($x < $y,'1'); |
118 | ok ($x == $y,''); |
119 | |
120 | $x = Math::BigRat->new('-124'); $y = Math::BigRat->new('-122'); |
121 | ok ($x->bacmp($y),1); |
122 | |
123 | $x = Math::BigRat->new('-124'); $y = Math::BigRat->new('-122'); |
124 | ok ($x->bcmp($y),-1); |
125 | |
8f675a64 |
126 | $x = Math::BigRat->new('3/7'); $y = Math::BigRat->new('5/7'); |
127 | ok ($x+$y,'8/7'); |
128 | |
129 | $x = Math::BigRat->new('3/7'); $y = Math::BigRat->new('5/7'); |
130 | ok ($x*$y,'15/49'); |
131 | |
132 | $x = Math::BigRat->new('3/5'); $y = Math::BigRat->new('5/7'); |
133 | ok ($x*$y,'3/7'); |
134 | |
135 | $x = Math::BigRat->new('3/5'); $y = Math::BigRat->new('5/7'); |
136 | ok ($x/$y,'21/25'); |
137 | |
184f15d5 |
138 | $x = Math::BigRat->new('-144/9'); $x->bsqrt(); ok ($x,'NaN'); |
139 | $x = Math::BigRat->new('144/9'); $x->bsqrt(); ok ($x,'4'); |
140 | |
141 | # done |
142 | |
143 | 1; |
144 | |