Skip to content

Commit

Permalink
fix (#256)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bin-Huang committed Nov 15, 2022
1 parent 5781b44 commit dba7426
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
6 changes: 6 additions & 0 deletions slice.go
Expand Up @@ -513,10 +513,16 @@ func Slice[T any](collection []T, start int, end int) []T {
if start > size {
start = size
}
if start < 0 {
start = 0
}

if end > size {
end = size
}
if end < 0 {
end = 0
}

return collection[start:end]
}
Expand Down
6 changes: 6 additions & 0 deletions slice_test.go
Expand Up @@ -623,6 +623,9 @@ func TestSlice(t *testing.T) {
out13 := Slice(in, 5, 0)
out14 := Slice(in, 6, 4)
out15 := Slice(in, 6, 7)
out16 := Slice(in, -10, 1)
out17 := Slice(in, -1, 3)
out18 := Slice(in, -10, 7)

is.Equal([]int{}, out1)
is.Equal([]int{0}, out2)
Expand All @@ -639,6 +642,9 @@ func TestSlice(t *testing.T) {
is.Equal([]int{}, out13)
is.Equal([]int{}, out14)
is.Equal([]int{}, out15)
is.Equal([]int{0}, out16)
is.Equal([]int{0, 1, 2}, out17)
is.Equal([]int{0, 1, 2, 3, 4}, out18)
}

func TestReplace(t *testing.T) {
Expand Down

0 comments on commit dba7426

Please sign in to comment.