types/views: add SliceEqual, like std slices.Equal

Updates tailscale/corp#6198

Change-Id: I38614a4552c9fa933036aa493c7cdb57c7ffe2d2
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
pull/9104/head
Brad Fitzpatrick 1 year ago committed by Brad Fitzpatrick
parent b407fdef70
commit e7d1538a2d

@ -10,6 +10,7 @@ import (
"encoding/json"
"errors"
"maps"
"slices"
"go4.org/mem"
)
@ -275,6 +276,11 @@ func SliceContains[T comparable](v Slice[T], e T) bool {
return false
}
// SliceEqual is like the standard library's slices.Equal, but for two views.
func SliceEqual[T comparable](a, b Slice[T]) bool {
return slices.Equal(a.ж, b.ж)
}
// SliceEqualAnyOrder reports whether a and b contain the same elements, regardless of order.
// The underlying slices for a and b can be nil.
func SliceEqualAnyOrder[T comparable](a, b Slice[T]) bool {

@ -150,3 +150,17 @@ func TestLenIter(t *testing.T) {
t.Errorf("got %q; want %q", got, orig)
}
}
func TestSliceEqual(t *testing.T) {
a := SliceOf([]string{"foo", "bar"})
b := SliceOf([]string{"foo", "bar"})
if !SliceEqual(a, b) {
t.Errorf("got a != b")
}
if !SliceEqual(a.SliceTo(0), b.SliceTo(0)) {
t.Errorf("got a[:0] != b[:0]")
}
if SliceEqual(a.SliceTo(2), a.SliceTo(1)) {
t.Error("got a[:2] == a[:1]")
}
}

Loading…
Cancel
Save