Skip to content

Commit

Permalink
Merge pull request #11 from risteli/sibling_slices
Browse files Browse the repository at this point in the history
Fixes slices equality, when slices are extracted from the same array
  • Loading branch information
daniel-nichter committed Jul 14, 2019
2 parents 5b97bda + 1c3abb1 commit 3c08b27
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 3 deletions.
7 changes: 4 additions & 3 deletions deep.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,12 +282,13 @@ func (c *cmp) equals(a, b reflect.Value, level int) {
return
}

if a.Pointer() == b.Pointer() {
aLen := a.Len()
bLen := b.Len()

if a.Pointer() == b.Pointer() && aLen == bLen {
return
}

aLen := a.Len()
bLen := b.Len()
n := aLen
if bLen > aLen {
n = bLen
Expand Down
71 changes: 71 additions & 0 deletions deep_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,77 @@ func TestSlice(t *testing.T) {
}
}

func TestSiblingSlices(t *testing.T) {
father := []int{1, 2, 3, 4}
a := father[0:3]
b := father[0:3]

diff := deep.Equal(a, b)
if len(diff) > 0 {
t.Error("should be equal:", diff)
}
diff = deep.Equal(b, a)
if len(diff) > 0 {
t.Error("should be equal:", diff)
}

a = father[0:3]
b = father[0:2]
diff = deep.Equal(a, b)
if diff == nil {
t.Fatal("no diff")
}
if len(diff) != 1 {
t.Error("too many diff:", diff)
}
if diff[0] != "slice[2]: 3 != <no value>" {
t.Error("wrong diff:", diff[0])
}

a = father[0:2]
b = father[0:3]

diff = deep.Equal(a, b)
if diff == nil {
t.Fatal("no diff")
}
if len(diff) != 1 {
t.Error("too many diff:", diff)
}
if diff[0] != "slice[2]: <no value> != 3" {
t.Error("wrong diff:", diff[0])
}

a = father[0:2]
b = father[2:4]

diff = deep.Equal(a, b)
if diff == nil {
t.Fatal("no diff")
}
if len(diff) != 2 {
t.Error("too many diff:", diff)
}
if diff[0] != "slice[0]: 1 != 3" {
t.Error("wrong diff:", diff[0])
}
if diff[1] != "slice[1]: 2 != 4" {
t.Error("wrong diff:", diff[1])
}

a = father[0:0]
b = father[1:1]

diff = deep.Equal(a, b)
if len(diff) > 0 {
t.Error("should be equal:", diff)
}
diff = deep.Equal(b, a)
if len(diff) > 0 {
t.Error("should be equal:", diff)
}
}

func TestNilInterface(t *testing.T) {
type T struct{ i int }

Expand Down

0 comments on commit 3c08b27

Please sign in to comment.