OLD | NEW |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // Deep equality test via reflection | 5 // Deep equality test via reflection |
6 | 6 |
7 package reflect | 7 package reflect |
8 | 8 |
9 // During deepValueEqual, must keep track of checks that are | 9 // During deepValueEqual, must keep track of checks that are |
10 // in progress. The comparison algorithm assumes that all | 10 // in progress. The comparison algorithm assumes that all |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 return false | 97 return false |
98 } | 98 } |
99 for _, k := range v1.MapKeys() { | 99 for _, k := range v1.MapKeys() { |
100 if !deepValueEqual(v1.MapIndex(k), v2.MapIndex(k), visit
ed, depth+1) { | 100 if !deepValueEqual(v1.MapIndex(k), v2.MapIndex(k), visit
ed, depth+1) { |
101 return false | 101 return false |
102 } | 102 } |
103 } | 103 } |
104 return true | 104 return true |
105 default: | 105 default: |
106 // Normal equality suffices | 106 // Normal equality suffices |
107 » » return v1.Interface() == v2.Interface() | 107 » » return valueInterface(v1, false) == valueInterface(v2, false) |
108 } | 108 } |
109 | 109 |
110 panic("Not reached") | 110 panic("Not reached") |
111 } | 111 } |
112 | 112 |
113 // DeepEqual tests for deep equality. It uses normal == equality where possible | 113 // DeepEqual tests for deep equality. It uses normal == equality where possible |
114 // but will scan members of arrays, slices, and fields of structs. It correctly | 114 // but will scan members of arrays, slices, and fields of structs. It correctly |
115 // handles recursive types. | 115 // handles recursive types. |
116 func DeepEqual(a1, a2 interface{}) bool { | 116 func DeepEqual(a1, a2 interface{}) bool { |
117 if a1 == nil || a2 == nil { | 117 if a1 == nil || a2 == nil { |
118 return a1 == a2 | 118 return a1 == a2 |
119 } | 119 } |
120 v1 := ValueOf(a1) | 120 v1 := ValueOf(a1) |
121 v2 := ValueOf(a2) | 121 v2 := ValueOf(a2) |
122 if v1.Type() != v2.Type() { | 122 if v1.Type() != v2.Type() { |
123 return false | 123 return false |
124 } | 124 } |
125 return deepValueEqual(v1, v2, make(map[uintptr]*visit), 0) | 125 return deepValueEqual(v1, v2, make(map[uintptr]*visit), 0) |
126 } | 126 } |
OLD | NEW |