Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package common
import (
"testing"
"github.com/smartystreets/goconvey/convey"
)
var (
testInt64Source = []int64{1}
stringSource = []string{"1"}
testResult = 2
)
func TestContains(t *testing.T) {
convey.Convey("test Contains", t, func() {
convey.Convey("int32Tool Contains success", func() {
tool := int32Tool{}
sources := []int32{1}
existVal, unExistVal := int32(1), int32(2)
convey.So(tool.Contains(sources, existVal), convey.ShouldBeTrue)
convey.So(tool.Contains(sources, unExistVal), convey.ShouldBeFalse)
})
convey.Convey("stringTool Contains success", func() {
tool := stringTool{}
sources := []string{"1"}
existVal, unExistVal := "1", "2"
convey.So(tool.Contains(sources, existVal), convey.ShouldBeTrue)
convey.So(tool.Contains(sources, unExistVal), convey.ShouldBeFalse)
})
})
}
func TestSameElement(t *testing.T) {
convey.Convey("test SameElement", t, func() {
convey.Convey("SameElement success", func() {
tool := int64Tool{}
existVal, unExistVal := []int64{1}, []int64{2}
convey.So(tool.SameElement(testInt64Source, existVal), convey.ShouldBeTrue)
convey.So(tool.SameElement(testInt64Source, unExistVal), convey.ShouldBeFalse)
})
})
}
func TestRemove(t *testing.T) {
convey.Convey("test Remove", t, func() {
convey.Convey("Remove success", func() {
tool := int64Tool{}
var emptySources []int64
existVal, unExistVal := int64(1), int64(2)
convey.So(len(tool.Remove(emptySources, existVal)), convey.ShouldEqual, 0)
convey.So(len(tool.Remove(testInt64Source, existVal)), convey.ShouldEqual, 0)
convey.So(len(tool.Remove(testInt64Source, unExistVal)), convey.ShouldEqual, len(testInt64Source))
})
})
}
func TestIndex(t *testing.T) {
convey.Convey("test Index", t, func() {
convey.Convey("Index success", func() {
tool := int64Tool{}
existVal, unExistVal := int64(1), int64(2)
convey.So(tool.Index(testInt64Source, existVal), convey.ShouldEqual, 0)
convey.So(tool.Index(testInt64Source, unExistVal), convey.ShouldEqual, -1)
})
})
}
func TestToHexString(t *testing.T) {
convey.Convey("test ToHexString", t, func() {
convey.Convey("ToHexString success", func() {
tool := int64Tool{}
convey.So(tool.ToHexString(testInt64Source), convey.ShouldEqual, "1")
})
convey.Convey("ToHexString multiple numbers success", func() {
tool := int64Tool{}
int64Slice := []int64{1, 3, 5}
convey.So(tool.ToHexString(int64Slice), convey.ShouldEqual, "1,3,5")
})
})
}
func TestHexStringToInt(t *testing.T) {
convey.Convey("test string HexStringToInt", t, func() {
convey.Convey("HexStringToInt success", func() {
tool := stringTool{}
hexString := []string{"a"}
errHexString := []string{"xx"}
convey.So(tool.HexStringToInt(hexString)[0], convey.ShouldEqual, 10)
convey.So(len(tool.HexStringToInt(errHexString)), convey.ShouldEqual, 0)
})
})
}
func TestInt64ToolIndex(t *testing.T) {
convey.Convey("test int64Tool.Index", t, func() {
tool := int64Tool{}
testVal := []int64{1}
existVal, unExistVal := int64(1), int64(2)
convey.So(tool.Index(testVal, existVal), convey.ShouldEqual, 0)
convey.So(tool.Index(testVal, unExistVal), convey.ShouldEqual, -1)
})
}
func TestInt64ToolAbs(t *testing.T) {
convey.Convey("test int64Tool.Abs case positive number", t, func() {
tool := int64Tool{}
testVal1, testVal2 := int64(1), int64(2)
convey.So(tool.Abs(testVal1, testVal2), convey.ShouldEqual, 1)
})
convey.Convey("test int64Tool.Abs case negative number", t, func() {
tool := int64Tool{}
testVal1, testVal2 := int64(1), int64(-1)
convey.So(tool.Abs(testVal1, testVal2), convey.ShouldEqual, testResult)
})
}
func TestContainsInt64(t *testing.T) {
convey.Convey("test Contains case 1", t, func() {
tool := int64Tool{}
sources := []int64{1, 0}
target := int64(0)
convey.So(tool.Contains(sources, target), convey.ShouldBeTrue)
})
convey.Convey("test Contains case2", t, func() {
tool := int64Tool{}
sources := []int64{1}
target := int64(0)
convey.So(tool.Contains(sources, target), convey.ShouldBeFalse)
})
}