xref: /openbsd-src/gnu/usr.bin/perl/cpan/Math-BigInt/t/trap.t (revision 3d61058aa5c692477b6d18acfbbdb653a9930ff9)
1# -*- mode: perl; -*-
2
3# test that config ( trap_nan => 1, trap_inf => 1) really works/dies
4
5use strict;
6use warnings;
7
8use Test::More tests => 90;
9
10my $mbi = 'Math::BigInt';
11my $mbf = 'Math::BigFloat';
12my $mbr = 'Math::BigRat';
13
14use_ok($mbi);
15use_ok($mbf);
16use_ok($mbr);
17
18my $x;
19
20foreach my $class ($mbi, $mbf, $mbr) {
21
22    # can do?
23    can_ok($class, 'config');
24
25    ###########################################################################
26    # Default values.
27    ###########################################################################
28
29    # defaults are okay?
30    is($class->config("trap_nan"), 0, 'trap_nan defaults to 0');
31    is($class->config("trap_inf"), 0, 'trap_inf defaults to 0');
32
33    ###########################################################################
34    # Trap NaN.
35    ###########################################################################
36
37    # can set?
38    $class->config( trap_nan => 1 );
39    is($class->config("trap_nan"), 1, qq|$class->config( trap_nan => 1 );|);
40
41    # can reset?
42    $class->config( trap_nan => 0 );
43    is($class->config("trap_nan"), 0, qq|$class->config( trap_nan => 0 );|);
44
45    # can set via hash ref?
46    $class->config( { trap_nan => 1 } );
47    is($class->config("trap_nan"), 1, qq|$class->config( { trap_nan => 1 } );|);
48
49    # 0/0 => NaN
50    $x = $class->new("0");
51    eval { $x->bdiv(0); };
52    like($@, qr/^Tried to /, qq|\$x = $class->new("0"); \$x->bdiv(0);|);
53
54    # new() didn't modify $x
55    is($x, 0, qq|\$x = $class->new("0"); \$x->bdiv(0);|);
56
57    # also test that new() still works normally
58    eval { $x = $class->new('42'); $x->bnan(); };
59    like($@, qr/^Tried to /, 'died');
60    is($x, 42, '$x after new() never modified');
61
62    # can reset?
63    $class->config( trap_nan => 0 );
64    is($class->config("trap_nan"), 0, qq|$class->config( trap_nan => 0 );|);
65
66    ###########################################################################
67    # Trap inf.
68    ###########################################################################
69
70    # can set?
71    $class->config( trap_inf => 1 );
72    is($class->config("trap_inf"), 1, 'trap_inf enabled');
73
74    eval { $x = $class->new('4711'); $x->binf(); };
75    like($@, qr/^Tried to /, 'died');
76    is($x, 4711, '$x after new() never modified');
77
78    eval { $x = $class->new('inf'); };
79    like($@, qr/^Tried to /, 'died');
80    is($x, 4711, '$x after new() never modified');
81
82    eval { $x = $class->new('-inf'); };
83    like($@, qr/^Tried to /, 'died');
84    is($x, 4711, '$x after new() never modified');
85
86    # +$x/0 => +inf
87    eval { $x = $class->new('4711'); $x->bdiv(0); };
88    like($@, qr/^Tried to /, 'died');
89    is($x, 4711, '$x after new() never modified');
90
91    # -$x/0 => -inf
92    eval { $x = $class->new('-0815'); $x->bdiv(0); };
93    like($@, qr/^Tried to /, 'died');
94    is($x, '-815', '$x after new not modified');
95
96    $class->config( trap_nan => 1 );
97    # 0/0 => NaN
98    eval { $x = $class->new('0'); $x->bdiv(0); };
99    like($@, qr/^Tried to /, 'died');
100    is($x, '0', '$x after new not modified');
101}
102
103##############################################################################
104# Math::BigInt
105
106$x = Math::BigInt->new(2);
107eval { $x = $mbi->new('0.1'); };
108is($x, 2, 'never modified since it dies');
109
110eval { $x = $mbi->new('0a.1'); };
111is($x, 2, 'never modified since it dies');
112
113##############################################################################
114# Math::BigFloat
115
116$x = Math::BigFloat->new(2);
117eval { $x = $mbf->new('0.1a'); };
118is($x, 2, 'never modified since it dies');
119
120##############################################################################
121# BigRat
122
123Math::BigRat->config(trap_nan => 1,
124                     trap_inf => 1);
125
126for my $trap (qw/ 0.1a +inf inf -inf /) {
127    my $x = Math::BigRat->new('7/4');
128
129    note("");           # this is just for some space in the output
130
131    # In each of the cases below, $x is not modified, because the code dies.
132
133    eval { $x = $mbr->new("$trap"); };
134    is($x, "7/4", qq|\$x = $mbr->new("$trap");|);
135
136    eval { $x = $mbr->new("$trap"); };
137    is($x, "7/4", qq|\$x = $mbr->new("$trap");|);
138
139    eval { $x = $mbr->new("$trap/7"); };
140    is($x, "7/4", qq|\$x = $mbr->new("$trap/7");|);
141}
142
143# all tests done
144