File: as_rat-mbi.t

package info (click to toggle)
libmath-bigint-perl 2.005003-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,144 kB
  • sloc: perl: 14,389; pascal: 6,476; makefile: 2
file content (148 lines) | stat: -rw-r--r-- 4,565 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# -*- mode: perl; -*-

use strict;
use warnings;

use Test::More;
use Scalar::Util qw< refaddr >;

use Math::BigInt;

my ($x, $y);

note("as_rat() as a class method");

$x = Math::BigInt -> as_rat("Inf");
subtest '$x = Math::BigInt -> as_rat("Inf");' => sub {
    plan tests => 2;
    is(ref($x), 'Math::BigRat', '$x is a Math::BigRat');
    cmp_ok($x, "==", "Inf", '$x == Inf');
};

$x = Math::BigInt -> as_rat("-Inf");
subtest '$x = Math::BigInt -> as_rat("-Inf");' => sub {
    plan tests => 2;
    is(ref($x), 'Math::BigRat', '$x is a Math::BigRat');
    cmp_ok($x, "==", "-Inf", '$x == -Inf');
};

$x = Math::BigInt -> as_rat("NaN");
subtest '$x = Math::BigInt -> as_rat("NaN");' => sub {
    plan tests => 2;
    is(ref($x), 'Math::BigRat', '$x is a Math::BigRat');
    is($x, "NaN", '$x is NaN');
};

$x = Math::BigInt -> as_rat("2");
subtest '$x = Math::BigInt -> as_rat("2");' => sub {
    plan tests => 2;
    is(ref($x), 'Math::BigRat', '$x is a Math::BigRat');
    cmp_ok($x, "==", 2, '$x == 2');
};

note("as_rat() as an instance method");

$x = Math::BigInt -> new("Inf"); $y = $x -> as_rat();
subtest '$x = Math::BigInt -> new("Inf"); $y = $x -> as_rat();' => sub {
    plan tests => 4;
    is(ref($x), 'Math::BigInt', '$x is a Math::BigInt');
    is(ref($y), 'Math::BigRat', '$y is a Math::BigRat');
    cmp_ok($y, "==", "Inf", '$y == Inf');
    isnt(refaddr($x), refaddr($y), '$x and $y are different objects');
};

$x = Math::BigInt -> new("-Inf"); $y = $x -> as_rat();
subtest '$x = Math::BigInt -> new("-Inf"); $y = $x -> as_rat();' => sub {
    plan tests => 4;
    is(ref($x), 'Math::BigInt', '$x is a Math::BigInt');
    is(ref($y), 'Math::BigRat', '$y is a Math::BigRat');
    cmp_ok($y, "==", "-Inf", '$y == -Inf');
    isnt(refaddr($x), refaddr($y), '$x and $y are different objects');
};

$x = Math::BigInt -> new("NaN"); $y = $x -> as_rat();
subtest '$x = Math::BigInt -> new("NaN"); $y = $x -> as_rat();' => sub {
    plan tests => 4;
    is(ref($x), 'Math::BigInt', '$x is a Math::BigInt');
    is(ref($y), 'Math::BigRat', '$y is a Math::BigRat');
    is($y, "NaN", '$y is NaN');
    isnt(refaddr($x), refaddr($y), '$x and $y are different objects');
};

$x = Math::BigInt -> new("2"); $y = $x -> as_rat();
subtest '$x = Math::BigInt -> new("2"); $y = $x -> as_rat();' => sub {
    plan tests => 4;
    is(ref($x), 'Math::BigInt', '$x is a Math::BigInt');
    is(ref($y), 'Math::BigRat', '$y is a Math::BigRat');
    cmp_ok($y, "==", 2, '$y == 2');
    isnt(refaddr($x), refaddr($y), '$x and $y are different objects');
};

note("as_rat() returns a Math::BigRat regardless of upgrading/downgrading");

require Math::BigRat;
Math::BigInt -> upgrade("Math::BigFloat");
Math::BigRat -> downgrade("Math::BigInt");
Math::BigRat -> upgrade("Math::BigFloat");

$x = Math::BigInt -> new("3");
$y = $x -> as_rat();

subtest '$x = Math::BigInt -> new("3"); $y = $x -> as_rat();' => sub {
    plan tests => 3;
    is(ref($x), 'Math::BigInt', 'class of $y');
    is(ref($y), 'Math::BigRat', 'class of $y');
    cmp_ok($y -> numify(), "==", 3, 'value of $y');
};

$y = Math::BigInt -> as_rat("3");

subtest '$y = Math::BigInt -> as_rat("3");' => sub {
    plan tests => 2;
    is(ref($y), 'Math::BigRat', 'class of $y');
    cmp_ok($y -> numify(), "==", 3, 'value of $y');
};

# Upgrading prevents the "3.5" to become a Math::BigInt with the value "NaN".

$y = Math::BigInt -> as_rat("3.5");

subtest '$y = Math::BigInt -> as_rat("3.5");' => sub {
    plan tests => 2;
    is(ref($y), 'Math::BigRat', 'class of $y');
    is($y, "7/2", 'value of $y');
};

note("as_rat() preserves all instance variables");

Math::BigInt -> upgrade(undef);
Math::BigRat -> downgrade(undef);
Math::BigRat -> upgrade(undef);

$x = Math::BigInt -> new("3");

$x -> accuracy(2);
$y = $x -> as_rat();

subtest '$x = Math::BigInt -> new("3"); $x -> accuracy(2); $y = $x -> as_rat()'
  => sub {
      plan tests => 4;
      is($x -> accuracy(), 2, 'accuracy of $x');
      is($x -> precision(), undef, 'precision of $x');
      is($y -> accuracy(), $x -> accuracy(), 'accuracy of $y');
      is($y -> precision(), $x -> precision(), 'precision of $y');
  };

$x -> precision(2);
$y = $x -> as_rat();

subtest '$x = Math::BigInt -> new("3"); $x -> precision(2); $y = $x -> as_rat()'
  => sub {
      plan tests => 4;
      is($x -> accuracy(), undef, 'accuracy of $x');
      is($x -> precision(), 2, 'precision of $x');
      is($y -> accuracy(), $x -> accuracy(), 'accuracy of $y');
      is($y -> precision(), $x -> precision(), 'precision of $y');
  };

done_testing();