xref: /openbsd-src/gnu/usr.bin/perl/cpan/Math-BigInt-FastCalc/t/bigintfc.t (revision eac174f2741a08d8deb8aae59a7f778ef9b5d770)
1*eac174f2Safresh1# -*- mode: perl; -*-
2*eac174f2Safresh1
3*eac174f2Safresh1# Test Math::BigInt::FastCalc
4b8851fccSafresh1
5b8851fccSafresh1use strict;
6*eac174f2Safresh1use warnings;
7*eac174f2Safresh1
8*eac174f2Safresh1use Test::More tests => 524;
9b8851fccSafresh1
10b8851fccSafresh1use Math::BigInt::FastCalc;
11b8851fccSafresh1
12*eac174f2Safresh1my ($BASE_LEN, $BASE, $AND_BITS, $XOR_BITS, $OR_BITS,
13*eac174f2Safresh1    $BASE_LEN_SMALL, $MAX_VAL,
14*eac174f2Safresh1    $MAX_BITS, $MAX_EXP_F, $MAX_EXP_I, $USE_INT)
15*eac174f2Safresh1  = Math::BigInt::Calc -> _base_len();
16b8851fccSafresh1
17*eac174f2Safresh1note(<<"EOF");
18b8851fccSafresh1
19*eac174f2Safresh1BASE_LEN  = $BASE_LEN
20*eac174f2Safresh1BASE      = $BASE
21*eac174f2Safresh1MAX_VAL   = $MAX_VAL
22*eac174f2Safresh1AND_BITS  = $AND_BITS
23*eac174f2Safresh1XOR_BITS  = $XOR_BITS
24*eac174f2Safresh1OR_BITS   = $OR_BITS
25*eac174f2Safresh1MAX_EXP_F = $MAX_EXP_F
26*eac174f2Safresh1MAX_EXP_I = $MAX_EXP_I
27*eac174f2Safresh1USE_INT   = $USE_INT
28*eac174f2Safresh1EOF
29b8851fccSafresh1
30*eac174f2Safresh1my $LIB = 'Math::BigInt::FastCalc';
31*eac174f2Safresh1my $REF = 'ARRAY';
32b8851fccSafresh1
33b8851fccSafresh1# _new and _str
34*eac174f2Safresh1
35*eac174f2Safresh1my $x = $LIB->_new("123");
36*eac174f2Safresh1my $y = $LIB->_new("321");
37*eac174f2Safresh1is(ref($x), $REF, q|ref($x) is a $REF|);
38*eac174f2Safresh1is($LIB->_str($x), 123,     qq|$LIB->_str(\$x) = 123|);
39*eac174f2Safresh1is($LIB->_str($y), 321,     qq|$LIB->_str(\$y) = 321|);
40b8851fccSafresh1
41b8851fccSafresh1###############################################################################
42b8851fccSafresh1# _add, _sub, _mul, _div
43*eac174f2Safresh1
44*eac174f2Safresh1is($LIB->_str($LIB->_add($x, $y)), 444,
45*eac174f2Safresh1   qq|$LIB->_str($LIB->_add(\$x, \$y)) = 444|);
46*eac174f2Safresh1is($LIB->_str($LIB->_sub($x, $y)), 123,
47*eac174f2Safresh1   qq|$LIB->_str($LIB->_sub(\$x, \$y)) = 123|);
48*eac174f2Safresh1is($LIB->_str($LIB->_mul($x, $y)), 39483,
49*eac174f2Safresh1   qq|$LIB->_str($LIB->_mul(\$x, \$y)) = 39483|);
50*eac174f2Safresh1is($LIB->_str($LIB->_div($x, $y)), 123,
51*eac174f2Safresh1   qq|$LIB->_str($LIB->_div(\$x, \$y)) = 123|);
52b8851fccSafresh1
53b8851fccSafresh1###############################################################################
54b8851fccSafresh1# check that mul/div doesn't change $y
55b8851fccSafresh1# and returns the same reference, not something new
56b8851fccSafresh1
57*eac174f2Safresh1is($LIB->_str($LIB->_mul($x, $y)), 39483,
58*eac174f2Safresh1   qq|$LIB->_str($LIB->_mul(\$x, \$y)) = 39483|);
59*eac174f2Safresh1is($LIB->_str($x), 39483,
60*eac174f2Safresh1   qq|$LIB->_str(\$x) = 39483|);
61*eac174f2Safresh1is($LIB->_str($y), 321,
62*eac174f2Safresh1   qq|$LIB->_str(\$y) = 321|);
63b8851fccSafresh1
64*eac174f2Safresh1is($LIB->_str($LIB->_div($x, $y)), 123,
65*eac174f2Safresh1   qq|$LIB->_str($LIB->_div(\$x, \$y)) = 123|);
66*eac174f2Safresh1is($LIB->_str($x), 123,
67*eac174f2Safresh1   qq|$LIB->_str(\$x) = 123|);
68*eac174f2Safresh1is($LIB->_str($y), 321,
69*eac174f2Safresh1   qq|$LIB->_str(\$y) = 321|);
70b8851fccSafresh1
71*eac174f2Safresh1$x = $LIB->_new("39483");
72*eac174f2Safresh1my ($x1, $r1) = $LIB->_div($x, $y);
73*eac174f2Safresh1is("$x1", "$x", q|"$x1" = "$x"|);
74*eac174f2Safresh1$LIB->_inc($x1);
75*eac174f2Safresh1is("$x1", "$x", q|"$x1" = "$x"|);
76*eac174f2Safresh1is($LIB->_str($r1), "0", qq|$LIB->_str(\$r1) = "0"|);
77*eac174f2Safresh1
78*eac174f2Safresh1$x = $LIB->_new("39483");       # reset
79b8851fccSafresh1
80b8851fccSafresh1###############################################################################
81b8851fccSafresh1
82*eac174f2Safresh1my $z = $LIB->_new("2");
83*eac174f2Safresh1is($LIB->_str($LIB->_add($x, $z)), 39485,
84*eac174f2Safresh1   qq|$LIB->_str($LIB->_add(\$x, \$z)) = 39485|);
85*eac174f2Safresh1my ($re, $rr) = $LIB->_div($x, $y);
86*eac174f2Safresh1
87*eac174f2Safresh1is($LIB->_str($re), 123, qq|$LIB->_str(\$re) = 123|);
88*eac174f2Safresh1is($LIB->_str($rr), 2,   qq|$LIB->_str(\$rr) = 2|);
89b8851fccSafresh1
90b8851fccSafresh1# is_zero, _is_one, _one, _zero
91b8851fccSafresh1
92*eac174f2Safresh1ok(! $LIB->_is_zero($x), qq|$LIB->_is_zero(\$x)|);
93*eac174f2Safresh1ok(! $LIB->_is_one($x),  qq|$LIB->_is_one(\$x)|);
94*eac174f2Safresh1
95*eac174f2Safresh1is($LIB->_str($LIB->_zero()), "0", qq|$LIB->_str($LIB->_zero()) = "0"|);
96*eac174f2Safresh1is($LIB->_str($LIB->_one()),  "1", qq|$LIB->_str($LIB->_one())  = "1"|);
97b8851fccSafresh1
98b8851fccSafresh1# _two() and _ten()
99b8851fccSafresh1
100*eac174f2Safresh1is($LIB->_str($LIB->_two()), "2",  qq|$LIB->_str($LIB->_two()) = "2"|);
101*eac174f2Safresh1is($LIB->_str($LIB->_ten()), "10", qq|$LIB->_str($LIB->_ten()) = "10"|);
102b8851fccSafresh1
103*eac174f2Safresh1ok(! $LIB->_is_ten($LIB->_two()), qq|$LIB->_is_ten($LIB->_two()) is false|);
104*eac174f2Safresh1ok(  $LIB->_is_two($LIB->_two()), qq|$LIB->_is_two($LIB->_two()) is true|);
105*eac174f2Safresh1ok(  $LIB->_is_ten($LIB->_ten()), qq|$LIB->_is_ten($LIB->_ten()) is true|);
106*eac174f2Safresh1ok(! $LIB->_is_two($LIB->_ten()), qq|$LIB->_is_two($LIB->_ten()) is false|);
107b8851fccSafresh1
108*eac174f2Safresh1ok(  $LIB->_is_one($LIB->_one()), qq|$LIB->_is_one($LIB->_one()) is true|);
109*eac174f2Safresh1ok(! $LIB->_is_one($LIB->_two()), qq|$LIB->_is_one($LIB->_two()) is false|);
110*eac174f2Safresh1ok(! $LIB->_is_one($LIB->_ten()), qq|$LIB->_is_one($LIB->_ten()) is false|);
111b8851fccSafresh1
112*eac174f2Safresh1ok(! $LIB->_is_one($LIB->_zero()),  qq/$LIB->_is_one($LIB->_zero()) is false/);
113*eac174f2Safresh1ok(  $LIB->_is_zero($LIB->_zero()), qq|$LIB->_is_zero($LIB->_zero()) is true|);
114*eac174f2Safresh1ok(! $LIB->_is_zero($LIB->_one()),  qq/$LIB->_is_zero($LIB->_one()) is false/);
115b8851fccSafresh1
116b8851fccSafresh1# is_odd, is_even
117b8851fccSafresh1
118*eac174f2Safresh1ok(  $LIB->_is_odd($LIB->_one()),   qq/$LIB->_is_odd($LIB->_one()) is true/);
119*eac174f2Safresh1ok(! $LIB->_is_odd($LIB->_zero()),  qq/$LIB->_is_odd($LIB->_zero()) is false/);
120*eac174f2Safresh1ok(! $LIB->_is_even($LIB->_one()),  qq/$LIB->_is_even($LIB->_one()) is false/);
121*eac174f2Safresh1ok(  $LIB->_is_even($LIB->_zero()), qq/$LIB->_is_even($LIB->_zero()) is true/);
122b8851fccSafresh1
123*eac174f2Safresh1# _alen and _len
124b8851fccSafresh1
125*eac174f2Safresh1for my $method (qw/_alen _len/) {
126*eac174f2Safresh1    $x = $LIB->_new("1");
127*eac174f2Safresh1    is($LIB->$method($x), 1, qq|$LIB->$method(\$x) = 1|);
128*eac174f2Safresh1    $x = $LIB->_new("12");
129*eac174f2Safresh1    is($LIB->$method($x), 2, qq|$LIB->$method(\$x) = 2|);
130*eac174f2Safresh1    $x = $LIB->_new("123");
131*eac174f2Safresh1    is($LIB->$method($x), 3, qq|$LIB->$method(\$x) = 3|);
132*eac174f2Safresh1    $x = $LIB->_new("1234");
133*eac174f2Safresh1    is($LIB->$method($x), 4, qq|$LIB->$method(\$x) = 4|);
134*eac174f2Safresh1    $x = $LIB->_new("12345");
135*eac174f2Safresh1    is($LIB->$method($x), 5, qq|$LIB->$method(\$x) = 5|);
136*eac174f2Safresh1    $x = $LIB->_new("123456");
137*eac174f2Safresh1    is($LIB->$method($x), 6, qq|$LIB->$method(\$x) = 6|);
138*eac174f2Safresh1    $x = $LIB->_new("1234567");
139*eac174f2Safresh1    is($LIB->$method($x), 7, qq|$LIB->$method(\$x) = 7|);
140*eac174f2Safresh1    $x = $LIB->_new("12345678");
141*eac174f2Safresh1    is($LIB->$method($x), 8, qq|$LIB->$method(\$x) = 8|);
142*eac174f2Safresh1    $x = $LIB->_new("123456789");
143*eac174f2Safresh1    is($LIB->$method($x), 9, qq|$LIB->$method(\$x) = 9|);
144b8851fccSafresh1
145*eac174f2Safresh1    $x = $LIB->_new("8");
146*eac174f2Safresh1    is($LIB->$method($x), 1, qq|$LIB->$method(\$x) = 1|);
147*eac174f2Safresh1    $x = $LIB->_new("21");
148*eac174f2Safresh1    is($LIB->$method($x), 2, qq|$LIB->$method(\$x) = 2|);
149*eac174f2Safresh1    $x = $LIB->_new("321");
150*eac174f2Safresh1    is($LIB->$method($x), 3, qq|$LIB->$method(\$x) = 3|);
151*eac174f2Safresh1    $x = $LIB->_new("4321");
152*eac174f2Safresh1    is($LIB->$method($x), 4, qq|$LIB->$method(\$x) = 4|);
153*eac174f2Safresh1    $x = $LIB->_new("54321");
154*eac174f2Safresh1    is($LIB->$method($x), 5, qq|$LIB->$method(\$x) = 5|);
155*eac174f2Safresh1    $x = $LIB->_new("654321");
156*eac174f2Safresh1    is($LIB->$method($x), 6, qq|$LIB->$method(\$x) = 6|);
157*eac174f2Safresh1    $x = $LIB->_new("7654321");
158*eac174f2Safresh1    is($LIB->$method($x), 7, qq|$LIB->$method(\$x) = 7|);
159*eac174f2Safresh1    $x = $LIB->_new("87654321");
160*eac174f2Safresh1    is($LIB->$method($x), 8, qq|$LIB->$method(\$x) = 8|);
161*eac174f2Safresh1    $x = $LIB->_new("987654321");
162*eac174f2Safresh1    is($LIB->$method($x), 9, qq|$LIB->$method(\$x) = 9|);
163*eac174f2Safresh1
164*eac174f2Safresh1    $x = $LIB->_new("0");
165*eac174f2Safresh1    is($LIB->$method($x), 1, qq|$LIB->$method(\$x) = 1|);
166*eac174f2Safresh1    $x = $LIB->_new("20");
167*eac174f2Safresh1    is($LIB->$method($x), 2, qq|$LIB->$method(\$x) = 2|);
168*eac174f2Safresh1    $x = $LIB->_new("320");
169*eac174f2Safresh1    is($LIB->$method($x), 3, qq|$LIB->$method(\$x) = 3|);
170*eac174f2Safresh1    $x = $LIB->_new("4320");
171*eac174f2Safresh1    is($LIB->$method($x), 4, qq|$LIB->$method(\$x) = 4|);
172*eac174f2Safresh1    $x = $LIB->_new("54320");
173*eac174f2Safresh1    is($LIB->$method($x), 5, qq|$LIB->$method(\$x) = 5|);
174*eac174f2Safresh1    $x = $LIB->_new("654320");
175*eac174f2Safresh1    is($LIB->$method($x), 6, qq|$LIB->$method(\$x) = 6|);
176*eac174f2Safresh1    $x = $LIB->_new("7654320");
177*eac174f2Safresh1    is($LIB->$method($x), 7, qq|$LIB->$method(\$x) = 7|);
178*eac174f2Safresh1    $x = $LIB->_new("87654320");
179*eac174f2Safresh1    is($LIB->$method($x), 8, qq|$LIB->$method(\$x) = 8|);
180*eac174f2Safresh1    $x = $LIB->_new("987654320");
181*eac174f2Safresh1    is($LIB->$method($x), 9, qq|$LIB->$method(\$x) = 9|);
182*eac174f2Safresh1
183*eac174f2Safresh1    for (my $i = 1; $i < 9; $i++) {
184b8851fccSafresh1        my $a = "$i" . '0' x ($i - 1);
185*eac174f2Safresh1        $x = $LIB->_new($a);
186*eac174f2Safresh1        is($LIB->_len($x), $i, qq|$LIB->_len(\$x) = $i|);
187b8851fccSafresh1    }
188b8851fccSafresh1}
189b8851fccSafresh1
190b8851fccSafresh1# _digit
191*eac174f2Safresh1
192*eac174f2Safresh1$x = $LIB->_new("123456789");
193*eac174f2Safresh1is($LIB->_digit($x, 0),   9, qq|$LIB->_digit(\$x, 0) = 9|);
194*eac174f2Safresh1is($LIB->_digit($x, 1),   8, qq|$LIB->_digit(\$x, 1) = 8|);
195*eac174f2Safresh1is($LIB->_digit($x, 2),   7, qq|$LIB->_digit(\$x, 2) = 7|);
196*eac174f2Safresh1is($LIB->_digit($x, 8),   1, qq|$LIB->_digit(\$x, 8) = 1|);
197*eac174f2Safresh1is($LIB->_digit($x, 9),   0, qq|$LIB->_digit(\$x, 9) = 0|);
198*eac174f2Safresh1is($LIB->_digit($x, -1),  1, qq|$LIB->_digit(\$x, -1) = 1|);
199*eac174f2Safresh1is($LIB->_digit($x, -2),  2, qq|$LIB->_digit(\$x, -2) = 2|);
200*eac174f2Safresh1is($LIB->_digit($x, -3),  3, qq|$LIB->_digit(\$x, -3) = 3|);
201*eac174f2Safresh1is($LIB->_digit($x, -9),  9, qq|$LIB->_digit(\$x, -9) = 9|);
202*eac174f2Safresh1is($LIB->_digit($x, -10), 0, qq|$LIB->_digit(\$x, -10) = 0|);
203b8851fccSafresh1
204b8851fccSafresh1# _copy
205*eac174f2Safresh1
206*eac174f2Safresh1foreach (qw/ 1 12 123 1234 12345 123456 1234567 12345678 123456789/) {
207*eac174f2Safresh1    $x = $LIB->_new("$_");
208*eac174f2Safresh1    is($LIB->_str($LIB->_copy($x)), "$_",
209*eac174f2Safresh1       qq|$LIB->_str($LIB->_copy(\$x)) = "$_"|);
210*eac174f2Safresh1    is($LIB->_str($x), "$_",           # did _copy destroy original x?
211*eac174f2Safresh1       qq|$LIB->_str(\$x) = "$_"|);
212b8851fccSafresh1}
213b8851fccSafresh1
214b8851fccSafresh1# _zeros
215*eac174f2Safresh1
216*eac174f2Safresh1$x = $LIB->_new("1256000000");
217*eac174f2Safresh1is($LIB->_zeros($x), 6, qq|$LIB->_zeros(\$x) = 6|);
218*eac174f2Safresh1
219*eac174f2Safresh1$x = $LIB->_new("152");
220*eac174f2Safresh1is($LIB->_zeros($x), 0, qq|$LIB->_zeros(\$x) = 0|);
221*eac174f2Safresh1
222*eac174f2Safresh1$x = $LIB->_new("123000");
223*eac174f2Safresh1is($LIB->_zeros($x), 3, qq|$LIB->_zeros(\$x) = 3|);
224*eac174f2Safresh1
225*eac174f2Safresh1$x = $LIB->_new("0");
226*eac174f2Safresh1is($LIB->_zeros($x), 0, qq|$LIB->_zeros(\$x) = 0|);
227b8851fccSafresh1
228b8851fccSafresh1# _lsft, _rsft
229b8851fccSafresh1
230*eac174f2Safresh1$x = $LIB->_new("10");
231*eac174f2Safresh1$y = $LIB->_new("3");
232*eac174f2Safresh1is($LIB->_str($LIB->_lsft($x, $y, 10)), 10000,
233*eac174f2Safresh1   qq|$LIB->_str($LIB->_lsft(\$x, \$y, 10)) = 10000|);
234b8851fccSafresh1
235*eac174f2Safresh1$x = $LIB->_new("20");
236*eac174f2Safresh1$y = $LIB->_new("3");
237*eac174f2Safresh1is($LIB->_str($LIB->_lsft($x, $y, 10)), 20000,
238*eac174f2Safresh1   qq|$LIB->_str($LIB->_lsft(\$x, \$y, 10)) = 20000|);
239b8851fccSafresh1
240*eac174f2Safresh1$x = $LIB->_new("128");
241*eac174f2Safresh1$y = $LIB->_new("4");
242*eac174f2Safresh1is($LIB->_str($LIB->_lsft($x, $y, 2)), 128 << 4,
243*eac174f2Safresh1   qq|$LIB->_str($LIB->_lsft(\$x, \$y, 2)) = 128 << 4|);
244*eac174f2Safresh1
245*eac174f2Safresh1$x = $LIB->_new("1000");
246*eac174f2Safresh1$y = $LIB->_new("3");
247*eac174f2Safresh1is($LIB->_str($LIB->_rsft($x, $y, 10)), 1,
248*eac174f2Safresh1   qq|$LIB->_str($LIB->_rsft(\$x, \$y, 10)) = 1|);
249*eac174f2Safresh1
250*eac174f2Safresh1$x = $LIB->_new("20000");
251*eac174f2Safresh1$y = $LIB->_new("3");
252*eac174f2Safresh1is($LIB->_str($LIB->_rsft($x, $y, 10)), 20,
253*eac174f2Safresh1   qq|$LIB->_str($LIB->_rsft(\$x, \$y, 10)) = 20|);
254*eac174f2Safresh1
255*eac174f2Safresh1$x = $LIB->_new("256");
256*eac174f2Safresh1$y = $LIB->_new("4");
257*eac174f2Safresh1is($LIB->_str($LIB->_rsft($x, $y, 2)), 256 >> 4,
258*eac174f2Safresh1   qq|$LIB->_str($LIB->_rsft(\$x, \$y, 2)) = 256 >> 4|);
259*eac174f2Safresh1
260*eac174f2Safresh1$x = $LIB->_new("6411906467305339182857313397200584952398");
261*eac174f2Safresh1$y = $LIB->_new("45");
262*eac174f2Safresh1is($LIB->_str($LIB->_rsft($x, $y, 10)), 0,
263*eac174f2Safresh1   qq|$LIB->_str($LIB->_rsft(\$x, \$y, 10)) = 0|);
264*eac174f2Safresh1
265*eac174f2Safresh1# _lsft() with large bases
266*eac174f2Safresh1
267*eac174f2Safresh1for my $xstr ("1", "2", "3") {
268*eac174f2Safresh1    for my $nstr ("1", "2", "3") {
269*eac174f2Safresh1        for my $bpow (25, 50, 75) {
270*eac174f2Safresh1            my $bstr = "1" . ("0" x $bpow);
271*eac174f2Safresh1            my $expected = $xstr . ("0" x ($bpow * $nstr));
272*eac174f2Safresh1            my $xobj = $LIB->_new($xstr);
273*eac174f2Safresh1            my $nobj = $LIB->_new($nstr);
274*eac174f2Safresh1            my $bobj = $LIB->_new($bstr);
275*eac174f2Safresh1
276*eac174f2Safresh1            is($LIB->_str($LIB->_lsft($xobj, $nobj, $bobj)), $expected,
277*eac174f2Safresh1               qq|$LIB->_str($LIB->_lsft($LIB->_new("$xstr"), |
278*eac174f2Safresh1                                    . qq|$LIB->_new("$nstr"), |
279*eac174f2Safresh1                                    . qq|$LIB->_new("$bstr")))|);
280*eac174f2Safresh1            is($LIB->_str($nobj), $nstr, q|$n is unmodified|);
281*eac174f2Safresh1            is($LIB->_str($bobj), $bstr, q|$b is unmodified|);
282*eac174f2Safresh1        }
283*eac174f2Safresh1    }
284*eac174f2Safresh1}
285b8851fccSafresh1
286b8851fccSafresh1# _acmp
287b8851fccSafresh1
288*eac174f2Safresh1$x = $LIB->_new("123456789");
289*eac174f2Safresh1$y = $LIB->_new("987654321");
290*eac174f2Safresh1is($LIB->_acmp($x, $y), -1, qq|$LIB->_acmp(\$x, \$y) = -1|);
291*eac174f2Safresh1is($LIB->_acmp($y, $x), 1,  qq|$LIB->_acmp(\$y, \$x) = 1|);
292*eac174f2Safresh1is($LIB->_acmp($x, $x), 0,  qq|$LIB->_acmp(\$x, \$x) = 0|);
293*eac174f2Safresh1is($LIB->_acmp($y, $y), 0,  qq|$LIB->_acmp(\$y, \$y) = 0|);
294*eac174f2Safresh1$x = $LIB->_new("12");
295*eac174f2Safresh1$y = $LIB->_new("12");
296*eac174f2Safresh1is($LIB->_acmp($x, $y), 0,  qq|$LIB->_acmp(\$x, \$y) = 0|);
297*eac174f2Safresh1$x = $LIB->_new("21");
298*eac174f2Safresh1is($LIB->_acmp($x, $y), 1,  qq|$LIB->_acmp(\$x, \$y) = 1|);
299*eac174f2Safresh1is($LIB->_acmp($y, $x), -1, qq|$LIB->_acmp(\$y, \$x) = -1|);
300*eac174f2Safresh1$x = $LIB->_new("123456789");
301*eac174f2Safresh1$y = $LIB->_new("1987654321");
302*eac174f2Safresh1is($LIB->_acmp($x, $y), -1, qq|$LIB->_acmp(\$x, \$y) = -1|);
303*eac174f2Safresh1is($LIB->_acmp($y, $x), +1, qq|$LIB->_acmp(\$y, \$x) = +1|);
304b8851fccSafresh1
305*eac174f2Safresh1$x = $LIB->_new("1234567890123456789");
306*eac174f2Safresh1$y = $LIB->_new("987654321012345678");
307*eac174f2Safresh1is($LIB->_acmp($x, $y), 1,  qq|$LIB->_acmp(\$x, \$y) = 1|);
308*eac174f2Safresh1is($LIB->_acmp($y, $x), -1, qq|$LIB->_acmp(\$y, \$x) = -1|);
309*eac174f2Safresh1is($LIB->_acmp($x, $x), 0,  qq|$LIB->_acmp(\$x, \$x) = 0|);
310*eac174f2Safresh1is($LIB->_acmp($y, $y), 0,  qq|$LIB->_acmp(\$y, \$y) = 0|);
311*eac174f2Safresh1
312*eac174f2Safresh1$x = $LIB->_new("1234");
313*eac174f2Safresh1$y = $LIB->_new("987654321012345678");
314*eac174f2Safresh1is($LIB->_acmp($x, $y), -1, qq|$LIB->_acmp(\$x, \$y) = -1|);
315*eac174f2Safresh1is($LIB->_acmp($y, $x), 1,  qq|$LIB->_acmp(\$y, \$x) = 1|);
316*eac174f2Safresh1is($LIB->_acmp($x, $x), 0,  qq|$LIB->_acmp(\$x, \$x) = 0|);
317*eac174f2Safresh1is($LIB->_acmp($y, $y), 0,  qq|$LIB->_acmp(\$y, \$y) = 0|);
318b8851fccSafresh1
319b8851fccSafresh1# _modinv
320*eac174f2Safresh1
321*eac174f2Safresh1$x = $LIB->_new("8");
322*eac174f2Safresh1$y = $LIB->_new("5033");
323*eac174f2Safresh1my ($xmod, $sign) = $LIB->_modinv($x, $y);
324*eac174f2Safresh1is($LIB->_str($xmod), "629",            # -629 % 5033 == 4404
325*eac174f2Safresh1   qq|$LIB->_str(\$xmod) = "629"|);
326*eac174f2Safresh1is($sign, "-", q|$sign = "-"|);
327b8851fccSafresh1
328b8851fccSafresh1# _div
329*eac174f2Safresh1
330*eac174f2Safresh1$x = $LIB->_new("3333");
331*eac174f2Safresh1$y = $LIB->_new("1111");
332*eac174f2Safresh1is($LIB->_str(scalar($LIB->_div($x, $y))), 3,
333*eac174f2Safresh1   qq|$LIB->_str(scalar($LIB->_div(\$x, \$y))) = 3|);
334*eac174f2Safresh1
335*eac174f2Safresh1$x = $LIB->_new("33333");
336*eac174f2Safresh1$y = $LIB->_new("1111");
337*eac174f2Safresh1($x, $y) = $LIB->_div($x, $y);
338*eac174f2Safresh1is($LIB->_str($x), 30, qq|$LIB->_str(\$x) = 30|);
339*eac174f2Safresh1is($LIB->_str($y),  3, qq|$LIB->_str(\$y) = 3|);
340*eac174f2Safresh1
341*eac174f2Safresh1$x = $LIB->_new("123");
342*eac174f2Safresh1$y = $LIB->_new("1111");
343*eac174f2Safresh1($x, $y) = $LIB->_div($x, $y);
344*eac174f2Safresh1is($LIB->_str($x), 0,   qq|$LIB->_str(\$x) = 0|);
345*eac174f2Safresh1is($LIB->_str($y), 123, qq|$LIB->_str(\$y) = 123|);
346b8851fccSafresh1
347b8851fccSafresh1# _num
348*eac174f2Safresh1
349*eac174f2Safresh1foreach (qw/1 12 123 1234 12345 1234567 12345678 123456789 1234567890/) {
350*eac174f2Safresh1
351*eac174f2Safresh1    $x = $LIB->_new("$_");
352*eac174f2Safresh1    is(ref($x), $REF, q|ref($x) = "$REF"|);
353*eac174f2Safresh1    is($LIB->_str($x), "$_", qq|$LIB->_str(\$x) = "$_"|);
354*eac174f2Safresh1
355*eac174f2Safresh1    $x = $LIB->_num($x);
356*eac174f2Safresh1    is(ref($x), "", q|ref($x) = ""|);
357*eac174f2Safresh1    is($x,      $_, qq|\$x = $_|);
358b8851fccSafresh1}
359b8851fccSafresh1
360b8851fccSafresh1# _sqrt
361*eac174f2Safresh1
362*eac174f2Safresh1$x = $LIB->_new("144");
363*eac174f2Safresh1is($LIB->_str($LIB->_sqrt($x)), "12",
364*eac174f2Safresh1   qq|$LIB->_str($LIB->_sqrt(\$x)) = "12"|);
365*eac174f2Safresh1$x = $LIB->_new("144000000000000");
366*eac174f2Safresh1is($LIB->_str($LIB->_sqrt($x)), "12000000",
367*eac174f2Safresh1   qq|$LIB->_str($LIB->_sqrt(\$x)) = "12000000"|);
368b8851fccSafresh1
369b8851fccSafresh1# _root
370*eac174f2Safresh1
371*eac174f2Safresh1$x = $LIB->_new("81");
372*eac174f2Safresh1my $n = $LIB->_new("3");        # 4*4*4 = 64, 5*5*5 = 125
373*eac174f2Safresh1is($LIB->_str($LIB->_root($x, $n)), "4",
374*eac174f2Safresh1   qq|$LIB->_str($LIB->_root(\$x, \$n)) = "4"|); # 4.xx => 4.0
375*eac174f2Safresh1
376*eac174f2Safresh1$x = $LIB->_new("81");
377*eac174f2Safresh1$n = $LIB->_new("4");          # 3*3*3*3 == 81
378*eac174f2Safresh1is($LIB->_str($LIB->_root($x, $n)), "3",
379*eac174f2Safresh1   qq|$LIB->_str($LIB->_root(\$x, \$n)) = "3"|);
380b8851fccSafresh1
381b8851fccSafresh1# _pow (and _root)
382b8851fccSafresh1
383*eac174f2Safresh1$x = $LIB->_new("0");
384*eac174f2Safresh1$n = $LIB->_new("3");          # 0 ** y => 0
385*eac174f2Safresh1is($LIB->_str($LIB->_pow($x, $n)), 0,
386*eac174f2Safresh1   qq|$LIB->_str($LIB->_pow(\$x, \$n)) = 0|);
387b8851fccSafresh1
388*eac174f2Safresh1$x = $LIB->_new("3");
389*eac174f2Safresh1$n = $LIB->_new("0");          # x ** 0 => 1
390*eac174f2Safresh1is($LIB->_str($LIB->_pow($x, $n)), 1,
391*eac174f2Safresh1   qq|$LIB->_str($LIB->_pow(\$x, \$n)) = 1|);
392b8851fccSafresh1
393*eac174f2Safresh1$x = $LIB->_new("1");
394*eac174f2Safresh1$n = $LIB->_new("3");          # 1 ** y => 1
395*eac174f2Safresh1is($LIB->_str($LIB->_pow($x, $n)), 1,
396*eac174f2Safresh1   qq|$LIB->_str($LIB->_pow(\$x, \$n)) = 1|);
397b8851fccSafresh1
398*eac174f2Safresh1$x = $LIB->_new("5");
399*eac174f2Safresh1$n = $LIB->_new("1");          # x ** 1 => x
400*eac174f2Safresh1is($LIB->_str($LIB->_pow($x, $n)), 5,
401*eac174f2Safresh1   qq|$LIB->_str($LIB->_pow(\$x, \$n)) = 5|);
402b8851fccSafresh1
403*eac174f2Safresh1$x = $LIB->_new("81");
404*eac174f2Safresh1$n = $LIB->_new("3");          # 81 ** 3 == 531441
405*eac174f2Safresh1is($LIB->_str($LIB->_pow($x, $n)), 81 ** 3,
406*eac174f2Safresh1   qq|$LIB->_str($LIB->_pow(\$x, \$n)) = 81 ** 3|);
407b8851fccSafresh1
408*eac174f2Safresh1is($LIB->_str($LIB->_root($x, $n)), 81,
409*eac174f2Safresh1   qq|$LIB->_str($LIB->_root(\$x, \$n)) = 81|);
410b8851fccSafresh1
411*eac174f2Safresh1$x = $LIB->_new("81");
412*eac174f2Safresh1is($LIB->_str($LIB->_pow($x, $n)), 81 ** 3,
413*eac174f2Safresh1   qq|$LIB->_str($LIB->_pow(\$x, \$n)) = 81 ** 3|);
414*eac174f2Safresh1is($LIB->_str($LIB->_pow($x, $n)), "150094635296999121",      # 531441 ** 3
415*eac174f2Safresh1   qq|$LIB->_str($LIB->_pow(\$x, \$n)) = "150094635296999121"|);
416b8851fccSafresh1
417*eac174f2Safresh1is($LIB->_str($LIB->_root($x, $n)), "531441",
418*eac174f2Safresh1   qq|$LIB->_str($LIB->_root(\$x, \$n)) = "531441"|);
419*eac174f2Safresh1is($LIB->_str($LIB->_root($x, $n)), "81",
420*eac174f2Safresh1   qq|$LIB->_str($LIB->_root(\$x, \$n)) = "81"|);
421*eac174f2Safresh1
422*eac174f2Safresh1$x = $LIB->_new("81");
423*eac174f2Safresh1$n = $LIB->_new("14");
424*eac174f2Safresh1is($LIB->_str($LIB->_pow($x, $n)), "523347633027360537213511521",
425*eac174f2Safresh1   qq|$LIB->_str($LIB->_pow(\$x, \$n)) = "523347633027360537213511521"|);
426*eac174f2Safresh1is($LIB->_str($LIB->_root($x, $n)), "81",
427*eac174f2Safresh1   qq|$LIB->_str($LIB->_root(\$x, \$n)) = "81"|);
428*eac174f2Safresh1
429*eac174f2Safresh1$x = $LIB->_new("523347633027360537213511520");
430*eac174f2Safresh1is($LIB->_str($LIB->_root($x, $n)), "80",
431*eac174f2Safresh1   qq|$LIB->_str($LIB->_root(\$x, \$n)) = "80"|);
432*eac174f2Safresh1
433*eac174f2Safresh1$x = $LIB->_new("523347633027360537213511522");
434*eac174f2Safresh1is($LIB->_str($LIB->_root($x, $n)), "81",
435*eac174f2Safresh1   qq|$LIB->_str($LIB->_root(\$x, \$n)) = "81"|);
436*eac174f2Safresh1
437*eac174f2Safresh1my $res = [ qw/9 31 99 316 999 3162 9999 31622 99999/ ];
438b8851fccSafresh1
439b8851fccSafresh1# 99 ** 2 = 9801, 999 ** 2 = 998001 etc
440*eac174f2Safresh1
441*eac174f2Safresh1for my $i (2 .. 9) {
442*eac174f2Safresh1    $x = '9' x $i;
443*eac174f2Safresh1    $x = $LIB->_new($x);
444*eac174f2Safresh1    $n = $LIB->_new("2");
445b8851fccSafresh1    my $rc = '9' x ($i-1). '8' . '0' x ($i - 1) . '1';
446b8851fccSafresh1    print "# _pow( ", '9' x $i, ", 2) \n" unless
447*eac174f2Safresh1      is($LIB->_str($LIB->_pow($x, $n)), $rc,
448*eac174f2Safresh1         qq|$LIB->_str($LIB->_pow(\$x, \$n)) = $rc|);
449b8851fccSafresh1
450*eac174f2Safresh1  SKIP: {
451*eac174f2Safresh1        # If $i > $BASE_LEN, the test takes a really long time.
452*eac174f2Safresh1        skip "$i > $BASE_LEN", 2 unless $i <= $BASE_LEN;
453b8851fccSafresh1
454*eac174f2Safresh1        $x = '9' x $i;
455*eac174f2Safresh1        $x = $LIB->_new($x);
456*eac174f2Safresh1        $n = '9' x $i;
457*eac174f2Safresh1        $n = $LIB->_new($n);
458*eac174f2Safresh1        print "# _root( ", '9' x $i, ", ", 9 x $i, ") \n";
459*eac174f2Safresh1        print "# _root( ", '9' x $i, ", ", 9 x $i, ") \n"
460*eac174f2Safresh1          unless is($LIB->_str($LIB->_root($x, $n)), '1',
461*eac174f2Safresh1                    qq|$LIB->_str($LIB->_root(\$x, \$n)) = '1'|);
462*eac174f2Safresh1
463*eac174f2Safresh1        $x = '9' x $i;
464*eac174f2Safresh1        $x = $LIB->_new($x);
465*eac174f2Safresh1        $n = $LIB->_new("2");
466*eac174f2Safresh1        print "# BASE_LEN $BASE_LEN _root( ", '9' x $i, ", ", 9 x $i, ") \n"
467*eac174f2Safresh1          unless is($LIB->_str($LIB->_root($x, $n)), $res->[$i-2],
468*eac174f2Safresh1                    qq|$LIB->_str($LIB->_root(\$x, \$n)) = $res->[$i-2]|);
469b8851fccSafresh1    }
470b8851fccSafresh1}
471b8851fccSafresh1
472b8851fccSafresh1##############################################################################
473b8851fccSafresh1# _fac
474*eac174f2Safresh1
475*eac174f2Safresh1$x = $LIB->_new("0");
476*eac174f2Safresh1is($LIB->_str($LIB->_fac($x)), "1",
477*eac174f2Safresh1   qq|$LIB->_str($LIB->_fac(\$x)) = "1"|);
478*eac174f2Safresh1
479*eac174f2Safresh1$x = $LIB->_new("1");
480*eac174f2Safresh1is($LIB->_str($LIB->_fac($x)), "1",
481*eac174f2Safresh1   qq|$LIB->_str($LIB->_fac(\$x)) = "1"|);
482*eac174f2Safresh1
483*eac174f2Safresh1$x = $LIB->_new("2");
484*eac174f2Safresh1is($LIB->_str($LIB->_fac($x)), "2",
485*eac174f2Safresh1   qq|$LIB->_str($LIB->_fac(\$x)) = "2"|);
486*eac174f2Safresh1
487*eac174f2Safresh1$x = $LIB->_new("3");
488*eac174f2Safresh1is($LIB->_str($LIB->_fac($x)), "6",
489*eac174f2Safresh1   qq|$LIB->_str($LIB->_fac(\$x)) = "6"|);
490*eac174f2Safresh1
491*eac174f2Safresh1$x = $LIB->_new("4");
492*eac174f2Safresh1is($LIB->_str($LIB->_fac($x)), "24",
493*eac174f2Safresh1   qq|$LIB->_str($LIB->_fac(\$x)) = "24"|);
494*eac174f2Safresh1
495*eac174f2Safresh1$x = $LIB->_new("5");
496*eac174f2Safresh1is($LIB->_str($LIB->_fac($x)), "120",
497*eac174f2Safresh1   qq|$LIB->_str($LIB->_fac(\$x)) = "120"|);
498*eac174f2Safresh1
499*eac174f2Safresh1$x = $LIB->_new("10");
500*eac174f2Safresh1is($LIB->_str($LIB->_fac($x)), "3628800",
501*eac174f2Safresh1   qq|$LIB->_str($LIB->_fac(\$x)) = "3628800"|);
502*eac174f2Safresh1
503*eac174f2Safresh1$x = $LIB->_new("11");
504*eac174f2Safresh1is($LIB->_str($LIB->_fac($x)), "39916800",
505*eac174f2Safresh1   qq|$LIB->_str($LIB->_fac(\$x)) = "39916800"|);
506*eac174f2Safresh1
507*eac174f2Safresh1$x = $LIB->_new("12");
508*eac174f2Safresh1is($LIB->_str($LIB->_fac($x)), "479001600",
509*eac174f2Safresh1   qq|$LIB->_str($LIB->_fac(\$x)) = "479001600"|);
510*eac174f2Safresh1
511*eac174f2Safresh1$x = $LIB->_new("13");
512*eac174f2Safresh1is($LIB->_str($LIB->_fac($x)), "6227020800",
513*eac174f2Safresh1   qq|$LIB->_str($LIB->_fac(\$x)) = "6227020800"|);
514b8851fccSafresh1
515b8851fccSafresh1# test that _fac modifies $x in place for small arguments
516b8851fccSafresh1
517*eac174f2Safresh1$x = $LIB->_new("3");
518*eac174f2Safresh1$LIB->_fac($x);
519*eac174f2Safresh1is($LIB->_str($x), "6",
520*eac174f2Safresh1   qq|$LIB->_str(\$x) = "6"|);
521*eac174f2Safresh1
522*eac174f2Safresh1$x = $LIB->_new("13");
523*eac174f2Safresh1$LIB->_fac($x);
524*eac174f2Safresh1is($LIB->_str($x), "6227020800",
525*eac174f2Safresh1   qq|$LIB->_str(\$x) = "6227020800"|);
526*eac174f2Safresh1
527b8851fccSafresh1# _inc and _dec
528*eac174f2Safresh1
529*eac174f2Safresh1for (qw/1 11 121 1231 12341 1234561 12345671 123456781 1234567891/) {
530*eac174f2Safresh1    $x = $LIB->_new("$_");
531*eac174f2Safresh1    $LIB->_inc($x);
532*eac174f2Safresh1    my $expected = substr($_, 0, length($_) - 1) . '2';
533*eac174f2Safresh1    is($LIB->_str($x), $expected, qq|$LIB->_str(\$x) = $expected|);
534*eac174f2Safresh1    $LIB->_dec($x);
535*eac174f2Safresh1    is($LIB->_str($x), $_, qq|$LIB->_str(\$x) = $_|);
536b8851fccSafresh1}
537b8851fccSafresh1
538*eac174f2Safresh1for (qw/19 119 1219 12319 1234519 12345619 123456719 1234567819/) {
539*eac174f2Safresh1    $x = $LIB->_new("$_");
540*eac174f2Safresh1    $LIB->_inc($x);
541*eac174f2Safresh1    my $expected = substr($_, 0, length($_)-2) . '20';
542*eac174f2Safresh1    is($LIB->_str($x), $expected, qq|$LIB->_str(\$x) = $expected|);
543*eac174f2Safresh1    $LIB->_dec($x);
544*eac174f2Safresh1    is($LIB->_str($x), $_, qq|$LIB->_str(\$x) = $_|);
545*eac174f2Safresh1}
546b8851fccSafresh1
547*eac174f2Safresh1for (1 .. 20) {
548*eac174f2Safresh1    my $p = "9" x $_;                       # = $q - 1
549*eac174f2Safresh1    my $q = "1" . ("0" x $_);               # = $p + 1
550*eac174f2Safresh1
551*eac174f2Safresh1    $x = $LIB->_new("$p");
552*eac174f2Safresh1    $LIB->_inc($x);
553*eac174f2Safresh1    is($LIB->_str($x), $q, qq|\$x = $LIB->_new("$p"); $LIB->_inc()|);
554*eac174f2Safresh1
555*eac174f2Safresh1    $x = $LIB->_new("$q");
556*eac174f2Safresh1    $LIB->_dec($x);
557*eac174f2Safresh1    is($LIB->_str($x), $p, qq|\$x = $LIB->_new("$q"); $LIB->_dec()|);
558*eac174f2Safresh1}
559*eac174f2Safresh1
560*eac174f2Safresh1for (1 .. 20) {
561*eac174f2Safresh1    my $p = "1" . ("0" x $_);               # = $q - 1
562*eac174f2Safresh1    my $q = "1" . ("0" x ($_ - 1)) . "1";   # = $p + 1
563*eac174f2Safresh1
564*eac174f2Safresh1    $x = $LIB->_new("$p");
565*eac174f2Safresh1    $LIB->_inc($x);
566*eac174f2Safresh1    is($LIB->_str($x), $q, qq|\$x = $LIB->_new("$p"); $LIB->_inc()|);
567*eac174f2Safresh1
568*eac174f2Safresh1    $x = $LIB->_new("$q");
569*eac174f2Safresh1    $LIB->_dec($x);
570*eac174f2Safresh1    is($LIB->_str($x), $p, qq|\$x = $LIB->_new("$q"); $LIB->_dec()|);
571*eac174f2Safresh1}
572*eac174f2Safresh1
573*eac174f2Safresh1$x = $LIB->_new("1000");
574*eac174f2Safresh1$LIB->_inc($x);
575*eac174f2Safresh1is($LIB->_str($x), "1001", qq|$LIB->_str(\$x) = "1001"|);
576*eac174f2Safresh1$LIB->_dec($x);
577*eac174f2Safresh1is($LIB->_str($x), "1000", qq|$LIB->_str(\$x) = "1000"|);
578*eac174f2Safresh1
579*eac174f2Safresh1my $BL = $LIB -> _base_len();
580b8851fccSafresh1
581b8851fccSafresh1$x = '1' . '0' x $BL;
582*eac174f2Safresh1$z = '1' . '0' x ($BL - 1);
583*eac174f2Safresh1$z .= '1';
584*eac174f2Safresh1$x = $LIB->_new($x);
585*eac174f2Safresh1$LIB->_inc($x);
586*eac174f2Safresh1is($LIB->_str($x), $z, qq|$LIB->_str(\$x) = $z|);
587b8851fccSafresh1
588*eac174f2Safresh1$x = '1' . '0' x $BL;
589*eac174f2Safresh1$z = '9' x $BL;
590*eac174f2Safresh1$x = $LIB->_new($x);
591*eac174f2Safresh1$LIB->_dec($x);
592*eac174f2Safresh1is($LIB->_str($x), $z, qq|$LIB->_str(\$x) = $z|);
593b8851fccSafresh1
594b8851fccSafresh1# should not happen:
595*eac174f2Safresh1# $x = $LIB->_new("-2");
596*eac174f2Safresh1# $y = $LIB->_new("4");
597*eac174f2Safresh1# is($LIB->_acmp($x, $y), -1, qq|$LIB->_acmp($x, $y) = -1|);
598b8851fccSafresh1
599b8851fccSafresh1###############################################################################
600b8851fccSafresh1# _mod
601*eac174f2Safresh1
602*eac174f2Safresh1$x = $LIB->_new("1000");
603*eac174f2Safresh1$y = $LIB->_new("3");
604*eac174f2Safresh1is($LIB->_str(scalar($LIB->_mod($x, $y))), 1,
605*eac174f2Safresh1   qq|$LIB->_str(scalar($LIB->_mod(\$x, \$y))) = 1|);
606*eac174f2Safresh1
607*eac174f2Safresh1$x = $LIB->_new("1000");
608*eac174f2Safresh1$y = $LIB->_new("2");
609*eac174f2Safresh1is($LIB->_str(scalar($LIB->_mod($x, $y))), 0,
610*eac174f2Safresh1   qq|$LIB->_str(scalar($LIB->_mod(\$x, \$y))) = 0|);
611b8851fccSafresh1
612b8851fccSafresh1# _and, _or, _xor
613*eac174f2Safresh1
614*eac174f2Safresh1$x = $LIB->_new("5");
615*eac174f2Safresh1$y = $LIB->_new("2");
616*eac174f2Safresh1is($LIB->_str(scalar($LIB->_xor($x, $y))), 7,
617*eac174f2Safresh1   qq|$LIB->_str(scalar($LIB->_xor(\$x, \$y))) = 7|);
618*eac174f2Safresh1
619*eac174f2Safresh1$x = $LIB->_new("5");
620*eac174f2Safresh1$y = $LIB->_new("2");
621*eac174f2Safresh1is($LIB->_str(scalar($LIB->_or($x, $y))), 7,
622*eac174f2Safresh1   qq|$LIB->_str(scalar($LIB->_or(\$x, \$y))) = 7|);
623*eac174f2Safresh1
624*eac174f2Safresh1$x = $LIB->_new("5");
625*eac174f2Safresh1$y = $LIB->_new("3");
626*eac174f2Safresh1is($LIB->_str(scalar($LIB->_and($x, $y))), 1,
627*eac174f2Safresh1   qq|$LIB->_str(scalar($LIB->_and(\$x, \$y))) = 1|);
628b8851fccSafresh1
629b8851fccSafresh1# _from_hex, _from_bin, _from_oct
630*eac174f2Safresh1
631*eac174f2Safresh1is($LIB->_str($LIB->_from_hex("0xFf")), 255,
632*eac174f2Safresh1   qq|$LIB->_str($LIB->_from_hex("0xFf")) = 255|);
633*eac174f2Safresh1is($LIB->_str($LIB->_from_bin("0b10101011")), 160+11,
634*eac174f2Safresh1   qq|$LIB->_str($LIB->_from_bin("0b10101011")) = 160+11|);
635*eac174f2Safresh1is($LIB->_str($LIB->_from_oct("0100")), 8*8,
636*eac174f2Safresh1   qq|$LIB->_str($LIB->_from_oct("0100")) = 8*8|);
637*eac174f2Safresh1is($LIB->_str($LIB->_from_oct("01000")), 8*8*8,
638*eac174f2Safresh1   qq|$LIB->_str($LIB->_from_oct("01000")) = 8*8*8|);
639*eac174f2Safresh1is($LIB->_str($LIB->_from_oct("010001")), 8*8*8*8+1,
640*eac174f2Safresh1   qq|$LIB->_str($LIB->_from_oct("010001")) = 8*8*8*8+1|);
641*eac174f2Safresh1is($LIB->_str($LIB->_from_oct("010007")), 8*8*8*8+7,
642*eac174f2Safresh1   qq|$LIB->_str($LIB->_from_oct("010007")) = 8*8*8*8+7|);
643b8851fccSafresh1
644b8851fccSafresh1# _as_hex, _as_bin, as_oct
645b8851fccSafresh1
646*eac174f2Safresh1is($LIB->_str($LIB->_from_hex($LIB->_as_hex($LIB->_new("128")))), 128,
647*eac174f2Safresh1   qq|$LIB->_str($LIB->_from_hex($LIB->_as_hex(|
648*eac174f2Safresh1   . qq|$LIB->_new("128")))) = 128|);
649*eac174f2Safresh1is($LIB->_str($LIB->_from_bin($LIB->_as_bin($LIB->_new("128")))), 128,
650*eac174f2Safresh1   qq|$LIB->_str($LIB->_from_bin($LIB->_as_bin(|
651*eac174f2Safresh1   . qq|$LIB->_new("128")))) = 128|);
652*eac174f2Safresh1is($LIB->_str($LIB->_from_oct($LIB->_as_oct($LIB->_new("128")))), 128,
653*eac174f2Safresh1   qq|$LIB->_str($LIB->_from_oct($LIB->_as_oct(|
654*eac174f2Safresh1   . qq|$LIB->_new("128")))) = 128|);
655*eac174f2Safresh1
656*eac174f2Safresh1is($LIB->_str($LIB->_from_oct($LIB->_as_oct($LIB->_new("123456")))),
657*eac174f2Safresh1   123456,
658*eac174f2Safresh1   qq|$LIB->_str($LIB->_from_oct($LIB->_as_oct|
659*eac174f2Safresh1   . qq|($LIB->_new("123456")))) = 123456|);
660*eac174f2Safresh1is($LIB->_str($LIB->_from_oct($LIB->_as_oct($LIB->_new("123456789")))),
661*eac174f2Safresh1   "123456789",
662*eac174f2Safresh1   qq|$LIB->_str($LIB->_from_oct($LIB->_as_oct(|
663*eac174f2Safresh1   . qq|$LIB->_new("123456789")))) = "123456789"|);
664*eac174f2Safresh1is($LIB->_str($LIB->_from_oct($LIB->_as_oct($LIB->_new("1234567890123")))),
665*eac174f2Safresh1   "1234567890123",
666*eac174f2Safresh1   qq|$LIB->_str($LIB->_from_oct($LIB->_as_oct(|
667*eac174f2Safresh1   . qq|$LIB->_new("1234567890123")))) = "1234567890123"|);
668*eac174f2Safresh1
669*eac174f2Safresh1my $long = "123456789012345678901234567890";
670*eac174f2Safresh1is($LIB->_str($LIB->_from_hex($LIB->_as_hex($LIB->_new($long)))), $long,
671*eac174f2Safresh1   qq|$LIB->_str($LIB->_from_hex($LIB->_as_hex(|
672*eac174f2Safresh1   . qq|$LIB->_new("$long")))) = "$long"|);
673*eac174f2Safresh1is($LIB->_str($LIB->_from_bin($LIB->_as_bin($LIB->_new($long)))), $long,
674*eac174f2Safresh1   qq|$LIB->_str($LIB->_from_bin($LIB->_as_bin(|
675*eac174f2Safresh1   . qq|$LIB->_new("$long")))) = "$long"|);
676*eac174f2Safresh1is($LIB->_str($LIB->_from_oct($LIB->_as_oct($LIB->_new($long)))), $long,
677*eac174f2Safresh1   qq|$LIB->_str($LIB->_from_oct($LIB->_as_oct(|
678*eac174f2Safresh1   . qq|$LIB->_new("$long")))) = "$long"|);
679*eac174f2Safresh1
680*eac174f2Safresh1is($LIB->_str($LIB->_from_hex($LIB->_as_hex($LIB->_new("0")))), 0,
681*eac174f2Safresh1   qq|$LIB->_str($LIB->_from_hex($LIB->_as_hex(|
682*eac174f2Safresh1   . qq|$LIB->_new("0")))) = 0|);
683*eac174f2Safresh1is($LIB->_str($LIB->_from_bin($LIB->_as_bin($LIB->_new("0")))), 0,
684*eac174f2Safresh1   qq|$LIB->_str($LIB->_from_bin($LIB->_as_bin(|
685*eac174f2Safresh1   . qq|$LIB->_new("0")))) = 0|);
686*eac174f2Safresh1is($LIB->_str($LIB->_from_oct($LIB->_as_oct($LIB->_new("0")))), 0,
687*eac174f2Safresh1   qq|$LIB->_str($LIB->_from_oct($LIB->_as_oct(|
688*eac174f2Safresh1   . qq|$LIB->_new("0")))) = 0|);
689*eac174f2Safresh1
690*eac174f2Safresh1is($LIB->_as_hex($LIB->_new("0")), "0x0",
691*eac174f2Safresh1   qq|$LIB->_as_hex($LIB->_new("0")) = "0x0"|);
692*eac174f2Safresh1is($LIB->_as_bin($LIB->_new("0")), "0b0",
693*eac174f2Safresh1   qq|$LIB->_as_bin($LIB->_new("0")) = "0b0"|);
694*eac174f2Safresh1is($LIB->_as_oct($LIB->_new("0")), "00",
695*eac174f2Safresh1   qq|$LIB->_as_oct($LIB->_new("0")) = "00"|);
696*eac174f2Safresh1
697*eac174f2Safresh1is($LIB->_as_hex($LIB->_new("12")), "0xc",
698*eac174f2Safresh1   qq|$LIB->_as_hex($LIB->_new("12")) = "0xc"|);
699*eac174f2Safresh1is($LIB->_as_bin($LIB->_new("12")), "0b1100",
700*eac174f2Safresh1   qq|$LIB->_as_bin($LIB->_new("12")) = "0b1100"|);
701*eac174f2Safresh1is($LIB->_as_oct($LIB->_new("64")), "0100",
702*eac174f2Safresh1   qq|$LIB->_as_oct($LIB->_new("64")) = "0100"|);
703b8851fccSafresh1
704b8851fccSafresh1# _1ex
705*eac174f2Safresh1
706*eac174f2Safresh1is($LIB->_str($LIB->_1ex(0)), "1",
707*eac174f2Safresh1   qq|$LIB->_str($LIB->_1ex(0)) = "1"|);
708*eac174f2Safresh1is($LIB->_str($LIB->_1ex(1)), "10",
709*eac174f2Safresh1   qq|$LIB->_str($LIB->_1ex(1)) = "10"|);
710*eac174f2Safresh1is($LIB->_str($LIB->_1ex(2)), "100",
711*eac174f2Safresh1   qq|$LIB->_str($LIB->_1ex(2)) = "100"|);
712*eac174f2Safresh1is($LIB->_str($LIB->_1ex(12)), "1000000000000",
713*eac174f2Safresh1   qq|$LIB->_str($LIB->_1ex(12)) = "1000000000000"|);
714*eac174f2Safresh1is($LIB->_str($LIB->_1ex(16)), "10000000000000000",
715*eac174f2Safresh1   qq|$LIB->_str($LIB->_1ex(16)) = "10000000000000000"|);
716b8851fccSafresh1
717b8851fccSafresh1# _check
718*eac174f2Safresh1
719*eac174f2Safresh1$x = $LIB->_new("123456789");
720*eac174f2Safresh1is($LIB->_check($x), 0,
721*eac174f2Safresh1   qq|$LIB->_check(\$x) = 0|);
722*eac174f2Safresh1is($LIB->_check(123), "123 is not a reference",
723*eac174f2Safresh1   qq|$LIB->_check(123) = "123 is not a reference"|);
724b8851fccSafresh1
725b8851fccSafresh1###############################################################################
726b8851fccSafresh1# __strip_zeros
727b8851fccSafresh1
728b8851fccSafresh1{
729b8851fccSafresh1    no strict 'refs';
730*eac174f2Safresh1
731b8851fccSafresh1    # correct empty arrays
732*eac174f2Safresh1    $x = &{$LIB."::__strip_zeros"}([]);
733*eac174f2Safresh1    is(@$x, 1, q|@$x = 1|);
734*eac174f2Safresh1    is($x->[0], 0, q|$x->[0] = 0|);
735*eac174f2Safresh1
736b8851fccSafresh1    # don't strip single elements
737*eac174f2Safresh1    $x = &{$LIB."::__strip_zeros"}([0]);
738*eac174f2Safresh1    is(@$x, 1, q|@$x = 1|);
739*eac174f2Safresh1    is($x->[0], 0, q|$x->[0] = 0|);
740*eac174f2Safresh1    $x = &{$LIB."::__strip_zeros"}([1]);
741*eac174f2Safresh1    is(@$x, 1, q|@$x = 1|);
742*eac174f2Safresh1    is($x->[0], 1, q|$x->[0] = 1|);
743*eac174f2Safresh1
744b8851fccSafresh1    # don't strip non-zero elements
745*eac174f2Safresh1    $x = &{$LIB."::__strip_zeros"}([0, 1]);
746*eac174f2Safresh1    is(@$x, 2, q|@$x = 2|);
747*eac174f2Safresh1    is($x->[0], 0, q|$x->[0] = 0|);
748*eac174f2Safresh1    is($x->[1], 1, q|$x->[1] = 1|);
749*eac174f2Safresh1    $x = &{$LIB."::__strip_zeros"}([0, 1, 2]);
750*eac174f2Safresh1    is(@$x, 3, q|@$x = 3|);
751*eac174f2Safresh1    is($x->[0], 0, q|$x->[0] = 0|);
752*eac174f2Safresh1    is($x->[1], 1, q|$x->[1] = 1|);
753*eac174f2Safresh1    is($x->[2], 2, q|$x->[2] = 2|);
754b8851fccSafresh1
755b8851fccSafresh1    # but strip leading zeros
756*eac174f2Safresh1    $x = &{$LIB."::__strip_zeros"}([0, 1, 2, 0]);
757*eac174f2Safresh1    is(@$x, 3, q|@$x = 3|);
758*eac174f2Safresh1    is($x->[0], 0, q|$x->[0] = 0|);
759*eac174f2Safresh1    is($x->[1], 1, q|$x->[1] = 1|);
760*eac174f2Safresh1    is($x->[2], 2, q|$x->[2] = 2|);
761b8851fccSafresh1
762*eac174f2Safresh1    $x = &{$LIB."::__strip_zeros"}([0, 1, 2, 0, 0]);
763*eac174f2Safresh1    is(@$x, 3, q|@$x = 3|);
764*eac174f2Safresh1    is($x->[0], 0, q|$x->[0] = 0|);
765*eac174f2Safresh1    is($x->[1], 1, q|$x->[1] = 1|);
766*eac174f2Safresh1    is($x->[2], 2, q|$x->[2] = 2|);
767b8851fccSafresh1
768*eac174f2Safresh1    $x = &{$LIB."::__strip_zeros"}([0, 1, 2, 0, 0, 0]);
769*eac174f2Safresh1    is(@$x, 3, q|@$x = 3|);
770*eac174f2Safresh1    is($x->[0], 0, q|$x->[0] = 0|);
771*eac174f2Safresh1    is($x->[1], 1, q|$x->[1] = 1|);
772*eac174f2Safresh1    is($x->[2], 2, q|$x->[2] = 2|);
773b8851fccSafresh1
774b8851fccSafresh1    # collapse multiple zeros
775*eac174f2Safresh1    $x = &{$LIB."::__strip_zeros"}([0, 0, 0, 0]);
776*eac174f2Safresh1    is(@$x, 1, q|@$x = 1|);
777*eac174f2Safresh1    is($x->[0], 0, q|$x->[0] = 0|);
778b8851fccSafresh1}
779