Skip to content

Commit

Permalink
test remove from slice
Browse files Browse the repository at this point in the history
  • Loading branch information
kmulvey committed Oct 15, 2022
1 parent 2ea782c commit 02bc30c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
5 changes: 5 additions & 0 deletions remove.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package goutils

// RemoveElementFromArray does just that, if an index is passed in
// that is larger than the slice length then the original slice is returned.
func RemoveElementFromArray[T any](slice []T, s int) []T {
if s >= len(slice) {
return slice
}
return append(slice[:s], slice[s+1:]...)
}
31 changes: 31 additions & 0 deletions remove_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package goutils

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestRemoveElementFromArray(t *testing.T) {
t.Parallel()

var testArr = make([]int, 10)
for i := 0; i < 10; i++ {
testArr[i] = i
}

testArr = RemoveElementFromArray(testArr, 5)
assert.Equal(t, 9, len(testArr))
for i := 0; i < 9; i++ {
assert.False(t, testArr[i] == 5)
}

testArr = RemoveElementFromArray(testArr, 1)
assert.Equal(t, 8, len(testArr))
for i := 0; i < 8; i++ {
assert.False(t, testArr[i] == 1)
}

testArr = RemoveElementFromArray(testArr, 10)
assert.Equal(t, 8, len(testArr))
}

0 comments on commit 02bc30c

Please sign in to comment.