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