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