1
0
mirror of https://github.com/golang/go synced 2024-11-20 05:44:44 -07:00

net: make use of reflect.DeepEqual in IP manipulation tests

Also applies camel style to test case holders.

R=golang-dev, r
CC=golang-dev
https://golang.org/cl/7884043
This commit is contained in:
Mikio Hara 2013-03-20 01:07:18 +09:00
parent 5ccb769112
commit 3560f3b9b8

View File

@ -5,23 +5,12 @@
package net
import (
"bytes"
"reflect"
"runtime"
"testing"
)
func isEqual(a, b []byte) bool {
if a == nil && b == nil {
return true
}
if a == nil || b == nil {
return false
}
return bytes.Equal(a, b)
}
var parseiptests = []struct {
var parseIPTests = []struct {
in string
out IP
}{
@ -37,18 +26,18 @@ var parseiptests = []struct {
}
func TestParseIP(t *testing.T) {
for _, tt := range parseiptests {
if out := ParseIP(tt.in); !isEqual(out, tt.out) {
for _, tt := range parseIPTests {
if out := ParseIP(tt.in); !reflect.DeepEqual(out, tt.out) {
t.Errorf("ParseIP(%q) = %v, want %v", tt.in, out, tt.out)
}
}
}
var ipstringtests = []struct {
var ipStringTests = []struct {
in IP
out string
out string // see RFC 5952
}{
// cf. RFC 5952 (A Recommendation for IPv6 Address Text Representation)
{IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0x1, 0x23, 0, 0x12, 0, 0x1}, "2001:db8::123:12:1"},
{IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x1}, "2001:db8::1"},
{IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0x1, 0, 0, 0, 0x1, 0, 0, 0, 0x1}, "2001:db8:0:1:0:1:0:1"},
@ -61,14 +50,14 @@ var ipstringtests = []struct {
}
func TestIPString(t *testing.T) {
for _, tt := range ipstringtests {
for _, tt := range ipStringTests {
if out := tt.in.String(); out != tt.out {
t.Errorf("IP.String(%v) = %q, want %q", tt.in, out, tt.out)
}
}
}
var ipmasktests = []struct {
var ipMaskTests = []struct {
in IP
mask IPMask
out IP
@ -82,14 +71,14 @@ var ipmasktests = []struct {
}
func TestIPMask(t *testing.T) {
for _, tt := range ipmasktests {
for _, tt := range ipMaskTests {
if out := tt.in.Mask(tt.mask); out == nil || !tt.out.Equal(out) {
t.Errorf("IP(%v).Mask(%v) = %v, want %v", tt.in, tt.mask, out, tt.out)
}
}
}
var ipmaskstringtests = []struct {
var ipMaskStringTests = []struct {
in IPMask
out string
}{
@ -101,14 +90,14 @@ var ipmaskstringtests = []struct {
}
func TestIPMaskString(t *testing.T) {
for _, tt := range ipmaskstringtests {
for _, tt := range ipMaskStringTests {
if out := tt.in.String(); out != tt.out {
t.Errorf("IPMask.String(%v) = %q, want %q", tt.in, out, tt.out)
}
}
}
var parsecidrtests = []struct {
var parseCIDRTests = []struct {
in string
ip IP
net *IPNet
@ -138,18 +127,18 @@ var parsecidrtests = []struct {
}
func TestParseCIDR(t *testing.T) {
for _, tt := range parsecidrtests {
for _, tt := range parseCIDRTests {
ip, net, err := ParseCIDR(tt.in)
if !reflect.DeepEqual(err, tt.err) {
t.Errorf("ParseCIDR(%q) = %v, %v; want %v, %v", tt.in, ip, net, tt.ip, tt.net)
}
if err == nil && (!tt.ip.Equal(ip) || !tt.net.IP.Equal(net.IP) || !isEqual(net.Mask, tt.net.Mask)) {
t.Errorf("ParseCIDR(%q) = %v, {%v, %v}; want %v {%v, %v}", tt.in, ip, net.IP, net.Mask, tt.ip, tt.net.IP, tt.net.Mask)
if err == nil && (!tt.ip.Equal(ip) || !tt.net.IP.Equal(net.IP) || !reflect.DeepEqual(net.Mask, tt.net.Mask)) {
t.Errorf("ParseCIDR(%q) = %v, {%v, %v}; want %v, {%v, %v}", tt.in, ip, net.IP, net.Mask, tt.ip, tt.net.IP, tt.net.Mask)
}
}
}
var ipnetcontainstests = []struct {
var ipNetContainsTests = []struct {
ip IP
net *IPNet
ok bool
@ -165,14 +154,14 @@ var ipnetcontainstests = []struct {
}
func TestIPNetContains(t *testing.T) {
for _, tt := range ipnetcontainstests {
for _, tt := range ipNetContainsTests {
if ok := tt.net.Contains(tt.ip); ok != tt.ok {
t.Errorf("IPNet(%v).Contains(%v) = %v, want %v", tt.net, tt.ip, ok, tt.ok)
}
}
}
var ipnetstringtests = []struct {
var ipNetStringTests = []struct {
in *IPNet
out string
}{
@ -183,14 +172,14 @@ var ipnetstringtests = []struct {
}
func TestIPNetString(t *testing.T) {
for _, tt := range ipnetstringtests {
for _, tt := range ipNetStringTests {
if out := tt.in.String(); out != tt.out {
t.Errorf("IPNet.String(%v) = %q, want %q", tt.in, out, tt.out)
}
}
}
var cidrmasktests = []struct {
var cidrMaskTests = []struct {
ones int
bits int
out IPMask
@ -210,8 +199,8 @@ var cidrmasktests = []struct {
}
func TestCIDRMask(t *testing.T) {
for _, tt := range cidrmasktests {
if out := CIDRMask(tt.ones, tt.bits); !isEqual(out, tt.out) {
for _, tt := range cidrMaskTests {
if out := CIDRMask(tt.ones, tt.bits); !reflect.DeepEqual(out, tt.out) {
t.Errorf("CIDRMask(%v, %v) = %v, want %v", tt.ones, tt.bits, out, tt.out)
}
}
@ -229,7 +218,7 @@ var (
v4maskzero = IPMask{0, 0, 0, 0}
)
var networknumberandmasktests = []struct {
var networkNumberAndMaskTests = []struct {
in IPNet
out IPNet
}{
@ -251,19 +240,19 @@ var networknumberandmasktests = []struct {
}
func TestNetworkNumberAndMask(t *testing.T) {
for _, tt := range networknumberandmasktests {
for _, tt := range networkNumberAndMaskTests {
ip, m := networkNumberAndMask(&tt.in)
out := &IPNet{IP: ip, Mask: m}
if !reflect.DeepEqual(&tt.out, out) {
t.Errorf("networkNumberAndMask(%v) = %v; want %v", tt.in, out, &tt.out)
t.Errorf("networkNumberAndMask(%v) = %v, want %v", tt.in, out, &tt.out)
}
}
}
var splitjointests = []struct {
Host string
Port string
Join string
var splitJoinTests = []struct {
host string
port string
join string
}{
{"www.google.com", "80", "www.google.com:80"},
{"127.0.0.1", "1234", "127.0.0.1:1234"},
@ -274,9 +263,9 @@ var splitjointests = []struct {
{"www.google.com", "", "www.google.com:"}, // Go 1.0 behaviour
}
var splitfailuretests = []struct {
HostPort string
Err string
var splitFailureTests = []struct {
hostPort string
err string
}{
{"www.google.com", "missing port in address"},
{"127.0.0.1", "missing port in address"},
@ -294,32 +283,32 @@ var splitfailuretests = []struct {
}
func TestSplitHostPort(t *testing.T) {
for _, tt := range splitjointests {
if host, port, err := SplitHostPort(tt.Join); host != tt.Host || port != tt.Port || err != nil {
t.Errorf("SplitHostPort(%q) = %q, %q, %v; want %q, %q, nil", tt.Join, host, port, err, tt.Host, tt.Port)
for _, tt := range splitJoinTests {
if host, port, err := SplitHostPort(tt.join); host != tt.host || port != tt.port || err != nil {
t.Errorf("SplitHostPort(%q) = %q, %q, %v; want %q, %q, nil", tt.join, host, port, err, tt.host, tt.port)
}
}
for _, tt := range splitfailuretests {
if _, _, err := SplitHostPort(tt.HostPort); err == nil {
t.Errorf("SplitHostPort(%q) should have failed", tt.HostPort)
for _, tt := range splitFailureTests {
if _, _, err := SplitHostPort(tt.hostPort); err == nil {
t.Errorf("SplitHostPort(%q) should have failed", tt.hostPort)
} else {
e := err.(*AddrError)
if e.Err != tt.Err {
t.Errorf("SplitHostPort(%q) = _, _, %q; want %q", tt.HostPort, e.Err, tt.Err)
if e.Err != tt.err {
t.Errorf("SplitHostPort(%q) = _, _, %q; want %q", tt.hostPort, e.Err, tt.err)
}
}
}
}
func TestJoinHostPort(t *testing.T) {
for _, tt := range splitjointests {
if join := JoinHostPort(tt.Host, tt.Port); join != tt.Join {
t.Errorf("JoinHostPort(%q, %q) = %q; want %q", tt.Host, tt.Port, join, tt.Join)
for _, tt := range splitJoinTests {
if join := JoinHostPort(tt.host, tt.port); join != tt.join {
t.Errorf("JoinHostPort(%q, %q) = %q; want %q", tt.host, tt.port, join, tt.join)
}
}
}
var ipaftests = []struct {
var ipAddrFamilyTests = []struct {
in IP
af4 bool
af6 bool
@ -342,7 +331,7 @@ var ipaftests = []struct {
}
func TestIPAddrFamily(t *testing.T) {
for _, tt := range ipaftests {
for _, tt := range ipAddrFamilyTests {
if af := tt.in.To4() != nil; af != tt.af4 {
t.Errorf("verifying IPv4 address family for %q = %v, want %v", tt.in, af, tt.af4)
}
@ -352,7 +341,7 @@ func TestIPAddrFamily(t *testing.T) {
}
}
var ipscopetests = []struct {
var ipAddrScopeTests = []struct {
scope func(IP) bool
in IP
ok bool
@ -393,7 +382,7 @@ func name(f interface{}) string {
}
func TestIPAddrScope(t *testing.T) {
for _, tt := range ipscopetests {
for _, tt := range ipAddrScopeTests {
if ok := tt.scope(tt.in); ok != tt.ok {
t.Errorf("%s(%q) = %v, want %v", name(tt.scope), tt.in, ok, tt.ok)
}