Upgrade to Math::BigInt v1.65, Math::BigRat v0.10,
[p5sagit/p5-mst-13.2.git] / lib / Math / BigInt / t / trap.t
1 #!/usr/bin/perl -w
2
3 # test that config ( trap_nan => 1, trap_inf => 1) really works/dies
4
5 use strict;
6 use Test;
7
8 BEGIN
9   {
10   $| = 1;
11   chdir 't' if -d 't';
12   unshift @INC, '../lib'; # for running manually
13   plan tests => 35;
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?
25   ok ($class->can('config'));
26   ok ($class->config()->{trap_nan}, 0);
27   ok ($class->config()->{trap_inf}, 0);
28
29   # can set?
30   $cfg = $class->config( trap_nan => 1 ); ok ($cfg->{trap_nan},1);
31
32   # also test that new() still works normally
33   eval ("\$x = \$class->new('42'); \$x->bnan();");
34   ok ($@ =~/^Tried to set/, 1);
35   ok ($x,42);                           # after new() never modified
36
37   # can reset?
38   $cfg = $class->config( trap_nan => 0 ); ok ($cfg->{trap_nan},0);
39   
40   # can set?
41   $cfg = $class->config( trap_inf => 1 ); ok ($cfg->{trap_inf},1);
42   eval ("\$x = \$class->new('4711'); \$x->binf();");
43   ok ($@ =~/^Tried to set/, 1);
44   ok ($x,4711);                         # after new() never modified
45   
46   # +$x/0 => +inf
47   eval ("\$x = \$class->new('4711'); \$x->bdiv(0);");
48   ok ($@ =~/^Tried to set/, 1);
49   ok ($x,4711);                         # after new() never modified
50   
51   # -$x/0 => -inf
52   eval ("\$x = \$class->new('-0815'); \$x->bdiv(0);");
53   ok ($@ =~/^Tried to set/, 1);
54   ok ($x,-815);                         # after new() never modified
55   
56   $cfg = $class->config( trap_nan => 1 );
57   # 0/0 => NaN
58   eval ("\$x = \$class->new('0'); \$x->bdiv(0);");
59   ok ($@ =~/^Tried to set/, 1);
60   ok ($x,0);                            # after new() never modified
61   }
62
63 ##############################################################################
64 # BigInt
65
66 $x = Math::BigInt->new(2);
67 eval ("\$x = \$mbi->new('0.1');");
68 ok ($x,2);                              # never modified since it dies
69 eval ("\$x = \$mbi->new('0a.1');");
70 ok ($x,2);                              # never modified since it dies
71
72
73 ##############################################################################
74 # BigFloat
75
76 $x = Math::BigFloat->new(2);
77 eval ("\$x = \$mbf->new('0.1a');");
78 ok ($x,2);                              # never modified since it dies
79
80 # all tests done
81