Skip to content

Commit

Permalink
compare
Browse files Browse the repository at this point in the history
  • Loading branch information
YairSlobodin1 committed Dec 11, 2024
1 parent 8fe73c9 commit 382dc7d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
23 changes: 15 additions & 8 deletions pkg/netset/ipblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,21 @@ func (b *IPBlock) IsSingleIPAddress() bool {
return b.ipRange.IsSingleNumber()
}

// Smaller returns true if this ipblock is smaller than other ipblock, and false o.w
// smaller == this.firstIP is smaller than other.firstIP
// if this.firstIP is equal to other.firstIP, decide by the lastIP
func (b *IPBlock) Smaller(other *IPBlock) bool {
if b.ipRange.Min() == other.ipRange.Min() {
return b.ipRange.Max() < other.ipRange.Max()
}
return b.ipRange.Min() < other.ipRange.Min()
// Compare returns -1 if this<other, 1 if this>other, 0 o.w.
func (b *IPBlock) Compare(other *IPBlock) int {
switch {
case b.ipRange.Min() < other.ipRange.Min():
return -1
case b.ipRange.Min() > other.ipRange.Min():
return 1
case b.ipRange.Max() < other.ipRange.Max():
return -1
case b.ipRange.Max() > other.ipRange.Max():
return 1
default:
return 0
}

}

Check failure on line 170 in pkg/netset/ipblock.go

View workflow job for this annotation

GitHub Actions / golangci-lint

unnecessary trailing newline (whitespace)

// Split returns a set of IPBlock objects, each with a single range of ips
Expand Down
5 changes: 3 additions & 2 deletions pkg/netset/ipblock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ func TestOps(t *testing.T) {

require.Equal(t, ipb7, ipb7.FirstIPAddressObject())

require.True(t, ipb5.Smaller(ipb6))
require.True(t, ipb1.Smaller(ipb2))
require.Equal(t, ipb5.Compare(ipb6), -1)
require.Equal(t, ipb2.Compare(ipb1), 1)
require.Equal(t, ipb3.Compare(ipb4), 0)
}

func TestConversions(t *testing.T) {
Expand Down

0 comments on commit 382dc7d

Please sign in to comment.