Merge changes I91063ebb,Id859723b

* changes:
  Differentiate between empty and nil input
  Do not modify input in-place
This commit is contained in:
Spandan Das
2023-04-28 00:07:57 +00:00
committed by Gerrit Code Review
3 changed files with 18 additions and 4 deletions

View File

@@ -513,9 +513,8 @@ func (m *ApexModuleBase) checkApexAvailableProperty(mctx BaseModuleContext) {
// exactly the same set of APEXes (and platform), i.e. if their apex_available // exactly the same set of APEXes (and platform), i.e. if their apex_available
// properties have the same elements. // properties have the same elements.
func AvailableToSameApexes(mod1, mod2 ApexModule) bool { func AvailableToSameApexes(mod1, mod2 ApexModule) bool {
// Use CopyOf to prevent non-determinism (b/275313114#comment1) mod1ApexAvail := SortedUniqueStrings(mod1.apexModuleBase().ApexProperties.Apex_available)
mod1ApexAvail := SortedUniqueStrings(CopyOf(mod1.apexModuleBase().ApexProperties.Apex_available)) mod2ApexAvail := SortedUniqueStrings(mod2.apexModuleBase().ApexProperties.Apex_available)
mod2ApexAvail := SortedUniqueStrings(CopyOf(mod2.apexModuleBase().ApexProperties.Apex_available))
if len(mod1ApexAvail) != len(mod2ApexAvail) { if len(mod1ApexAvail) != len(mod2ApexAvail) {
return false return false
} }

View File

@@ -26,7 +26,11 @@ import (
// CopyOf returns a new slice that has the same contents as s. // CopyOf returns a new slice that has the same contents as s.
func CopyOf(s []string) []string { func CopyOf(s []string) []string {
return append([]string(nil), s...) // If the input is nil, return nil and not an empty list
if s == nil {
return s
}
return append([]string{}, s...)
} }
// Concat returns a new slice concatenated from the two input slices. It does not change the input // Concat returns a new slice concatenated from the two input slices. It does not change the input
@@ -276,6 +280,8 @@ func RemoveFromList(s string, list []string) (bool, []string) {
// FirstUniqueStrings returns all unique elements of a slice of strings, keeping the first copy of // FirstUniqueStrings returns all unique elements of a slice of strings, keeping the first copy of
// each. It modifies the slice contents in place, and returns a subslice of the original slice. // each. It modifies the slice contents in place, and returns a subslice of the original slice.
func FirstUniqueStrings(list []string) []string { func FirstUniqueStrings(list []string) []string {
// Do not moodify the input in-place, operate on a copy instead.
list = CopyOf(list)
// 128 was chosen based on BenchmarkFirstUniqueStrings results. // 128 was chosen based on BenchmarkFirstUniqueStrings results.
if len(list) > 128 { if len(list) > 128 {
return firstUniqueStringsMap(list) return firstUniqueStringsMap(list)
@@ -332,6 +338,7 @@ func LastUniqueStrings(list []string) []string {
// SortedUniqueStrings returns what the name says // SortedUniqueStrings returns what the name says
func SortedUniqueStrings(list []string) []string { func SortedUniqueStrings(list []string) []string {
// FirstUniqueStrings creates a copy of `list`, so the input remains untouched.
unique := FirstUniqueStrings(list) unique := FirstUniqueStrings(list)
sort.Strings(unique) sort.Strings(unique)
return unique return unique

View File

@@ -381,6 +381,14 @@ func TestRemoveFromList(t *testing.T) {
} }
} }
func TestCopyOfEmptyAndNil(t *testing.T) {
emptyList := []string{}
copyOfEmptyList := CopyOf(emptyList)
AssertBoolEquals(t, "Copy of an empty list should be an empty list and not nil", true, copyOfEmptyList != nil)
copyOfNilList := CopyOf(nil)
AssertBoolEquals(t, "Copy of a nil list should be a nil list and not an empty list", true, copyOfNilList == nil)
}
func ExampleCopyOf() { func ExampleCopyOf() {
a := []string{"1", "2", "3"} a := []string{"1", "2", "3"}
b := CopyOf(a) b := CopyOf(a)