...

Source file src/k8s.io/utils/third_party/forked/golang/reflect/deep_equal_test.go

Documentation: k8s.io/utils/third_party/forked/golang/reflect

     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  	err := e.AddFuncs(
    20  		func(a, b int) bool {
    21  			return a+1 == b
    22  		},
    23  		func(a, b Bar) bool {
    24  			return a.X*10 == b.X
    25  		},
    26  	)
    27  	if err != nil {
    28  		t.Fatalf("Unexpected: %v", err)
    29  	}
    30  
    31  	type Foo struct {
    32  		X int
    33  	}
    34  
    35  	table := []struct {
    36  		a, b  interface{}
    37  		equal bool
    38  	}{
    39  		{1, 2, true},
    40  		{2, 1, false},
    41  		{"foo", "fo", false},
    42  		{"foo", "foo", true},
    43  		{"foo", "foobar", false},
    44  		{Foo{1}, Foo{2}, true},
    45  		{Foo{2}, Foo{1}, false},
    46  		{Bar{1}, Bar{10}, true},
    47  		{&Bar{1}, &Bar{10}, true},
    48  		{Baz{Bar{1}}, Baz{Bar{10}}, true},
    49  		{[...]string{}, [...]string{"1", "2", "3"}, false},
    50  		{[...]string{"1"}, [...]string{"1", "2", "3"}, false},
    51  		{[...]string{"1", "2", "3"}, [...]string{}, false},
    52  		{[...]string{"1", "2", "3"}, [...]string{"1", "2", "3"}, true},
    53  		{map[string]int{"foo": 1}, map[string]int{}, false},
    54  		{map[string]int{"foo": 1}, map[string]int{"foo": 2}, true},
    55  		{map[string]int{"foo": 2}, map[string]int{"foo": 1}, false},
    56  		{map[string]int{"foo": 1}, map[string]int{"foo": 2, "bar": 6}, false},
    57  		{map[string]int{"foo": 1, "bar": 6}, map[string]int{"foo": 2}, false},
    58  		{map[string]int{}, map[string]int(nil), true},
    59  		{[]string(nil), []string(nil), true},
    60  		{[]string{}, []string(nil), true},
    61  		{[]string(nil), []string{}, true},
    62  		{[]string{"1"}, []string(nil), false},
    63  		{[]string{}, []string{"1", "2", "3"}, false},
    64  		{[]string{"1"}, []string{"1", "2", "3"}, false},
    65  		{[]string{"1", "2", "3"}, []string{}, false},
    66  	}
    67  
    68  	for _, item := range table {
    69  		if e, a := item.equal, e.DeepEqual(item.a, item.b); e != a {
    70  			t.Errorf("Expected (%+v == %+v) == %v, but got %v", item.a, item.b, e, a)
    71  		}
    72  	}
    73  }
    74  
    75  func TestDerivates(t *testing.T) {
    76  	e := Equalities{}
    77  	type Bar struct {
    78  		X int
    79  	}
    80  	type Baz struct {
    81  		Y Bar
    82  	}
    83  	err := e.AddFuncs(
    84  		func(a, b int) bool {
    85  			return a+1 == b
    86  		},
    87  		func(a, b Bar) bool {
    88  			return a.X*10 == b.X
    89  		},
    90  	)
    91  	if err != nil {
    92  		t.Fatalf("Unexpected: %v", err)
    93  	}
    94  
    95  	type Foo struct {
    96  		X int
    97  	}
    98  
    99  	table := []struct {
   100  		a, b  interface{}
   101  		equal bool
   102  	}{
   103  		{1, 2, true},
   104  		{2, 1, false},
   105  		{"foo", "fo", false},
   106  		{"foo", "foo", true},
   107  		{"foo", "foobar", false},
   108  		{Foo{1}, Foo{2}, true},
   109  		{Foo{2}, Foo{1}, false},
   110  		{Bar{1}, Bar{10}, true},
   111  		{&Bar{1}, &Bar{10}, true},
   112  		{Baz{Bar{1}}, Baz{Bar{10}}, true},
   113  		{[...]string{}, [...]string{"1", "2", "3"}, false},
   114  		{[...]string{"1"}, [...]string{"1", "2", "3"}, false},
   115  		{[...]string{"1", "2", "3"}, [...]string{}, false},
   116  		{[...]string{"1", "2", "3"}, [...]string{"1", "2", "3"}, true},
   117  		{map[string]int{"foo": 1}, map[string]int{}, false},
   118  		{map[string]int{"foo": 1}, map[string]int{"foo": 2}, true},
   119  		{map[string]int{"foo": 2}, map[string]int{"foo": 1}, false},
   120  		{map[string]int{"foo": 1}, map[string]int{"foo": 2, "bar": 6}, true},
   121  		{map[string]int{"foo": 1, "bar": 6}, map[string]int{"foo": 2}, false},
   122  		{map[string]int{}, map[string]int(nil), true},
   123  		{[]string(nil), []string(nil), true},
   124  		{[]string{}, []string(nil), true},
   125  		{[]string(nil), []string{}, true},
   126  		{[]string{"1"}, []string(nil), false},
   127  		{[]string{}, []string{"1", "2", "3"}, true},
   128  		{[]string{"1"}, []string{"1", "2", "3"}, true},
   129  		{[]string{"1", "2", "3"}, []string{}, false},
   130  	}
   131  
   132  	for _, item := range table {
   133  		if e, a := item.equal, e.DeepDerivative(item.a, item.b); e != a {
   134  			t.Errorf("Expected (%+v ~ %+v) == %v, but got %v", item.a, item.b, e, a)
   135  		}
   136  	}
   137  }
   138  

View as plain text