Commit | Line | Data |
990fb837 |
1 | #!/usr/bin/perl -w |
2 | |
3 | # test that config ( trap_nan => 1, trap_inf => 1) really works/dies |
4 | |
5 | use strict; |
233f7bc0 |
6 | use Test::More; |
990fb837 |
7 | |
8 | BEGIN |
9 | { |
10 | $| = 1; |
11 | chdir 't' if -d 't'; |
12 | unshift @INC, '../lib'; # for running manually |
233f7bc0 |
13 | plan tests => 43; |
990fb837 |
14 | } |
15 | |
16 | use Math::BigInt; |
17 | use Math::BigFloat; |
18 | |
19 | my $mbi = 'Math::BigInt'; my $mbf = 'Math::BigFloat'; |
20 | my ($cfg,$x); |
21 | |
22 | foreach my $class ($mbi, $mbf) |
23 | { |
24 | # can do and defaults are okay? |
233f7bc0 |
25 | ok ($class->can('config'), 'can config()'); |
26 | is ($class->config()->{trap_nan}, 0, 'trap_nan defaults to 0'); |
27 | is ($class->config()->{trap_inf}, 0, 'trap_inf defaults to 0'); |
990fb837 |
28 | |
29 | # can set? |
233f7bc0 |
30 | $cfg = $class->config( trap_nan => 1 ); |
31 | is ($cfg->{trap_nan},1, 'trap_nan now true'); |
990fb837 |
32 | |
33 | # also test that new() still works normally |
34 | eval ("\$x = \$class->new('42'); \$x->bnan();"); |
233f7bc0 |
35 | like ($@, qr/^Tried to set/, 'died'); |
36 | is ($x,42,'$x after new() never modified'); |
990fb837 |
37 | |
38 | # can reset? |
233f7bc0 |
39 | $cfg = $class->config( trap_nan => 0 ); |
40 | is ($cfg->{trap_nan}, 0, 'trap_nan disabled'); |
990fb837 |
41 | |
42 | # can set? |
233f7bc0 |
43 | $cfg = $class->config( trap_inf => 1 ); |
44 | is ($cfg->{trap_inf}, 1, 'trap_inf enabled'); |
45 | |
990fb837 |
46 | eval ("\$x = \$class->new('4711'); \$x->binf();"); |
233f7bc0 |
47 | like ($@, qr/^Tried to set/, 'died'); |
48 | is ($x,4711,'$x after new() never modified'); |
49 | |
50 | eval ("\$x = \$class->new('inf');"); |
51 | like ($@, qr/^Tried to set/, 'died'); |
52 | is ($x,4711,'$x after new() never modified'); |
53 | |
54 | eval ("\$x = \$class->new('-inf');"); |
55 | like ($@, qr/^Tried to set/, 'died'); |
56 | is ($x,4711,'$x after new() never modified'); |
990fb837 |
57 | |
58 | # +$x/0 => +inf |
59 | eval ("\$x = \$class->new('4711'); \$x->bdiv(0);"); |
233f7bc0 |
60 | like ($@, qr/^Tried to set/, 'died'); |
61 | is ($x,4711,'$x after new() never modified'); |
990fb837 |
62 | |
63 | # -$x/0 => -inf |
64 | eval ("\$x = \$class->new('-0815'); \$x->bdiv(0);"); |
233f7bc0 |
65 | like ($@, qr/^Tried to set/, 'died'); |
66 | is ($x,'-815', '$x after new not modified'); |
990fb837 |
67 | |
68 | $cfg = $class->config( trap_nan => 1 ); |
69 | # 0/0 => NaN |
70 | eval ("\$x = \$class->new('0'); \$x->bdiv(0);"); |
233f7bc0 |
71 | like ($@, qr/^Tried to set/, 'died'); |
72 | is ($x,'0', '$x after new not modified'); |
990fb837 |
73 | } |
74 | |
75 | ############################################################################## |
76 | # BigInt |
77 | |
78 | $x = Math::BigInt->new(2); |
79 | eval ("\$x = \$mbi->new('0.1');"); |
233f7bc0 |
80 | is ($x,2,'never modified since it dies'); |
990fb837 |
81 | eval ("\$x = \$mbi->new('0a.1');"); |
233f7bc0 |
82 | is ($x,2,'never modified since it dies'); |
990fb837 |
83 | |
84 | ############################################################################## |
85 | # BigFloat |
86 | |
87 | $x = Math::BigFloat->new(2); |
88 | eval ("\$x = \$mbf->new('0.1a');"); |
233f7bc0 |
89 | is ($x,2,'never modified since it dies'); |
990fb837 |
90 | |
91 | # all tests done |
92 | |