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 |
8f675a64 |
11 | plan tests => 61; |
184f15d5 |
12 | } |
13 | |
14 | # testing of Math::BigRat |
15 | |
16 | use Math::BigRat; |
17 | |
18 | my ($x,$y,$z); |
19 | |
20 | $x = Math::BigRat->new(1234); ok ($x,1234); |
8f675a64 |
21 | ok ($x->isa('Math::BigRat')); |
22 | ok (!$x->isa('Math::BigFloat')); |
23 | ok (!$x->isa('Math::BigInt')); |
24 | |
25 | ############################################################################## |
26 | # new |
27 | |
28 | $x = Math::BigRat->new(1234); ok ($x,1234); |
184f15d5 |
29 | $x = Math::BigRat->new('1234/1'); ok ($x,1234); |
30 | $x = Math::BigRat->new('1234/2'); ok ($x,617); |
31 | |
32 | $x = Math::BigRat->new('100/1.0'); ok ($x,100); |
33 | $x = Math::BigRat->new('10.0/1.0'); ok ($x,10); |
34 | $x = Math::BigRat->new('0.1/10'); ok ($x,'1/100'); |
35 | $x = Math::BigRat->new('0.1/0.1'); ok ($x,'1'); |
36 | $x = Math::BigRat->new('1e2/10'); ok ($x,10); |
37 | $x = Math::BigRat->new('1e2/1e1'); ok ($x,10); |
38 | $x = Math::BigRat->new('1 / 3'); ok ($x,'1/3'); |
39 | $x = Math::BigRat->new('-1 / 3'); ok ($x,'-1/3'); |
40 | $x = Math::BigRat->new('NaN'); ok ($x,'NaN'); |
41 | $x = Math::BigRat->new('inf'); ok ($x,'inf'); |
42 | $x = Math::BigRat->new('-inf'); ok ($x,'-inf'); |
43 | $x = Math::BigRat->new('1/'); ok ($x,'NaN'); |
44 | |
8f675a64 |
45 | # input ala '1+1/3' isn't parsed ok yet |
46 | $x = Math::BigRat->new('1+1/3'); ok ($x,'NaN'); |
47 | |
48 | ############################################################################## |
49 | # mixed arguments |
50 | |
51 | ok (Math::BigRat->new('3/7')->badd(1),'10/7'); |
52 | ok (Math::BigRat->new('3/10')->badd(1.1),'7/5'); |
53 | ok (Math::BigRat->new('3/7')->badd(Math::BigInt->new(1)),'10/7'); |
54 | ok (Math::BigRat->new('3/10')->badd(Math::BigFloat->new('1.1')),'7/5'); |
55 | |
56 | ok (Math::BigRat->new('3/7')->bsub(1),'-4/7'); |
57 | ok (Math::BigRat->new('3/10')->bsub(1.1),'-4/5'); |
58 | ok (Math::BigRat->new('3/7')->bsub(Math::BigInt->new(1)),'-4/7'); |
59 | ok (Math::BigRat->new('3/10')->bsub(Math::BigFloat->new('1.1')),'-4/5'); |
60 | |
61 | ok (Math::BigRat->new('3/7')->bmul(1),'3/7'); |
62 | ok (Math::BigRat->new('3/10')->bmul(1.1),'33/100'); |
63 | ok (Math::BigRat->new('3/7')->bmul(Math::BigInt->new(1)),'3/7'); |
64 | ok (Math::BigRat->new('3/10')->bmul(Math::BigFloat->new('1.1')),'33/100'); |
65 | |
66 | ok (Math::BigRat->new('3/7')->bdiv(1),'3/7'); |
67 | ok (Math::BigRat->new('3/10')->bdiv(1.1),'3/11'); |
68 | ok (Math::BigRat->new('3/7')->bdiv(Math::BigInt->new(1)),'3/7'); |
69 | ok (Math::BigRat->new('3/10')->bdiv(Math::BigFloat->new('1.1')),'3/11'); |
70 | |
71 | ############################################################################## |
184f15d5 |
72 | $x = Math::BigRat->new('1/4'); $y = Math::BigRat->new('1/3'); |
73 | ok ($x + $y, '7/12'); |
74 | ok ($x * $y, '1/12'); |
75 | ok ($x / $y, '3/4'); |
76 | |
77 | $x = Math::BigRat->new('7/5'); $x *= '3/2'; |
78 | ok ($x,'21/10'); |
79 | $x -= '0.1'; |
80 | ok ($x,'2'); # not 21/10 |
81 | |
82 | $x = Math::BigRat->new('2/3'); $y = Math::BigRat->new('3/2'); |
83 | ok ($x > $y,''); |
84 | ok ($x < $y,1); |
85 | ok ($x == $y,''); |
86 | |
87 | $x = Math::BigRat->new('-2/3'); $y = Math::BigRat->new('3/2'); |
88 | ok ($x > $y,''); |
89 | ok ($x < $y,'1'); |
90 | ok ($x == $y,''); |
91 | |
92 | $x = Math::BigRat->new('-2/3'); $y = Math::BigRat->new('-2/3'); |
93 | ok ($x > $y,''); |
94 | ok ($x < $y,''); |
95 | ok ($x == $y,'1'); |
96 | |
97 | $x = Math::BigRat->new('-2/3'); $y = Math::BigRat->new('-1/3'); |
98 | ok ($x > $y,''); |
99 | ok ($x < $y,'1'); |
100 | ok ($x == $y,''); |
101 | |
102 | $x = Math::BigRat->new('-124'); $y = Math::BigRat->new('-122'); |
103 | ok ($x->bacmp($y),1); |
104 | |
105 | $x = Math::BigRat->new('-124'); $y = Math::BigRat->new('-122'); |
106 | ok ($x->bcmp($y),-1); |
107 | |
8f675a64 |
108 | $x = Math::BigRat->new('3/7'); $y = Math::BigRat->new('5/7'); |
109 | ok ($x+$y,'8/7'); |
110 | |
111 | $x = Math::BigRat->new('3/7'); $y = Math::BigRat->new('5/7'); |
112 | ok ($x*$y,'15/49'); |
113 | |
114 | $x = Math::BigRat->new('3/5'); $y = Math::BigRat->new('5/7'); |
115 | ok ($x*$y,'3/7'); |
116 | |
117 | $x = Math::BigRat->new('3/5'); $y = Math::BigRat->new('5/7'); |
118 | ok ($x/$y,'21/25'); |
119 | |
184f15d5 |
120 | $x = Math::BigRat->new('-144/9'); $x->bsqrt(); ok ($x,'NaN'); |
121 | $x = Math::BigRat->new('144/9'); $x->bsqrt(); ok ($x,'4'); |
122 | |
123 | # done |
124 | |
125 | 1; |
126 | |