Improve utils of slices (#22379)
- Move the file `compare.go` and `slice.go` to `slice.go`.
- Fix `ExistsInSlice`, it's buggy
- It uses `sort.Search`, so it assumes that the input slice is sorted.
- It passes `func(i int) bool { return slice[i] == target })` to
`sort.Search`, that's incorrect, check the doc of `sort.Search`.
- Conbine `IsInt64InSlice(int64, []int64)` and `ExistsInSlice(string,
[]string)` to `SliceContains[T]([]T, T)`.
- Conbine `IsSliceInt64Eq([]int64, []int64)` and `IsEqualSlice([]string,
[]string)` to `SliceSortedEqual[T]([]T, T)`.
- Add `SliceEqual[T]([]T, T)` as a distinction from
`SliceSortedEqual[T]([]T, T)`.
- Redesign `RemoveIDFromList([]int64, int64) ([]int64, bool)` to
`SliceRemoveAll[T]([]T, T) []T`.
- Add `SliceContainsFunc[T]([]T, func(T) bool)` and
`SliceRemoveAllFunc[T]([]T, func(T) bool)` for general use.
- Add comments to explain why not `golang.org/x/exp/slices`.
- Add unit tests.
2023-01-11 13:31:16 +08:00
|
|
|
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSliceContains(t *testing.T) {
|
|
|
|
assert.True(t, SliceContains([]int{2, 0, 2, 3}, 2))
|
|
|
|
assert.True(t, SliceContains([]int{2, 0, 2, 3}, 0))
|
|
|
|
assert.True(t, SliceContains([]int{2, 0, 2, 3}, 3))
|
|
|
|
|
|
|
|
assert.True(t, SliceContains([]string{"2", "0", "2", "3"}, "0"))
|
|
|
|
assert.True(t, SliceContains([]float64{2, 0, 2, 3}, 0))
|
|
|
|
assert.True(t, SliceContains([]bool{false, true, false}, true))
|
|
|
|
|
|
|
|
assert.False(t, SliceContains([]int{2, 0, 2, 3}, 4))
|
|
|
|
assert.False(t, SliceContains([]int{}, 4))
|
|
|
|
assert.False(t, SliceContains(nil, 4))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSliceContainsString(t *testing.T) {
|
|
|
|
assert.True(t, SliceContainsString([]string{"c", "b", "a", "b"}, "a"))
|
|
|
|
assert.True(t, SliceContainsString([]string{"c", "b", "a", "b"}, "b"))
|
|
|
|
assert.True(t, SliceContainsString([]string{"c", "b", "a", "b"}, "A", true))
|
|
|
|
assert.True(t, SliceContainsString([]string{"C", "B", "A", "B"}, "a", true))
|
|
|
|
|
|
|
|
assert.False(t, SliceContainsString([]string{"c", "b", "a", "b"}, "z"))
|
|
|
|
assert.False(t, SliceContainsString([]string{"c", "b", "a", "b"}, "A"))
|
|
|
|
assert.False(t, SliceContainsString([]string{}, "a"))
|
|
|
|
assert.False(t, SliceContainsString(nil, "a"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSliceSortedEqual(t *testing.T) {
|
|
|
|
assert.True(t, SliceSortedEqual([]int{2, 0, 2, 3}, []int{2, 0, 2, 3}))
|
|
|
|
assert.True(t, SliceSortedEqual([]int{3, 0, 2, 2}, []int{2, 0, 2, 3}))
|
|
|
|
assert.True(t, SliceSortedEqual([]int{}, []int{}))
|
|
|
|
assert.True(t, SliceSortedEqual([]int(nil), nil))
|
|
|
|
assert.True(t, SliceSortedEqual([]int(nil), []int{}))
|
|
|
|
assert.True(t, SliceSortedEqual([]int{}, []int{}))
|
|
|
|
|
|
|
|
assert.True(t, SliceSortedEqual([]string{"2", "0", "2", "3"}, []string{"2", "0", "2", "3"}))
|
|
|
|
assert.True(t, SliceSortedEqual([]float64{2, 0, 2, 3}, []float64{2, 0, 2, 3}))
|
|
|
|
assert.True(t, SliceSortedEqual([]bool{false, true, false}, []bool{false, true, false}))
|
|
|
|
|
|
|
|
assert.False(t, SliceSortedEqual([]int{2, 0, 2}, []int{2, 0, 2, 3}))
|
|
|
|
assert.False(t, SliceSortedEqual([]int{}, []int{2, 0, 2, 3}))
|
|
|
|
assert.False(t, SliceSortedEqual(nil, []int{2, 0, 2, 3}))
|
|
|
|
assert.False(t, SliceSortedEqual([]int{2, 0, 2, 4}, []int{2, 0, 2, 3}))
|
|
|
|
assert.False(t, SliceSortedEqual([]int{2, 0, 0, 3}, []int{2, 0, 2, 3}))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSliceEqual(t *testing.T) {
|
|
|
|
assert.True(t, SliceEqual([]int{2, 0, 2, 3}, []int{2, 0, 2, 3}))
|
|
|
|
assert.True(t, SliceEqual([]int{}, []int{}))
|
|
|
|
assert.True(t, SliceEqual([]int(nil), nil))
|
|
|
|
assert.True(t, SliceEqual([]int(nil), []int{}))
|
|
|
|
assert.True(t, SliceEqual([]int{}, []int{}))
|
|
|
|
|
|
|
|
assert.True(t, SliceEqual([]string{"2", "0", "2", "3"}, []string{"2", "0", "2", "3"}))
|
|
|
|
assert.True(t, SliceEqual([]float64{2, 0, 2, 3}, []float64{2, 0, 2, 3}))
|
|
|
|
assert.True(t, SliceEqual([]bool{false, true, false}, []bool{false, true, false}))
|
|
|
|
|
|
|
|
assert.False(t, SliceEqual([]int{3, 0, 2, 2}, []int{2, 0, 2, 3}))
|
|
|
|
assert.False(t, SliceEqual([]int{2, 0, 2}, []int{2, 0, 2, 3}))
|
|
|
|
assert.False(t, SliceEqual([]int{}, []int{2, 0, 2, 3}))
|
|
|
|
assert.False(t, SliceEqual(nil, []int{2, 0, 2, 3}))
|
|
|
|
assert.False(t, SliceEqual([]int{2, 0, 2, 4}, []int{2, 0, 2, 3}))
|
|
|
|
assert.False(t, SliceEqual([]int{2, 0, 0, 3}, []int{2, 0, 2, 3}))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSliceRemoveAll(t *testing.T) {
|
2023-04-23 05:56:27 +08:00
|
|
|
assert.ElementsMatch(t, []int{2, 2, 3}, SliceRemoveAll([]int{2, 0, 2, 3}, 0))
|
|
|
|
assert.ElementsMatch(t, []int{0, 3}, SliceRemoveAll([]int{2, 0, 2, 3}, 2))
|
|
|
|
assert.Empty(t, SliceRemoveAll([]int{0, 0, 0, 0}, 0))
|
|
|
|
assert.ElementsMatch(t, []int{2, 0, 2, 3}, SliceRemoveAll([]int{2, 0, 2, 3}, 4))
|
|
|
|
assert.Empty(t, SliceRemoveAll([]int{}, 0))
|
|
|
|
assert.ElementsMatch(t, []int(nil), SliceRemoveAll([]int(nil), 0))
|
|
|
|
assert.Empty(t, SliceRemoveAll([]int{}, 0))
|
|
|
|
|
|
|
|
assert.ElementsMatch(t, []string{"2", "2", "3"}, SliceRemoveAll([]string{"2", "0", "2", "3"}, "0"))
|
|
|
|
assert.ElementsMatch(t, []float64{2, 2, 3}, SliceRemoveAll([]float64{2, 0, 2, 3}, 0))
|
|
|
|
assert.ElementsMatch(t, []bool{false, false}, SliceRemoveAll([]bool{false, true, false}, true))
|
Improve utils of slices (#22379)
- Move the file `compare.go` and `slice.go` to `slice.go`.
- Fix `ExistsInSlice`, it's buggy
- It uses `sort.Search`, so it assumes that the input slice is sorted.
- It passes `func(i int) bool { return slice[i] == target })` to
`sort.Search`, that's incorrect, check the doc of `sort.Search`.
- Conbine `IsInt64InSlice(int64, []int64)` and `ExistsInSlice(string,
[]string)` to `SliceContains[T]([]T, T)`.
- Conbine `IsSliceInt64Eq([]int64, []int64)` and `IsEqualSlice([]string,
[]string)` to `SliceSortedEqual[T]([]T, T)`.
- Add `SliceEqual[T]([]T, T)` as a distinction from
`SliceSortedEqual[T]([]T, T)`.
- Redesign `RemoveIDFromList([]int64, int64) ([]int64, bool)` to
`SliceRemoveAll[T]([]T, T) []T`.
- Add `SliceContainsFunc[T]([]T, func(T) bool)` and
`SliceRemoveAllFunc[T]([]T, func(T) bool)` for general use.
- Add comments to explain why not `golang.org/x/exp/slices`.
- Add unit tests.
2023-01-11 13:31:16 +08:00
|
|
|
}
|