code.gitea.io/gitea@v1.22.3/modules/util/slice_test.go (about) 1 // Copyright 2023 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package util 5 6 import ( 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 ) 11 12 func TestSliceContainsString(t *testing.T) { 13 assert.True(t, SliceContainsString([]string{"c", "b", "a", "b"}, "a")) 14 assert.True(t, SliceContainsString([]string{"c", "b", "a", "b"}, "b")) 15 assert.True(t, SliceContainsString([]string{"c", "b", "a", "b"}, "A", true)) 16 assert.True(t, SliceContainsString([]string{"C", "B", "A", "B"}, "a", true)) 17 18 assert.False(t, SliceContainsString([]string{"c", "b", "a", "b"}, "z")) 19 assert.False(t, SliceContainsString([]string{"c", "b", "a", "b"}, "A")) 20 assert.False(t, SliceContainsString([]string{}, "a")) 21 assert.False(t, SliceContainsString(nil, "a")) 22 } 23 24 func TestSliceSortedEqual(t *testing.T) { 25 assert.True(t, SliceSortedEqual([]int{2, 0, 2, 3}, []int{2, 0, 2, 3})) 26 assert.True(t, SliceSortedEqual([]int{3, 0, 2, 2}, []int{2, 0, 2, 3})) 27 assert.True(t, SliceSortedEqual([]int{}, []int{})) 28 assert.True(t, SliceSortedEqual([]int(nil), nil)) 29 assert.True(t, SliceSortedEqual([]int(nil), []int{})) 30 assert.True(t, SliceSortedEqual([]int{}, []int{})) 31 32 assert.True(t, SliceSortedEqual([]string{"2", "0", "2", "3"}, []string{"2", "0", "2", "3"})) 33 assert.True(t, SliceSortedEqual([]float64{2, 0, 2, 3}, []float64{2, 0, 2, 3})) 34 assert.True(t, SliceSortedEqual([]bool{false, true, false}, []bool{false, true, false})) 35 36 assert.False(t, SliceSortedEqual([]int{2, 0, 2}, []int{2, 0, 2, 3})) 37 assert.False(t, SliceSortedEqual([]int{}, []int{2, 0, 2, 3})) 38 assert.False(t, SliceSortedEqual(nil, []int{2, 0, 2, 3})) 39 assert.False(t, SliceSortedEqual([]int{2, 0, 2, 4}, []int{2, 0, 2, 3})) 40 assert.False(t, SliceSortedEqual([]int{2, 0, 0, 3}, []int{2, 0, 2, 3})) 41 } 42 43 func TestSliceRemoveAll(t *testing.T) { 44 assert.ElementsMatch(t, []int{2, 2, 3}, SliceRemoveAll([]int{2, 0, 2, 3}, 0)) 45 assert.ElementsMatch(t, []int{0, 3}, SliceRemoveAll([]int{2, 0, 2, 3}, 2)) 46 assert.Empty(t, SliceRemoveAll([]int{0, 0, 0, 0}, 0)) 47 assert.ElementsMatch(t, []int{2, 0, 2, 3}, SliceRemoveAll([]int{2, 0, 2, 3}, 4)) 48 assert.Empty(t, SliceRemoveAll([]int{}, 0)) 49 assert.ElementsMatch(t, []int(nil), SliceRemoveAll([]int(nil), 0)) 50 assert.Empty(t, SliceRemoveAll([]int{}, 0)) 51 52 assert.ElementsMatch(t, []string{"2", "2", "3"}, SliceRemoveAll([]string{"2", "0", "2", "3"}, "0")) 53 assert.ElementsMatch(t, []float64{2, 2, 3}, SliceRemoveAll([]float64{2, 0, 2, 3}, 0)) 54 assert.ElementsMatch(t, []bool{false, false}, SliceRemoveAll([]bool{false, true, false}, true)) 55 }