diff --git a/slice/slice.go b/slice/slice.go index b8ada2f..e56979a 100644 --- a/slice/slice.go +++ b/slice/slice.go @@ -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 { diff --git a/slice/slice_test.go b/slice/slice_test.go index b074bda..6b3727d 100644 --- a/slice/slice_test.go +++ b/slice/slice_test.go @@ -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 {