summaryrefslogtreecommitdiff
path: root/android/util_test.go
diff options
context:
space:
mode:
author Colin Cross <ccross@android.com> 2022-02-17 11:13:37 -0800
committer Colin Cross <ccross@android.com> 2022-02-17 15:52:07 -0800
commit9eb853bcf2ae7a124fc9b94979825bc6d7bf6930 (patch)
treebca552dd304d7d2b99c85abbcda32b3d355fa2e9 /android/util_test.go
parentc2cdd8ab73aa793dd3810f97cb831d0e14f0579b (diff)
Add SortedStringValues and SortedUniqueStringValues
Add SortedStringValues and SortedUniqueStringValues that return the values of a string-valued map. Also make the SortedStringKeys function use a similar implementation with MapRange, which avoids iterating over the keys twice, once in MapKeys to build the list of key reflect.Values and once over the reflect.Values to convert them to strings. Test: util_test.go Change-Id: I4fc990a5036421e8926094ee158fafe606d0f10b
Diffstat (limited to 'android/util_test.go')
-rw-r--r--android/util_test.go114
1 files changed, 114 insertions, 0 deletions
diff --git a/android/util_test.go b/android/util_test.go
index 09bec01cc..9b9253b49 100644
--- a/android/util_test.go
+++ b/android/util_test.go
@@ -640,3 +640,117 @@ func BenchmarkFirstUniqueStrings(b *testing.B) {
})
}
}
+
+func TestSortedStringKeys(t *testing.T) {
+ testCases := []struct {
+ name string
+ in interface{}
+ expected []string
+ }{
+ {
+ name: "nil",
+ in: map[string]string(nil),
+ expected: nil,
+ },
+ {
+ name: "empty",
+ in: map[string]string{},
+ expected: nil,
+ },
+ {
+ name: "simple",
+ in: map[string]string{"a": "foo", "b": "bar"},
+ expected: []string{"a", "b"},
+ },
+ {
+ name: "interface values",
+ in: map[string]interface{}{"a": nil, "b": nil},
+ expected: []string{"a", "b"},
+ },
+ }
+
+ for _, tt := range testCases {
+ t.Run(tt.name, func(t *testing.T) {
+ got := SortedStringKeys(tt.in)
+ if g, w := got, tt.expected; !reflect.DeepEqual(g, w) {
+ t.Errorf("wanted %q, got %q", w, g)
+ }
+ })
+ }
+}
+
+func TestSortedStringValues(t *testing.T) {
+ testCases := []struct {
+ name string
+ in interface{}
+ expected []string
+ }{
+ {
+ name: "nil",
+ in: map[string]string(nil),
+ expected: nil,
+ },
+ {
+ name: "empty",
+ in: map[string]string{},
+ expected: nil,
+ },
+ {
+ name: "simple",
+ in: map[string]string{"foo": "a", "bar": "b"},
+ expected: []string{"a", "b"},
+ },
+ {
+ name: "duplicates",
+ in: map[string]string{"foo": "a", "bar": "b", "baz": "b"},
+ expected: []string{"a", "b", "b"},
+ },
+ }
+
+ for _, tt := range testCases {
+ t.Run(tt.name, func(t *testing.T) {
+ got := SortedStringValues(tt.in)
+ if g, w := got, tt.expected; !reflect.DeepEqual(g, w) {
+ t.Errorf("wanted %q, got %q", w, g)
+ }
+ })
+ }
+}
+
+func TestSortedUniqueStringValues(t *testing.T) {
+ testCases := []struct {
+ name string
+ in interface{}
+ expected []string
+ }{
+ {
+ name: "nil",
+ in: map[string]string(nil),
+ expected: nil,
+ },
+ {
+ name: "empty",
+ in: map[string]string{},
+ expected: nil,
+ },
+ {
+ name: "simple",
+ in: map[string]string{"foo": "a", "bar": "b"},
+ expected: []string{"a", "b"},
+ },
+ {
+ name: "duplicates",
+ in: map[string]string{"foo": "a", "bar": "b", "baz": "b"},
+ expected: []string{"a", "b"},
+ },
+ }
+
+ for _, tt := range testCases {
+ t.Run(tt.name, func(t *testing.T) {
+ got := SortedUniqueStringValues(tt.in)
+ if g, w := got, tt.expected; !reflect.DeepEqual(g, w) {
+ t.Errorf("wanted %q, got %q", w, g)
+ }
+ })
+ }
+}