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