type NotBasic Basic
-type Recursive struct {
- x int;
- r *Recursive
-}
-
-type Complex struct {
- a int;
- b [3]*Complex;
- c *string;
- d map[float]float
-}
-
type DeepEqualTest struct {
a, b interface{};
eq bool;
DeepEqualTest{ &[3]int{ 1, 2, 3 }, &[3]int{ 1, 2, 3 }, true },
DeepEqualTest{ Basic{ 1, 0.5 }, Basic{ 1, 0.5 }, true },
DeepEqualTest{ os.Error(nil), os.Error(nil), true },
+ DeepEqualTest{ map[int]string{ 1:"one", 2:"two" }, map[int]string{ 2:"two", 1:"one" }, true },
// Inequalities
DeepEqualTest{ 1, 2, false },
DeepEqualTest{ &[3]int{ 1, 2, 3 }, &[3]int{ 1, 2, 4 }, false },
DeepEqualTest{ Basic{ 1, 0.5 }, Basic{ 1, 0.6 }, false },
DeepEqualTest{ Basic{ 1, 0 }, Basic{ 2, 0 }, false },
+ DeepEqualTest{ map[int]string{ 1:"one", 3:"two" }, map[int]string{ 2:"two", 1:"one" }, false },
+ DeepEqualTest{ map[int]string{ 1:"one", 2:"txo" }, map[int]string{ 2:"two", 1:"one" }, false },
+ DeepEqualTest{ map[int]string{ 1:"one", }, map[int]string{ 2:"two", 1:"one" }, false },
+ DeepEqualTest{ map[int]string{ 2:"two", 1:"one" }, map[int]string{ 1:"one", }, false },
// Mismatched types
DeepEqualTest{ 1, 1.0, false },
DeepEqualTest{ []int{ 1, 2, 3 }, [3]int{ 1, 2, 3 }, false },
DeepEqualTest{ &[3]interface{} { 1, 2, 4 }, &[3]interface{} { 1, 2, "s" }, false },
DeepEqualTest{ Basic{ 1, 0.5 }, NotBasic{ 1, 0.5 }, false },
+ DeepEqualTest{ map[uint]string{ 1:"one", 2:"two" }, map[int]string{ 2:"two", 1:"one" }, false },
}
func TestDeepEqual(t *testing.T) {
}
}
+type Recursive struct {
+ x int;
+ r *Recursive
+}
+
func TestDeepEqualRecursiveStruct(t *testing.T) {
a, b := new(Recursive), new(Recursive);
*a = Recursive{ 12, a };
}
}
+type Complex struct {
+ a int;
+ b [3]*Complex;
+ c *string;
+ d map[float]float
+}
+
func TestDeepEqualComplexStruct(t *testing.T) {
m := make(map[float]float);
stra, strb := "hello", "hello";
return i1 == i2;
}
return deepValueEqual(NewValue(i1), NewValue(i2), visited, depth+1);
- case *MapValue:
- // TODO(dnadasi): Implement this fully once MapValue is implemented
- return v1.Interface() == v2.Interface();
case *PtrValue:
return deepValueEqual(v.Elem(), v2.(*PtrValue).Elem(), visited, depth+1);
case *StructValue:
}
}
return true;
+ case *MapValue:
+ map1 := v;
+ map2 := v2.(*MapValue);
+ if map1.Len() != map2.Len() {
+ return false;
+ }
+ for i, k := range map1.Keys() {
+ if !deepValueEqual(map1.Get(k), map2.Get(k), visited, depth+1) {
+ return false;
+ }
+ }
+ return true;
default:
// Normal equality suffices
return v1.Interface() == v2.Interface();
// DeepEqual tests for deep equality. It uses normal == equality where possible
// but will scan members of arrays, slices, and fields of structs. It correctly
-// handles recursive types. Until reflection supports maps, maps are equal iff
-// they are identical.
+// handles recursive types.
func DeepEqual(a1, a2 interface{}) bool {
v1 := NewValue(a1);
v2 := NewValue(a2);