summaryrefslogtreecommitdiff
path: root/cpan/Math-BigRat/t/badd-mbr.t
blob: 1f96200736f8ce00a5fdea1ea03a6b7cb1cf24a1 (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
# -*- mode: perl; -*-

use strict;
use warnings;

use Test::More tests => 173;

my $class;

BEGIN {
    $class = 'Math::BigRat';
    use_ok($class);
}

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

    my ($xval, $yval, $zval) = split /:/;
    my ($x, $y, $got, @got);

    for my $context_is_scalar (0, 1) {
        for my $y_is_scalar (0, 1) {

            my $test = qq|\$x = $class -> new("$xval");|;

            $test .= $y_is_scalar
                     ? qq| \$y = "$yval";|
                     : qq| \$y = $class -> new("$yval");|;

            $test .= $context_is_scalar
                     ? qq| \$got = \$x -> badd(\$y);|
                     : qq| \@got = \$x -> badd(\$y);|;

            my $desc = "badd() in ";
            $desc .= $context_is_scalar ? "scalar context" : "list context";
            $desc .= $y_is_scalar ? " with y as scalar" : " with y as object";

            subtest $desc,
              sub {
                  plan tests => $context_is_scalar ? 7 : 8;

                  eval $test;
                  is($@, "", "'$test' gives emtpy \$\@");

                  if ($context_is_scalar) {

                      # Check output.

                      is(ref($got), $class,
                         "'$test' output arg is a $class");

                      is($got -> bstr(), $zval,
                         "'$test' output arg has the right value");

                  } else {

                      # Check number of output arguments.

                      cmp_ok(scalar @got, '==', 1,
                             "'$test' gives one output arg");

                      # Check output.

                      is(ref($got[0]), $class,
                         "'$test' output arg is a $class");

                      is($got[0] -> bstr(), $zval,
                         "'$test' output arg has the right value");
                  }

                  # Check the invocand.

                  is(ref($x), $class,
                     "'$test' invocand is still a $class");

                  is($x -> bstr(), $zval,
                     "'$test' invocand has the right value");

                  # Check the input argument.

                  if ($y_is_scalar) {

                      is(ref($y), '',
                         "'$test' second input arg is still a scalar");

                      is($y, $yval,
                         "'$test' second input arg is unmodified");

                  } else {

                      is(ref($y), $class,
                         "'$test' second input arg is still a $class");

                      is($y -> bstr(), $yval,
                         "'$test' second input arg is unmodified");
                  }
              };
        }
    }
}

__DATA__

# x and/or y is NaN

NaN:NaN:NaN

NaN:-inf:NaN
NaN:-3:NaN
NaN:0:NaN
NaN:3:NaN
NaN:inf:NaN

-inf:NaN:NaN
-3:NaN:NaN
0:NaN:NaN
3:NaN:NaN
inf:NaN:NaN

# x = inf

inf:-inf:NaN
inf:-3:inf
inf:-2:inf
inf:-1:inf
inf:0:inf
inf:1:inf
inf:2:inf
inf:3:inf
inf:inf:inf

# x = -inf

-inf:-inf:-inf
-inf:-3:-inf
-inf:-2:-inf
-inf:-1:-inf
-inf:0:-inf
-inf:1:-inf
-inf:2:-inf
-inf:3:-inf
-inf:inf:NaN

# y = inf

-3:inf:inf
-2:inf:inf
-1:inf:inf
0:inf:inf
1:inf:inf
2:inf:inf
3:inf:inf

# y = -inf

-3:-inf:-inf
-2:-inf:-inf
-1:-inf:-inf
0:-inf:-inf
1:-inf:-inf
2:-inf:-inf
3:-inf:-inf