47 lines
875 B
Go
47 lines
875 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
"testing"
|
|
)
|
|
|
|
func TestParseRange(t *testing.T) {
|
|
expected := []int{0, 1, 2, 6}
|
|
actual := parseRange("0-2,-5,6,-7-7,1, 11", 10)
|
|
if fmt.Sprint(expected) != fmt.Sprint(actual) {
|
|
t.Errorf("unexpected %#v\n", actual)
|
|
}
|
|
}
|
|
|
|
func TestParseRangeEmpty(t *testing.T) {
|
|
expectedLen := 0
|
|
actual := parseRange("", 10)
|
|
if expectedLen != len(actual) {
|
|
t.Errorf("unexpected %#v\n", actual)
|
|
}
|
|
}
|
|
|
|
func TestParseIP(t *testing.T) {
|
|
var ip net.IP
|
|
if err := ip.UnmarshalText([]byte("127.0.0.1")); err != nil {
|
|
t.Error(err)
|
|
}
|
|
if ip.To4() == nil {
|
|
t.Error("Failed to parse ipv4")
|
|
}
|
|
if ip.To16() == nil {
|
|
t.Error("Failed to parse as ipv6")
|
|
}
|
|
|
|
if err := ip.UnmarshalText([]byte("::1")); err != nil {
|
|
t.Error(err)
|
|
}
|
|
if ip.To16() == nil {
|
|
t.Error("Failed to parse ipv6")
|
|
}
|
|
if ip.To4() != nil {
|
|
t.Error("ipv6 parsed as ipv4")
|
|
}
|
|
}
|