Upgrade to Math::BigInt 1.48.
[p5sagit/p5-mst-13.2.git] / lib / Math / BigInt / t / bigintc.t
CommitLineData
0716bf9b 1#!/usr/bin/perl -w
2
3use strict;
4use Test;
5
6BEGIN
7 {
8 $| = 1;
027dc388 9 chdir 't' if -d 't';
0716bf9b 10 unshift @INC, '../lib'; # for running manually
394e6ffb 11 plan tests => 63;
0716bf9b 12 }
13
027dc388 14# testing of Math::BigInt::Calc, primarily for interface/api and not for the
0716bf9b 15# math functionality
16
17use Math::BigInt::Calc;
18
574bacfe 19my $C = 'Math::BigInt::Calc'; # pass classname to sub's
20
0716bf9b 21# _new and _str
bd05a461 22my $x = $C->_new(\"123"); my $y = $C->_new(\"321");
23ok (ref($x),'ARRAY'); ok (${$C->_str($x)},123); ok (${$C->_str($y)},321);
0716bf9b 24
25# _add, _sub, _mul, _div
bd05a461 26ok (${$C->_str($C->_add($x,$y))},444);
27ok (${$C->_str($C->_sub($x,$y))},123);
28ok (${$C->_str($C->_mul($x,$y))},39483);
29ok (${$C->_str($C->_div($x,$y))},123);
0716bf9b 30
bd05a461 31ok (${$C->_str($C->_mul($x,$y))},39483);
32ok (${$C->_str($x)},39483);
33ok (${$C->_str($y)},321);
34my $z = $C->_new(\"2");
35ok (${$C->_str($C->_add($x,$z))},39485);
36my ($re,$rr) = $C->_div($x,$y);
0716bf9b 37
bd05a461 38ok (${$C->_str($re)},123); ok (${$C->_str($rr)},2);
0716bf9b 39
40# is_zero, _is_one, _one, _zero
bd05a461 41ok ($C->_is_zero($x),0);
42ok ($C->_is_one($x),0);
0716bf9b 43
bd05a461 44ok ($C->_is_one($C->_one()),1); ok ($C->_is_one($C->_zero()),0);
45ok ($C->_is_zero($C->_zero()),1); ok ($C->_is_zero($C->_one()),0);
574bacfe 46
47# is_odd, is_even
bd05a461 48ok ($C->_is_odd($C->_one()),1); ok ($C->_is_odd($C->_zero()),0);
49ok ($C->_is_even($C->_one()),0); ok ($C->_is_even($C->_zero()),1);
0716bf9b 50
574bacfe 51# _digit
bd05a461 52$x = $C->_new(\"123456789");
53ok ($C->_digit($x,0),9);
54ok ($C->_digit($x,1),8);
55ok ($C->_digit($x,2),7);
56ok ($C->_digit($x,-1),1);
57ok ($C->_digit($x,-2),2);
58ok ($C->_digit($x,-3),3);
574bacfe 59
60# _copy
bd05a461 61$x = $C->_new(\"12356");
62ok (${$C->_str($C->_copy($x))},12356);
574bacfe 63
64# _zeros
bd05a461 65$x = $C->_new(\"1256000000"); ok ($C->_zeros($x),6);
66$x = $C->_new(\"152"); ok ($C->_zeros($x),0);
67$x = $C->_new(\"123000"); ok ($C->_zeros($x),3);
574bacfe 68
69# _lsft, _rsft
bd05a461 70$x = $C->_new(\"10"); $y = $C->_new(\"3");
71ok (${$C->_str($C->_lsft($x,$y,10))},10000);
72$x = $C->_new(\"20"); $y = $C->_new(\"3");
73ok (${$C->_str($C->_lsft($x,$y,10))},20000);
74$x = $C->_new(\"128"); $y = $C->_new(\"4");
75if (!defined $C->_lsft($x,$y,2))
574bacfe 76 {
77 ok (1,1)
78 }
79else
80 {
81 ok ('_lsft','undef');
82 }
bd05a461 83$x = $C->_new(\"1000"); $y = $C->_new(\"3");
84ok (${$C->_str($C->_rsft($x,$y,10))},1);
85$x = $C->_new(\"20000"); $y = $C->_new(\"3");
86ok (${$C->_str($C->_rsft($x,$y,10))},20);
87$x = $C->_new(\"256"); $y = $C->_new(\"4");
88if (!defined $C->_rsft($x,$y,2))
574bacfe 89 {
90 ok (1,1)
91 }
92else
93 {
94 ok ('_rsft','undef');
95 }
0716bf9b 96
574bacfe 97# _acmp
bd05a461 98$x = $C->_new(\"123456789");
99$y = $C->_new(\"987654321");
100ok ($C->_acmp($x,$y),-1);
101ok ($C->_acmp($y,$x),1);
102ok ($C->_acmp($x,$x),0);
103ok ($C->_acmp($y,$y),0);
574bacfe 104
105# _div
bd05a461 106$x = $C->_new(\"3333"); $y = $C->_new(\"1111");
107ok (${$C->_str(scalar $C->_div($x,$y))},3);
108$x = $C->_new(\"33333"); $y = $C->_new(\"1111"); ($x,$y) = $C->_div($x,$y);
109ok (${$C->_str($x)},30); ok (${$C->_str($y)},3);
110$x = $C->_new(\"123"); $y = $C->_new(\"1111");
111($x,$y) = $C->_div($x,$y); ok (${$C->_str($x)},0); ok (${$C->_str($y)},123);
574bacfe 112
113# _num
bd05a461 114$x = $C->_new(\"12345"); $x = $C->_num($x); ok (ref($x)||'',''); ok ($x,12345);
574bacfe 115
e745a66c 116# _inc
117$x = $C->_new(\"1000"); $C->_inc($x); ok (${$C->_str($x)},'1001');
118$C->_dec($x); ok (${$C->_str($x)},'1000');
119
120my $BL = Math::BigInt::Calc::_base_len();
121$x = '1' . '0' x $BL;
122$z = '1' . '0' x ($BL-1); $z .= '1';
123$x = $C->_new(\$x); $C->_inc($x); ok (${$C->_str($x)},$z);
124
125$x = '1' . '0' x $BL; $z = '9' x $BL;
126$x = $C->_new(\$x); $C->_dec($x); ok (${$C->_str($x)},$z);
127
574bacfe 128# should not happen:
bd05a461 129# $x = $C->_new(\"-2"); $y = $C->_new(\"4"); ok ($C->_acmp($x,$y),-1);
574bacfe 130
394e6ffb 131# _mod
132$x = $C->_new(\"1000"); $y = $C->_new(\"3");
133ok (${$C->_str(scalar $C->_mod($x,$y))},1);
134$x = $C->_new(\"1000"); $y = $C->_new(\"2");
135ok (${$C->_str(scalar $C->_mod($x,$y))},0);
136
137# _and, _or, _xor
138$x = $C->_new(\"5"); $y = $C->_new(\"2");
139ok (${$C->_str(scalar $C->_xor($x,$y))},7);
140$x = $C->_new(\"5"); $y = $C->_new(\"2");
141ok (${$C->_str(scalar $C->_or($x,$y))},7);
142$x = $C->_new(\"5"); $y = $C->_new(\"3");
143ok (${$C->_str(scalar $C->_and($x,$y))},1);
144
145# _from_hex, _from_bin
146ok (${$C->_str(scalar $C->_from_hex(\"0xFf"))},255);
147ok (${$C->_str(scalar $C->_from_bin(\"0b10101011"))},160+11);
148
574bacfe 149# _check
bd05a461 150$x = $C->_new(\"123456789");
151ok ($C->_check($x),0);
152ok ($C->_check(123),'123 is not a reference');
0716bf9b 153
154# done
155
1561;
574bacfe 157