Skip to content

Commit

Permalink
ref(comparers): prevent in-place sort
Browse files Browse the repository at this point in the history
Close #4
  • Loading branch information
arsham committed Apr 8, 2022
1 parent 9e12fed commit 1337b45
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
23 changes: 15 additions & 8 deletions comparers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,43 @@ func equal[T constraints.Ordered](a, b []T) bool {
if len(a) != len(b) {
return false
}
slices.Sort(a)
slices.Sort(b)
for i := range a {
if a[i] != b[i] {

aa := make([]T, len(a))
copy(aa, a)
slices.Sort(aa)

bb := make([]T, len(b))
copy(bb, b)
slices.Sort(bb)

for i := range aa {
if aa[i] != bb[i] {
return false
}
}
return true
}

// IntSliceComparer is a go-cmp comparer that doesn't care if the slices in
// question is not sorted. Note that this will sort the slices in place.
// question is not sorted. The slices are copies and can be safely reused.
var IntSliceComparer = cmp.Comparer(func(a, b []int) bool {
return equal(a, b)
})

// Int32SliceComparer is a go-cmp comparer that doesn't care if the slices in
// question is not sorted. Note that this will sort the slices in place.
// question is not sorted. The slices are copies and can be safely reused.
var Int32SliceComparer = cmp.Comparer(func(a, b []int32) bool {
return equal(a, b)
})

// Int64SliceComparer is a go-cmp comparer that doesn't care if the slices in
// question is not sorted. Note that this will sort the slices in place.
// question is not sorted. The slices are copies and can be safely reused.
var Int64SliceComparer = cmp.Comparer(func(a, b []int64) bool {
return equal(a, b)
})

// StringSliceComparer is a go-cmp comparer that doesn't care if the slices in
// question is not sorted. Note that this will sort the slices in place.
// question is not sorted. The slices are copies and can be safely reused.
var StringSliceComparer = cmp.Comparer(func(a, b []string) bool {
return equal(a, b)
})
20 changes: 20 additions & 0 deletions comparers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ func testComparerIntSliceComparer(t *testing.T) {
assert.NotEmpty(t, cmp.Diff(a, b, testament.IntSliceComparer))
assert.Empty(t, cmp.Diff(a, a, testament.IntSliceComparer))

a = []int{3, 1, 2}
b = []int{3, 1, 2}
cmp.Diff(a, b, testament.IntSliceComparer)
assert.Equal(t, []int{3, 1, 2}, b)

f := func(a []int) bool {
b := make([]int, len(a))
copy(b, a)
Expand All @@ -51,6 +56,11 @@ func testComparerInt32SliceComparer(t *testing.T) {
assert.NotEmpty(t, cmp.Diff(a, b, testament.Int32SliceComparer))
assert.Empty(t, cmp.Diff(a, a, testament.Int32SliceComparer))

a = []int32{3, 1, 2}
b = []int32{3, 1, 2}
cmp.Diff(a, b, testament.IntSliceComparer)
assert.Equal(t, []int32{3, 1, 2}, b)

f := func(a []int32) bool {
b := make([]int32, len(a))
copy(b, a)
Expand All @@ -74,6 +84,11 @@ func testComparerInt64SliceComparer(t *testing.T) {
assert.NotEmpty(t, cmp.Diff(a, b, testament.Int64SliceComparer))
assert.Empty(t, cmp.Diff(a, a, testament.Int64SliceComparer))

a = []int64{3, 1, 2}
b = []int64{3, 1, 2}
cmp.Diff(a, b, testament.IntSliceComparer)
assert.Equal(t, []int64{3, 1, 2}, b)

f := func(a []int64) bool {
b := make([]int64, len(a))
copy(b, a)
Expand All @@ -97,6 +112,11 @@ func testComparerStringSliceComparer(t *testing.T) {
assert.NotEmpty(t, cmp.Diff(a, b, testament.StringSliceComparer))
assert.Empty(t, cmp.Diff(a, a, testament.StringSliceComparer))

a = []string{"d", "a", "b"}
b = []string{"d", "a", "b"}
cmp.Diff(a, b, testament.IntSliceComparer)
assert.Equal(t, []string{"d", "a", "b"}, b)

f := func(a []string) bool {
b := make([]string, len(a))
copy(b, a)
Expand Down

0 comments on commit 1337b45

Please sign in to comment.