github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/third_party/forked/golang/reflect/deep_equal_test.go (about)

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package reflect
     6  
     7  import (
     8  	"testing"
     9  )
    10  
    11  func TestEqualities(t *testing.T) {
    12  	e := Equalities{}
    13  	type Bar struct {
    14  		X int
    15  	}
    16  	type Baz struct {
    17  		Y Bar
    18  	}
    19  	type Zap struct {
    20  		A []int
    21  		B map[string][]int
    22  	}
    23  	err := e.AddFuncs(
    24  		func(a, b int) bool {
    25  			return a+1 == b
    26  		},
    27  		func(a, b Bar) bool {
    28  			return a.X*10 == b.X
    29  		},
    30  	)
    31  	if err != nil {
    32  		t.Fatalf("Unexpected: %v", err)
    33  	}
    34  
    35  	type Foo struct {
    36  		X int
    37  	}
    38  
    39  	type Case struct {
    40  		a, b  interface{}
    41  		equal bool
    42  	}
    43  
    44  	table := []Case{
    45  		{1, 2, true},
    46  		{2, 1, false},
    47  		{"foo", "fo", false},
    48  		{"foo", "foo", true},
    49  		{"foo", "foobar", false},
    50  		{Foo{1}, Foo{2}, true},
    51  		{Foo{2}, Foo{1}, false},
    52  		{Bar{1}, Bar{10}, true},
    53  		{&Bar{1}, &Bar{10}, true},
    54  		{Baz{Bar{1}}, Baz{Bar{10}}, true},
    55  		{[...]string{}, [...]string{"1", "2", "3"}, false},
    56  		{[...]string{"1"}, [...]string{"1", "2", "3"}, false},
    57  		{[...]string{"1", "2", "3"}, [...]string{}, false},
    58  		{[...]string{"1", "2", "3"}, [...]string{"1", "2", "3"}, true},
    59  		{map[string]int{"foo": 1}, map[string]int{}, false},
    60  		{map[string]int{"foo": 1}, map[string]int{"foo": 2}, true},
    61  		{map[string]int{"foo": 2}, map[string]int{"foo": 1}, false},
    62  		{map[string]int{"foo": 1}, map[string]int{"foo": 2, "bar": 6}, false},
    63  		{map[string]int{"foo": 1, "bar": 6}, map[string]int{"foo": 2}, false},
    64  		{map[string]int{}, map[string]int(nil), true},
    65  		{[]string(nil), []string(nil), true},
    66  		{[]string{}, []string(nil), true},
    67  		{[]string(nil), []string{}, true},
    68  		{[]string{"1"}, []string(nil), false},
    69  		{[]string{}, []string{"1", "2", "3"}, false},
    70  		{[]string{"1"}, []string{"1", "2", "3"}, false},
    71  		{[]string{"1", "2", "3"}, []string{}, false},
    72  	}
    73  
    74  	for _, item := range table {
    75  		if e, a := item.equal, e.DeepEqual(item.a, item.b); e != a {
    76  			t.Errorf("Expected (%+v == %+v) == %v, but got %v", item.a, item.b, e, a)
    77  		}
    78  	}
    79  
    80  	// Cases which hinge upon implicit nil/empty map/slice equality
    81  	implicitTable := []Case{
    82  		{map[string][]int{}, map[string][]int(nil), true},
    83  		{[]int{}, []int(nil), true},
    84  		{map[string][]int{"foo": nil}, map[string][]int{"foo": {}}, true},
    85  		{Zap{A: nil, B: map[string][]int{"foo": nil}}, Zap{A: []int{}, B: map[string][]int{"foo": {}}}, true},
    86  	}
    87  
    88  	for _, item := range implicitTable {
    89  		if e, a := item.equal, e.DeepEqual(item.a, item.b); e != a {
    90  			t.Errorf("Expected (%+v == %+v) == %v, but got %v", item.a, item.b, e, a)
    91  		}
    92  	}
    93  
    94  	for _, item := range implicitTable {
    95  		if e, a := !item.equal, e.DeepEqualWithNilDifferentFromEmpty(item.a, item.b); e != a {
    96  			t.Errorf("Expected (%+v == %+v) == %v, but got %v", item.a, item.b, e, a)
    97  		}
    98  	}
    99  }
   100  
   101  func TestDerivatives(t *testing.T) {
   102  	e := Equalities{}
   103  	type Bar struct {
   104  		X int
   105  	}
   106  	type Baz struct {
   107  		Y Bar
   108  	}
   109  	err := e.AddFuncs(
   110  		func(a, b int) bool {
   111  			return a+1 == b
   112  		},
   113  		func(a, b Bar) bool {
   114  			return a.X*10 == b.X
   115  		},
   116  	)
   117  	if err != nil {
   118  		t.Fatalf("Unexpected: %v", err)
   119  	}
   120  
   121  	type Foo struct {
   122  		X int
   123  	}
   124  
   125  	table := []struct {
   126  		a, b  interface{}
   127  		equal bool
   128  	}{
   129  		{1, 2, true},
   130  		{2, 1, false},
   131  		{"foo", "fo", false},
   132  		{"foo", "foo", true},
   133  		{"foo", "foobar", false},
   134  		{Foo{1}, Foo{2}, true},
   135  		{Foo{2}, Foo{1}, false},
   136  		{Bar{1}, Bar{10}, true},
   137  		{&Bar{1}, &Bar{10}, true},
   138  		{Baz{Bar{1}}, Baz{Bar{10}}, true},
   139  		{[...]string{}, [...]string{"1", "2", "3"}, false},
   140  		{[...]string{"1"}, [...]string{"1", "2", "3"}, false},
   141  		{[...]string{"1", "2", "3"}, [...]string{}, false},
   142  		{[...]string{"1", "2", "3"}, [...]string{"1", "2", "3"}, true},
   143  		{map[string]int{"foo": 1}, map[string]int{}, false},
   144  		{map[string]int{"foo": 1}, map[string]int{"foo": 2}, true},
   145  		{map[string]int{"foo": 2}, map[string]int{"foo": 1}, false},
   146  		{map[string]int{"foo": 1}, map[string]int{"foo": 2, "bar": 6}, true},
   147  		{map[string]int{"foo": 1, "bar": 6}, map[string]int{"foo": 2}, false},
   148  		{map[string]int{}, map[string]int(nil), true},
   149  		{[]string(nil), []string(nil), true},
   150  		{[]string{}, []string(nil), true},
   151  		{[]string(nil), []string{}, true},
   152  		{[]string{"1"}, []string(nil), false},
   153  		{[]string{}, []string{"1", "2", "3"}, true},
   154  		{[]string{"1"}, []string{"1", "2", "3"}, true},
   155  		{[]string{"1", "2", "3"}, []string{}, false},
   156  	}
   157  
   158  	for _, item := range table {
   159  		if e, a := item.equal, e.DeepDerivative(item.a, item.b); e != a {
   160  			t.Errorf("Expected (%+v ~ %+v) == %v, but got %v", item.a, item.b, e, a)
   161  		}
   162  	}
   163  }