summaryrefslogtreecommitdiff
path: root/dist/bignum/t/bigint.t
blob: 431a6a668c345e8dc5ae949ad3406fa00d7ef9b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/perl -w

###############################################################################

use Test::More tests => 51;

use bigint qw/hex oct/;

###############################################################################
# _constant tests

foreach (qw/ 
  123:123
  123.4:123
  1.4:1
  0.1:0
  -0.1:0
  -1.1:-1
  -123.4:-123
  -123:-123
  123e2:123e2
  123e-1:12
  123e-4:0
  123e-3:0
  123.345e-1:12
  123.456e+2:12345
  1234.567e+3:1234567
  1234.567e+4:1234567E1
  1234.567e+6:1234567E3
  /)
  {
  my ($x,$y) = split /:/;
  print "# Try $x\n";
  is (bigint::_float_constant("$x"),"$y");
  }

foreach (qw/ 
  0100:64
  0200:128
  0x100:256
  0b1001:9
  /)
  {
  my ($x,$y) = split /:/;
  print "# Try $x\n";
  is (bigint::_binary_constant("$x"),"$y");
  }

###############################################################################
# general tests

my $x = 5; like (ref($x), qr/^Math::BigInt/);		# :constant

# todo:  is (2 + 2.5,4.5);				# should still work
# todo: $x = 2 + 3.5; is (ref($x),'Math::BigFloat');

$x = 2 ** 255; like (ref($x), qr/^Math::BigInt/);

is (12->bfac(),479001600);
is (9/4,2);

is (4.5+4.5,8);					# truncate
like (ref(4.5+4.5), qr/^Math::BigInt/);


###############################################################################
# accuracy and precision

is (bigint->accuracy(), undef);
is (bigint->accuracy(12),12);
is (bigint->accuracy(),12);

is (bigint->precision(), undef);
is (bigint->precision(12),12);
is (bigint->precision(),12);

is (bigint->round_mode(),'even');
is (bigint->round_mode('odd'),'odd');
is (bigint->round_mode(),'odd');

###############################################################################
# hex() and oct()

my $c = 'Math::BigInt';

is (ref(hex(1)), $c);
is (ref(hex(0x1)), $c);
is (ref(hex("af")), $c);
is (hex("af"), Math::BigInt->new(0xaf));
is (ref(hex("0x1")), $c);

is (ref(oct("0x1")), $c);
is (ref(oct("01")), $c);
is (ref(oct("0b01")), $c);
is (ref(oct("1")), $c);
is (ref(oct(" 1")), $c);
is (ref(oct(" 0x1")), $c);

is (ref(oct(0x1)), $c);
is (ref(oct(01)), $c);
is (ref(oct(0b01)), $c);
is (ref(oct(1)), $c);