5c480536bdda0b609f6ffa8d929890db6eca2f82
[p5sagit/p5-mst-13.2.git] / lib / Math / BigInt / t / config.t
1 #!/usr/bin/perl -w
2
3 use strict;
4 use Test;
5
6 BEGIN
7   {
8   $| = 1;
9   chdir 't' if -d 't';
10   unshift @INC, '../lib'; # for running manually
11   plan tests => 51;
12   } 
13
14 # test whether Math::BigInt->config() and Math::BigFloat->config() works
15
16 use Math::BigInt;
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 ok ($cfg->{lib},'Math::BigInt::Calc');
31 ok ($cfg->{lib_version}, $Math::BigInt::Calc::VERSION);
32 ok ($cfg->{class},$mbi);
33 ok ($cfg->{upgrade}||'','');
34 ok ($cfg->{div_scale},40);
35
36 ok ($cfg->{precision}||0,0);    # should test for undef
37 ok ($cfg->{accuracy}||0,0);
38
39 ok ($cfg->{round_mode},'even');
40
41 ok ($cfg->{trap_nan},0);
42 ok ($cfg->{trap_inf},0);
43
44 ##############################################################################
45 # BigFloat
46
47 ok ($mbf->can('config'));
48
49 $cfg = $mbf->config();
50
51 ok (ref($cfg),'HASH');
52
53 ok ($cfg->{lib},'Math::BigInt::Calc');
54 ok ($cfg->{with},$mbi);
55 ok ($cfg->{lib_version}, $Math::BigInt::Calc::VERSION);
56 ok ($cfg->{class},$mbf);
57 ok ($cfg->{upgrade}||'','');
58 ok ($cfg->{div_scale},40);
59
60 ok ($cfg->{precision}||0,0);    # should test for undef
61 ok ($cfg->{accuracy}||0,0);
62
63 ok ($cfg->{round_mode},'even');
64
65 ok ($cfg->{trap_nan},0);
66 ok ($cfg->{trap_inf},0);
67
68 ##############################################################################
69 # test setting values
70
71 my $test = {
72    trap_nan => 1, 
73    trap_inf => 1, 
74    accuracy => 2,
75    precision => 3,
76    round_mode => 'zero',
77    div_scale => '100',
78    upgrade => 'Math::BigInt::SomeClass',
79    downgrade => 'Math::BigInt::SomeClass',
80   };
81
82 my $c;
83
84 foreach my $key (keys %$test)
85   {
86   # see if setting in MBI works
87   eval ( "$mbi\->config( $key => '$test->{$key}' );" );
88   $c = $mbi->config(); ok ("$key = $c->{$key}", "$key = $test->{$key}");
89   $c = $mbf->config(); 
90   # see if setting it in MBI leaves MBF alone
91   if (($c->{$key}||0) ne $test->{$key})
92     {
93     ok (1,1);
94     }
95   else
96     {
97     ok ("$key eq $c->{$key}","$key ne $test->{$key}");
98     }
99
100   # see if setting in MBF works
101   eval ( "$mbf\->config( $key => '$test->{$key}' );" );
102   $c = $mbf->config(); ok ("$key = $c->{$key}", "$key = $test->{$key}");
103   }
104
105 ##############################################################################
106 # test setting illegal keys (should croak)
107   
108 my $never_reached = 0;
109 eval ("$mbi\->config( 'some_garbage' => 1 ); $never_reached = 1;");
110 ok ($never_reached,0);
111
112 $never_reached = 0;
113 eval ("$mbf\->config( 'some_garbage' => 1 ); $never_reached = 1;");
114 ok ($never_reached,0);
115
116 # this does not work. Why?
117 #ok (@!, "Illegal keys 'some_garbage' passed to Math::BigInt->config() at ./config.t line 104");
118
119 # all tests done
120