[PATCH9 BigInt v1.60 fix for "\n"
[p5sagit/p5-mst-13.2.git] / lib / Math / BigInt / t / bigintc.t
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
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;
19   plan tests => 80 + $additional;
20   }
21
22 # testing of Math::BigInt::Calc, primarily for interface/api and not for the
23 # math functionality
24
25 my $C = 'Math::BigInt::Calc';   # pass classname to sub's
26
27 # _new and _str
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);
30
31 ###############################################################################
32 # _add, _sub, _mul, _div
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);
37
38 ###############################################################################
39 # check that mul/div doesn't change $y
40 # and returns the same reference, not something new
41 ok (${$C->_str($C->_mul($x,$y))},39483);
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 ###############################################################################
57 my $z = $C->_new(\"2");
58 ok (${$C->_str($C->_add($x,$z))},39485);
59 my ($re,$rr) = $C->_div($x,$y);
60
61 ok (${$C->_str($re)},123); ok (${$C->_str($rr)},2);
62
63 # is_zero, _is_one, _one, _zero
64 ok ($C->_is_zero($x),0);
65 ok ($C->_is_one($x),0);
66
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);
69
70 # is_odd, is_even
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);
73
74 # _digit
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);
82
83 # _copy
84 $x = $C->_new(\"12356");
85 ok (${$C->_str($C->_copy($x))},12356);
86
87 # _zeros
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); 
91
92 # _lsft, _rsft
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);
97
98 $x = $C->_new(\"128"); $y = $C->_new(\"4");
99 ok (${$C->_str($C->_lsft($x,$y,2))}, 128 << 4);
100
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");
106 ok (${$C->_str($C->_rsft($x,$y,2))},256 >> 4);
107
108 # _acmp
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);
115
116 # _div
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);
123
124 # _num
125 $x = $C->_new(\"12345"); $x = $C->_num($x); ok (ref($x)||'',''); ok ($x,12345);
126
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
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
152 # should not happen:
153 # $x = $C->_new(\"-2"); $y = $C->_new(\"4"); ok ($C->_acmp($x,$y),-1);
154
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
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
177 # _check
178 $x = $C->_new(\"123456789");
179 ok ($C->_check($x),0);
180 ok ($C->_check(123),'123 is not a reference');
181
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
215 # done
216
217 1;
218