1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-07 22:22:29 +08:00

feat: add Equal function for slice

This commit is contained in:
dudaodong
2022-06-17 15:37:36 +08:00
parent 690e746811
commit a2541dac03
2 changed files with 33 additions and 0 deletions

View File

@@ -203,6 +203,28 @@ func DifferenceBy(slice interface{}, comparedSlice interface{}, iterateeFn inter
return res.Interface()
}
// Equal checks if two slices are equal: the same length and all elements' order and value are equal
func Equal(slice1, slice2 interface{}) bool {
sv1 := sliceValue(slice1)
sv2 := sliceValue(slice2)
if sv1.Type().Elem() != sv2.Type().Elem() {
return false
}
if sv1.Len() != sv2.Len() {
return false
}
for i := 0; i < sv1.Len(); i++ {
if sv1.Index(i).Interface() != sv2.Index(i).Interface() {
return false
}
}
return true
}
// Every return true if all of the values in the slice pass the predicate function.
// The function signature should be func(index int, value interface{}) bool .
func Every(slice, function interface{}) bool {

View File

@@ -73,6 +73,17 @@ func TestConcat(t *testing.T) {
assert.Equal([]int{1, 2, 3, 4, 5}, Concat([]int{1, 2, 3}, []int{4}, 5))
}
func TestEqual(t *testing.T) {
assert := internal.NewAssert(t, "TestEqual")
slice1 := []int{1, 2, 3}
slice2 := []int{1, 2, 3}
slice3 := []int{3, 2, 1}
assert.Equal(true, Equal(slice1, slice2))
assert.Equal(false, Equal(slice1, slice3))
}
func TestEvery(t *testing.T) {
nums := []int{1, 2, 3, 5}
isEven := func(i, num int) bool {