bignum 0.22 take 4 (hex()/oct() overloading)
[p5sagit/p5-mst-13.2.git] / lib / bignum / t / bigint.t
CommitLineData
126f3c5f 1#!/usr/bin/perl -w
2
3###############################################################################
4
5use Test;
6use strict;
7
8BEGIN
9 {
10 $| = 1;
11 chdir 't' if -d 't';
12 unshift @INC, '../lib';
d1a15766 13 plan tests => 51;
126f3c5f 14 }
15
d1a15766 16use bigint qw/hex oct/;
126f3c5f 17
18###############################################################################
19# _constant tests
20
21foreach (qw/
22 123:123
23 123.4:123
24 1.4:1
25 0.1:0
26 -0.1:0
27 -1.1:-1
28 -123.4:-123
29 -123:-123
30 123e2:123e2
31 123e-1:12
32 123e-4:0
33 123e-3:0
34 123.345e-1:12
35 123.456e+2:12345
36 1234.567e+3:1234567
37 1234.567e+4:1234567E1
38 1234.567e+6:1234567E3
39 /)
40 {
41 my ($x,$y) = split /:/;
42 print "# Try $x\n";
95a2d02c 43 ok (bigint::_float_constant("$x"),"$y");
44 }
45
46foreach (qw/
47 0100:64
48 0200:128
49 0x100:256
50 0b1001:9
51 /)
52 {
53 my ($x,$y) = split /:/;
54 print "# Try $x\n";
55 ok (bigint::_binary_constant("$x"),"$y");
126f3c5f 56 }
57
58###############################################################################
59# general tests
60
61my $x = 5; ok (ref($x) =~ /^Math::BigInt/); # :constant
62
63# todo: ok (2 + 2.5,4.5); # should still work
64# todo: $x = 2 + 3.5; ok (ref($x),'Math::BigFloat');
65
66$x = 2 ** 255; ok (ref($x) =~ /^Math::BigInt/);
67
68ok (12->bfac(),479001600);
69ok (9/4,2);
70
71ok (4.5+4.5,8); # truncate
72ok (ref(4.5+4.5) =~ /^Math::BigInt/);
73
74
75###############################################################################
76# accurarcy and precision
77
990fb837 78ok_undef (bigint->accuracy());
79ok (bigint->accuracy(12),12);
80ok (bigint->accuracy(),12);
126f3c5f 81
990fb837 82ok_undef (bigint->precision());
83ok (bigint->precision(12),12);
84ok (bigint->precision(),12);
85
86ok (bigint->round_mode(),'even');
87ok (bigint->round_mode('odd'),'odd');
88ok (bigint->round_mode(),'odd');
126f3c5f 89
90###############################################################################
d1a15766 91# hex() and oct()
92
93my $c = 'Math::BigInt';
94
95ok (ref(hex(1)), $c);
96ok (ref(hex(0x1)), $c);
97ok (ref(hex("af")), $c);
98ok (hex("af"), Math::BigInt->new(0xaf));
99ok (ref(hex("0x1")), $c);
100
101ok (ref(oct("0x1")), $c);
102ok (ref(oct("01")), $c);
103ok (ref(oct("0b01")), $c);
104ok (ref(oct("1")), $c);
105ok (ref(oct(" 1")), $c);
106ok (ref(oct(" 0x1")), $c);
107
108ok (ref(oct(0x1)), $c);
109ok (ref(oct(01)), $c);
110ok (ref(oct(0b01)), $c);
111ok (ref(oct(1)), $c);
112
113###############################################################################
126f3c5f 114###############################################################################
115# Perl 5.005 does not like ok ($x,undef)
116
117sub ok_undef
118 {
119 my $x = shift;
120
121 ok (1,1) and return if !defined $x;
122 ok ($x,'undef');
123 }