summaryrefslogtreecommitdiff
path: root/cpan/Math-BigRat/t/trap.t
blob: bab85d4e5dd797362bab811dd7cc7e11ed695303 (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
103
104
105
#!perl

# test that config( trap_nan => 1, trap_inf => 1) really works/dies

use strict;
use warnings;

use Test::More tests => 29;

use Math::BigRat;

my $mbr = 'Math::BigRat';
my $x;

foreach my $class ($mbr) {

    # can do?
    can_ok($class, 'config');

    ###########################################################################
    # Default values.
    ###########################################################################

    # defaults are okay?
    is($class->config("trap_nan"), 0, qq|$class->config("trap_nan")|);
    is($class->config("trap_inf"), 0, qq|$class->config("trap_inf")|);

    ###########################################################################
    # Trap NaN.
    ###########################################################################

    # can set?
    $class->config( trap_nan => 1 );
    is($class->config("trap_nan"), 1, q|$class->config("trap_nan")|);

    # can reset?
    $class->config( trap_nan => 0 );
    is($class->config("trap_nan"), 0, qq|$class->config("trap_nan")|);

    # can set via hash ref?
    $class->config( { trap_nan => 1 } );
    is($class->config("trap_nan"), 1, q|$class->config("trap_nan")|);

    # also test that new() still works normally
    eval { $x = $class->new("42"); $x->bnan(); };
    like($@, qr/^Tried to set/, qq|\$x = $class->new("42"); \$x->bnan();|);
    # after new() never modified
    is($x, 42, qq|\$x = $class->new("42"); \$x->bnan();|);

    # 0/0 => NaN
    eval { $x = $class->new("0"); $x->bdiv(0); };
    like($@, qr/^Tried to set/, qq|\$x = $class->new("0"); \$x->bdiv(0);|);
    # after new() never modified
    is($x, 0, qq|\$x = $class->new("0"); \$x->bdiv(0);|);

    ###########################################################################
    # Trap inf.
    ###########################################################################

    # can set?
    $class->config( trap_inf => 1 );
    is($class->config("trap_inf"), 1, qq|$class->config("trap_inf")|);

    eval { $x = $class->new("4711"); $x->binf(); };
    like($@, qr/^Tried to set/, qq|\$x = $class->new("4711"); \$x->binf();|);
    # after new() never modified
    is($x, 4711, qq|\$x = $class->new("4711"); \$x->binf();|);

    # +$x/0 => +inf
    eval { $x = $class->new("4711"); $x->bdiv(0); };
    like($@, qr/^Tried to set/, qq|\$x =\$class->new("4711"); \$x->bdiv(0);|);
    # after new() never modified
    is($x, 4711, qq|\$x =\$class->new("4711"); \$x->bdiv(0);|);

    # -$x/0 => -inf
    eval { $x = $class->new("-0815"); $x->bdiv(0); };
    like($@, qr/^Tried to set/, qq|\$x = $class->new("-0815"); \$x->bdiv(0);|);
    # after new() never modified
    is($x, -815, qq|\$x = $class->new("-0815"); \$x->bdiv(0);|);
}

##############################################################################
# BigRat

Math::BigRat->config(trap_nan => 1,
                     trap_inf => 1);

for my $trap (qw/ 0.1a +inf inf -inf /) {
    my $x = Math::BigRat->new('7/4');

    note("");           # this is just for some space in the output

    # In each of the cases below, $x is not modified, because the code dies.

    eval { $x = $mbr->new("$trap"); };
    is($x, "7/4", qq|\$x = $mbr->new("$trap");|);

    eval { $x = $mbr->new("$trap"); };
    is($x, "7/4", qq|\$x = $mbr->new("$trap");|);

    eval { $x = $mbr->new("$trap/7"); };
    is($x, "7/4", qq|\$x = $mbr->new("$trap/7");|);
}

# all tests done