From 77e1427845340838c4e7a02919b4dde0069e31cb Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Sun, 27 Nov 2016 09:19:52 +0100 Subject: [PATCH] remove unnecessary equals() --- common/net/address.go | 43 ------------------------------ common/net/address_test.go | 48 ---------------------------------- common/net/destination.go | 4 --- common/net/destination_test.go | 32 ----------------------- testing/assert/address.go | 4 +-- 5 files changed, 2 insertions(+), 129 deletions(-) diff --git a/common/net/address.go b/common/net/address.go index 6cf5bfbc0..150ed9535 100644 --- a/common/net/address.go +++ b/common/net/address.go @@ -49,7 +49,6 @@ type Address interface { Family() AddressFamily String() string // String representation of this Address - Equals(Address) bool } // ParseAddress parses a string into an Address. The return value will be an IPAddress when @@ -109,17 +108,6 @@ func (this ipv4Address) String() string { return this.IP().String() } -func (this ipv4Address) Equals(another Address) bool { - anotherIPv4, ok := another.(ipv4Address) - if !ok { - return false - } - return this[0] == anotherIPv4[0] && - this[1] == anotherIPv4[1] && - this[2] == anotherIPv4[2] && - this[3] == anotherIPv4[3] -} - type ipv6Address [16]byte func (addr ipv6Address) IP() net.IP { @@ -138,29 +126,6 @@ func (this ipv6Address) String() string { return "[" + this.IP().String() + "]" } -func (this ipv6Address) Equals(another Address) bool { - anotherIPv6, ok := another.(ipv6Address) - if !ok { - return false - } - return this[0] == anotherIPv6[0] && - this[1] == anotherIPv6[1] && - this[2] == anotherIPv6[2] && - this[3] == anotherIPv6[3] && - this[4] == anotherIPv6[4] && - this[5] == anotherIPv6[5] && - this[6] == anotherIPv6[6] && - this[7] == anotherIPv6[7] && - this[8] == anotherIPv6[8] && - this[9] == anotherIPv6[9] && - this[10] == anotherIPv6[10] && - this[11] == anotherIPv6[11] && - this[12] == anotherIPv6[12] && - this[13] == anotherIPv6[13] && - this[14] == anotherIPv6[14] && - this[15] == anotherIPv6[15] -} - type domainAddress string func (addr domainAddress) IP() net.IP { @@ -179,14 +144,6 @@ func (this domainAddress) String() string { return this.Domain() } -func (this domainAddress) Equals(another Address) bool { - anotherDomain, ok := another.(domainAddress) - if !ok { - return false - } - return this.Domain() == anotherDomain.Domain() -} - func (this *IPOrDomain) AsAddress() Address { if this == nil { return nil diff --git a/common/net/address_test.go b/common/net/address_test.go index 9d939c0db..2279e984b 100644 --- a/common/net/address_test.go +++ b/common/net/address_test.go @@ -72,51 +72,3 @@ func TestNetIPv4Address(t *testing.T) { assert.Address(addr).IsIPv4() assert.Address(addr).EqualsString("1.2.3.4") } - -func TestIPv4AddressEquals(t *testing.T) { - assert := assert.On(t) - - addr := v2net.IPAddress([]byte{1, 2, 3, 4}) - assert.Address(addr).NotEquals(nil) - - addr2 := v2net.IPAddress([]byte{1, 2, 3, 4}) - assert.Address(addr).Equals(addr2) - - addr3 := v2net.IPAddress([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6}) - assert.Address(addr).NotEquals(addr3) - - addr4 := v2net.IPAddress([]byte{1, 2, 3, 5}) - assert.Address(addr).NotEquals(addr4) -} - -func TestIPv6AddressEquals(t *testing.T) { - assert := assert.On(t) - - addr := v2net.IPAddress([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6}) - assert.Bool(addr.Equals(nil)).IsFalse() - - addr2 := v2net.IPAddress([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6}) - assert.Bool(addr.Equals(addr2)).IsTrue() - - addr3 := v2net.IPAddress([]byte{1, 2, 3, 4}) - assert.Bool(addr.Equals(addr3)).IsFalse() - - addr4 := v2net.IPAddress([]byte{1, 3, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6}) - assert.Bool(addr.Equals(addr4)).IsFalse() -} - -func TestDomainAddressEquals(t *testing.T) { - assert := assert.On(t) - - addr := v2net.DomainAddress("v2ray.com") - assert.Bool(addr.Equals(nil)).IsFalse() - - addr2 := v2net.DomainAddress("v2ray.com") - assert.Bool(addr.Equals(addr2)).IsTrue() - - addr3 := v2net.DomainAddress("www.v2ray.com") - assert.Bool(addr.Equals(addr3)).IsFalse() - - addr4 := v2net.IPAddress([]byte{1, 3, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6}) - assert.Bool(addr.Equals(addr4)).IsFalse() -} diff --git a/common/net/destination.go b/common/net/destination.go index bc8a80a69..450a27f81 100644 --- a/common/net/destination.go +++ b/common/net/destination.go @@ -48,10 +48,6 @@ func (this Destination) String() string { return this.Network.UrlPrefix() + ":" + this.NetAddr() } -func (this Destination) Equals(another Destination) bool { - return this.Network == another.Network && this.Port == another.Port && this.Address.Equals(another.Address) -} - func (this *Endpoint) AsDestination() Destination { return Destination{ Network: this.Network, diff --git a/common/net/destination_test.go b/common/net/destination_test.go index e33db899d..b13fd18db 100644 --- a/common/net/destination_test.go +++ b/common/net/destination_test.go @@ -24,35 +24,3 @@ func TestUDPDestination(t *testing.T) { assert.Destination(dest).IsUDP() assert.Destination(dest).EqualsString("udp:[2001:4860:4860::8888]:53") } - -func TestTCPDestinationEquals(t *testing.T) { - assert := assert.On(t) - - dest := v2net.TCPDestination(v2net.IPAddress([]byte{1, 2, 3, 4}), 80) - assert.Bool(dest.Equals(v2net.Destination{})).IsFalse() - - dest2 := v2net.TCPDestination(v2net.IPAddress([]byte{1, 2, 3, 4}), 80) - assert.Bool(dest.Equals(dest2)).IsTrue() - - dest3 := v2net.UDPDestination(v2net.IPAddress([]byte{1, 2, 3, 4}), 80) - assert.Bool(dest.Equals(dest3)).IsFalse() - - dest4 := v2net.TCPDestination(v2net.DomainAddress("v2ray.com"), 80) - assert.Bool(dest.Equals(dest4)).IsFalse() -} - -func TestUDPDestinationEquals(t *testing.T) { - assert := assert.On(t) - - dest := v2net.UDPDestination(v2net.IPAddress([]byte{1, 2, 3, 4}), 80) - assert.Bool(dest.Equals(v2net.Destination{})).IsFalse() - - dest2 := v2net.UDPDestination(v2net.IPAddress([]byte{1, 2, 3, 4}), 80) - assert.Bool(dest.Equals(dest2)).IsTrue() - - dest3 := v2net.TCPDestination(v2net.IPAddress([]byte{1, 2, 3, 4}), 80) - assert.Bool(dest.Equals(dest3)).IsFalse() - - dest4 := v2net.UDPDestination(v2net.DomainAddress("v2ray.com"), 80) - assert.Bool(dest.Equals(dest4)).IsFalse() -} diff --git a/testing/assert/address.go b/testing/assert/address.go index 28d2544b6..7eb66ea19 100644 --- a/testing/assert/address.go +++ b/testing/assert/address.go @@ -20,13 +20,13 @@ type AddressSubject struct { } func (subject *AddressSubject) NotEquals(another v2net.Address) { - if subject.value.Equals(another) { + if subject.value == another { subject.Fail("not equals to", another.String()) } } func (subject *AddressSubject) Equals(another v2net.Address) { - if !subject.value.Equals(another) { + if subject.value != another { subject.Fail("equals to", another.String()) } }