mirror of
https://github.com/tailscale/tailscale.git
synced 2025-08-11 05:07:33 +00:00
util/slicesx: add Partition function
Signed-off-by: Andrew Dunham <andrew@du.nham.ca> Change-Id: If97995ca9ee9fad40f327420dcb1857dd7ea2315
This commit is contained in:
@@ -44,6 +44,7 @@ func BenchmarkInterleave(b *testing.B) {
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func TestShuffle(t *testing.T) {
|
||||
var sl []int
|
||||
for i := 0; i < 100; i++ {
|
||||
@@ -64,3 +65,23 @@ func TestShuffle(t *testing.T) {
|
||||
t.Errorf("expected shuffle after 10 tries")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPartition(t *testing.T) {
|
||||
var sl []int
|
||||
for i := 1; i <= 10; i++ {
|
||||
sl = append(sl, i)
|
||||
}
|
||||
|
||||
evens, odds := Partition(sl, func(elem int) bool {
|
||||
return elem%2 == 0
|
||||
})
|
||||
|
||||
wantEvens := []int{2, 4, 6, 8, 10}
|
||||
wantOdds := []int{1, 3, 5, 7, 9}
|
||||
if !reflect.DeepEqual(evens, wantEvens) {
|
||||
t.Errorf("evens: got %v, want %v", evens, wantEvens)
|
||||
}
|
||||
if !reflect.DeepEqual(odds, wantOdds) {
|
||||
t.Errorf("odds: got %v, want %v", odds, wantOdds)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user