summaryrefslogtreecommitdiff
path: root/cpan/Math-BigInt/t/_e_math.t
blob: 1c136a565e0ea4b5a73d1a21e729be3180f40367 (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
#!perl

# test the helper math routines in Math::BigFloat

use strict;
use warnings;

use Test::More tests => 26;

use Math::BigFloat lib => 'Calc';

#############################################################################
# add

{
    my $a = Math::BigInt::Calc->_new("123");
    my $b = Math::BigInt::Calc->_new("321");

    test_add(123, 321, '+', '+');
    test_add(123, 321, '+', '-');
    test_add(123, 321, '-', '+');

    test_add(321, 123, '-', '+');
    test_add(321, 123, '+', '-');

    test_add(10,  1, '+', '-');
    test_add(10,  1, '-', '+');
    test_add( 1, 10, '-', '+');

  SKIP: {
        skip q|$x -> _zero() does not (yet?) modify the first argument|, 2;

        test_add(123, 123, '-', '+');
        test_add(123, 123, '+', '-');
    }

    test_add(123, 123, '+', '+');
    test_add(123, 123, '-', '-');

    test_add(0, 0, '-', '+');
    test_add(0, 0, '+', '-');
    test_add(0, 0, '+', '+');
    test_add(0, 0, '-', '-');          # gives "-0"! TODO: fix this!
}

#############################################################################
# sub

{
    my $a = Math::BigInt::Calc->_new("123");
    my $b = Math::BigInt::Calc->_new("321");

    test_sub(123, 321, '+', '-');
    test_sub(123, 321, '-', '+');

    test_sub(123, 123, '-', '+');
    test_sub(123, 123, '+', '-');

  SKIP: {
        skip q|$x -> _zero() does not (yet?) modify the first argument|, 2;

        test_sub(123, 123, '+', '+');
        test_sub(123, 123, '-', '-');
    }

    test_sub(0, 0, '-', '+');          # gives "-0"! TODO: fix this!
    test_sub(0, 0, '+', '-');
    test_sub(0, 0, '+', '+');
    test_sub(0, 0, '-', '-');
}

###############################################################################

sub test_add {
    my ($a, $b, $as, $bs) = @_;

    my $aa = Math::BigInt::Calc -> _new($a);
    my $bb = Math::BigInt::Calc -> _new($b);
    my ($x, $xs) = Math::BigFloat::_e_add($aa, $bb, "$as", "$bs");
    my $got = $xs . Math::BigInt::Calc->_str($x);

    my $expected = sprintf("%+d", "$as$a" + "$bs$b");

    subtest qq|Math::BigFloat::_e_add($a, $b, "$as", "$bs");|
      => sub {
          plan tests => 2;

          is($got, $expected, 'output has the correct value');
          is(Math::BigInt::Calc->_str($x),
             Math::BigInt::Calc->_str($aa),
             'first operand to _e_add() is modified'
            );
      };
}

sub test_sub {
    my ($a, $b, $as, $bs) = @_;

    my $aa = Math::BigInt::Calc -> _new($a);
    my $bb = Math::BigInt::Calc -> _new($b);
    my ($x, $xs) = Math::BigFloat::_e_sub($aa, $bb, "$as", "$bs");
    my $got = $xs . Math::BigInt::Calc->_str($x);

    my $expected = sprintf("%+d", "$as$a" - "$bs$b");

    subtest qq|Math::BigFloat::_e_sub($a, $b, "$as", "$bs");|
      => sub {
          plan tests => 2;

          is($got, $expected, 'output has the correct value');
          is(Math::BigInt::Calc->_str($x),
             Math::BigInt::Calc->_str($aa),
             'first operand to _e_sub() is modified'
            );
      };
}