diff --git a/IP.pm b/IP.pm index e0cd544..98922be 100644 --- a/IP.pm +++ b/IP.pm @@ -423,26 +423,23 @@ } sub Exclude { - my @low; - my @up; return ([],[]) if $_[0] == $_[1]; return ([$_[0]],[]) unless $_[1]->within($_[0]); + my @low; + my @up; my ($s1,$s2) = $_[0]->split($_[0]->masklen+1); while ($s1 != $_[1] && $s2 != $_[1]) { if ($_[1]->within($s1)) { - push @up, $s2; + unshift @up, $s2; ($s1,$s2) = $s1->split($s1->masklen+1); - } elsif ($_[1]->within($s2)) { + } else { push @low, $s1; ($s1,$s2) = $s2->split($s2->masklen+1); - } else { - croak("exclude error: $_[1] is not contained in $_[0]"); } } - push @up, $s2 if $s1 == $_[1]; push @low, $s1 if $s2 == $_[1]; - @up = reverse @up; - return (\@low, \@up); + unshift @up, $s2 if $s1 == $_[1]; + return (\@low,\@up); } sub hostenumref($) { diff --git a/t/v4-exclude.t b/t/v4-exclude.t index 45749a3..101e5a2 100644 --- a/t/v4-exclude.t +++ b/t/v4-exclude.t @@ -14,7 +14,7 @@ ['1.2.3.4/32','1.2.3.5/32',['1.2.3.4/32'],[]], ); -plan tests => 2 * scalar @r; +plan tests => 4 * scalar @r; SKIP: { foreach my $case (@r) { @@ -23,5 +23,9 @@ my $ex_up = [map { NetAddr::IP->new($_) } @{$case->[3]}]; is("@$up", "@$ex_up", "Upper half of Exclude($case->[0],$case->[1]) is @$ex_up"); is("@$low", "@$ex_low", "Lower half of Exclude($case->[0],$case->[1]) is @$ex_low"); + + ($low, $up) = NetAddr::IP->new($case->[0])->Exclude(NetAddr::IP->new($case->[1])); + is("@$up", "@$ex_up", "Upper half of ($case->[0])->Exclude($case->[1]) is @$ex_up"); + is("@$low", "@$ex_low", "Lower half of ($case->[0])->Exclude($case->[1]) is @$ex_low"); } } diff --git a/t/v6-exclude.t b/t/v6-exclude.t index fbbada1..53aadd6 100644 --- a/t/v6-exclude.t +++ b/t/v6-exclude.t @@ -1,5 +1,5 @@ use Test::More; -use NetAddr::IP qw(Exclude); +use NetAddr::IP qw(:lower Exclude); my @r = ( ['2001:db8::/32','2001:db8::/33', [], ['2001:db8:8000::/33']], @@ -11,7 +11,7 @@ ['2001:db8::/128','2001:db8:1::/128', ['2001:db8::/128'], []], ); -plan tests => 2 * scalar @r; +plan tests => 4 * scalar @r; SKIP: { foreach my $case (@r) { @@ -20,5 +20,9 @@ my $ex_up = [map { NetAddr::IP->new($_) } @{$case->[3]}]; is("@$up", "@$ex_up", "Upper half of Exclude($case->[0],$case->[1]) is @$ex_up"); is("@$low", "@$ex_low", "Lower half of Exclude($case->[0],$case->[1]) is @$ex_low"); + + ($low, $up) = NetAddr::IP->new($case->[0])->Exclude(NetAddr::IP->new($case->[1])); + is("@$up", "@$ex_up", "Upper half of ($case->[0])->Exclude($case->[1]) is @$ex_up"); + is("@$low", "@$ex_low", "Lower half of ($case->[0])->Exclude($case->[1]) is @$ex_low"); } }