10 unshift @INC, '../lib'; # for running manually
14 # testing of Math::BigInt::Calc, primarily for interface/api and not for the
17 use Math::BigInt::Calc;
19 my $C = 'Math::BigInt::Calc'; # pass classname to sub's
22 my $x = $C->_new(\"123"); my $y = $C->_new(\"321");
23 ok (ref($x),'ARRAY'); ok (${$C->_str($x)},123); ok (${$C->_str($y)},321);
25 # _add, _sub, _mul, _div
26 ok (${$C->_str($C->_add($x,$y))},444);
27 ok (${$C->_str($C->_sub($x,$y))},123);
28 ok (${$C->_str($C->_mul($x,$y))},39483);
29 ok (${$C->_str($C->_div($x,$y))},123);
31 ok (${$C->_str($C->_mul($x,$y))},39483);
32 ok (${$C->_str($x)},39483);
33 ok (${$C->_str($y)},321);
34 my $z = $C->_new(\"2");
35 ok (${$C->_str($C->_add($x,$z))},39485);
36 my ($re,$rr) = $C->_div($x,$y);
38 ok (${$C->_str($re)},123); ok (${$C->_str($rr)},2);
40 # is_zero, _is_one, _one, _zero
41 ok ($C->_is_zero($x),0);
42 ok ($C->_is_one($x),0);
44 ok ($C->_is_one($C->_one()),1); ok ($C->_is_one($C->_zero()),0);
45 ok ($C->_is_zero($C->_zero()),1); ok ($C->_is_zero($C->_one()),0);
48 ok ($C->_is_odd($C->_one()),1); ok ($C->_is_odd($C->_zero()),0);
49 ok ($C->_is_even($C->_one()),0); ok ($C->_is_even($C->_zero()),1);
52 $x = $C->_new(\"123456789");
53 ok ($C->_digit($x,0),9);
54 ok ($C->_digit($x,1),8);
55 ok ($C->_digit($x,2),7);
56 ok ($C->_digit($x,-1),1);
57 ok ($C->_digit($x,-2),2);
58 ok ($C->_digit($x,-3),3);
61 $x = $C->_new(\"12356");
62 ok (${$C->_str($C->_copy($x))},12356);
65 $x = $C->_new(\"1256000000"); ok ($C->_zeros($x),6);
66 $x = $C->_new(\"152"); ok ($C->_zeros($x),0);
67 $x = $C->_new(\"123000"); ok ($C->_zeros($x),3);
70 $x = $C->_new(\"10"); $y = $C->_new(\"3");
71 ok (${$C->_str($C->_lsft($x,$y,10))},10000);
72 $x = $C->_new(\"20"); $y = $C->_new(\"3");
73 ok (${$C->_str($C->_lsft($x,$y,10))},20000);
74 $x = $C->_new(\"128"); $y = $C->_new(\"4");
75 if (!defined $C->_lsft($x,$y,2))
83 $x = $C->_new(\"1000"); $y = $C->_new(\"3");
84 ok (${$C->_str($C->_rsft($x,$y,10))},1);
85 $x = $C->_new(\"20000"); $y = $C->_new(\"3");
86 ok (${$C->_str($C->_rsft($x,$y,10))},20);
87 $x = $C->_new(\"256"); $y = $C->_new(\"4");
88 if (!defined $C->_rsft($x,$y,2))
98 $x = $C->_new(\"123456789");
99 $y = $C->_new(\"987654321");
100 ok ($C->_acmp($x,$y),-1);
101 ok ($C->_acmp($y,$x),1);
102 ok ($C->_acmp($x,$x),0);
103 ok ($C->_acmp($y,$y),0);
106 $x = $C->_new(\"3333"); $y = $C->_new(\"1111");
107 ok (${$C->_str(scalar $C->_div($x,$y))},3);
108 $x = $C->_new(\"33333"); $y = $C->_new(\"1111"); ($x,$y) = $C->_div($x,$y);
109 ok (${$C->_str($x)},30); ok (${$C->_str($y)},3);
110 $x = $C->_new(\"123"); $y = $C->_new(\"1111");
111 ($x,$y) = $C->_div($x,$y); ok (${$C->_str($x)},0); ok (${$C->_str($y)},123);
114 $x = $C->_new(\"12345"); $x = $C->_num($x); ok (ref($x)||'',''); ok ($x,12345);
117 $x = $C->_new(\"1000"); $C->_inc($x); ok (${$C->_str($x)},'1001');
118 $C->_dec($x); ok (${$C->_str($x)},'1000');
120 my $BL = Math::BigInt::Calc::_base_len();
121 $x = '1' . '0' x $BL;
122 $z = '1' . '0' x ($BL-1); $z .= '1';
123 $x = $C->_new(\$x); $C->_inc($x); ok (${$C->_str($x)},$z);
125 $x = '1' . '0' x $BL; $z = '9' x $BL;
126 $x = $C->_new(\$x); $C->_dec($x); ok (${$C->_str($x)},$z);
129 # $x = $C->_new(\"-2"); $y = $C->_new(\"4"); ok ($C->_acmp($x,$y),-1);
132 $x = $C->_new(\"1000"); $y = $C->_new(\"3");
133 ok (${$C->_str(scalar $C->_mod($x,$y))},1);
134 $x = $C->_new(\"1000"); $y = $C->_new(\"2");
135 ok (${$C->_str(scalar $C->_mod($x,$y))},0);
138 $x = $C->_new(\"5"); $y = $C->_new(\"2");
139 ok (${$C->_str(scalar $C->_xor($x,$y))},7);
140 $x = $C->_new(\"5"); $y = $C->_new(\"2");
141 ok (${$C->_str(scalar $C->_or($x,$y))},7);
142 $x = $C->_new(\"5"); $y = $C->_new(\"3");
143 ok (${$C->_str(scalar $C->_and($x,$y))},1);
145 # _from_hex, _from_bin
146 ok (${$C->_str(scalar $C->_from_hex(\"0xFf"))},255);
147 ok (${$C->_str(scalar $C->_from_bin(\"0b10101011"))},160+11);
150 $x = $C->_new(\"123456789");
151 ok ($C->_check($x),0);
152 ok ($C->_check(123),'123 is not a reference');