use Test::More;
use NetAddr::IP qw(Exclude Subtract);
my @r = (
['10.0.0.0/8','10.0.0.0/9'],
['10.0.0.0/8','10.0.0.0/8'],
['10.0.0.0/8','10.128.0.0/9'],
['10.0.0.0/8','10.128.0.0/10'],
['10.0.0.0/8','10.0.0.0/32'],
['192.168.0.0/24','192.168.0.255/32'],
['192.168.0.0/23','192.168.0.127/32'],
['1.2.3.4/32','1.2.3.4/32'],
['0.0.0.0/0','0.0.0.0/32'],
);
plan tests => 1 + 19 * scalar @r;
SKIP: {
my $res = Subtract([],[]);
is("@$res", "", "Subtract([],[]) returns an empty list");
foreach my $case (@r) {
my $whole = [NetAddr::IP->new($case->[0])];
my $part1 = [NetAddr::IP->new($case->[1])];
my ($low,$up) = Exclude($whole->[0],$part1->[0]);
my $part2 = [@{$low},@{$up}];
my $res = [];
$res = Subtract($whole,$part1);
is("@$res", "@$part2", "Subtract(@$whole,@$part1) returns @$part2");
$res = $whole->[0]->Subtract($part1);
is("@$res", "@$part2", "($whole->[0])->Subtract(@$part1) returns @$part2");
$res = Subtract($whole,$part2);
is("@$res", "@$part1", "Subtract(@$whole,@$part2) returns @$part1");
$res = $whole->[0]->Subtract($part2);
is("@$res", "@$part1", "($whole->[0])->Subtract(@$part2) returns @$part1");
$res = Subtract($part1,$part2);
is("@$res", "@$part1", "Subtract(@$part1,@$part2) returns @$part1");
$res = $part1->[0]->Subtract($part2);
is("@$res", "@$part1", "($part1->[0])->Subtract(@$part2) returns @$part1");
$res = Subtract($part2,$part1);
is("@$res", "@$part2", "Subtract(@$part2,@$part1) returns @$part2");
$res = Subtract(Subtract($whole,$part1),$part2);
is("@$res", "", "Subtract(Subtract(@$whole,@$part1),$part2) returns an empty list");
$res = Subtract($whole->[0]->Subtract($part1),$part2);
is("@$res", "", "Subtract(($whole->[0])->Subtract(@$part1),$part2) returns an empty list");
$res = Subtract(Subtract($whole,$part2),$part1);
is("@$res", "", "Subtract(Subtract(@$whole,@$part2),$part1) returns an empty list");
$res = Subtract($whole->[0]->Subtract($part2),$part1);
is("@$res", "", "Subtract(($whole->[0])->Subtract(@$part2),$part1) returns an empty list");
$res = Subtract($whole,$whole);
is("@$res", "", "Subtract(@$whole,@$whole) returns an empty list");
$res = $whole->[0]->Subtract($whole);
is("@$res", "", "($whole->[0])->Subtract(@$whole) returns an empty list");
$res = Subtract($part1,$part1);
is("@$res", "", "Subtract(@$part1,@$part1) returns an empty list");
$res = $part1->[0]->Subtract($part1);
is("@$res", "", "($part1->[0])->Subract(@$part1) returns an empty list");
$res = Subtract($part2,$part2);
is("@$res", "", "Subtract(@$part2,@$part2) returns an empty list");
$res = Subtract($part1,$whole);
is("@$res", "", "Subtract(@$part1,@$whole) returns an empty list");
$res = $part1->[0]->Subtract($whole);
is("@$res", "", "($part1->[0])->Subtract(@$whole) returns an empty list");
$res = Subtract($part2,$whole);
is("@$res", "", "Subtract(@$part2,@$whole) returns an empty list");
}
}