mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-23 13:52:26 +08:00
feat: add Equal function for slice
This commit is contained in:
@@ -203,6 +203,28 @@ func DifferenceBy(slice interface{}, comparedSlice interface{}, iterateeFn inter
|
|||||||
return res.Interface()
|
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.
|
// 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 .
|
// The function signature should be func(index int, value interface{}) bool .
|
||||||
func Every(slice, function interface{}) bool {
|
func Every(slice, function interface{}) bool {
|
||||||
|
|||||||
@@ -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))
|
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) {
|
func TestEvery(t *testing.T) {
|
||||||
nums := []int{1, 2, 3, 5}
|
nums := []int{1, 2, 3, 5}
|
||||||
isEven := func(i, num int) bool {
|
isEven := func(i, num int) bool {
|
||||||
|
|||||||
Reference in New Issue
Block a user