mirror of
https://github.com/tailscale/tailscale.git
synced 2025-03-23 17:51:05 +00:00
types/views: add SliceEqual, like std slices.Equal
Updates tailscale/corp#6198 Change-Id: I38614a4552c9fa933036aa493c7cdb57c7ffe2d2 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
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…
x
Reference in New Issue
Block a user