10 unshift @INC, '../lib'; # for running manually
13 use Math::BigInt::Calc;
20 # testing of Math::BigInt::Calc
22 my $C = 'Math::BigInt::Calc'; # pass classname to sub's
25 my $x = $C->_new(\"123"); my $y = $C->_new(\"321");
26 ok (ref($x),'ARRAY'); ok (${$C->_str($x)},123); ok (${$C->_str($y)},321);
28 ###############################################################################
29 # _add, _sub, _mul, _div
30 ok (${$C->_str($C->_add($x,$y))},444);
31 ok (${$C->_str($C->_sub($x,$y))},123);
32 ok (${$C->_str($C->_mul($x,$y))},39483);
33 ok (${$C->_str($C->_div($x,$y))},123);
35 ###############################################################################
36 # check that mul/div doesn't change $y
37 # and returns the same reference, not something new
38 ok (${$C->_str($C->_mul($x,$y))},39483);
39 ok (${$C->_str($x)},39483); ok (${$C->_str($y)},321);
41 ok (${$C->_str($C->_div($x,$y))},123);
42 ok (${$C->_str($x)},123); ok (${$C->_str($y)},321);
44 $x = $C->_new(\"39483");
45 my ($x1,$r1) = $C->_div($x,$y);
49 ok (${$C->_str($r1)},'0');
51 $x = $C->_new(\"39483"); # reset
53 ###############################################################################
54 my $z = $C->_new(\"2");
55 ok (${$C->_str($C->_add($x,$z))},39485);
56 my ($re,$rr) = $C->_div($x,$y);
58 ok (${$C->_str($re)},123); ok (${$C->_str($rr)},2);
60 # is_zero, _is_one, _one, _zero
61 ok ($C->_is_zero($x)||0,0);
62 ok ($C->_is_one($x)||0,0);
64 ok (${$C->_str($C->_zero())},"0");
65 ok (${$C->_str($C->_one())},"1");
67 # _two() (only used internally)
68 ok (${$C->_str($C->_two())},"2");
70 ok ($C->_is_one($C->_one()),1);
72 ok ($C->_is_one($C->_zero()) || 0,0);
74 ok ($C->_is_zero($C->_zero()),1);
76 ok ($C->_is_zero($C->_one()) || 0,0);
79 ok ($C->_is_odd($C->_one()),1); ok ($C->_is_odd($C->_zero())||0,0);
80 ok ($C->_is_even($C->_one()) || 0,0); ok ($C->_is_even($C->_zero()),1);
83 $x = $C->_new(\"1"); ok ($C->_len($x),1);
84 $x = $C->_new(\"12"); ok ($C->_len($x),2);
85 $x = $C->_new(\"123"); ok ($C->_len($x),3);
86 $x = $C->_new(\"1234"); ok ($C->_len($x),4);
87 $x = $C->_new(\"12345"); ok ($C->_len($x),5);
88 $x = $C->_new(\"123456"); ok ($C->_len($x),6);
89 $x = $C->_new(\"1234567"); ok ($C->_len($x),7);
90 $x = $C->_new(\"12345678"); ok ($C->_len($x),8);
91 $x = $C->_new(\"123456789"); ok ($C->_len($x),9);
93 $x = $C->_new(\"8"); ok ($C->_len($x),1);
94 $x = $C->_new(\"21"); ok ($C->_len($x),2);
95 $x = $C->_new(\"321"); ok ($C->_len($x),3);
96 $x = $C->_new(\"4321"); ok ($C->_len($x),4);
97 $x = $C->_new(\"54321"); ok ($C->_len($x),5);
98 $x = $C->_new(\"654321"); ok ($C->_len($x),6);
99 $x = $C->_new(\"7654321"); ok ($C->_len($x),7);
100 $x = $C->_new(\"87654321"); ok ($C->_len($x),8);
101 $x = $C->_new(\"987654321"); ok ($C->_len($x),9);
103 for (my $i = 1; $i < 9; $i++)
105 my $a = "$i" . '0' x ($i-1);
107 print "# Tried len '$a'\n" unless ok ($C->_len($x),$i);
111 $x = $C->_new(\"123456789");
112 ok ($C->_digit($x,0),9);
113 ok ($C->_digit($x,1),8);
114 ok ($C->_digit($x,2),7);
115 ok ($C->_digit($x,-1),1);
116 ok ($C->_digit($x,-2),2);
117 ok ($C->_digit($x,-3),3);
120 foreach (qw/ 1 12 123 1234 12345 123456 1234567 12345678 123456789/)
122 $x = $C->_new(\"$_");
123 ok (${$C->_str($C->_copy($x))},"$_");
124 ok (${$C->_str($x)},"$_"); # did _copy destroy original x?
128 $x = $C->_new(\"1256000000"); ok ($C->_zeros($x),6);
129 $x = $C->_new(\"152"); ok ($C->_zeros($x),0);
130 $x = $C->_new(\"123000"); ok ($C->_zeros($x),3);
133 $x = $C->_new(\"10"); $y = $C->_new(\"3");
134 ok (${$C->_str($C->_lsft($x,$y,10))},10000);
135 $x = $C->_new(\"20"); $y = $C->_new(\"3");
136 ok (${$C->_str($C->_lsft($x,$y,10))},20000);
138 $x = $C->_new(\"128"); $y = $C->_new(\"4");
139 ok (${$C->_str($C->_lsft($x,$y,2))}, 128 << 4);
141 $x = $C->_new(\"1000"); $y = $C->_new(\"3");
142 ok (${$C->_str($C->_rsft($x,$y,10))},1);
143 $x = $C->_new(\"20000"); $y = $C->_new(\"3");
144 ok (${$C->_str($C->_rsft($x,$y,10))},20);
145 $x = $C->_new(\"256"); $y = $C->_new(\"4");
146 ok (${$C->_str($C->_rsft($x,$y,2))},256 >> 4);
148 $x = $C->_new(\"6411906467305339182857313397200584952398");
149 $y = $C->_new(\"45");
150 ok (${$C->_str($C->_rsft($x,$y,10))},0);
153 $x = $C->_new(\"123456789");
154 $y = $C->_new(\"987654321");
155 ok ($C->_acmp($x,$y),-1);
156 ok ($C->_acmp($y,$x),1);
157 ok ($C->_acmp($x,$x),0);
158 ok ($C->_acmp($y,$y),0);
160 $x = $C->_new(\"1234567890123456789");
161 $y = $C->_new(\"987654321012345678");
162 ok ($C->_acmp($x,$y),1);
163 ok ($C->_acmp($y,$x),-1);
164 ok ($C->_acmp($x,$x),0);
165 ok ($C->_acmp($y,$y),0);
167 $x = $C->_new(\"1234");
168 $y = $C->_new(\"987654321012345678");
169 ok ($C->_acmp($x,$y),-1);
170 ok ($C->_acmp($y,$x),1);
171 ok ($C->_acmp($x,$x),0);
172 ok ($C->_acmp($y,$y),0);
176 $y = $C->_new(\"5033");
177 my ($xmod,$sign) = $C->_modinv($x,$y);
178 ok (${$C->_str($xmod)},'629'); # -629 % 5033 == 4404
182 $x = $C->_new(\"3333"); $y = $C->_new(\"1111");
183 ok (${$C->_str(scalar $C->_div($x,$y))},3);
184 $x = $C->_new(\"33333"); $y = $C->_new(\"1111"); ($x,$y) = $C->_div($x,$y);
185 ok (${$C->_str($x)},30); ok (${$C->_str($y)},3);
186 $x = $C->_new(\"123"); $y = $C->_new(\"1111");
187 ($x,$y) = $C->_div($x,$y); ok (${$C->_str($x)},0); ok (${$C->_str($y)},123);
190 foreach (qw/1 12 123 1234 12345 1234567 12345678 123456789 1234567890/)
192 $x = $C->_new(\"$_");
193 ok (ref($x)||'','ARRAY'); ok (${$C->_str($x)},"$_");
194 $x = $C->_num($x); ok (ref($x)||'',''); ok ($x,$_);
198 $x = $C->_new(\"144"); ok (${$C->_str($C->_sqrt($x))},'12');
199 $x = $C->_new(\"144000000000000"); ok (${$C->_str($C->_sqrt($x))},'12000000');
202 $x = $C->_new(\"81"); my $n = $C->_new(\"3"); # 4*4*4 = 64, 5*5*5 = 125
203 ok (${$C->_str($C->_root($x,$n))},'4'); # 4.xx => 4.0
204 $x = $C->_new(\"81"); $n = $C->_new(\"4"); # 3*3*3*3 == 81
205 ok (${$C->_str($C->_root($x,$n))},'3');
208 $x = $C->_new(\"0"); ok (${$C->_str($C->_fac($x))},'1');
209 $x = $C->_new(\"1"); ok (${$C->_str($C->_fac($x))},'1');
210 $x = $C->_new(\"2"); ok (${$C->_str($C->_fac($x))},'2');
211 $x = $C->_new(\"3"); ok (${$C->_str($C->_fac($x))},'6');
212 $x = $C->_new(\"4"); ok (${$C->_str($C->_fac($x))},'24');
213 $x = $C->_new(\"5"); ok (${$C->_str($C->_fac($x))},'120');
214 $x = $C->_new(\"10"); ok (${$C->_str($C->_fac($x))},'3628800');
215 $x = $C->_new(\"11"); ok (${$C->_str($C->_fac($x))},'39916800');
216 $x = $C->_new(\"12"); ok (${$C->_str($C->_fac($x))},'479001600');
218 ##############################################################################
220 foreach (qw/1 11 121 1231 12341 1234561 12345671 123456781 1234567891/)
222 $x = $C->_new(\"$_"); $C->_inc($x);
223 print "# \$x = ",${$C->_str($x)},"\n"
224 unless ok (${$C->_str($x)},substr($_,0,length($_)-1) . '2');
225 $C->_dec($x); ok (${$C->_str($x)},$_);
227 foreach (qw/19 119 1219 12319 1234519 12345619 123456719 1234567819/)
229 $x = $C->_new(\"$_"); $C->_inc($x);
230 print "# \$x = ",${$C->_str($x)},"\n"
231 unless ok (${$C->_str($x)},substr($_,0,length($_)-2) . '20');
232 $C->_dec($x); ok (${$C->_str($x)},$_);
234 foreach (qw/999 9999 99999 9999999 99999999 999999999 9999999999 99999999999/)
236 $x = $C->_new(\"$_"); $C->_inc($x);
237 print "# \$x = ",${$C->_str($x)},"\n"
238 unless ok (${$C->_str($x)}, '1' . '0' x (length($_)));
239 $C->_dec($x); ok (${$C->_str($x)},$_);
242 $x = $C->_new(\"1000"); $C->_inc($x); ok (${$C->_str($x)},'1001');
243 $C->_dec($x); ok (${$C->_str($x)},'1000');
248 $BL = &{"$C"."::_base_len"}();
251 $x = '1' . '0' x $BL;
252 $z = '1' . '0' x ($BL-1); $z .= '1';
253 $x = $C->_new(\$x); $C->_inc($x); ok (${$C->_str($x)},$z);
255 $x = '1' . '0' x $BL; $z = '9' x $BL;
256 $x = $C->_new(\$x); $C->_dec($x); ok (${$C->_str($x)},$z);
259 # $x = $C->_new(\"-2"); $y = $C->_new(\"4"); ok ($C->_acmp($x,$y),-1);
261 ###############################################################################
263 $x = $C->_new(\"1000"); $y = $C->_new(\"3");
264 ok (${$C->_str(scalar $C->_mod($x,$y))},1);
265 $x = $C->_new(\"1000"); $y = $C->_new(\"2");
266 ok (${$C->_str(scalar $C->_mod($x,$y))},0);
269 $x = $C->_new(\"5"); $y = $C->_new(\"2");
270 ok (${$C->_str(scalar $C->_xor($x,$y))},7);
271 $x = $C->_new(\"5"); $y = $C->_new(\"2");
272 ok (${$C->_str(scalar $C->_or($x,$y))},7);
273 $x = $C->_new(\"5"); $y = $C->_new(\"3");
274 ok (${$C->_str(scalar $C->_and($x,$y))},1);
276 # _from_hex, _from_bin
277 ok (${$C->_str(scalar $C->_from_hex(\"0xFf"))},255);
278 ok (${$C->_str(scalar $C->_from_bin(\"0b10101011"))},160+11);
281 ok (${$C->_str(scalar $C->_from_hex( $C->_as_hex( $C->_new(\"128"))))}, 128);
282 ok (${$C->_str(scalar $C->_from_bin( $C->_as_bin( $C->_new(\"128"))))}, 128);
283 ok (${$C->_str(scalar $C->_from_hex( $C->_as_hex( $C->_new(\"0"))))}, 0);
284 ok (${$C->_str(scalar $C->_from_bin( $C->_as_bin( $C->_new(\"0"))))}, 0);
285 ok ( ${$C->_as_hex( $C->_new(\"0"))}, '0x0');
286 ok ( ${$C->_as_bin( $C->_new(\"0"))}, '0b0');
287 ok ( ${$C->_as_hex( $C->_new(\"12"))}, '0xc');
288 ok ( ${$C->_as_bin( $C->_new(\"12"))}, '0b1100');
291 $x = $C->_new(\"123456789");
292 ok ($C->_check($x),0);
293 ok ($C->_check(123),'123 is not a reference');
295 ###############################################################################
300 # correct empty arrays
301 $x = &{$C."::__strip_zeros"}([]); ok (@$x,1); ok ($x->[0],0);
302 # don't strip single elements
303 $x = &{$C."::__strip_zeros"}([0]); ok (@$x,1); ok ($x->[0],0);
304 $x = &{$C."::__strip_zeros"}([1]); ok (@$x,1); ok ($x->[0],1);
305 # don't strip non-zero elements
306 $x = &{$C."::__strip_zeros"}([0,1]);
307 ok (@$x,2); ok ($x->[0],0); ok ($x->[1],1);
308 $x = &{$C."::__strip_zeros"}([0,1,2]);
309 ok (@$x,3); ok ($x->[0],0); ok ($x->[1],1); ok ($x->[2],2);
311 # but strip leading zeros
312 $x = &{$C."::__strip_zeros"}([0,1,2,0]);
313 ok (@$x,3); ok ($x->[0],0); ok ($x->[1],1); ok ($x->[2],2);
315 $x = &{$C."::__strip_zeros"}([0,1,2,0,0]);
316 ok (@$x,3); ok ($x->[0],0); ok ($x->[1],1); ok ($x->[2],2);
318 $x = &{$C."::__strip_zeros"}([0,1,2,0,0,0]);
319 ok (@$x,3); ok ($x->[0],0); ok ($x->[1],1); ok ($x->[2],2);
321 # collapse multiple zeros
322 $x = &{$C."::__strip_zeros"}([0,0,0,0]);
323 ok (@$x,1); ok ($x->[0],0);