Commit | Line | Data |
0716bf9b |
1 | #!/usr/bin/perl -w |
2 | |
3 | use strict; |
4 | use Test; |
5 | |
6 | BEGIN |
7 | { |
8 | $| = 1; |
027dc388 |
9 | chdir 't' if -d 't'; |
0716bf9b |
10 | unshift @INC, '../lib'; # for running manually |
61f5c3f5 |
11 | } |
12 | |
13 | use Math::BigInt::Calc; |
14 | |
15 | BEGIN |
16 | { |
17 | my $additional = 0; |
18 | $additional = 27 if $Math::BigInt::Calc::VERSION > 0.18; |
b3abae2a |
19 | plan tests => 80 + $additional; |
0716bf9b |
20 | } |
21 | |
027dc388 |
22 | # testing of Math::BigInt::Calc, primarily for interface/api and not for the |
0716bf9b |
23 | # math functionality |
24 | |
574bacfe |
25 | my $C = 'Math::BigInt::Calc'; # pass classname to sub's |
26 | |
0716bf9b |
27 | # _new and _str |
bd05a461 |
28 | my $x = $C->_new(\"123"); my $y = $C->_new(\"321"); |
29 | ok (ref($x),'ARRAY'); ok (${$C->_str($x)},123); ok (${$C->_str($y)},321); |
0716bf9b |
30 | |
61f5c3f5 |
31 | ############################################################################### |
0716bf9b |
32 | # _add, _sub, _mul, _div |
bd05a461 |
33 | ok (${$C->_str($C->_add($x,$y))},444); |
34 | ok (${$C->_str($C->_sub($x,$y))},123); |
35 | ok (${$C->_str($C->_mul($x,$y))},39483); |
36 | ok (${$C->_str($C->_div($x,$y))},123); |
0716bf9b |
37 | |
61f5c3f5 |
38 | ############################################################################### |
39 | # check that mul/div doesn't change $y |
40 | # and returns the same reference, not something new |
bd05a461 |
41 | ok (${$C->_str($C->_mul($x,$y))},39483); |
61f5c3f5 |
42 | ok (${$C->_str($x)},39483); ok (${$C->_str($y)},321); |
43 | |
44 | ok (${$C->_str($C->_div($x,$y))},123); |
45 | ok (${$C->_str($x)},123); ok (${$C->_str($y)},321); |
46 | |
47 | $x = $C->_new(\"39483"); |
48 | my ($x1,$r1) = $C->_div($x,$y); |
49 | ok ("$x1","$x"); |
50 | $C->_inc($x1); |
51 | ok ("$x1","$x"); |
52 | ok (${$C->_str($r1)},'0'); |
53 | |
54 | $x = $C->_new(\"39483"); # reset |
55 | |
56 | ############################################################################### |
bd05a461 |
57 | my $z = $C->_new(\"2"); |
58 | ok (${$C->_str($C->_add($x,$z))},39485); |
59 | my ($re,$rr) = $C->_div($x,$y); |
0716bf9b |
60 | |
bd05a461 |
61 | ok (${$C->_str($re)},123); ok (${$C->_str($rr)},2); |
0716bf9b |
62 | |
63 | # is_zero, _is_one, _one, _zero |
bd05a461 |
64 | ok ($C->_is_zero($x),0); |
65 | ok ($C->_is_one($x),0); |
0716bf9b |
66 | |
bd05a461 |
67 | ok ($C->_is_one($C->_one()),1); ok ($C->_is_one($C->_zero()),0); |
68 | ok ($C->_is_zero($C->_zero()),1); ok ($C->_is_zero($C->_one()),0); |
574bacfe |
69 | |
70 | # is_odd, is_even |
bd05a461 |
71 | ok ($C->_is_odd($C->_one()),1); ok ($C->_is_odd($C->_zero()),0); |
72 | ok ($C->_is_even($C->_one()),0); ok ($C->_is_even($C->_zero()),1); |
0716bf9b |
73 | |
574bacfe |
74 | # _digit |
bd05a461 |
75 | $x = $C->_new(\"123456789"); |
76 | ok ($C->_digit($x,0),9); |
77 | ok ($C->_digit($x,1),8); |
78 | ok ($C->_digit($x,2),7); |
79 | ok ($C->_digit($x,-1),1); |
80 | ok ($C->_digit($x,-2),2); |
81 | ok ($C->_digit($x,-3),3); |
574bacfe |
82 | |
83 | # _copy |
bd05a461 |
84 | $x = $C->_new(\"12356"); |
85 | ok (${$C->_str($C->_copy($x))},12356); |
574bacfe |
86 | |
87 | # _zeros |
bd05a461 |
88 | $x = $C->_new(\"1256000000"); ok ($C->_zeros($x),6); |
89 | $x = $C->_new(\"152"); ok ($C->_zeros($x),0); |
90 | $x = $C->_new(\"123000"); ok ($C->_zeros($x),3); |
574bacfe |
91 | |
92 | # _lsft, _rsft |
bd05a461 |
93 | $x = $C->_new(\"10"); $y = $C->_new(\"3"); |
94 | ok (${$C->_str($C->_lsft($x,$y,10))},10000); |
95 | $x = $C->_new(\"20"); $y = $C->_new(\"3"); |
96 | ok (${$C->_str($C->_lsft($x,$y,10))},20000); |
61f5c3f5 |
97 | |
bd05a461 |
98 | $x = $C->_new(\"128"); $y = $C->_new(\"4"); |
61f5c3f5 |
99 | ok (${$C->_str($C->_lsft($x,$y,2))}, 128 << 4); |
100 | |
bd05a461 |
101 | $x = $C->_new(\"1000"); $y = $C->_new(\"3"); |
102 | ok (${$C->_str($C->_rsft($x,$y,10))},1); |
103 | $x = $C->_new(\"20000"); $y = $C->_new(\"3"); |
104 | ok (${$C->_str($C->_rsft($x,$y,10))},20); |
105 | $x = $C->_new(\"256"); $y = $C->_new(\"4"); |
61f5c3f5 |
106 | ok (${$C->_str($C->_rsft($x,$y,2))},256 >> 4); |
0716bf9b |
107 | |
574bacfe |
108 | # _acmp |
bd05a461 |
109 | $x = $C->_new(\"123456789"); |
110 | $y = $C->_new(\"987654321"); |
111 | ok ($C->_acmp($x,$y),-1); |
112 | ok ($C->_acmp($y,$x),1); |
113 | ok ($C->_acmp($x,$x),0); |
114 | ok ($C->_acmp($y,$y),0); |
574bacfe |
115 | |
116 | # _div |
bd05a461 |
117 | $x = $C->_new(\"3333"); $y = $C->_new(\"1111"); |
118 | ok (${$C->_str(scalar $C->_div($x,$y))},3); |
119 | $x = $C->_new(\"33333"); $y = $C->_new(\"1111"); ($x,$y) = $C->_div($x,$y); |
120 | ok (${$C->_str($x)},30); ok (${$C->_str($y)},3); |
121 | $x = $C->_new(\"123"); $y = $C->_new(\"1111"); |
122 | ($x,$y) = $C->_div($x,$y); ok (${$C->_str($x)},0); ok (${$C->_str($y)},123); |
574bacfe |
123 | |
124 | # _num |
bd05a461 |
125 | $x = $C->_new(\"12345"); $x = $C->_num($x); ok (ref($x)||'',''); ok ($x,12345); |
574bacfe |
126 | |
b3abae2a |
127 | # _sqrt |
128 | $x = $C->_new(\"144"); ok (${$C->_str($C->_sqrt($x))},'12'); |
129 | |
130 | # _fac |
131 | $x = $C->_new(\"0"); ok (${$C->_str($C->_fac($x))},'1'); |
132 | $x = $C->_new(\"1"); ok (${$C->_str($C->_fac($x))},'1'); |
133 | $x = $C->_new(\"2"); ok (${$C->_str($C->_fac($x))},'2'); |
134 | $x = $C->_new(\"3"); ok (${$C->_str($C->_fac($x))},'6'); |
135 | $x = $C->_new(\"4"); ok (${$C->_str($C->_fac($x))},'24'); |
136 | $x = $C->_new(\"5"); ok (${$C->_str($C->_fac($x))},'120'); |
137 | $x = $C->_new(\"10"); ok (${$C->_str($C->_fac($x))},'3628800'); |
138 | $x = $C->_new(\"11"); ok (${$C->_str($C->_fac($x))},'39916800'); |
139 | |
e745a66c |
140 | # _inc |
141 | $x = $C->_new(\"1000"); $C->_inc($x); ok (${$C->_str($x)},'1001'); |
142 | $C->_dec($x); ok (${$C->_str($x)},'1000'); |
143 | |
144 | my $BL = Math::BigInt::Calc::_base_len(); |
145 | $x = '1' . '0' x $BL; |
146 | $z = '1' . '0' x ($BL-1); $z .= '1'; |
147 | $x = $C->_new(\$x); $C->_inc($x); ok (${$C->_str($x)},$z); |
148 | |
149 | $x = '1' . '0' x $BL; $z = '9' x $BL; |
150 | $x = $C->_new(\$x); $C->_dec($x); ok (${$C->_str($x)},$z); |
151 | |
574bacfe |
152 | # should not happen: |
bd05a461 |
153 | # $x = $C->_new(\"-2"); $y = $C->_new(\"4"); ok ($C->_acmp($x,$y),-1); |
574bacfe |
154 | |
394e6ffb |
155 | # _mod |
156 | $x = $C->_new(\"1000"); $y = $C->_new(\"3"); |
157 | ok (${$C->_str(scalar $C->_mod($x,$y))},1); |
158 | $x = $C->_new(\"1000"); $y = $C->_new(\"2"); |
159 | ok (${$C->_str(scalar $C->_mod($x,$y))},0); |
160 | |
161 | # _and, _or, _xor |
162 | $x = $C->_new(\"5"); $y = $C->_new(\"2"); |
163 | ok (${$C->_str(scalar $C->_xor($x,$y))},7); |
164 | $x = $C->_new(\"5"); $y = $C->_new(\"2"); |
165 | ok (${$C->_str(scalar $C->_or($x,$y))},7); |
166 | $x = $C->_new(\"5"); $y = $C->_new(\"3"); |
167 | ok (${$C->_str(scalar $C->_and($x,$y))},1); |
168 | |
169 | # _from_hex, _from_bin |
170 | ok (${$C->_str(scalar $C->_from_hex(\"0xFf"))},255); |
171 | ok (${$C->_str(scalar $C->_from_bin(\"0b10101011"))},160+11); |
172 | |
61f5c3f5 |
173 | # _as_hex, _as_bin |
174 | ok (${$C->_str(scalar $C->_from_hex( $C->_as_hex( $C->_new(\"128"))))}, 128); |
175 | ok (${$C->_str(scalar $C->_from_bin( $C->_as_bin( $C->_new(\"128"))))}, 128); |
176 | |
574bacfe |
177 | # _check |
bd05a461 |
178 | $x = $C->_new(\"123456789"); |
179 | ok ($C->_check($x),0); |
180 | ok ($C->_check(123),'123 is not a reference'); |
0716bf9b |
181 | |
61f5c3f5 |
182 | ############################################################################### |
183 | # _to_large and _to_small (last since they toy with BASE_LEN etc) |
184 | |
185 | exit if $Math::BigInt::Calc::VERSION < 0.19; |
186 | |
187 | $C->_base_len(5,7); $x = [ qw/67890 12345 67890 12345/ ]; $C->_to_large($x); |
188 | ok (@$x,3); |
189 | ok ($x->[0], '4567890'); ok ($x->[1], '7890123'); ok ($x->[2], '123456'); |
190 | |
191 | $C->_base_len(5,7); $x = [ qw/54321 54321 54321 54321/ ]; $C->_to_large($x); |
192 | ok (@$x,3); |
193 | ok ($x->[0], '2154321'); ok ($x->[1], '4321543'); ok ($x->[2], '543215'); |
194 | |
195 | $C->_base_len(6,7); $x = [ qw/654321 654321 654321 654321/ ]; |
196 | $C->_to_large($x); ok (@$x,4); |
197 | ok ($x->[0], '1654321'); ok ($x->[1], '2165432'); |
198 | ok ($x->[2], '3216543'); ok ($x->[3], '654'); |
199 | |
200 | $C->_base_len(5,7); $C->_to_small($x); ok (@$x,5); |
201 | ok ($x->[0], '54321'); ok ($x->[1], '43216'); |
202 | ok ($x->[2], '32165'); ok ($x->[3], '21654'); |
203 | ok ($x->[4], '6543'); |
204 | |
205 | $C->_base_len(7,10); $x = [ qw/0000000 0000000 9999990 9999999/ ]; |
206 | $C->_to_large($x); ok (@$x,3); |
207 | ok ($x->[0], '0000000000'); ok ($x->[1], '9999900000'); |
208 | ok ($x->[2], '99999999'); |
209 | |
210 | $C->_base_len(7,10); $x = [ qw/0000000 0000000 9999990 9999999 99/ ]; |
211 | $C->_to_large($x); ok (@$x,3); |
212 | ok ($x->[0], '0000000000'); ok ($x->[1], '9999900000'); |
213 | ok ($x->[2], '9999999999'); |
214 | |
0716bf9b |
215 | # done |
216 | |
217 | 1; |
574bacfe |
218 | |