From 7922ab8da69cc777a86d1062831d479cb908bd98 Mon Sep 17 00:00:00 2001 From: Logan Chien Date: Tue, 6 Mar 2018 18:29:27 +0800 Subject: [PATCH] RemoveFromList() should remove all matches RemoveFromList() should remove all matches. Before this commit, RemoveFromList() only removes the first match. This commit rewrites RemoveFromList() so that it will remove all matches. Besides, a unit test is written. Note: aosp/461936 wants to make sure libc.so precedes libdl.so in DT_NEEDED entries. However, if there are two "libdl" in shared_libs, aosp/461936 won't achieve its goal because RemoveFromList() (prior to this commit) only removes the first "libdl". Bug: 62815515 Test: Build sailfish and check libstagefright.so Change-Id: I9bec563cbf800bff4bd508fb21e28869a92cfe56 --- android/util.go | 13 +++++++--- android/util_test.go | 59 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 3 deletions(-) diff --git a/android/util.go b/android/util.go index 854d78280..e9b976435 100644 --- a/android/util.go +++ b/android/util.go @@ -101,11 +101,18 @@ func RemoveListFromList(list []string, filter_out []string) (result []string) { func RemoveFromList(s string, list []string) (bool, []string) { i := IndexList(s, list) - if i != -1 { - return true, append(list[:i], list[i+1:]...) - } else { + if i == -1 { return false, list } + + result := make([]string, 0, len(list)-1) + result = append(result, list[:i]...) + for _, l := range list[i+1:] { + if l != s { + result = append(result, l) + } + } + return true, result } // FirstUniqueStrings returns all unique elements of a slice of strings, keeping the first copy of diff --git a/android/util_test.go b/android/util_test.go index dc8b9080c..1c791b240 100644 --- a/android/util_test.go +++ b/android/util_test.go @@ -300,3 +300,62 @@ func TestRemoveListFromList(t *testing.T) { t.Errorf(" got: %#v", out) } } + +func TestRemoveFromList(t *testing.T) { + testcases := []struct { + name string + key string + input []string + expectedFound bool + expectedOut []string + }{ + { + name: "remove_one_match", + key: "a", + input: []string{"a", "b", "c"}, + expectedFound: true, + expectedOut: []string{"b", "c"}, + }, + { + name: "remove_three_matches", + key: "a", + input: []string{"a", "b", "a", "c", "a"}, + expectedFound: true, + expectedOut: []string{"b", "c"}, + }, + { + name: "remove_zero_matches", + key: "X", + input: []string{"a", "b", "a", "c", "a"}, + expectedFound: false, + expectedOut: []string{"a", "b", "a", "c", "a"}, + }, + { + name: "remove_all_matches", + key: "a", + input: []string{"a", "a", "a", "a"}, + expectedFound: true, + expectedOut: []string{}, + }, + } + + for _, testCase := range testcases { + t.Run(testCase.name, func(t *testing.T) { + found, out := RemoveFromList(testCase.key, testCase.input) + if found != testCase.expectedFound { + t.Errorf("incorrect output:") + t.Errorf(" key: %#v", testCase.key) + t.Errorf(" input: %#v", testCase.input) + t.Errorf(" expected: %#v", testCase.expectedFound) + t.Errorf(" got: %#v", found) + } + if !reflect.DeepEqual(out, testCase.expectedOut) { + t.Errorf("incorrect output:") + t.Errorf(" key: %#v", testCase.key) + t.Errorf(" input: %#v", testCase.input) + t.Errorf(" expected: %#v", testCase.expectedOut) + t.Errorf(" got: %#v", out) + } + }) + } +}