package ipfunc

import (
	"github.com/stretchr/testify/assert"
	"testing"
)

func TestNewIPSubnet(t *testing.T) {
	ipnet, err := NewIPSubnet("10.0.0.0/24")
	assert.Nil(t, err, "Should be able to create IPSubnet")

	assert.Equal(t, 254, len(ipnet.freeIPs))
}

func TestRemoveIPFromSubnet(t *testing.T) {
	ipnet, err := NewIPSubnet("10.0.0.0/24")
	assert.Nil(t, err, "Should be able to create IPSubnet")

	ipnet.Remove("10.0.0.43")
	assert.Equal(t, 253, len(ipnet.freeIPs))

	ipnet.Remove("10.0.0.47")

	assert.Equal(t, 252, len(ipnet.freeIPs))
}

func TestGetNextFreeIP(t *testing.T) {
	ipnet, err := NewIPSubnet("10.0.0.0/28")
	assert.Nil(t, err, "Should be able to create IPSubnet")

	ip, err := ipnet.GetNextFreeIP()
	assert.Nil(t, err, "schould not return an error")

	assert.Equalf(t, "10.0.0.1", ip, "should return next ip")

	ip, err = ipnet.GetNextFreeIP()
	assert.Nil(t, err, "schould not return an error")

	assert.Equalf(t, "10.0.0.2", ip, "should return next ip")

	assert.Equal(t, 12, len(ipnet.freeIPs))
}