From 3402ba0298edb6c3ecfa4e09b7998d29f6c39397 Mon Sep 17 00:00:00 2001 From: Jaehyun Yeom Date: Fri, 8 Jul 2016 03:32:44 +0000 Subject: [PATCH 1/2] Fixed typo in comment from 'no not' to 'do not'. --- slice/minus.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/slice/minus.go b/slice/minus.go index ef5ca01..a08753e 100644 --- a/slice/minus.go +++ b/slice/minus.go @@ -1,7 +1,7 @@ package slice // MinusStrings gets a new []string containing all items in s, that -// no not appear in minus. +// do not appear in minus. func MinusStrings(s, minus []string) []string { a := []string{} From 4b3a6b8163bae7913e23a944baa9a4e7c06460d4 Mon Sep 17 00:00:00 2001 From: Jaehyun Yeom Date: Fri, 8 Jul 2016 08:45:47 +0000 Subject: [PATCH 2/2] Reimplemented PlusStrings using append variadic. --- slice/plus.go | 19 +++--------------- slice/plus_test.go | 50 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 17 deletions(-) diff --git a/slice/plus.go b/slice/plus.go index 8fb9eee..a14d748 100644 --- a/slice/plus.go +++ b/slice/plus.go @@ -1,22 +1,9 @@ package slice -// PlusStrings adds two []string arrays together, returning a new []string. +// PlusStrings adds two []string arrays together, returning a new copied []string. func PlusStrings(s, plus []string) []string { - var l int - - a := []string{} - - l = len(s) - for i := 0; i < l; i++ { - a = append(a, s[i]) - } - - l = len(plus) - for i := 0; i < l; i++ { - a = append(a, plus[i]) - } - - return a + a := make([]string, 0, len(s)+len(plus)) + return append(append(a, s...), plus...) } diff --git a/slice/plus_test.go b/slice/plus_test.go index 8f2cecd..33441dd 100644 --- a/slice/plus_test.go +++ b/slice/plus_test.go @@ -1,8 +1,9 @@ package slice import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) func TestPlusStrings(t *testing.T) { @@ -20,3 +21,50 @@ func TestPlusStrings(t *testing.T) { } } + +func TestPlusStrings_ensureCopy(t *testing.T) { + + s1 := []string{"one", "two", "three", "four", "five"} + s2 := []string{"six", "seven"} + + all := PlusStrings(s1[:2], s2) + s1[0] = "eight" + + if assert.Equal(t, 4, len(all)) { + assert.Equal(t, "one", all[0]) + assert.Equal(t, "two", all[1]) + assert.Equal(t, "six", all[2]) + assert.Equal(t, "seven", all[3]) + } + + if assert.Equal(t, 5, len(s1)) { + assert.Equal(t, "eight", s1[0]) + assert.Equal(t, "two", s1[1]) + assert.Equal(t, "three", s1[2]) + assert.Equal(t, "four", s1[3]) + assert.Equal(t, "five", s1[4]) + } + +} + +func TestPlusStrings_ensureCopyNilPlus(t *testing.T) { + + s1 := []string{"one", "two", "three", "four", "five"} + + all := PlusStrings(s1[:2], nil) + s1[0] = "eight" + + if assert.Equal(t, 2, len(all)) { + assert.Equal(t, "one", all[0]) + assert.Equal(t, "two", all[1]) + } + + if assert.Equal(t, 5, len(s1)) { + assert.Equal(t, "eight", s1[0]) + assert.Equal(t, "two", s1[1]) + assert.Equal(t, "three", s1[2]) + assert.Equal(t, "four", s1[3]) + assert.Equal(t, "five", s1[4]) + } + +}