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