Move Math::BigInt from ext/ to cpan/
[p5sagit/p5-mst-13.2.git] / cpan / Math-BigInt / t / config.t
1 #!/usr/bin/perl -w
2
3 use strict;
4 use Test::More;
5
6 BEGIN
7   {
8   $| = 1;
9   chdir 't' if -d 't';
10   unshift @INC, '../lib'; # for running manually
11   plan tests => 55;
12   } 
13
14 # test whether Math::BigInt->config() and Math::BigFloat->config() works
15
16 use Math::BigInt lib => 'Calc';
17 use Math::BigFloat;
18
19 my $mbi = 'Math::BigInt'; my $mbf = 'Math::BigFloat';
20
21 ##############################################################################
22 # BigInt
23
24 ok ($mbi->can('config'));
25
26 my $cfg = $mbi->config();
27
28 ok (ref($cfg),'HASH');
29
30 is ($cfg->{lib},'Math::BigInt::Calc', 'lib');
31 is ($cfg->{lib_version}, $Math::BigInt::Calc::VERSION, 'lib_version');
32 is ($cfg->{class},$mbi,'class');
33 is ($cfg->{upgrade}||'','', 'upgrade');
34 is ($cfg->{div_scale},40, 'div_Scale');
35
36 is ($cfg->{precision}||0,0, 'precision');       # should test for undef
37 is ($cfg->{accuracy}||0,0,'accuracy');
38 is ($cfg->{round_mode},'even','round_mode');
39
40 is ($cfg->{trap_nan},0, 'trap_nan');
41 is ($cfg->{trap_inf},0, 'trap_inf');
42
43 is ($mbi->config('lib'), 'Math::BigInt::Calc', 'config("lib")');
44
45 # can set via hash ref?
46 $cfg = $mbi->config( { trap_nan => 1 } );
47 is ($cfg->{trap_nan},1, 'can set via hash ref');
48
49 # reset for later
50 $mbi->config( trap_nan => 0 );
51
52 ##############################################################################
53 # BigFloat
54
55 ok ($mbf->can('config'));
56
57 $cfg = $mbf->config();
58
59 ok (ref($cfg),'HASH');
60
61 is ($cfg->{lib},'Math::BigInt::Calc', 'lib');
62 is ($cfg->{with},'Math::BigInt::Calc', 'with');
63 is ($cfg->{lib_version}, $Math::BigInt::Calc::VERSION, 'lib_version');
64 is ($cfg->{class},$mbf,'class');
65 is ($cfg->{upgrade}||'','', 'upgrade');
66 is ($cfg->{div_scale},40, 'div_Scale');
67
68 is ($cfg->{precision}||0,0, 'precision');       # should test for undef
69 is ($cfg->{accuracy}||0,0,'accuracy');
70 is ($cfg->{round_mode},'even','round_mode');
71
72 is ($cfg->{trap_nan},0, 'trap_nan');
73 is ($cfg->{trap_inf},0, 'trap_inf');
74
75 is ($mbf->config('lib'), 'Math::BigInt::Calc', 'config("lib")');
76
77 # can set via hash ref?
78 $cfg = $mbf->config( { trap_nan => 1 } );
79 is ($cfg->{trap_nan},1, 'can set via hash ref');
80
81 # reset for later
82 $mbf->config( trap_nan => 0 );
83
84 ##############################################################################
85 # test setting values
86
87 my $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
98 my $c;
99
100 foreach 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     {
109     is (1,1);
110     }
111   else
112     {
113     is ("$key eq $c->{$key}","$key ne $test->{$key}", "$key");
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   
124 $@ = ""; my $never_reached = 0;
125 eval ("$mbi\->config( 'some_garbage' => 1 ); $never_reached = 1;");
126 is ($never_reached,0);
127
128 $@ = ""; $never_reached = 0;
129 eval ("$mbf\->config( 'some_garbage' => 1 ); $never_reached = 1;");
130 is ($never_reached,0);
131
132 # this does not work. Why?
133 #ok ($@ eq "Illegal keys 'some_garbage' passed to Math::BigInt->config() at ./config.t line 104", 1);
134
135 # all tests done
136