xref: /openbsd-src/gnu/usr.bin/perl/cpan/Math-BigInt/t/mbimbf.t (revision 3d61058aa5c692477b6d18acfbbdb653a9930ff9)
1# -*- mode: perl; -*-
2
3# test rounding, accuracy, precision and fallback, round_mode and mixing
4# of classes
5
6use strict;
7use warnings;
8
9use Test::More tests => 712             # tests in require'd file
10                        + 52;           # tests in this file
11
12use Math::BigInt only => 'Calc';
13use Math::BigFloat;
14
15our $mbi = 'Math::BigInt';
16our $mbf = 'Math::BigFloat';
17
18require './t/mbimbf.inc';
19
20# some tests that won't work with subclasses, since the things are only
21# guaranteed in the Math::Big(Int|Float) (unless subclass chooses to support
22# this)
23
24Math::BigInt->round_mode("even");       # reset for tests
25Math::BigFloat->round_mode("even");     # reset for tests
26
27is($Math::BigInt::rnd_mode,   "even", '$Math::BigInt::rnd_mode = "even"');
28is($Math::BigFloat::rnd_mode, "even", '$Math::BigFloat::rnd_mode = "even"');
29
30my $x = eval '$mbi->round_mode("huhmbi");';
31like($@, qr/^Unknown round mode 'huhmbi' at/,
32     '$mbi->round_mode("huhmbi")');
33
34$x = eval '$mbf->round_mode("huhmbf");';
35like($@, qr/^Unknown round mode 'huhmbf' at/,
36     '$mbf->round_mode("huhmbf")');
37
38# old way (now with test for validity)
39$x = eval '$Math::BigInt::rnd_mode = "huhmbi";';
40like($@, qr/^Unknown round mode 'huhmbi' at/,
41     '$Math::BigInt::rnd_mode = "huhmbi"');
42$x = eval '$Math::BigFloat::rnd_mode = "huhmbf";';
43like($@, qr/^Unknown round mode 'huhmbf' at/,
44     '$Math::BigFloat::rnd_mode = "huhmbf"');
45
46# see if accessor also changes old variable
47$mbi->round_mode('odd');
48is($Math::BigInt::rnd_mode, 'odd', '$Math::BigInt::rnd_mode = "odd"');
49
50$mbf->round_mode('odd');
51is($Math::BigInt::rnd_mode, 'odd', '$Math::BigInt::rnd_mode = "odd"');
52
53foreach my $class (qw/Math::BigInt Math::BigFloat/) {
54    is($class->accuracy(5),  5,     "set A ...");
55    is($class->precision(),  undef, "... and now P must be cleared");
56    is($class->precision(5), 5,     "set P ...");
57    is($class->accuracy(),   undef, "... and now A must be cleared");
58}
59
60foreach my $class (qw/Math::BigInt Math::BigFloat/)  {
61    my $x;
62
63    # Accuracy
64
65    # set and check the class accuracy
66    $class->accuracy(1);
67    is($class->accuracy(), 1, "$class has A of 1");
68
69    # a new instance gets the class accuracy
70    $x = $class->new(123);
71    is($x->accuracy(), 1, '$x has A of 1');
72
73    # set and check the instance accuracy
74    $x->accuracy(2);
75    is($x->accuracy(), 2, '$x has A of 2');
76
77    # change the class accuracy
78    $class->accuracy(3);
79    is($class->accuracy(), 3, "$class has A of 3");
80
81    # verify that the instance accuracy hasn't changed
82    is($x->accuracy(), 2, '$x still has A of 2');
83
84    # change the instance accuracy
85    $x->accuracy(undef);
86    is($x->accuracy(), undef, '$x now has A of undef');
87
88    # check the class accuracy
89    is($class->accuracy(), 3, "$class still has A of 3");
90
91    # change the class accuracy again
92    $class->accuracy(undef);
93    is($class->accuracy(), undef, "$class now has A of undef");
94
95    # Precision
96
97    # set and check the class precision
98    $class->precision(1);
99    is($class->precision(), 1, "$class has A of 1");
100
101    # a new instance gets the class precision
102    $x = $class->new(123);
103    is($x->precision(), 1, '$x has A of 1');
104
105    # set and check the instance precision
106    $x->precision(2);
107    is($x->precision(), 2, '$x has A of 2');
108
109    # change the class precision
110    $class->precision(3);
111    is($class->precision(), 3, "$class has A of 3");
112
113    # verify that the instance precision hasn't changed
114    is($x->precision(), 2, '$x still has A of 2');
115
116    # change the instance precision
117    $x->precision(undef);
118    is($x->precision(), undef, '$x now has A of undef');
119
120    # check the class precision
121    is($class->precision(), 3, "$class still has A of 3");
122
123    # change the class precision again
124    $class->precision(undef);
125    is($class->precision(), undef, "$class now has A of undef");
126}
127
128# bug with blog(Math::BigFloat, Math::BigInt)
129$x = Math::BigFloat->new(100);
130$x = $x->blog(Math::BigInt->new(10));
131
132is($x, 2, 'bug with blog(Math::BigFloat, Math::BigInt)');
133
134# bug until v1.88 for sqrt() with enough digits
135for my $i (80, 88, 100) {
136    $x = Math::BigFloat->new("1." . ("0" x $i) . "1");
137    $x = $x->bsqrt;
138    is($x, 1, '$x->bsqrt() with many digits');
139}
140