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