File: bmuladd-mbi.t

package info (click to toggle)
libmath-bigint-perl 1.999838-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 9,424 kB
  • sloc: perl: 10,236; pascal: 5,387; makefile: 2
file content (91 lines) | stat: -rw-r--r-- 2,881 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
# -*- mode: perl; -*-

use strict;
use warnings;

use Test::More tests => 1012;
use Scalar::Util qw< refaddr >;

use Math::BigInt;

my ($x, $y);

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

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

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

$x = Math::BigInt -> new("2"); $y = $x -> bmuladd("3", "5");
subtest '$x = Math::BigInt -> new("2"); $y = $x -> bmuladd("3", "5");' => sub {
    plan tests => 4;
    is(ref($x), 'Math::BigInt', '$x is a Math::BigInt');
    is(ref($y), 'Math::BigInt', '$y is a Math::BigInt');
    is(refaddr($x), refaddr($y), '$x and $y are the same object');
    cmp_ok($x, "==", 11, '$x == 11');
};

# Check consistency of bmuladd() vs. bmul() + badd()

my @values = qw/ -Inf -3 -2 -1 0 1 2 3 Inf NaN /;
for my $a (@values) {
    for my $b (@values) {
        for my $c (@values) {

            my $test = qq|Math::BigInt -> new("$a") -> bmuladd("$b", "$c")|
                     . qq| vs. Math::BigInt -> new("$a") -> bmul("$b")|
                     . qq| -> badd("$c")|;

            $x = Math::BigInt -> new("$a") -> bmuladd("$b", "$c");
            $y = Math::BigInt -> new("$a") -> bmul("$b") -> badd("$c");

            subtest $test => sub {
                plan tests => 3;
                is(ref($x), 'Math::BigInt', '$x is a Math::BigInt');
                is(ref($y), 'Math::BigInt', '$y is a Math::BigInt');
                if ($x -> is_nan() && $y -> is_nan()) {
                    is($x, $y, '$x == $y');
                } else {
                    cmp_ok($x, "==", $y, '$x == $y');
                }
            };
        }
    }
}

# Test when the same object appears more than once.

my $t;
for my $a (-2, 2) {
    for my $b (-3, 3) {

        $x = Math::BigInt -> new("$a"); $y = $x -> bmuladd("$b", $x);
        $t = qq|\$x = Math::BigInt->new("$a"); \$y = \$x -> bmuladd("$b", \$x);|;
        subtest $t => sub {
            plan tests => 2;
            is(ref($x), 'Math::BigInt', 'class of $x');
            cmp_ok($x, "==", $a * $b + $a, 'value of $x');
        };

        $x = Math::BigInt -> new("$a"); $y = $x -> bmuladd($x, "$b");
        $t = qq|\$x = Math::BigInt->new("$a"); \$y = \$x -> bmuladd(\$x, "$b");|;
        subtest $t => sub {
            plan tests => 2;
            is(ref($x), 'Math::BigInt', 'class of $x');
            cmp_ok($x, "==", $a * $a + $b, 'value of $x');
        };
    }

    $x = Math::BigInt -> new("$a"); $y = $x -> bmuladd($x, $x);
    $t = qq|\$x = Math::BigInt->new("$a"); \$y = \$x -> bmuladd(\$x, \$x);|;
    subtest $t => sub {
        plan tests => 2;
        is(ref($x), 'Math::BigInt', 'class of $x');
        cmp_ok($x, "==", $a * $a + $a, 'value of $x');
    };
}