mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2025-09-13 07:02:16 +00:00
good good study, day day up!
This commit is contained in:
127
vendor/gopl.io/ch13/equal/equal.go
generated
vendored
Normal file
127
vendor/gopl.io/ch13/equal/equal.go
generated
vendored
Normal file
@@ -0,0 +1,127 @@
|
||||
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
|
||||
// See page 359.
|
||||
|
||||
// Package equal provides a deep equivalence relation for arbitrary values.
|
||||
package equal
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
//!+
|
||||
func equal(x, y reflect.Value, seen map[comparison]bool) bool {
|
||||
if !x.IsValid() || !y.IsValid() {
|
||||
return x.IsValid() == y.IsValid()
|
||||
}
|
||||
if x.Type() != y.Type() {
|
||||
return false
|
||||
}
|
||||
|
||||
// ...cycle check omitted (shown later)...
|
||||
|
||||
//!-
|
||||
//!+cyclecheck
|
||||
// cycle check
|
||||
if x.CanAddr() && y.CanAddr() {
|
||||
xptr := unsafe.Pointer(x.UnsafeAddr())
|
||||
yptr := unsafe.Pointer(y.UnsafeAddr())
|
||||
if xptr == yptr {
|
||||
return true // identical references
|
||||
}
|
||||
c := comparison{xptr, yptr, x.Type()}
|
||||
if seen[c] {
|
||||
return true // already seen
|
||||
}
|
||||
seen[c] = true
|
||||
}
|
||||
//!-cyclecheck
|
||||
//!+
|
||||
switch x.Kind() {
|
||||
case reflect.Bool:
|
||||
return x.Bool() == y.Bool()
|
||||
|
||||
case reflect.String:
|
||||
return x.String() == y.String()
|
||||
|
||||
// ...numeric cases omitted for brevity...
|
||||
|
||||
//!-
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32,
|
||||
reflect.Int64:
|
||||
return x.Int() == y.Int()
|
||||
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32,
|
||||
reflect.Uint64, reflect.Uintptr:
|
||||
return x.Uint() == y.Uint()
|
||||
|
||||
case reflect.Float32, reflect.Float64:
|
||||
return x.Float() == y.Float()
|
||||
|
||||
case reflect.Complex64, reflect.Complex128:
|
||||
return x.Complex() == y.Complex()
|
||||
//!+
|
||||
case reflect.Chan, reflect.UnsafePointer, reflect.Func:
|
||||
return x.Pointer() == y.Pointer()
|
||||
|
||||
case reflect.Ptr, reflect.Interface:
|
||||
return equal(x.Elem(), y.Elem(), seen)
|
||||
|
||||
case reflect.Array, reflect.Slice:
|
||||
if x.Len() != y.Len() {
|
||||
return false
|
||||
}
|
||||
for i := 0; i < x.Len(); i++ {
|
||||
if !equal(x.Index(i), y.Index(i), seen) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
|
||||
// ...struct and map cases omitted for brevity...
|
||||
//!-
|
||||
case reflect.Struct:
|
||||
for i, n := 0, x.NumField(); i < n; i++ {
|
||||
if !equal(x.Field(i), y.Field(i), seen) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
|
||||
case reflect.Map:
|
||||
if x.Len() != y.Len() {
|
||||
return false
|
||||
}
|
||||
for _, k := range x.MapKeys() {
|
||||
if !equal(x.MapIndex(k), y.MapIndex(k), seen) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
//!+
|
||||
}
|
||||
panic("unreachable")
|
||||
}
|
||||
|
||||
//!-
|
||||
|
||||
//!+comparison
|
||||
// Equal reports whether x and y are deeply equal.
|
||||
//!-comparison
|
||||
//
|
||||
// Map keys are always compared with ==, not deeply.
|
||||
// (This matters for keys containing pointers or interfaces.)
|
||||
//!+comparison
|
||||
func Equal(x, y interface{}) bool {
|
||||
seen := make(map[comparison]bool)
|
||||
return equal(reflect.ValueOf(x), reflect.ValueOf(y), seen)
|
||||
}
|
||||
|
||||
type comparison struct {
|
||||
x, y unsafe.Pointer
|
||||
t reflect.Type
|
||||
}
|
||||
|
||||
//!-comparison
|
133
vendor/gopl.io/ch13/equal/equal_test.go
generated
vendored
Normal file
133
vendor/gopl.io/ch13/equal/equal_test.go
generated
vendored
Normal file
@@ -0,0 +1,133 @@
|
||||
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
|
||||
package equal
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestEqual(t *testing.T) {
|
||||
one, oneAgain, two := 1, 1, 2
|
||||
|
||||
type CyclePtr *CyclePtr
|
||||
var cyclePtr1, cyclePtr2 CyclePtr
|
||||
cyclePtr1 = &cyclePtr1
|
||||
cyclePtr2 = &cyclePtr2
|
||||
|
||||
type CycleSlice []CycleSlice
|
||||
var cycleSlice CycleSlice
|
||||
cycleSlice = append(cycleSlice, cycleSlice)
|
||||
|
||||
ch1, ch2 := make(chan int), make(chan int)
|
||||
var ch1ro <-chan int = ch1
|
||||
|
||||
type mystring string
|
||||
|
||||
var iface1, iface1Again, iface2 interface{} = &one, &oneAgain, &two
|
||||
|
||||
for _, test := range []struct {
|
||||
x, y interface{}
|
||||
want bool
|
||||
}{
|
||||
// basic types
|
||||
{1, 1, true},
|
||||
{1, 2, false}, // different values
|
||||
{1, 1.0, false}, // different types
|
||||
{"foo", "foo", true},
|
||||
{"foo", "bar", false},
|
||||
{mystring("foo"), "foo", false}, // different types
|
||||
// slices
|
||||
{[]string{"foo"}, []string{"foo"}, true},
|
||||
{[]string{"foo"}, []string{"bar"}, false},
|
||||
{[]string{}, []string(nil), true},
|
||||
// slice cycles
|
||||
{cycleSlice, cycleSlice, true},
|
||||
// maps
|
||||
{
|
||||
map[string][]int{"foo": {1, 2, 3}},
|
||||
map[string][]int{"foo": {1, 2, 3}},
|
||||
true,
|
||||
},
|
||||
{
|
||||
map[string][]int{"foo": {1, 2, 3}},
|
||||
map[string][]int{"foo": {1, 2, 3, 4}},
|
||||
false,
|
||||
},
|
||||
{
|
||||
map[string][]int{},
|
||||
map[string][]int(nil),
|
||||
true,
|
||||
},
|
||||
// pointers
|
||||
{&one, &one, true},
|
||||
{&one, &two, false},
|
||||
{&one, &oneAgain, true},
|
||||
{new(bytes.Buffer), new(bytes.Buffer), true},
|
||||
// pointer cycles
|
||||
{cyclePtr1, cyclePtr1, true},
|
||||
{cyclePtr2, cyclePtr2, true},
|
||||
{cyclePtr1, cyclePtr2, true}, // they're deeply equal
|
||||
// functions
|
||||
{(func())(nil), (func())(nil), true},
|
||||
{(func())(nil), func() {}, false},
|
||||
{func() {}, func() {}, false},
|
||||
// arrays
|
||||
{[...]int{1, 2, 3}, [...]int{1, 2, 3}, true},
|
||||
{[...]int{1, 2, 3}, [...]int{1, 2, 4}, false},
|
||||
// channels
|
||||
{ch1, ch1, true},
|
||||
{ch1, ch2, false},
|
||||
{ch1ro, ch1, false}, // NOTE: not equal
|
||||
// interfaces
|
||||
{&iface1, &iface1, true},
|
||||
{&iface1, &iface2, false},
|
||||
{&iface1Again, &iface1, true},
|
||||
} {
|
||||
if Equal(test.x, test.y) != test.want {
|
||||
t.Errorf("Equal(%v, %v) = %t",
|
||||
test.x, test.y, !test.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func Example_equal() {
|
||||
//!+
|
||||
fmt.Println(Equal([]int{1, 2, 3}, []int{1, 2, 3})) // "true"
|
||||
fmt.Println(Equal([]string{"foo"}, []string{"bar"})) // "false"
|
||||
fmt.Println(Equal([]string(nil), []string{})) // "true"
|
||||
fmt.Println(Equal(map[string]int(nil), map[string]int{})) // "true"
|
||||
//!-
|
||||
|
||||
// Output:
|
||||
// true
|
||||
// false
|
||||
// true
|
||||
// true
|
||||
}
|
||||
|
||||
func Example_equalCycle() {
|
||||
//!+cycle
|
||||
// Circular linked lists a -> b -> a and c -> c.
|
||||
type link struct {
|
||||
value string
|
||||
tail *link
|
||||
}
|
||||
a, b, c := &link{value: "a"}, &link{value: "b"}, &link{value: "c"}
|
||||
a.tail, b.tail, c.tail = b, a, c
|
||||
fmt.Println(Equal(a, a)) // "true"
|
||||
fmt.Println(Equal(b, b)) // "true"
|
||||
fmt.Println(Equal(c, c)) // "true"
|
||||
fmt.Println(Equal(a, b)) // "false"
|
||||
fmt.Println(Equal(a, c)) // "false"
|
||||
//!-cycle
|
||||
|
||||
// Output:
|
||||
// true
|
||||
// true
|
||||
// true
|
||||
// false
|
||||
// false
|
||||
}
|
Reference in New Issue
Block a user