summaryrefslogtreecommitdiff
path: root/cpan/Math-BigInt/t/bpow-mbi.t
blob: 6fbf27082102593d11a36def31f3a827be932ed2 (plain)
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# -*- mode: perl; -*-

use strict;
use warnings;

use Test::More tests => 99;

use Math::BigInt;

my $class = "Math::BigInt";

use Math::Complex ();

my $inf = $Math::Complex::Inf;
my $nan = $inf - $inf;

# The following is used to compute the data at the end of this file.

if (0) {
    for my $x (-$inf, -64, -3, -2, -1, 0, 1, 2, 3, 64, $inf) {
        for my $y (-$inf, -3, -2, -1, 0, 1, 2, 3, $inf) {

            # The exceptions here are based on Wolfram Alpha,
            # https://www.wolframalpha.com/

            my $z = $x == -$inf && $y == 0     ? $nan
                  : $x ==  $inf && $y == 0     ? $nan
                  : $x == -1    && $y == -$inf ? $nan
                  : $x == -1    && $y ==  $inf ? $nan
                  :                              int($x ** $y);

            # Unfortunately, Math::Big* uses "inf", not "Inf" as Perl.

            printf "%s\n", join ":", map {   $_ ==  $inf ?  "inf"
                                           : $_ == -$inf ? "-inf"
                                           : $_                   } $x, $y, $z;
        }
    }

    exit;
}

while (<DATA>) {
    s/#.*$//;                   # remove comments
    s/\s+$//;                   # remove trailing whitespace
    next unless length;         # skip empty lines

    my @args = split /:/;
    my $want = pop @args;

    my ($x, $y, $z);

    my $test = qq|\$x = $class -> new("$args[0]"); |
             . qq|\$y = $class -> new("$args[1]"); |
             . qq|\$z = \$x -> bpow(\$y)|;

    eval "$test";
    die $@ if $@;

    subtest $test => sub {
        plan tests => 5;

        is(ref($x), $class, "\$x is still a $class");

        is(ref($y), $class, "\$y is still a $class");
        is($y, $args[1], "\$y is unmodified");

        is(ref($z), $class, "\$z is a $class");
        is($z, $want, "\$z has the right value");
    };
}

__DATA__
-inf:-inf:0
-inf:-3:0
-inf:-2:0
-inf:-1:0
-inf:0:NaN
-inf:1:-inf
-inf:2:inf
-inf:3:-inf
-inf:inf:inf
-64:-inf:0
-64:-3:0
-64:-2:0
-64:-1:0
-64:0:1
-64:1:-64
-64:2:4096
-64:3:-262144
-64:inf:inf
-3:-inf:0
-3:-3:0
-3:-2:0
-3:-1:0
-3:0:1
-3:1:-3
-3:2:9
-3:3:-27
-3:inf:inf
-2:-inf:0
-2:-3:0
-2:-2:0
-2:-1:0
-2:0:1
-2:1:-2
-2:2:4
-2:3:-8
-2:inf:inf
-1:-inf:NaN
-1:-3:-1
-1:-2:1
-1:-1:-1
-1:0:1
-1:1:-1
-1:2:1
-1:3:-1
-1:inf:NaN
0:-inf:inf
0:-3:inf
0:-2:inf
0:-1:inf
0:0:1
0:1:0
0:2:0
0:3:0
0:inf:0
1:-inf:1
1:-3:1
1:-2:1
1:-1:1
1:0:1
1:1:1
1:2:1
1:3:1
1:inf:1
2:-inf:0
2:-3:0
2:-2:0
2:-1:0
2:0:1
2:1:2
2:2:4
2:3:8
2:inf:inf
3:-inf:0
3:-3:0
3:-2:0
3:-1:0
3:0:1
3:1:3
3:2:9
3:3:27
3:inf:inf
64:-inf:0
64:-3:0
64:-2:0
64:-1:0
64:0:1
64:1:64
64:2:4096
64:3:262144
64:inf:inf
inf:-inf:0
inf:-3:0
inf:-2:0
inf:-1:0
inf:0:NaN
inf:1:inf
inf:2:inf
inf:3:inf
inf:inf:inf