This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Update bignum, Math::BigInt, Math::BigInt::FastCalc, and Math::BigRat
[perl5.git] / cpan / Math-BigRat / t / trap.t
CommitLineData
6853e8af 1# -*- mode: perl; -*-
990fb837 2
6320cdc0 3# test that config( trap_nan => 1, trap_inf => 1) really works/dies
990fb837
RGS
4
5use strict;
11c955be
SH
6use warnings;
7
c32198f6 8use Test::More tests => 29;
990fb837
RGS
9
10use Math::BigRat;
11
3cc1ad36
SH
12my $mbr = 'Math::BigRat';
13my $x;
6320cdc0 14
3cc1ad36 15foreach my $class ($mbr) {
6320cdc0 16
3cc1ad36 17 # can do?
6320cdc0 18 can_ok($class, 'config');
3cc1ad36
SH
19
20 ###########################################################################
21 # Default values.
22 ###########################################################################
23
24 # defaults are okay?
25 is($class->config("trap_nan"), 0, qq|$class->config("trap_nan")|);
26 is($class->config("trap_inf"), 0, qq|$class->config("trap_inf")|);
27
28 ###########################################################################
29 # Trap NaN.
30 ###########################################################################
6320cdc0
SH
31
32 # can set?
3cc1ad36
SH
33 $class->config( trap_nan => 1 );
34 is($class->config("trap_nan"), 1, q|$class->config("trap_nan")|);
35
36 # can reset?
37 $class->config( trap_nan => 0 );
38 is($class->config("trap_nan"), 0, qq|$class->config("trap_nan")|);
6320cdc0
SH
39
40 # can set via hash ref?
3cc1ad36
SH
41 $class->config( { trap_nan => 1 } );
42 is($class->config("trap_nan"), 1, q|$class->config("trap_nan")|);
6320cdc0
SH
43
44 # also test that new() still works normally
3cc1ad36
SH
45 eval { $x = $class->new("42"); $x->bnan(); };
46 like($@, qr/^Tried to set/, qq|\$x = $class->new("42"); \$x->bnan();|);
6320cdc0 47 # after new() never modified
3cc1ad36 48 is($x, 42, qq|\$x = $class->new("42"); \$x->bnan();|);
6320cdc0 49
3cc1ad36
SH
50 # 0/0 => NaN
51 eval { $x = $class->new("0"); $x->bdiv(0); };
52 like($@, qr/^Tried to set/, qq|\$x = $class->new("0"); \$x->bdiv(0);|);
53 # after new() never modified
54 is($x, 0, qq|\$x = $class->new("0"); \$x->bdiv(0);|);
55
56 ###########################################################################
57 # Trap inf.
58 ###########################################################################
6320cdc0
SH
59
60 # can set?
3cc1ad36
SH
61 $class->config( trap_inf => 1 );
62 is($class->config("trap_inf"), 1, qq|$class->config("trap_inf")|);
63
64 eval { $x = $class->new("4711"); $x->binf(); };
65 like($@, qr/^Tried to set/, qq|\$x = $class->new("4711"); \$x->binf();|);
6320cdc0 66 # after new() never modified
3cc1ad36 67 is($x, 4711, qq|\$x = $class->new("4711"); \$x->binf();|);
6320cdc0
SH
68
69 # +$x/0 => +inf
3cc1ad36
SH
70 eval { $x = $class->new("4711"); $x->bdiv(0); };
71 like($@, qr/^Tried to set/, qq|\$x =\$class->new("4711"); \$x->bdiv(0);|);
6320cdc0 72 # after new() never modified
3cc1ad36 73 is($x, 4711, qq|\$x =\$class->new("4711"); \$x->bdiv(0);|);
6320cdc0
SH
74
75 # -$x/0 => -inf
3cc1ad36
SH
76 eval { $x = $class->new("-0815"); $x->bdiv(0); };
77 like($@, qr/^Tried to set/, qq|\$x = $class->new("-0815"); \$x->bdiv(0);|);
6320cdc0 78 # after new() never modified
3cc1ad36 79 is($x, -815, qq|\$x = $class->new("-0815"); \$x->bdiv(0);|);
6320cdc0 80}
990fb837
RGS
81
82##############################################################################
83# BigRat
84
6320cdc0
SH
85Math::BigRat->config(trap_nan => 1,
86 trap_inf => 1);
990fb837 87
6320cdc0
SH
88for my $trap (qw/ 0.1a +inf inf -inf /) {
89 my $x = Math::BigRat->new('7/4');
990fb837 90
6320cdc0
SH
91 note(""); # this is just for some space in the output
92
93 # In each of the cases below, $x is not modified, because the code dies.
94
3cc1ad36
SH
95 eval { $x = $mbr->new("$trap"); };
96 is($x, "7/4", qq|\$x = $mbr->new("$trap");|);
6320cdc0 97
3cc1ad36
SH
98 eval { $x = $mbr->new("$trap"); };
99 is($x, "7/4", qq|\$x = $mbr->new("$trap");|);
6320cdc0 100
3cc1ad36
SH
101 eval { $x = $mbr->new("$trap/7"); };
102 is($x, "7/4", qq|\$x = $mbr->new("$trap/7");|);
6320cdc0 103}
990fb837
RGS
104
105# all tests done