diff options
Diffstat (limited to 'android/util_test.go')
| -rw-r--r-- | android/util_test.go | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/android/util_test.go b/android/util_test.go index 0e28b568b..20161e52d 100644 --- a/android/util_test.go +++ b/android/util_test.go @@ -20,6 +20,7 @@ import ( "strconv" "strings" "testing" + "unsafe" ) var firstUniqueStringsTestCases = []struct { @@ -754,3 +755,65 @@ func TestSortedUniqueStringValues(t *testing.T) { }) } } + +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") + } + }) + } +} |