Commit | Line | Data |
---|---|---|
990fb837 RGS |
1 | #!/usr/bin/perl -w |
2 | ||
3 | # test that config ( trap_nan => 1, trap_inf => 1) really works/dies | |
4 | ||
5 | use strict; | |
c32198f6 | 6 | use Test::More tests => 29; |
990fb837 RGS |
7 | |
8 | use Math::BigRat; | |
9 | ||
10 | my $mbi = 'Math::BigRat'; | |
11 | my ($cfg,$x); | |
12 | ||
13 | foreach my $class ($mbi) | |
14 | { | |
15 | # can do and defaults are okay? | |
c32198f6 FR |
16 | can_ok ($class, 'config'); |
17 | is ($class->config()->{trap_nan}, 0); | |
18 | is ($class->config()->{trap_inf}, 0); | |
990fb837 RGS |
19 | |
20 | # can set? | |
c32198f6 | 21 | $cfg = $class->config( trap_nan => 1 ); is ($cfg->{trap_nan},1); |
990fb837 RGS |
22 | |
23 | # can set via hash ref? | |
c32198f6 | 24 | $cfg = $class->config( { trap_nan => 1 } ); is ($cfg->{trap_nan},1); |
990fb837 RGS |
25 | |
26 | # also test that new() still works normally | |
27 | eval ("\$x = \$class->new('42'); \$x->bnan();"); | |
c32198f6 FR |
28 | like ($@, qr/^Tried to set/); |
29 | is ($x,42); # after new() never modified | |
990fb837 RGS |
30 | |
31 | # can reset? | |
c32198f6 | 32 | $cfg = $class->config( trap_nan => 0 ); is ($cfg->{trap_nan},0); |
990fb837 RGS |
33 | |
34 | # can set? | |
c32198f6 | 35 | $cfg = $class->config( trap_inf => 1 ); is ($cfg->{trap_inf},1); |
990fb837 | 36 | eval ("\$x = \$class->new('4711'); \$x->binf();"); |
c32198f6 FR |
37 | like ($@, qr/^Tried to set/); |
38 | is ($x,4711); # after new() never modified | |
990fb837 RGS |
39 | |
40 | # +$x/0 => +inf | |
41 | eval ("\$x = \$class->new('4711'); \$x->bdiv(0);"); | |
c32198f6 FR |
42 | like ($@, qr/^Tried to set/); |
43 | is ($x,4711); # after new() never modified | |
990fb837 RGS |
44 | |
45 | # -$x/0 => -inf | |
46 | eval ("\$x = \$class->new('-0815'); \$x->bdiv(0);"); | |
c32198f6 FR |
47 | like ($@, qr/^Tried to set/); |
48 | is ($x,-815); # after new() never modified | |
990fb837 RGS |
49 | |
50 | $cfg = $class->config( trap_nan => 1 ); | |
51 | # 0/0 => NaN | |
52 | eval ("\$x = \$class->new('0'); \$x->bdiv(0);"); | |
c32198f6 FR |
53 | like ($@, qr/^Tried to set/); |
54 | is ($x,0); # after new() never modified | |
990fb837 RGS |
55 | } |
56 | ||
57 | ############################################################################## | |
58 | # BigRat | |
59 | ||
60 | $cfg = Math::BigRat->config( trap_nan => 1 ); | |
61 | ||
62 | for my $trap (qw/0.1a +inf inf -inf/) | |
63 | { | |
64 | my $x = Math::BigRat->new('7/4'); | |
65 | ||
66 | eval ("\$x = \$mbi->new('$trap');"); | |
c32198f6 | 67 | is ($x,'7/4'); # never modified since it dies |
990fb837 | 68 | eval ("\$x = \$mbi->new('$trap');"); |
c32198f6 | 69 | is ($x,'7/4'); # never modified since it dies |
990fb837 | 70 | eval ("\$x = \$mbi->new('$trap/7');"); |
c32198f6 | 71 | is ($x,'7/4'); # never modified since it dies |
990fb837 RGS |
72 | } |
73 | ||
74 | # all tests done | |
75 |