github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/Unknwon/com/slice_test.go (about) 1 // Copyright 2013 com authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"): you may 4 // not use this file except in compliance with the License. You may obtain 5 // a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 // License for the specific language governing permissions and limitations 13 // under the License. 14 15 package com 16 17 import ( 18 "fmt" 19 "testing" 20 21 . "github.com/insionng/yougam/libraries/smartystreets/goconvey/convey" 22 ) 23 24 func TestAppendStr(t *testing.T) { 25 Convey("Append a string to a slice with no duplicates", t, func() { 26 s := []string{"a"} 27 28 Convey("Append a string that does not exist in slice", func() { 29 s = AppendStr(s, "b") 30 So(len(s), ShouldEqual, 2) 31 }) 32 33 Convey("Append a string that does exist in slice", func() { 34 s = AppendStr(s, "b") 35 So(len(s), ShouldEqual, 2) 36 }) 37 }) 38 } 39 40 func TestCompareSliceStr(t *testing.T) { 41 Convey("Compares two 'string' type slices with elements and order", t, func() { 42 Convey("Compare two slices that do have same elements and order", func() { 43 So(CompareSliceStr( 44 []string{"1", "2", "3"}, []string{"1", "2", "3"}), ShouldBeTrue) 45 }) 46 47 Convey("Compare two slices that do have same elements but does not have same order", func() { 48 So(!CompareSliceStr( 49 []string{"2", "1", "3"}, []string{"1", "2", "3"}), ShouldBeTrue) 50 }) 51 52 Convey("Compare two slices that have different number of elements", func() { 53 So(!CompareSliceStr( 54 []string{"2", "1"}, []string{"1", "2", "3"}), ShouldBeTrue) 55 }) 56 }) 57 } 58 59 func TestCompareSliceStrU(t *testing.T) { 60 Convey("Compare two 'string' type slices with elements and ignore the order", t, func() { 61 Convey("Compare two slices that do have same elements and order", func() { 62 So(CompareSliceStrU( 63 []string{"1", "2", "3"}, []string{"1", "2", "3"}), ShouldBeTrue) 64 }) 65 66 Convey("Compare two slices that do have same elements but does not have same order", func() { 67 So(CompareSliceStrU( 68 []string{"2", "1", "3"}, []string{"1", "2", "3"}), ShouldBeTrue) 69 }) 70 71 Convey("Compare two slices that have different number of elements", func() { 72 So(!CompareSliceStrU( 73 []string{"2", "1"}, []string{"1", "2", "3"}), ShouldBeTrue) 74 }) 75 }) 76 } 77 78 func BenchmarkAppendStr(b *testing.B) { 79 s := []string{"a"} 80 for i := 0; i < b.N; i++ { 81 s = AppendStr(s, fmt.Sprint(b.N%3)) 82 } 83 } 84 85 func BenchmarkCompareSliceStr(b *testing.B) { 86 s1 := []string{"1", "2", "3"} 87 s2 := []string{"1", "2", "3"} 88 for i := 0; i < b.N; i++ { 89 CompareSliceStr(s1, s2) 90 } 91 } 92 93 func BenchmarkCompareSliceStrU(b *testing.B) { 94 s1 := []string{"1", "4", "2", "3"} 95 s2 := []string{"1", "2", "3", "4"} 96 for i := 0; i < b.N; i++ { 97 CompareSliceStrU(s1, s2) 98 } 99 }