summaryrefslogtreecommitdiff
path: root/cpan/Math-BigRat/t/trap.t
blob: ccd9ae8143eb3be778b393ffdda4f7fd4fc9d2df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/perl -w

# test that config ( trap_nan => 1, trap_inf => 1) really works/dies

use strict;
use Test;

BEGIN
  {
  $| = 1;
  chdir 't' if -d 't';
  unshift @INC, '../lib'; # for running manually
  plan tests => 29;
  } 

use Math::BigRat;

my $mbi = 'Math::BigRat';
my ($cfg,$x);

foreach my $class ($mbi)
  {
  # can do and defaults are okay?
  ok ($class->can('config'));
  ok ($class->config()->{trap_nan}, 0);
  ok ($class->config()->{trap_inf}, 0);

  # can set?
  $cfg = $class->config( trap_nan => 1 ); ok ($cfg->{trap_nan},1);
  
  # can set via hash ref?
  $cfg = $class->config( { trap_nan => 1 } ); ok ($cfg->{trap_nan},1);

  # also test that new() still works normally
  eval ("\$x = \$class->new('42'); \$x->bnan();");
  ok ($@ =~/^Tried to set/, 1);
  ok ($x,42); 				# after new() never modified

  # can reset?
  $cfg = $class->config( trap_nan => 0 ); ok ($cfg->{trap_nan},0);
  
  # can set?
  $cfg = $class->config( trap_inf => 1 ); ok ($cfg->{trap_inf},1);
  eval ("\$x = \$class->new('4711'); \$x->binf();");
  ok ($@ =~/^Tried to set/, 1);
  ok ($x,4711);				# after new() never modified
  
  # +$x/0 => +inf
  eval ("\$x = \$class->new('4711'); \$x->bdiv(0);");
  ok ($@ =~/^Tried to set/, 1);
  ok ($x,4711);				# after new() never modified
 
  # -$x/0 => -inf
  eval ("\$x = \$class->new('-0815'); \$x->bdiv(0);");
  ok ($@ =~/^Tried to set/, 1);
  ok ($x,-815);				# after new() never modified
  
  $cfg = $class->config( trap_nan => 1 );
  # 0/0 => NaN
  eval ("\$x = \$class->new('0'); \$x->bdiv(0);");
  ok ($@ =~/^Tried to set/, 1);
  ok ($x,0);				# after new() never modified
  }

##############################################################################
# BigRat

$cfg = Math::BigRat->config( trap_nan => 1 );

for my $trap (qw/0.1a +inf inf -inf/)
  {
  my $x = Math::BigRat->new('7/4');

  eval ("\$x = \$mbi->new('$trap');");
  print "# Got: $x\n" unless
   ok ($x,'7/4');			# never modified since it dies
  eval ("\$x = \$mbi->new('$trap');");
  print "# Got: $x\n" unless
   ok ($x,'7/4');			# never modified since it dies
  eval ("\$x = \$mbi->new('$trap/7');");
  print "# Got: $x\n" unless
   ok ($x,'7/4');			# never modified since it dies
  }

# all tests done