1N/A#!/usr/bin/perl -w
1N/A
1N/A# test that config ( trap_nan => 1, trap_inf => 1) really works/dies
1N/A
1N/Ause strict;
1N/Ause Test;
1N/A
1N/ABEGIN
1N/A {
1N/A $| = 1;
1N/A chdir 't' if -d 't';
1N/A unshift @INC, '../lib'; # for running manually
1N/A plan tests => 29;
1N/A }
1N/A
1N/Ause Math::BigRat;
1N/A
1N/Amy $mbi = 'Math::BigRat';
1N/Amy ($cfg,$x);
1N/A
1N/Aforeach my $class ($mbi)
1N/A {
1N/A # can do and defaults are okay?
1N/A ok ($class->can('config'));
1N/A ok ($class->config()->{trap_nan}, 0);
1N/A ok ($class->config()->{trap_inf}, 0);
1N/A
1N/A # can set?
1N/A $cfg = $class->config( trap_nan => 1 ); ok ($cfg->{trap_nan},1);
1N/A
1N/A # can set via hash ref?
1N/A $cfg = $class->config( { trap_nan => 1 } ); ok ($cfg->{trap_nan},1);
1N/A
1N/A # also test that new() still works normally
1N/A eval ("\$x = \$class->new('42'); \$x->bnan();");
1N/A ok ($@ =~/^Tried to set/, 1);
1N/A ok ($x,42); # after new() never modified
1N/A
1N/A # can reset?
1N/A $cfg = $class->config( trap_nan => 0 ); ok ($cfg->{trap_nan},0);
1N/A
1N/A # can set?
1N/A $cfg = $class->config( trap_inf => 1 ); ok ($cfg->{trap_inf},1);
1N/A eval ("\$x = \$class->new('4711'); \$x->binf();");
1N/A ok ($@ =~/^Tried to set/, 1);
1N/A ok ($x,4711); # after new() never modified
1N/A
1N/A # +$x/0 => +inf
1N/A eval ("\$x = \$class->new('4711'); \$x->bdiv(0);");
1N/A ok ($@ =~/^Tried to set/, 1);
1N/A ok ($x,4711); # after new() never modified
1N/A
1N/A # -$x/0 => -inf
1N/A eval ("\$x = \$class->new('-0815'); \$x->bdiv(0);");
1N/A ok ($@ =~/^Tried to set/, 1);
1N/A ok ($x,-815); # after new() never modified
1N/A
1N/A $cfg = $class->config( trap_nan => 1 );
1N/A # 0/0 => NaN
1N/A eval ("\$x = \$class->new('0'); \$x->bdiv(0);");
1N/A ok ($@ =~/^Tried to set/, 1);
1N/A ok ($x,0); # after new() never modified
1N/A }
1N/A
1N/A##############################################################################
1N/A# BigRat
1N/A
1N/A$cfg = Math::BigRat->config( trap_nan => 1 );
1N/A
1N/Afor my $trap (qw/0.1a +inf inf -inf/)
1N/A {
1N/A my $x = Math::BigRat->new('7/4');
1N/A
1N/A eval ("\$x = \$mbi->new('$trap');");
1N/A print "# Got: $x\n" unless
1N/A ok ($x,'7/4'); # never modified since it dies
1N/A eval ("\$x = \$mbi->new('$trap');");
1N/A print "# Got: $x\n" unless
1N/A ok ($x,'7/4'); # never modified since it dies
1N/A eval ("\$x = \$mbi->new('$trap/7');");
1N/A print "# Got: $x\n" unless
1N/A ok ($x,'7/4'); # never modified since it dies
1N/A }
1N/A
1N/A# all tests done
1N/A