11package stringutils
22
33import (
4+ "fmt"
45 "testing"
56
67 "github.com/stretchr/testify/assert"
@@ -48,6 +49,8 @@ func TestDeDup(t *testing.T) {
4849 keys []string
4950 want []string
5051 }{
52+ {"nil input" , nil , nil },
53+ {"empty input" , []string {}, nil },
5154 {"removes duplicates" , []string {"test" , "test" , "example" }, []string {"test" , "example" }},
5255 {"no duplicates" , []string {"test" , "test2" , "example" }, []string {"test" , "test2" , "example" }},
5356 }
@@ -65,6 +68,8 @@ func TestDeDupBig(t *testing.T) {
6568 keys []string
6669 want []string
6770 }{
71+ {"nil input" , nil , nil },
72+ {"empty input" , []string {}, nil },
6873 {"removes duplicates" , []string {"test" , "test" , "example" }, []string {"test" , "example" }},
6974 {"no duplicates" , []string {"test" , "test2" , "example" }, []string {"test" , "test2" , "example" }},
7075 }
@@ -82,8 +87,11 @@ func TestSliceToString(t *testing.T) {
8287 in []interface {}
8388 want []string
8489 }{
90+ {"nil input" , nil , nil },
91+ {"empty input" , []any {}, nil },
8592 {"converts number to string" , []any {1 , 2 , 3 }, []string {"1" , "2" , "3" }},
8693 {"converts mixed slice to string" , []any {1 , "aaa" , true , 0.55 }, []string {"1" , "aaa" , "true" , "0.55" }},
94+ {"converts slice of byte slices to string" , []any {[]byte ("hi" ), []byte ("there" )}, []string {"hi" , "there" }},
8795 }
8896
8997 for _ , tt := range tests {
@@ -169,3 +177,47 @@ func TestHasSuffixSlice(t *testing.T) {
169177 })
170178 }
171179}
180+
181+ func BenchmarkSliceToString (b * testing.B ) {
182+ tmpl := []any {[]byte ("fdjndfg" )}
183+ b .Run ("unsafe (small slice)" , func (b * testing.B ) {
184+ for i := 0 ; i < b .N ; i ++ {
185+ SliceToString (tmpl )
186+ }
187+ })
188+ b .Run ("type assert (small slice)" , func (b * testing.B ) {
189+ for i := 0 ; i < b .N ; i ++ {
190+ sliceToStringAllocs (tmpl )
191+ }
192+ })
193+
194+ for i := 0 ; i < 20 ; i ++ {
195+ tmpl = append (tmpl , tmpl ... )
196+ }
197+
198+ b .Run ("unsafe (big slice)" , func (b * testing.B ) {
199+ for i := 0 ; i < b .N ; i ++ {
200+ SliceToString (tmpl )
201+ }
202+ })
203+ b .Run ("type assert (big slice)" , func (b * testing.B ) {
204+ for i := 0 ; i < b .N ; i ++ {
205+ sliceToStringAllocs (tmpl )
206+ }
207+ })
208+ }
209+
210+ func sliceToStringAllocs (s []any ) []string {
211+ if len (s ) == 0 {
212+ return nil
213+ }
214+ strSlice := make ([]string , len (s ))
215+ for i , v := range s {
216+ if vb , ok := v .([]byte ); ok {
217+ strSlice [i ] = string (vb )
218+ continue
219+ }
220+ strSlice [i ] = fmt .Sprintf ("%v" , v )
221+ }
222+ return strSlice
223+ }
0 commit comments