05b5fccdfc8d21a2029f33f8dc7aab808504f1c2
[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   plan tests => 63;
12   }
13
14 # testing of Math::BigInt::Calc, primarily for interface/api and not for the
15 # math functionality
16
17 use Math::BigInt::Calc;
18
19 my $C = 'Math::BigInt::Calc';   # pass classname to sub's
20
21 # _new and _str
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);
24
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);
30
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);
37
38 ok (${$C->_str($re)},123); ok (${$C->_str($rr)},2);
39
40 # is_zero, _is_one, _one, _zero
41 ok ($C->_is_zero($x),0);
42 ok ($C->_is_one($x),0);
43
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);
46
47 # is_odd, is_even
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);
50
51 # _digit
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);
59
60 # _copy
61 $x = $C->_new(\"12356");
62 ok (${$C->_str($C->_copy($x))},12356);
63
64 # _zeros
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); 
68
69 # _lsft, _rsft
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)) 
76   {
77   ok (1,1) 
78   }
79 else
80   {
81   ok ('_lsft','undef');
82   }
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)) 
89   {
90   ok (1,1) 
91   }
92 else
93   {
94   ok ('_rsft','undef');
95   }
96
97 # _acmp
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);
104
105 # _div
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);
112
113 # _num
114 $x = $C->_new(\"12345"); $x = $C->_num($x); ok (ref($x)||'',''); ok ($x,12345);
115
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
128 # should not happen:
129 # $x = $C->_new(\"-2"); $y = $C->_new(\"4"); ok ($C->_acmp($x,$y),-1);
130
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
149 # _check
150 $x = $C->_new(\"123456789");
151 ok ($C->_check($x),0);
152 ok ($C->_check(123),'123 is not a reference');
153
154 # done
155
156 1;
157