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 |
a4e2b1c6 |
11 | plan tests => 136; |
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); |
a4e2b1c6 |
48 | $x = $cr->$func('1 / 3'); ok ($x,'1/3'); |
6de7f0cc |
49 | $x = $cr->$func('-1 / 3'); ok ($x,'-1/3'); |
a4e2b1c6 |
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'); |
6de7f0cc |
54 | |
55 | # input ala '1+1/3' isn't parsed ok yet |
a4e2b1c6 |
56 | $x = $cr->$func('1+1/3'); ok ($x,'NaN'); |
6de7f0cc |
57 | |
58 | ############################################################################ |
59 | # other classes as input |
60 | |
a4e2b1c6 |
61 | $x = $cr->$func($mbi->new(1231)); ok ($x,'1231'); |
62 | $x = $cr->$func($mbf->new(1232)); ok ($x,'1232'); |
6de7f0cc |
63 | $x = $cr->$func($mbf->new(1232.3)); ok ($x,'12323/10'); |
64 | } |
a4e2b1c6 |
65 | |
66 | $x = $cr->new('-0'); ok ($x,'0'); ok ($x->{_n}, '0'); ok ($x->{_d},'1'); |
67 | $x = $cr->new('NaN'); ok ($x,'NaN'); ok ($x->{_n}, '0'); ok ($x->{_d},'0'); |
68 | $x = $cr->new('-NaN'); ok ($x,'NaN'); ok ($x->{_n}, '0'); ok ($x->{_d},'0'); |
69 | $x = $cr->new('-1r4'); ok ($x,'NaN'); ok ($x->{_n}, '0'); ok ($x->{_d},'0'); |
70 | |
71 | $x = $cr->new('+inf'); ok ($x,'inf'); ok ($x->{_n}, '0'); ok ($x->{_d},'0'); |
72 | $x = $cr->new('-inf'); ok ($x,'-inf'); ok ($x->{_n}, '0'); ok ($x->{_d},'0'); |
73 | $x = $cr->new('123a4'); ok ($x,'NaN'); ok ($x->{_n}, '0'); ok ($x->{_d},'0'); |
74 | |
75 | # wrong inputs |
76 | $x = $cr->new('1e2e2'); ok ($x,'NaN'); ok ($x->{_n}, '0'); ok ($x->{_d},'0'); |
77 | $x = $cr->new('1+2+2'); ok ($x,'NaN'); ok ($x->{_n}, '0'); ok ($x->{_d},'0'); |
78 | # failed due to BigFlaot bug |
79 | $x = $cr->new('1.2.2'); ok ($x,'NaN'); ok ($x->{_n}, '0'); ok ($x->{_d},'0'); |
80 | |
81 | ok ($cr->new('123a4'),'NaN'); |
82 | ok ($cr->new('123e4'),'1230000'); |
83 | ok ($cr->new('-NaN'),'NaN'); |
84 | ok ($cr->new('NaN'),'NaN'); |
85 | ok ($cr->new('+inf'),'inf'); |
86 | ok ($cr->new('-inf'),'-inf'); |
8f675a64 |
87 | |
88 | ############################################################################## |
89 | # mixed arguments |
90 | |
a4e2b1c6 |
91 | ok ($cr->new('3/7')->badd(1),'10/7'); |
92 | ok ($cr->new('3/10')->badd(1.1),'7/5'); |
93 | ok ($cr->new('3/7')->badd($mbi->new(1)),'10/7'); |
94 | ok ($cr->new('3/10')->badd($mbf->new('1.1')),'7/5'); |
8f675a64 |
95 | |
a4e2b1c6 |
96 | ok ($cr->new('3/7')->bsub(1),'-4/7'); |
97 | ok ($cr->new('3/10')->bsub(1.1),'-4/5'); |
98 | ok ($cr->new('3/7')->bsub($mbi->new(1)),'-4/7'); |
99 | ok ($cr->new('3/10')->bsub($mbf->new('1.1')),'-4/5'); |
8f675a64 |
100 | |
a4e2b1c6 |
101 | ok ($cr->new('3/7')->bmul(1),'3/7'); |
102 | ok ($cr->new('3/10')->bmul(1.1),'33/100'); |
103 | ok ($cr->new('3/7')->bmul($mbi->new(1)),'3/7'); |
104 | ok ($cr->new('3/10')->bmul($mbf->new('1.1')),'33/100'); |
8f675a64 |
105 | |
a4e2b1c6 |
106 | ok ($cr->new('3/7')->bdiv(1),'3/7'); |
107 | ok ($cr->new('3/10')->bdiv(1.1),'3/11'); |
108 | ok ($cr->new('3/7')->bdiv($mbi->new(1)),'3/7'); |
109 | ok ($cr->new('3/10')->bdiv($mbf->new('1.1')),'3/11'); |
8f675a64 |
110 | |
111 | ############################################################################## |
a4e2b1c6 |
112 | $x = $cr->new('1/4'); $y = $cr->new('1/3'); |
184f15d5 |
113 | ok ($x + $y, '7/12'); |
114 | ok ($x * $y, '1/12'); |
115 | ok ($x / $y, '3/4'); |
116 | |
a4e2b1c6 |
117 | $x = $cr->new('7/5'); $x *= '3/2'; |
184f15d5 |
118 | ok ($x,'21/10'); |
119 | $x -= '0.1'; |
120 | ok ($x,'2'); # not 21/10 |
121 | |
a4e2b1c6 |
122 | $x = $cr->new('2/3'); $y = $cr->new('3/2'); |
184f15d5 |
123 | ok ($x > $y,''); |
124 | ok ($x < $y,1); |
125 | ok ($x == $y,''); |
126 | |
a4e2b1c6 |
127 | $x = $cr->new('-2/3'); $y = $cr->new('3/2'); |
184f15d5 |
128 | ok ($x > $y,''); |
129 | ok ($x < $y,'1'); |
130 | ok ($x == $y,''); |
131 | |
a4e2b1c6 |
132 | $x = $cr->new('-2/3'); $y = $cr->new('-2/3'); |
184f15d5 |
133 | ok ($x > $y,''); |
134 | ok ($x < $y,''); |
135 | ok ($x == $y,'1'); |
136 | |
a4e2b1c6 |
137 | $x = $cr->new('-2/3'); $y = $cr->new('-1/3'); |
184f15d5 |
138 | ok ($x > $y,''); |
139 | ok ($x < $y,'1'); |
140 | ok ($x == $y,''); |
141 | |
a4e2b1c6 |
142 | $x = $cr->new('-124'); $y = $cr->new('-122'); |
184f15d5 |
143 | ok ($x->bacmp($y),1); |
144 | |
a4e2b1c6 |
145 | $x = $cr->new('-124'); $y = $cr->new('-122'); |
184f15d5 |
146 | ok ($x->bcmp($y),-1); |
147 | |
a4e2b1c6 |
148 | $x = $cr->new('3/7'); $y = $cr->new('5/7'); |
8f675a64 |
149 | ok ($x+$y,'8/7'); |
150 | |
a4e2b1c6 |
151 | $x = $cr->new('3/7'); $y = $cr->new('5/7'); |
8f675a64 |
152 | ok ($x*$y,'15/49'); |
153 | |
a4e2b1c6 |
154 | $x = $cr->new('3/5'); $y = $cr->new('5/7'); |
8f675a64 |
155 | ok ($x*$y,'3/7'); |
156 | |
a4e2b1c6 |
157 | $x = $cr->new('3/5'); $y = $cr->new('5/7'); |
8f675a64 |
158 | ok ($x/$y,'21/25'); |
159 | |
a4e2b1c6 |
160 | $x = $cr->new('-144/9')->bsqrt(); ok ($x,'NaN'); |
161 | $x = $cr->new('144/9')->bsqrt(); ok ($x,'4'); |
184f15d5 |
162 | |
a4e2b1c6 |
163 | ############################################################################## |
164 | # bpow |
165 | |
166 | $x = $cr->new('2/1'); $z = $x->bpow('3/1'); ok ($x,'8'); |
167 | |
168 | ############################################################################## |
169 | # bfac |
170 | |
171 | $x = $cr->new('1'); $x->bfac(); ok ($x,'1'); |
172 | for (my $i = 0; $i < 8; $i++) |
173 | { |
174 | $x = $cr->new("$i/1")->bfac(); ok ($x,$mbi->new($i)->bfac()); |
175 | } |
176 | |
177 | # test for $self->bnan() vs. $x->bnan(); |
178 | $x = $cr->new('-1'); $x->bfac(); ok ($x,'NaN'); |
179 | |
180 | ############################################################################## |
181 | # binc/bdec |
182 | |
183 | $x = $cr->new('3/2'); ok ($x->binc(),'5/2'); |
184 | $x = $cr->new('15/6'); ok ($x->bdec(),'3/2'); |
185 | |
186 | ############################################################################## |
187 | # bfloor/bceil |
188 | |
189 | $x = $cr->new('-7/7'); ok ($x->{_n}, '1'); ok ($x->{_d}, '1'); |
190 | $x = $cr->new('-7/7')->bfloor(); ok ($x->{_n}, '1'); ok ($x->{_d}, '1'); |
191 | |
192 | ############################################################################## |
184f15d5 |
193 | # done |
194 | |
195 | 1; |
196 | |