Export ReverseSliceInPlace and ReverseSlice
... and add tests.
Test: TestReverseSliceInPlace, TestReverseSlice
Change-Id: I70f811207fdb2af891f7ef3fe19cbe5c7276135a
diff --git a/android/util_test.go b/android/util_test.go
index 0e28b56..20161e5 100644
--- a/android/util_test.go
+++ b/android/util_test.go
@@ -20,6 +20,7 @@
"strconv"
"strings"
"testing"
+ "unsafe"
)
var firstUniqueStringsTestCases = []struct {
@@ -754,3 +755,65 @@
})
}
}
+
+var reverseTestCases = []struct {
+ name string
+ in []string
+ expected []string
+}{
+ {
+ name: "nil",
+ in: nil,
+ expected: nil,
+ },
+ {
+ name: "empty",
+ in: []string{},
+ expected: []string{},
+ },
+ {
+ name: "one",
+ in: []string{"one"},
+ expected: []string{"one"},
+ },
+ {
+ name: "even",
+ in: []string{"one", "two"},
+ expected: []string{"two", "one"},
+ },
+ {
+ name: "odd",
+ in: []string{"one", "two", "three"},
+ expected: []string{"three", "two", "one"},
+ },
+}
+
+func TestReverseSliceInPlace(t *testing.T) {
+ for _, testCase := range reverseTestCases {
+ t.Run(testCase.name, func(t *testing.T) {
+ slice := CopyOf(testCase.in)
+ slice2 := slice
+ ReverseSliceInPlace(slice)
+ if !reflect.DeepEqual(slice, testCase.expected) {
+ t.Errorf("expected %#v, got %#v", testCase.expected, slice)
+ }
+ if unsafe.SliceData(slice) != unsafe.SliceData(slice2) {
+ t.Errorf("expected slices to share backing array")
+ }
+ })
+ }
+}
+
+func TestReverseSlice(t *testing.T) {
+ for _, testCase := range reverseTestCases {
+ t.Run(testCase.name, func(t *testing.T) {
+ slice := ReverseSlice(testCase.in)
+ if !reflect.DeepEqual(slice, testCase.expected) {
+ t.Errorf("expected %#v, got %#v", testCase.expected, slice)
+ }
+ if slice != nil && unsafe.SliceData(testCase.in) == unsafe.SliceData(slice) {
+ t.Errorf("expected slices to have different backing arrays")
+ }
+ })
+ }
+}