From 664ed38f72a89cf7d2405f193efe192cc974d0eb Mon Sep 17 00:00:00 2001 From: dsolerh Date: Thu, 18 Jul 2024 14:42:42 +0300 Subject: [PATCH] add example and test code --- slice_example_test.go | 12 ++++++++++++ slice_test.go | 20 ++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/slice_example_test.go b/slice_example_test.go index 0d64d8f0..d77244cf 100644 --- a/slice_example_test.go +++ b/slice_example_test.go @@ -183,6 +183,18 @@ func ExamplePartitionBy() { // [1 3] } +func ExamplePartitionIn2By() { + list := []int{-2, -1, 0, 1, 2} + + negative, positive := PartitionIn2By(list, func(x int) bool { return x < 0 }) + + fmt.Printf("%v\n", negative) + fmt.Printf("%v\n", positive) + // Output: + // [-2 -1] + // [0 1 2] +} + func ExampleFlatten() { list := [][]int{{0, 1, 2}, {3, 4, 5}} diff --git a/slice_test.go b/slice_test.go index abb9450e..28bc400f 100644 --- a/slice_test.go +++ b/slice_test.go @@ -292,6 +292,26 @@ func TestPartitionBy(t *testing.T) { }) is.IsType(nonempty[0], allStrings, "type preserved") } +func TestPartitionIn2By(t *testing.T) { + t.Parallel() + is := assert.New(t) + + isNegative := func(x int) bool { return x < 0 } + + negative1, positive1 := PartitionIn2By([]int{-2, -1, 0, 1, 2}, isNegative) + negative2, positive2 := PartitionIn2By([]int{}, isNegative) + + is.Equal(negative1, []int{-2, -1}) + is.Equal(positive1, []int{0, 1, 2}) + + is.Equal(negative2, []int{}) + is.Equal(positive2, []int{}) + + type myStrings []string + allStrings := myStrings{"", "foo", "bar"} + nonempty, _ := PartitionIn2By(allStrings, func(item string) bool { return len(item) != 0 }) + is.IsType(nonempty, allStrings, "type preserved") +} func TestFlatten(t *testing.T) { t.Parallel()