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
|
# Before `make install' is performed this script should be runnable with
# `make test'. After `make install' it should work as `perl 13-divide.t'
use 5.010001;
use Test::More tests => 5;
use Math::Utils qw(:polynomial);
use strict;
use warnings;
#
# returns 0 (equal) or 1 (not equal). There's no -1 value, unlike other cmp functions.
#
sub polycmp
{
my($p_ref1, $p_ref2) = @_;
my @polynomial1 = @$p_ref1;
my @polynomial2 = @$p_ref2;
return 1 if (scalar @polynomial1 != scalar @polynomial2);
foreach my $c1 (@polynomial1)
{
my $c2 = shift @polynomial2;
return 1 if ($c1 != $c2);
}
return 0;
}
#
# Groups of four: numerator, divisor, quotient, remainder.
#
my @case0 = (
[
[90, -53, 7, -70, 49, -7, -20, 4],
[9, -8, 4],
[10, 3, -1, -10, -3, 1],
[0, 0]
],
[
[1, 0, 0, 0, 34, 0, 0, 0, 1],
[1, 4, 8, 4, 1],
[1, -4, 8, -4, 1],
[0, 0, 0, 0]
],
[
[1, 6, 15, 32, 58, 88, 116, 160, 165, 138, 133],
[1, 3, 5, 7, 11, 13, 17, 19],
[1, 3, 1, 7],
[0, 0, 0, 0, 0, 0, 0]
],
[
[4, 12, 9, 3],
[1, 3, 3, 1],
[3],
[1, 3, 0]
],
[
[4, 13, 4, -9, 6],
[1, 2],
[4, 5, -6, 3],
[0]
]
);
foreach my $cref (@case0)
{
my($p_ref, $d_ref, $q_ref, $r_ref) = @$cref;
my($q, $r) = pl_div($p_ref, $d_ref);
my @polynomial = @$p_ref;
my @divisor = @$d_ref;
my @quotient = @$q;
my @remainder = @$r;
ok((polycmp($q_ref, $q) == 0 and polycmp($r_ref, $r) == 0),
" [ " . join(", ", @polynomial) . " ] /" .
" [ " . join(", ", @divisor) . " ] returns\n" .
" [ " . join(", ", @quotient) . " ] and" .
" [ " . join(", ", @remainder) . " ].\n"
);
}
1;
|