deep_equal_test.go 3.6 KB

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