This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Convert bignum tests from Test to Test::More.
[perl5.git] / dist / Math-BigInt / t / config.t
CommitLineData
b3abae2a
JH
1#!/usr/bin/perl -w
2
3use strict;
2ebb273f 4use Test::More;
b3abae2a
JH
5
6BEGIN
7 {
8 $| = 1;
9 chdir 't' if -d 't';
10 unshift @INC, '../lib'; # for running manually
2ebb273f 11 plan tests => 55;
b3abae2a
JH
12 }
13
990fb837 14# test whether Math::BigInt->config() and Math::BigFloat->config() works
b3abae2a 15
233f7bc0 16use Math::BigInt lib => 'Calc';
990fb837 17use Math::BigFloat;
b3abae2a 18
990fb837 19my $mbi = 'Math::BigInt'; my $mbf = 'Math::BigFloat';
b3abae2a 20
990fb837
RGS
21##############################################################################
22# BigInt
23
24ok ($mbi->can('config'));
25
26my $cfg = $mbi->config();
27
28ok (ref($cfg),'HASH');
29
2ebb273f
T
30is ($cfg->{lib},'Math::BigInt::Calc', 'lib');
31is ($cfg->{lib_version}, $Math::BigInt::Calc::VERSION, 'lib_version');
32is ($cfg->{class},$mbi,'class');
33is ($cfg->{upgrade}||'','', 'upgrade');
34is ($cfg->{div_scale},40, 'div_Scale');
990fb837 35
2ebb273f
T
36is ($cfg->{precision}||0,0, 'precision'); # should test for undef
37is ($cfg->{accuracy}||0,0,'accuracy');
38is ($cfg->{round_mode},'even','round_mode');
990fb837 39
2ebb273f
T
40is ($cfg->{trap_nan},0, 'trap_nan');
41is ($cfg->{trap_inf},0, 'trap_inf');
990fb837 42
2ebb273f
T
43is ($mbi->config('lib'), 'Math::BigInt::Calc', 'config("lib")');
44
45# can set via hash ref?
46$cfg = $mbi->config( { trap_nan => 1 } );
47is ($cfg->{trap_nan},1, 'can set via hash ref');
48
49# reset for later
50$mbi->config( trap_nan => 0 );
990fb837
RGS
51
52##############################################################################
53# BigFloat
54
55ok ($mbf->can('config'));
56
57$cfg = $mbf->config();
b3abae2a
JH
58
59ok (ref($cfg),'HASH');
60
2ebb273f
T
61is ($cfg->{lib},'Math::BigInt::Calc', 'lib');
62is ($cfg->{with},'Math::BigInt::Calc', 'with');
63is ($cfg->{lib_version}, $Math::BigInt::Calc::VERSION, 'lib_version');
64is ($cfg->{class},$mbf,'class');
65is ($cfg->{upgrade}||'','', 'upgrade');
66is ($cfg->{div_scale},40, 'div_Scale');
67
68is ($cfg->{precision}||0,0, 'precision'); # should test for undef
69is ($cfg->{accuracy}||0,0,'accuracy');
70is ($cfg->{round_mode},'even','round_mode');
71
72is ($cfg->{trap_nan},0, 'trap_nan');
73is ($cfg->{trap_inf},0, 'trap_inf');
b3abae2a 74
2ebb273f 75is ($mbf->config('lib'), 'Math::BigInt::Calc', 'config("lib")');
b3abae2a 76
2ebb273f
T
77# can set via hash ref?
78$cfg = $mbf->config( { trap_nan => 1 } );
79is ($cfg->{trap_nan},1, 'can set via hash ref');
b3abae2a 80
2ebb273f
T
81# reset for later
82$mbf->config( trap_nan => 0 );
990fb837
RGS
83
84##############################################################################
85# test setting values
86
87my $test = {
88 trap_nan => 1,
89 trap_inf => 1,
90 accuracy => 2,
91 precision => 3,
92 round_mode => 'zero',
93 div_scale => '100',
94 upgrade => 'Math::BigInt::SomeClass',
95 downgrade => 'Math::BigInt::SomeClass',
96 };
97
98my $c;
99
100foreach my $key (keys %$test)
101 {
102 # see if setting in MBI works
103 eval ( "$mbi\->config( $key => '$test->{$key}' );" );
104 $c = $mbi->config(); ok ("$key = $c->{$key}", "$key = $test->{$key}");
105 $c = $mbf->config();
106 # see if setting it in MBI leaves MBF alone
107 if (($c->{$key}||0) ne $test->{$key})
108 {
2ebb273f 109 is (1,1);
990fb837
RGS
110 }
111 else
112 {
2ebb273f 113 is ("$key eq $c->{$key}","$key ne $test->{$key}", "$key");
990fb837
RGS
114 }
115
116 # see if setting in MBF works
117 eval ( "$mbf\->config( $key => '$test->{$key}' );" );
118 $c = $mbf->config(); ok ("$key = $c->{$key}", "$key = $test->{$key}");
119 }
120
121##############################################################################
122# test setting illegal keys (should croak)
123
233f7bc0 124$@ = ""; my $never_reached = 0;
990fb837 125eval ("$mbi\->config( 'some_garbage' => 1 ); $never_reached = 1;");
2ebb273f 126is ($never_reached,0);
990fb837 127
233f7bc0 128$@ = ""; $never_reached = 0;
990fb837 129eval ("$mbf\->config( 'some_garbage' => 1 ); $never_reached = 1;");
2ebb273f 130is ($never_reached,0);
990fb837
RGS
131
132# this does not work. Why?
233f7bc0 133#ok ($@ eq "Illegal keys 'some_garbage' passed to Math::BigInt->config() at ./config.t line 104", 1);
990fb837 134
b3abae2a
JH
135# all tests done
136