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
CommitLineData
b3abae2a
JH
1#!/usr/bin/perl -w
2
3use strict;
4aa37faf 4use Test::More tests => 55;
b3abae2a 5
990fb837 6# test whether Math::BigInt->config() and Math::BigFloat->config() works
b3abae2a 7
233f7bc0 8use Math::BigInt lib => 'Calc';
990fb837 9use Math::BigFloat;
b3abae2a 10
990fb837 11my $mbi = 'Math::BigInt'; my $mbf = 'Math::BigFloat';
b3abae2a 12
990fb837
RGS
13##############################################################################
14# BigInt
15
16ok ($mbi->can('config'));
17
18my $cfg = $mbi->config();
19
20ok (ref($cfg),'HASH');
21
2ebb273f
T
22is ($cfg->{lib},'Math::BigInt::Calc', 'lib');
23is ($cfg->{lib_version}, $Math::BigInt::Calc::VERSION, 'lib_version');
24is ($cfg->{class},$mbi,'class');
25is ($cfg->{upgrade}||'','', 'upgrade');
26is ($cfg->{div_scale},40, 'div_Scale');
990fb837 27
2ebb273f
T
28is ($cfg->{precision}||0,0, 'precision'); # should test for undef
29is ($cfg->{accuracy}||0,0,'accuracy');
30is ($cfg->{round_mode},'even','round_mode');
990fb837 31
2ebb273f
T
32is ($cfg->{trap_nan},0, 'trap_nan');
33is ($cfg->{trap_inf},0, 'trap_inf');
990fb837 34
2ebb273f
T
35is ($mbi->config('lib'), 'Math::BigInt::Calc', 'config("lib")');
36
37# can set via hash ref?
38$cfg = $mbi->config( { trap_nan => 1 } );
39is ($cfg->{trap_nan},1, 'can set via hash ref');
40
41# reset for later
42$mbi->config( trap_nan => 0 );
990fb837
RGS
43
44##############################################################################
45# BigFloat
46
47ok ($mbf->can('config'));
48
49$cfg = $mbf->config();
b3abae2a
JH
50
51ok (ref($cfg),'HASH');
52
2ebb273f
T
53is ($cfg->{lib},'Math::BigInt::Calc', 'lib');
54is ($cfg->{with},'Math::BigInt::Calc', 'with');
55is ($cfg->{lib_version}, $Math::BigInt::Calc::VERSION, 'lib_version');
56is ($cfg->{class},$mbf,'class');
57is ($cfg->{upgrade}||'','', 'upgrade');
58is ($cfg->{div_scale},40, 'div_Scale');
59
60is ($cfg->{precision}||0,0, 'precision'); # should test for undef
61is ($cfg->{accuracy}||0,0,'accuracy');
62is ($cfg->{round_mode},'even','round_mode');
63
64is ($cfg->{trap_nan},0, 'trap_nan');
65is ($cfg->{trap_inf},0, 'trap_inf');
b3abae2a 66
2ebb273f 67is ($mbf->config('lib'), 'Math::BigInt::Calc', 'config("lib")');
b3abae2a 68
2ebb273f
T
69# can set via hash ref?
70$cfg = $mbf->config( { trap_nan => 1 } );
71is ($cfg->{trap_nan},1, 'can set via hash ref');
b3abae2a 72
2ebb273f
T
73# reset for later
74$mbf->config( trap_nan => 0 );
990fb837
RGS
75
76##############################################################################
77# test setting values
78
79my $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
90my $c;
91
92foreach 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 {
2ebb273f 101 is (1,1);
990fb837
RGS
102 }
103 else
104 {
2ebb273f 105 is ("$key eq $c->{$key}","$key ne $test->{$key}", "$key");
990fb837
RGS
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
233f7bc0 116$@ = ""; my $never_reached = 0;
990fb837 117eval ("$mbi\->config( 'some_garbage' => 1 ); $never_reached = 1;");
2ebb273f 118is ($never_reached,0);
990fb837 119
233f7bc0 120$@ = ""; $never_reached = 0;
990fb837 121eval ("$mbf\->config( 'some_garbage' => 1 ); $never_reached = 1;");
2ebb273f 122is ($never_reached,0);
990fb837
RGS
123
124# this does not work. Why?
233f7bc0 125#ok ($@ eq "Illegal keys 'some_garbage' passed to Math::BigInt->config() at ./config.t line 104", 1);
990fb837 126
b3abae2a
JH
127# all tests done
128