Skip to content

Commit

Permalink
Merge pull request #115 from bschaatsbergen/f/add-host-address-count-…
Browse files Browse the repository at this point in the history
…test

chore: add tests for host address counts
  • Loading branch information
bschaatsbergen authored Jul 21, 2024
2 parents 929e667 + 7b240b2 commit 7a6a773
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions pkg/core/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,65 @@ func TestGetAddressCount(t *testing.T) {
}
}

func TestGetHostAddressCount(t *testing.T) {
IPv4CIDR, err := core.ParseCIDR("10.0.0.0/16")
if err != nil {
t.Log(err)
t.Fail()
}

IPv6CIDR, err := core.ParseCIDR("2001:db8:1234:1a00::/106")
if err != nil {
t.Log(err)
t.Fail()
}

largeIPv4PrefixCIDR, err := core.ParseCIDR("172.16.18.0/31")
if err != nil {
t.Log(err)
t.Fail()
}

largestIPv4PrefixCIDR, err := core.ParseCIDR("172.16.18.0/32")
if err != nil {
t.Log(err)
t.Fail()
}

tests := []struct {
name string
cidr *net.IPNet
expectedCount *big.Int
}{
{
name: "Return the count of all distinct host addresses in a common IPv4 CIDR",
cidr: IPv4CIDR,
expectedCount: big.NewInt(65534),
},
{
name: "Return the count of all distinct host addresses in a common IPv6 CIDR",
cidr: IPv6CIDR,
expectedCount: big.NewInt(4194302),
},
{
name: "Return the count of all distinct host addresses in an uncommon (large prefix) IPv4 CIDR",
cidr: largeIPv4PrefixCIDR,
expectedCount: big.NewInt(2),
},
{
name: "Return the count of all distinct host addresses in an uncommon (largest prefix) IPv4 CIDR",
cidr: largestIPv4PrefixCIDR,
expectedCount: big.NewInt(1),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
count := core.GetHostAddressCount(tt.cidr)
assert.Equal(t, tt.expectedCount, count, "Both address counts should be equal")
})
}
}

func TestOverlaps(t *testing.T) {
firstIPv4CIDR, err := core.ParseCIDR("10.0.0.0/16")
if err != nil {
Expand Down

0 comments on commit 7a6a773

Please sign in to comment.