xref: /openbsd/gnu/usr.bin/perl/cpan/bignum/t/bignum.t (revision 274d7c50)
1#!/usr/bin/perl -w
2
3###############################################################################
4
5use strict;
6use Test::More tests => 35;
7
8use bignum qw/oct hex/;
9
10###############################################################################
11# general tests
12
13my $x = 5;
14like(ref($x), qr/^Math::BigInt/, '$x = 5 makes $x a Math::BigInt'); # :constant
15
16is(2 + 2.5, 4.5, '2 + 2.5 = 4.5');
17$x = 2 + 3.5;
18is(ref($x), 'Math::BigFloat', '$x = 2 + 3.5 makes $x a Math::BigFloat');
19
20is(2 * 2.1, 4.2, '2 * 2.1 = 4.2');
21$x = 2 + 2.1;
22is(ref($x), 'Math::BigFloat', '$x = 2 + 2.1 makes $x a Math::BigFloat');
23
24$x = 2 ** 255;
25like(ref($x), qr/^Math::BigInt/, '$x = 2 ** 255 makes $x a Math::BigInt');
26
27# see if Math::BigInt constant and upgrading works
28is(Math::BigInt::bsqrt("12"), '3.464101615137754587054892683011744733886',
29   'Math::BigInt::bsqrt("12")');
30is(sqrt(12), '3.464101615137754587054892683011744733886',
31   'sqrt(12)');
32
33is(2/3, "0.6666666666666666666666666666666666666667", '2/3');
34
35#is(2 ** 0.5, 'NaN');   # should be sqrt(2);
36
37is(12->bfac(), 479001600, '12->bfac() = 479001600');
38
39# see if Math::BigFloat constant works
40
41#                     0123456789          0123456789    <- default 40
42#           0123456789          0123456789
43is(1/3, '0.3333333333333333333333333333333333333333', '1/3');
44
45###############################################################################
46# accuracy and precision
47
48is(bignum->accuracy(),        undef,  'get accuracy');
49is(bignum->accuracy(12),      12,     'set accuracy to 12');
50is(bignum->accuracy(),        12,     'get accuracy again');
51
52is(bignum->precision(),       undef,  'get precision');
53is(bignum->precision(12),     12,     'set precision to 12');
54is(bignum->precision(),       12,     'get precision again');
55
56is(bignum->round_mode(),      'even', 'get round mode');
57is(bignum->round_mode('odd'), 'odd',  'set round mode');
58is(bignum->round_mode(),      'odd',  'get round mode again');
59
60###############################################################################
61# hex() and oct()
62
63my $class = 'Math::BigInt';
64
65is(ref(hex(1)),      $class, qq|ref(hex(1)) = $class|);
66is(ref(hex(0x1)),    $class, qq|ref(hex(0x1)) = $class|);
67is(ref(hex("af")),   $class, qq|ref(hex("af")) = $class|);
68is(ref(hex("0x1")),  $class, qq|ref(hex("0x1")) = $class|);
69
70is(hex("af"), Math::BigInt->new(0xaf),
71   qq|hex("af") = Math::BigInt->new(0xaf)|);
72
73is(ref(oct("0x1")),  $class, qq|ref(oct("0x1")) = $class|);
74is(ref(oct("01")),   $class, qq|ref(oct("01")) = $class|);
75is(ref(oct("0b01")), $class, qq|ref(oct("0b01")) = $class|);
76is(ref(oct("1")),    $class, qq|ref(oct("1")) = $class|);
77is(ref(oct(" 1")),   $class, qq|ref(oct(" 1")) = $class|);
78is(ref(oct(" 0x1")), $class, qq|ref(oct(" 0x1")) = $class|);
79
80is(ref(oct(0x1)),    $class, qq|ref(oct(0x1)) = $class|);
81is(ref(oct(01)),     $class, qq|ref(oct(01)) = $class|);
82is(ref(oct(0b01)),   $class, qq|ref(oct(0b01)) = $class|);
83is(ref(oct(1)),      $class, qq|ref(oct(1)) = $class|);
84