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 |
394e6ffb |
11 | plan tests => 63; |
0716bf9b |
12 | } |
13 | |
027dc388 |
14 | # testing of Math::BigInt::Calc, primarily for interface/api and not for the |
0716bf9b |
15 | # math functionality |
16 | |
17 | use Math::BigInt::Calc; |
18 | |
574bacfe |
19 | my $C = 'Math::BigInt::Calc'; # pass classname to sub's |
20 | |
0716bf9b |
21 | # _new and _str |
bd05a461 |
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); |
0716bf9b |
24 | |
25 | # _add, _sub, _mul, _div |
bd05a461 |
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); |
0716bf9b |
30 | |
bd05a461 |
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); |
0716bf9b |
37 | |
bd05a461 |
38 | ok (${$C->_str($re)},123); ok (${$C->_str($rr)},2); |
0716bf9b |
39 | |
40 | # is_zero, _is_one, _one, _zero |
bd05a461 |
41 | ok ($C->_is_zero($x),0); |
42 | ok ($C->_is_one($x),0); |
0716bf9b |
43 | |
bd05a461 |
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); |
574bacfe |
46 | |
47 | # is_odd, is_even |
bd05a461 |
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); |
0716bf9b |
50 | |
574bacfe |
51 | # _digit |
bd05a461 |
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); |
574bacfe |
59 | |
60 | # _copy |
bd05a461 |
61 | $x = $C->_new(\"12356"); |
62 | ok (${$C->_str($C->_copy($x))},12356); |
574bacfe |
63 | |
64 | # _zeros |
bd05a461 |
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); |
574bacfe |
68 | |
69 | # _lsft, _rsft |
bd05a461 |
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)) |
574bacfe |
76 | { |
77 | ok (1,1) |
78 | } |
79 | else |
80 | { |
81 | ok ('_lsft','undef'); |
82 | } |
bd05a461 |
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)) |
574bacfe |
89 | { |
90 | ok (1,1) |
91 | } |
92 | else |
93 | { |
94 | ok ('_rsft','undef'); |
95 | } |
0716bf9b |
96 | |
574bacfe |
97 | # _acmp |
bd05a461 |
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); |
574bacfe |
104 | |
105 | # _div |
bd05a461 |
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); |
574bacfe |
112 | |
113 | # _num |
bd05a461 |
114 | $x = $C->_new(\"12345"); $x = $C->_num($x); ok (ref($x)||'',''); ok ($x,12345); |
574bacfe |
115 | |
e745a66c |
116 | # _inc |
117 | $x = $C->_new(\"1000"); $C->_inc($x); ok (${$C->_str($x)},'1001'); |
118 | $C->_dec($x); ok (${$C->_str($x)},'1000'); |
119 | |
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); |
124 | |
125 | $x = '1' . '0' x $BL; $z = '9' x $BL; |
126 | $x = $C->_new(\$x); $C->_dec($x); ok (${$C->_str($x)},$z); |
127 | |
574bacfe |
128 | # should not happen: |
bd05a461 |
129 | # $x = $C->_new(\"-2"); $y = $C->_new(\"4"); ok ($C->_acmp($x,$y),-1); |
574bacfe |
130 | |
394e6ffb |
131 | # _mod |
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); |
136 | |
137 | # _and, _or, _xor |
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); |
144 | |
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); |
148 | |
574bacfe |
149 | # _check |
bd05a461 |
150 | $x = $C->_new(\"123456789"); |
151 | ok ($C->_check($x),0); |
152 | ok ($C->_check(123),'123 is not a reference'); |
0716bf9b |
153 | |
154 | # done |
155 | |
156 | 1; |
574bacfe |
157 | |