Commit | Line | Data |
f3d61276 |
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 => 51; |
12 | } |
13 | |
14 | # testing of Math::BigInt::BitVect, primarily for interface/api and not for the |
15 | # math functionality |
16 | |
17 | use Math::BigInt::Scalar; |
18 | |
19 | my $C = 'Math::BigInt::Scalar'; # 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),'SCALAR'); ok (${$C->_str($x)},123); ok (${$C->_str($y)},321); |
24 | |
25 | # _add, _sub, _mul, _div |
26 | |
27 | ok (${$C->_str($C->_add($x,$y))},444); |
28 | ok (${$C->_str($C->_sub($x,$y))},123); |
29 | ok (${$C->_str($C->_mul($x,$y))},39483); |
30 | ok (${$C->_str($C->_div($x,$y))},123); |
31 | |
32 | ok (${$C->_str($C->_mul($x,$y))},39483); |
33 | ok (${$C->_str($x)},39483); |
34 | ok (${$C->_str($y)},321); |
35 | my $z = $C->_new(\"2"); |
36 | ok (${$C->_str($C->_add($x,$z))},39485); |
37 | my ($re,$rr) = $C->_div($x,$y); |
38 | |
39 | ok (${$C->_str($re)},123); ok (${$C->_str($rr)},2); |
40 | |
41 | # is_zero, _is_one, _one, _zero |
42 | ok ($C->_is_zero($x),0); |
43 | ok ($C->_is_one($x),0); |
44 | |
45 | ok ($C->_is_one($C->_one()),1); ok ($C->_is_one($C->_zero()),0); |
46 | ok ($C->_is_zero($C->_zero()),1); ok ($C->_is_zero($C->_one()),0); |
47 | |
48 | # is_odd, is_even |
49 | ok ($C->_is_odd($C->_one()),1); ok ($C->_is_odd($C->_zero()),0); |
50 | ok ($C->_is_even($C->_one()),0); ok ($C->_is_even($C->_zero()),1); |
51 | |
52 | # _digit |
53 | $x = $C->_new(\"123456789"); |
54 | ok ($C->_digit($x,0),9); |
55 | ok ($C->_digit($x,1),8); |
56 | ok ($C->_digit($x,2),7); |
57 | ok ($C->_digit($x,-1),1); |
58 | ok ($C->_digit($x,-2),2); |
59 | ok ($C->_digit($x,-3),3); |
60 | |
61 | # _copy |
62 | $x = $C->_new(\"12356"); |
63 | ok (${$C->_str($C->_copy($x))},12356); |
64 | |
65 | # _acmp |
66 | $x = $C->_new(\"123456789"); |
67 | $y = $C->_new(\"987654321"); |
68 | ok ($C->_acmp($x,$y),-1); |
69 | ok ($C->_acmp($y,$x),1); |
70 | ok ($C->_acmp($x,$x),0); |
71 | ok ($C->_acmp($y,$y),0); |
72 | |
73 | # _div |
74 | $x = $C->_new(\"3333"); $y = $C->_new(\"1111"); |
75 | ok (${$C->_str( scalar $C->_div($x,$y))},3); |
76 | $x = $C->_new(\"33333"); $y = $C->_new(\"1111"); ($x,$y) = $C->_div($x,$y); |
77 | ok (${$C->_str($x)},30); ok (${$C->_str($y)},3); |
78 | $x = $C->_new(\"123"); $y = $C->_new(\"1111"); |
79 | ($x,$y) = $C->_div($x,$y); ok (${$C->_str($x)},0); ok (${$C->_str($y)},123); |
80 | |
81 | # _num |
82 | $x = $C->_new(\"12345"); $x = $C->_num($x); ok (ref($x)||'',''); ok ($x,12345); |
83 | |
84 | # _len |
85 | $x = $C->_new(\"12345"); $x = $C->_len($x); ok (ref($x)||'',''); ok ($x,5); |
86 | |
87 | # _and, _or, _xor |
88 | $x = $C->_new(\"3"); $y = $C->_new(\"4"); ok (${$C->_str( $C->_or($x,$y))},7); |
89 | $x = $C->_new(\"1"); $y = $C->_new(\"4"); ok (${$C->_str( $C->_xor($x,$y))},5); |
90 | $x = $C->_new(\"7"); $y = $C->_new(\"3"); ok (${$C->_str( $C->_and($x,$y))},3); |
91 | |
92 | # _pow |
93 | $x = $C->_new(\"2"); $y = $C->_new(\"4"); ok (${$C->_str( $C->_pow($x,$y))},16); |
94 | $x = $C->_new(\"2"); $y = $C->_new(\"5"); ok (${$C->_str( $C->_pow($x,$y))},32); |
95 | $x = $C->_new(\"3"); $y = $C->_new(\"3"); ok (${$C->_str( $C->_pow($x,$y))},27); |
96 | |
97 | |
98 | # should not happen: |
99 | # $x = $C->_new(\"-2"); $y = $C->_new(\"4"); ok ($C->_acmp($x,$y),-1); |
100 | |
101 | # _check |
102 | $x = $C->_new(\"123456789"); |
103 | ok ($C->_check($x),0); |
104 | ok ($C->_check(123),'123 is not a reference'); |
105 | |
106 | # done |
107 | |
108 | 1; |
109 | |