From 50a07d515c5187cf73b720d82a96938cde29d82e Mon Sep 17 00:00:00 2001 From: Yair Slobodin Date: Wed, 11 Dec 2024 15:45:06 +0200 Subject: [PATCH 1/5] smaller ipblock --- pkg/netset/ipblock.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pkg/netset/ipblock.go b/pkg/netset/ipblock.go index 1dcb173..5af4b4f 100644 --- a/pkg/netset/ipblock.go +++ b/pkg/netset/ipblock.go @@ -152,6 +152,16 @@ 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 means the 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() +} + // Split returns a set of IPBlock objects, each with a single range of ips func (b *IPBlock) Split() []*IPBlock { intervals := b.ipRange.Intervals() From 968e5c67a76745f78e626e14712b6f6510818ce3 Mon Sep 17 00:00:00 2001 From: Yair Slobodin Date: Wed, 11 Dec 2024 15:50:09 +0200 Subject: [PATCH 2/5] test --- pkg/netset/ipblock_test.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/pkg/netset/ipblock_test.go b/pkg/netset/ipblock_test.go index 83af0ce..1e0a287 100644 --- a/pkg/netset/ipblock_test.go +++ b/pkg/netset/ipblock_test.go @@ -42,12 +42,12 @@ func TestOps(t *testing.T) { intersect2 := minus.Intersect(intersect) require.True(t, intersect2.IsEmpty()) - ipb3, err := ipb2.NextIP() + ipb3, err := ipb2.NextIP() // ipb3 = 1.2.3.5 ipb4, _ := netset.IPBlockFromCidrOrAddress("1.2.3.5") require.Nil(t, err) require.Equal(t, ipb3, ipb4) - ipb5, err := ipb3.PreviousIP() + ipb5, err := ipb3.PreviousIP() // ipb5 = 1.2.3.4 require.Nil(t, err) require.Equal(t, ipb2, ipb5) @@ -79,6 +79,9 @@ func TestOps(t *testing.T) { require.False(t, t2) require.Equal(t, ipb7, ipb7.FirstIPAddressObject()) + + require.True(t, ipb5.Smaller(ipb6)) + require.True(t, ipb1.Smaller(ipb2)) } func TestConversions(t *testing.T) { From 8fe73c9f3f6d98580f9b31f9d405b558e0031169 Mon Sep 17 00:00:00 2001 From: Yair Slobodin Date: Wed, 11 Dec 2024 16:33:06 +0200 Subject: [PATCH 3/5] typo --- pkg/netset/ipblock.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/netset/ipblock.go b/pkg/netset/ipblock.go index 24c5b80..c618863 100644 --- a/pkg/netset/ipblock.go +++ b/pkg/netset/ipblock.go @@ -153,7 +153,7 @@ func (b *IPBlock) IsSingleIPAddress() bool { } // Smaller returns true if this ipblock is smaller than other ipblock, and false o.w -// smaller means the this.firstIP is smaller than other.firstIP +// 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() { From 382dc7d208cf30e0d992d2ff42dc5bba0860a32b Mon Sep 17 00:00:00 2001 From: Yair Slobodin Date: Wed, 11 Dec 2024 17:14:46 +0200 Subject: [PATCH 4/5] compare --- pkg/netset/ipblock.go | 23 +++++++++++++++-------- pkg/netset/ipblock_test.go | 5 +++-- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/pkg/netset/ipblock.go b/pkg/netset/ipblock.go index c618863..520a9cb 100644 --- a/pkg/netset/ipblock.go +++ b/pkg/netset/ipblock.go @@ -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 thisother, 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 + } + } // Split returns a set of IPBlock objects, each with a single range of ips diff --git a/pkg/netset/ipblock_test.go b/pkg/netset/ipblock_test.go index 5ec8294..f8d9b21 100644 --- a/pkg/netset/ipblock_test.go +++ b/pkg/netset/ipblock_test.go @@ -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) { From 20c64610db568bcd9d26e971e65a64b0d9d7b743 Mon Sep 17 00:00:00 2001 From: Yair Slobodin Date: Wed, 11 Dec 2024 17:17:37 +0200 Subject: [PATCH 5/5] linter --- pkg/netset/ipblock.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/netset/ipblock.go b/pkg/netset/ipblock.go index 520a9cb..5950bce 100644 --- a/pkg/netset/ipblock.go +++ b/pkg/netset/ipblock.go @@ -166,7 +166,6 @@ func (b *IPBlock) Compare(other *IPBlock) int { default: return 0 } - } // Split returns a set of IPBlock objects, each with a single range of ips