Feature/cover unit test (#81)

This commit is contained in:
libin
2024-07-31 19:51:01 +08:00
committed by GitHub
parent 0990bbce4d
commit a59af60d9a
60 changed files with 2571 additions and 2606 deletions

View File

@@ -1,128 +1,331 @@
package sliceUtil
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestInStrSlices(t *testing.T) {
slices := []string{"apple", "banana", "cherry"}
if !InSlice("apple", slices) {
t.Errorf("apple should be in slices %v", slices)
testCases := []struct {
name string
slice []string
element string
expected bool
}{
{
name: "apple in slice",
slice: []string{"apple", "banana", "cherry"},
element: "apple",
expected: true,
},
{
name: "banana in slice",
slice: []string{"apple", "banana", "cherry"},
element: "banana",
expected: true,
},
{
name: "orange not in slice",
slice: []string{"apple", "banana", "cherry"},
element: "orange",
expected: false,
},
}
if !InSlice("banana", slices) {
t.Errorf("banana should be in slices %v", slices)
}
if InSlice("orange", slices) {
t.Errorf("orange should not be in slices %v", slices)
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
res := InSlice(tc.element, tc.slice)
assert.Equal(t, tc.expected, res)
})
}
}
func TestInIntSlices(t *testing.T) {
slices := []int{1, 2, 3, 4, 5}
if !InSlice(3, slices) {
t.Errorf("Expected true, but got false")
testCases := []struct {
name string
slice []int
element int
expected bool
}{
{
name: "3 in slice",
slice: []int{1, 2, 3, 4, 5},
element: 3,
expected: true,
},
{
name: "6 not in slice",
slice: []int{1, 2, 3, 4, 5},
element: 6,
expected: false,
},
}
if InSlice(6, slices) {
t.Errorf("Expected false, but got true")
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
res := InSlice(tc.element, tc.slice)
assert.Equal(t, tc.expected, res)
})
}
}
func TestInInt8Slices(t *testing.T) {
slices := []int8{1, 2, 3, 4, 5}
if !InSlice(3, slices) {
t.Errorf("Expected true, but got false")
testCases := []struct {
name string
slice []int8
element int8
expected bool
}{
{
name: "3 in slice",
slice: []int8{1, 2, 3, 4, 5},
element: 3,
expected: true,
},
{
name: "6 not in slice",
slice: []int8{1, 2, 3, 4, 5},
element: 6,
expected: false,
},
}
if InSlice(6, slices) {
t.Errorf("Expected false, but got true")
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
res := InSlice(tc.element, tc.slice)
assert.Equal(t, tc.expected, res)
})
}
}
func TestInInt16Slices(t *testing.T) {
slices := []int16{1, 2, 3, 4, 5}
if !InSlice(3, slices) {
t.Errorf("Expected true, but got false")
testCases := []struct {
name string
slice []int16
element int16
expected bool
}{
{
name: "3 in slice",
slice: []int16{1, 2, 3, 4, 5},
element: 3,
expected: true,
},
{
name: "6 not in slice",
slice: []int16{1, 2, 3, 4, 5},
element: 6,
expected: false,
},
}
if InSlice(6, slices) {
t.Errorf("Expected false, but got true")
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
res := InSlice(tc.element, tc.slice)
assert.Equal(t, tc.expected, res)
})
}
}
func TestInInt32Slices(t *testing.T) {
slices := []int32{1, 2, 3, 4, 5}
if !InSlice(3, slices) {
t.Errorf("Expected true, but got false")
testCases := []struct {
name string
slice []int32
element int32
expected bool
}{
{
name: "3 in slice",
slice: []int32{1, 2, 3, 4, 5},
element: 3,
expected: true,
},
{
name: "6 not in slice",
slice: []int32{1, 2, 3, 4, 5},
element: 6,
expected: false,
},
}
if InSlice(6, slices) {
t.Errorf("Expected false, but got true")
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
res := InSlice(tc.element, tc.slice)
assert.Equal(t, tc.expected, res)
})
}
}
func TestInInt64Slices(t *testing.T) {
slices := []int64{1, 2, 3, 4, 5}
if !InSlice(3, slices) {
t.Errorf("Expected true, but got false")
testCases := []struct {
name string
slice []int64
element int64
expected bool
}{
{
name: "3 in slice",
slice: []int64{1, 2, 3, 4, 5},
element: 3,
expected: true,
},
{
name: "6 not in slice",
slice: []int64{1, 2, 3, 4, 5},
element: 6,
expected: false,
},
}
if InSlice(6, slices) {
t.Errorf("Expected false, but got true")
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
res := InSlice(tc.element, tc.slice)
assert.Equal(t, tc.expected, res)
})
}
}
func TestInUintSlices(t *testing.T) {
slices := []uint{1, 2, 3, 4, 5}
if !InSlice(3, slices) {
t.Errorf("Expected true, but got false")
testCases := []struct {
name string
slice []uint
element uint
expected bool
}{
{
name: "3 in slice",
slice: []uint{1, 2, 3, 4, 5},
element: 3,
expected: true,
},
{
name: "6 not in slice",
slice: []uint{1, 2, 3, 4, 5},
element: 6,
expected: false,
},
}
if InSlice(6, slices) {
t.Errorf("Expected false, but got true")
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
res := InSlice(tc.element, tc.slice)
assert.Equal(t, tc.expected, res)
})
}
}
func TestInUint8Slices(t *testing.T) {
slices := []uint8{1, 2, 3, 4, 5}
if !InSlice(3, slices) {
t.Errorf("Expected true, but got false")
testCases := []struct {
name string
slice []uint8
element uint8
expected bool
}{
{
name: "3 in slice",
slice: []uint8{1, 2, 3, 4, 5},
element: 3,
expected: true,
},
{
name: "6 not in slice",
slice: []uint8{1, 2, 3, 4, 5},
element: 6,
expected: false,
},
}
if InSlice(6, slices) {
t.Errorf("Expected false, but got true")
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
res := InSlice(tc.element, tc.slice)
assert.Equal(t, tc.expected, res)
})
}
}
func TestInUint16Slices(t *testing.T) {
slices := []uint16{1, 2, 3, 4, 5}
if !InSlice(3, slices) {
t.Errorf("Expected true, but got false")
testCases := []struct {
name string
slice []uint16
element uint16
expected bool
}{
{
name: "3 in slice",
slice: []uint16{1, 2, 3, 4, 5},
element: 3,
expected: true,
},
{
name: "6 not in slice",
slice: []uint16{1, 2, 3, 4, 5},
element: 6,
expected: false,
},
}
if InSlice(6, slices) {
t.Errorf("Expected false, but got true")
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
res := InSlice(tc.element, tc.slice)
assert.Equal(t, tc.expected, res)
})
}
}
func TestInUint32Slices(t *testing.T) {
slices := []uint32{1, 2, 3, 4, 5}
if !InSlice(3, slices) {
t.Errorf("Expected true, but got false")
testCases := []struct {
name string
slice []uint32
element uint32
expected bool
}{
{
name: "3 in slice",
slice: []uint32{1, 2, 3, 4, 5},
element: 3,
expected: true,
},
{
name: "6 not in slice",
slice: []uint32{1, 2, 3, 4, 5},
element: 6,
expected: false,
},
}
if InSlice(6, slices) {
t.Errorf("Expected false, but got true")
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
res := InSlice(tc.element, tc.slice)
assert.Equal(t, tc.expected, res)
})
}
}
func TestInUint64Slices(t *testing.T) {
slices := []uint64{1, 2, 3, 4, 5}
if !InSlice(3, slices) {
t.Errorf("Expected true, but got false")
testCases := []struct {
name string
slice []uint64
element uint64
expected bool
}{
{
name: "3 in slice",
slice: []uint64{1, 2, 3, 4, 5},
element: 3,
expected: true,
},
{
name: "6 not in slice",
slice: []uint64{1, 2, 3, 4, 5},
element: 6,
expected: false,
},
}
if InSlice(6, slices) {
t.Errorf("Expected false, but got true")
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
res := InSlice(tc.element, tc.slice)
assert.Equal(t, tc.expected, res)
})
}
}