Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit cc4da765 authored by Spandan Das's avatar Spandan Das
Browse files

Differentiate between empty and nil input

Previously, CopyOf on an empty list was returning nil. With the updates
to SortedUniqueStrings and FirstUniqueStrings, we need to differentiate
between empty lists and nil.

Test: m nothing
Change-Id: I91063ebbe5013cbda5d8f70efde4683c66581599
parent 8a8714c7
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
@@ -26,7 +26,11 @@ import (

// CopyOf returns a new slice that has the same contents as s.
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
+8 −0
Original line number Diff line number Diff line
@@ -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() {
	a := []string{"1", "2", "3"}
	b := CopyOf(a)