mirror of
https://github.com/tailscale/tailscale.git
synced 2025-08-11 05:07:33 +00:00
util/slicesx: add FirstElementEqual and LastElementEqual
And update a few callers as examples of motivation. (there are a couple others, but these are the ones where it's prettier) Updates #cleanup Change-Id: Ic8c5cb7af0a59c6e790a599136b591ebe16d38eb Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:

committed by
Brad Fitzpatrick

parent
910462a8e0
commit
cec779e771
@@ -197,3 +197,28 @@ func TestCutSuffix(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFirstLastEqual(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
in string
|
||||
v byte
|
||||
f func([]byte, byte) bool
|
||||
want bool
|
||||
}{
|
||||
{"first-empty", "", 'f', FirstEqual[byte], false},
|
||||
{"first-true", "foo", 'f', FirstEqual[byte], true},
|
||||
{"first-false", "foo", 'b', FirstEqual[byte], false},
|
||||
{"last-empty", "", 'f', LastEqual[byte], false},
|
||||
{"last-true", "bar", 'r', LastEqual[byte], true},
|
||||
{"last-false", "bar", 'o', LastEqual[byte], false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := tt.f([]byte(tt.in), tt.v); got != tt.want {
|
||||
t.Errorf("got %v; want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user