The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
#!/usr/bin/perl -w

# test the helper math routines in Math::BigFloat

use strict;
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");

my ($x, $xs) = Math::BigFloat::_e_add($a,$b,'+','+');
is (_str($x,$xs), '+444', 'add two positive numbers');
is (_str($a,''), '444', 'a modified');

($x,$xs) = _add (123,321,'+','+');
is (_str($x,$xs), '+444', 'add two positive numbers');

($x,$xs) = _add (123,321,'+','-');
is (_str($x,$xs), '-198', 'add +x + -y');
($x,$xs) = _add (123,321,'-','+');
is (_str($x,$xs), '+198', 'add -x + +y');

($x,$xs) = _add (321,123,'-','+');
is (_str($x,$xs), '-198', 'add -x + +y');
($x,$xs) = _add (321,123,'+','-');
is (_str($x,$xs), '+198', 'add +x + -y');

($x,$xs) = _add (10,1,'+','-');
is (_str($x,$xs), '+9', 'add 10 + -1');
($x,$xs) = _add (10,1,'-','+');
is (_str($x,$xs), '-9', 'add -10 + +1');
($x,$xs) = _add (1,10,'-','+');
is (_str($x,$xs), '+9', 'add -1 + 10');
($x,$xs) = _add (1,10,'+','-');
is (_str($x,$xs), '-9', 'add 1 + -10');

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

$a = Math::BigInt::Calc->_new("123");
$b = Math::BigInt::Calc->_new("321");
($x, $xs) = Math::BigFloat::_e_sub($b,$a,'+','+');
is (_str($x,$xs), '+198', 'sub two positive numbers');
is (_str($b,''), '198', 'a modified');

($x,$xs) = _sub (123,321,'+','-');
is (_str($x,$xs), '+444', 'sub +x + -y');
($x,$xs) = _sub (123,321,'-','+');
is (_str($x,$xs), '-444', 'sub -x + +y');

sub _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);
  is (Math::BigInt::Calc->_str($x), Math::BigInt::Calc->_str($aa),
    'param0 modified');
  ($x,$xs);
  }

sub _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);
  is (Math::BigInt::Calc->_str($x), Math::BigInt::Calc->_str($aa),
    'param0 modified');
  ($x,$xs);
  }

sub _str
  {
  my ($x,$s) = @_;

  $s . Math::BigInt::Calc->_str($x);
  }