142 KiB
Collection
collection 用于对 input 和 map 操作的工具函数
目录导航
列出了该 package 下所有的函数及类型定义,可通过目录导航进行快捷跳转 ❤️
展开 / 折叠目录导航
包级函数定义
| 函数名称 | 描述 |
|---|---|
| CloneSlice | 克隆切片,该函数是 slices.Clone 的快捷方式 |
| CloneMap | 克隆 map |
| CloneSliceN | 克隆 slice 为 n 个切片进行返回 |
| CloneMapN | 克隆 map 为 n 个 map 进行返回 |
| CloneSlices | 克隆多个切片 |
| CloneMaps | 克隆多个 map |
| InSlice | 检查 v 是否被包含在 slice 中,当 handler 返回 true 时,表示 v 和 slice 中的某个元素相匹配 |
| InComparableSlice | 检查 v 是否被包含在 slice 中 |
| AllInSlice | 检查 values 中的所有元素是否均被包含在 slice 中,当 handler 返回 true 时,表示 values 中的某个元素和 slice 中的某个元素相匹配 |
| AllInComparableSlice | 检查 values 中的所有元素是否均被包含在 slice 中 |
| AnyInSlice | 检查 values 中的任意一个元素是否被包含在 slice 中,当 handler 返回 true 时,表示 value 中的某个元素和 slice 中的某个元素相匹配 |
| AnyInComparableSlice | 检查 values 中的任意一个元素是否被包含在 slice 中 |
| InSlices | 通过将多个切片合并后检查 v 是否被包含在 slices 中,当 handler 返回 true 时,表示 v 和 slices 中的某个元素相匹配 |
| InComparableSlices | 通过将多个切片合并后检查 v 是否被包含在 slices 中 |
| AllInSlices | 通过将多个切片合并后检查 values 中的所有元素是否被包含在 slices 中,当 handler 返回 true 时,表示 values 中的某个元素和 slices 中的某个元素相匹配 |
| AllInComparableSlices | 通过将多个切片合并后检查 values 中的所有元素是否被包含在 slices 中 |
| AnyInSlices | 通过将多个切片合并后检查 values 中的任意一个元素是否被包含在 slices 中,当 handler 返回 true 时,表示 values 中的某个元素和 slices 中的某个元素相匹配 |
| AnyInComparableSlices | 通过将多个切片合并后检查 values 中的任意一个元素是否被包含在 slices 中 |
| InAllSlices | 检查 v 是否被包含在 slices 的每一项元素中,当 handler 返回 true 时,表示 v 和 slices 中的某个元素相匹配 |
| InAllComparableSlices | 检查 v 是否被包含在 slices 的每一项元素中 |
| AnyInAllSlices | 检查 slices 中的每一个元素是否均包含至少任意一个 values 中的元素,当 handler 返回 true 时,表示 value 中的某个元素和 slices 中的某个元素相匹配 |
| AnyInAllComparableSlices | 检查 slices 中的每一个元素是否均包含至少任意一个 values 中的元素 |
| KeyInMap | 检查 m 中是否包含特定 key |
| ValueInMap | 检查 m 中是否包含特定 value,当 handler 返回 true 时,表示 value 和 m 中的某个元素相匹配 |
| AllKeyInMap | 检查 m 中是否包含 keys 中所有的元素 |
| AllValueInMap | 检查 m 中是否包含 values 中所有的元素,当 handler 返回 true 时,表示 values 中的某个元素和 m 中的某个元素相匹配 |
| AnyKeyInMap | 检查 m 中是否包含 keys 中任意一个元素 |
| AnyValueInMap | 检查 m 中是否包含 values 中任意一个元素,当 handler 返回 true 时,表示 values 中的某个元素和 m 中的某个元素相匹配 |
| AllKeyInMaps | 检查 maps 中的每一个元素是否均包含 keys 中所有的元素 |
| AllValueInMaps | 检查 maps 中的每一个元素是否均包含 value 中所有的元素,当 handler 返回 true 时,表示 value 中的某个元素和 maps 中的某个元素相匹配 |
| AnyKeyInMaps | 检查 keys 中的任意一个元素是否被包含在 maps 中的任意一个元素中 |
| AnyValueInMaps | 检查 maps 中的任意一个元素是否包含 value 中的任意一个元素,当 handler 返回 true 时,表示 value 中的某个元素和 maps 中的某个元素相匹配 |
| KeyInAllMaps | 检查 key 是否被包含在 maps 的每一个元素中 |
| AnyKeyInAllMaps | 检查 maps 中的每一个元素是否均包含 keys 中任意一个元素 |
| ConvertSliceToAny | 将切片转换为任意类型的切片 |
| ConvertSliceToIndexMap | 将切片转换为索引为键的映射 |
| ConvertSliceToIndexOnlyMap | 将切片转换为索引为键的映射 |
| ConvertSliceToMap | 将切片转换为值为键的映射 |
| ConvertSliceToBoolMap | 将切片转换为值为键的映射 |
| ConvertMapKeysToSlice | 将映射的键转换为切片 |
| ConvertMapValuesToSlice | 将映射的值转换为切片 |
| InvertMap | 将映射的键和值互换 |
| ConvertMapValuesToBool | 将映射的值转换为布尔值 |
| ReverseSlice | 将切片反转 |
| ClearSlice | 清空切片 |
| ClearMap | 清空 map |
| DropSliceByIndices | 删除切片中特定索引的元素 |
| DropSliceByCondition | 删除切片中符合条件的元素 |
| DropSliceOverlappingElements | 删除切片中与另一个切片重叠的元素 |
| DeduplicateSliceInPlace | 去除切片中的重复元素 |
| DeduplicateSlice | 去除切片中的重复元素,返回新切片 |
| DeduplicateSliceInPlaceWithCompare | 去除切片中的重复元素,使用自定义的比较函数 |
| DeduplicateSliceWithCompare | 去除切片中的重复元素,使用自定义的比较函数,返回新的切片 |
| FilterOutByIndices | 过滤切片中特定索引的元素,返回过滤后的切片 |
| FilterOutByCondition | 过滤切片中符合条件的元素,返回过滤后的切片 |
| FilterOutByKey | 过滤 map 中特定的 key,返回过滤后的 map |
| FilterOutByValue | 过滤 map 中特定的 value,返回过滤后的 map |
| FilterOutByKeys | 过滤 map 中多个 key,返回过滤后的 map |
| FilterOutByValues | 过滤 map 中多个 values,返回过滤后的 map |
| FilterOutByMap | 过滤 map 中符合条件的元素,返回过滤后的 map |
| FindLoopedNextInSlice | 返回 i 的下一个数组成员,当 i 达到数组长度时从 0 开始 |
| FindLoopedPrevInSlice | 返回 i 的上一个数组成员,当 i 为 0 时从数组末尾开始 |
| FindCombinationsInSliceByRange | 获取给定数组的所有组合,且每个组合的成员数量限制在指定范围内 |
| FindFirstOrDefaultInSlice | 判断切片中是否存在元素,返回第一个元素,不存在则返回默认值 |
| FindOrDefaultInSlice | 判断切片中是否存在某个元素,返回第一个匹配的索引和元素,不存在则返回默认值 |
| FindOrDefaultInComparableSlice | 判断切片中是否存在某个元素,返回第一个匹配的索引和元素,不存在则返回默认值 |
| FindInSlice | 判断切片中是否存在某个元素,返回第一个匹配的索引和元素,不存在则索引返回 -1 |
| FindIndexInSlice | 判断切片中是否存在某个元素,返回第一个匹配的索引,不存在则索引返回 -1 |
| FindInComparableSlice | 判断切片中是否存在某个元素,返回第一个匹配的索引和元素,不存在则索引返回 -1 |
| FindIndexInComparableSlice | 判断切片中是否存在某个元素,返回第一个匹配的索引,不存在则索引返回 -1 |
| FindMinimumInComparableSlice | 获取切片中的最小值 |
| FindMinimumInSlice | 获取切片中的最小值 |
| FindMaximumInComparableSlice | 获取切片中的最大值 |
| FindMaximumInSlice | 获取切片中的最大值 |
| FindMin2MaxInComparableSlice | 获取切片中的最小值和最大值 |
| FindMin2MaxInSlice | 获取切片中的最小值和最大值 |
| FindMinFromComparableMap | 获取 map 中的最小值 |
| FindMinFromMap | 获取 map 中的最小值 |
| FindMaxFromComparableMap | 获取 map 中的最大值 |
| FindMaxFromMap | 获取 map 中的最大值 |
| FindMin2MaxFromComparableMap | 获取 map 中的最小值和最大值 |
| FindMin2MaxFromMap | 获取 map 中的最小值和最大值 |
| SwapSlice | 将切片中的两个元素进行交换 |
| MappingFromSlice | 将切片中的元素进行转换 |
| MappingFromMap | 将 map 中的元素进行转换 |
| MergeSlices | 合并切片 |
| MergeMaps | 合并 map,当多个 map 中存在相同的 key 时,后面的 map 中的 key 将会覆盖前面的 map 中的 key |
| MergeMapsWithSkip | 合并 map,当多个 map 中存在相同的 key 时,后面的 map 中的 key 将会被跳过 |
| ChooseRandomSliceElementRepeatN | 返回 slice 中的 n 个可重复随机元素 |
| ChooseRandomIndexRepeatN | 返回 slice 中的 n 个可重复随机元素的索引 |
| ChooseRandomSliceElement | 返回 slice 中随机一个元素,当 slice 长度为 0 时将会得到 V 的零值 |
| ChooseRandomIndex | 返回 slice 中随机一个元素的索引,当 slice 长度为 0 时将会得到 -1 |
| ChooseRandomSliceElementN | 返回 slice 中的 n 个不可重复的随机元素 |
| ChooseRandomIndexN | 获取切片中的 n 个随机元素的索引 |
| ChooseRandomMapKeyRepeatN | 获取 map 中的 n 个随机 key,允许重复 |
| ChooseRandomMapValueRepeatN | 获取 map 中的 n 个随机 n,允许重复 |
| ChooseRandomMapKeyAndValueRepeatN | 获取 map 中的 n 个随机 key 和 v,允许重复 |
| ChooseRandomMapKey | 获取 map 中的随机 key |
| ChooseRandomMapValue | 获取 map 中的随机 value |
| ChooseRandomMapKeyN | 获取 map 中的 inputN 个随机 key |
| ChooseRandomMapValueN | 获取 map 中的 n 个随机 value |
| ChooseRandomMapKeyAndValue | 获取 map 中的随机 key 和 v |
| ChooseRandomMapKeyAndValueN | 获取 map 中的 inputN 个随机 key 和 v |
| DescBy | 返回降序比较结果 |
| AscBy | 返回升序比较结果 |
| Desc | 对切片进行降序排序 |
| DescByClone | 对切片进行降序排序,返回排序后的切片 |
| Asc | 对切片进行升序排序 |
| AscByClone | 对切片进行升序排序,返回排序后的切片 |
| Shuffle | 对切片进行随机排序 |
| ShuffleByClone | 对切片进行随机排序,返回排序后的切片 |
类型定义
| 类型 | 名称 | 描述 |
|---|---|---|
STRUCT |
ComparisonHandler | 暂无描述... |
STRUCT |
OrderedValueGetter | 暂无描述... |
详情信息
func CloneSlice(slice S) S
克隆切片,该函数是 slices.Clone 的快捷方式
示例代码:
func ExampleCloneSlice() {
var slice = []int{1, 2, 3}
var result = collection.CloneSlice(slice)
fmt.Println(result)
}
查看 / 收起单元测试
func TestCloneSlice(t *testing.T) {
var cases = []struct {
name string
input []int
expected []int
}{{"TestCloneSlice_NonEmptySlice", []int{1, 2, 3}, []int{1, 2, 3}}, {"TestCloneSlice_EmptySlice", []int{}, []int{}}, {"TestCloneSlice_NilSlice", nil, nil}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
var actual = collection.CloneSlice(c.input)
if len(actual) != len(c.expected) {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "after clone, the length of input is not equal")
}
for i := 0; i < len(actual); i++ {
if actual[i] != c.expected[i] {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "after clone, the inputV of input is not equal")
}
}
})
}
}
func CloneMap(m M) M
克隆 map
示例代码:
func ExampleCloneMap() {
var m = map[int]int{1: 1, 2: 2, 3: 3}
var result = collection.CloneMap(m)
fmt.Println(len(result))
}
查看 / 收起单元测试
func TestCloneMap(t *testing.T) {
var cases = []struct {
name string
input map[int]int
expected map[int]int
}{{"TestCloneMap_NonEmptyMap", map[int]int{1: 1, 2: 2, 3: 3}, map[int]int{1: 1, 2: 2, 3: 3}}, {"TestCloneMap_EmptyMap", map[int]int{}, map[int]int{}}, {"TestCloneMap_NilMap", nil, nil}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
var actual = collection.CloneMap(c.input)
if len(actual) != len(c.expected) {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "after clone, the length of map is not equal")
}
for k, v := range actual {
if v != c.expected[k] {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "after clone, the inputV of map is not equal")
}
}
})
}
}
func CloneSliceN(slice S, n int) []S
克隆 slice 为 n 个切片进行返回
示例代码:
func ExampleCloneSliceN() {
var slice = []int{1, 2, 3}
var result = collection.CloneSliceN(slice, 2)
fmt.Println(len(result))
}
查看 / 收起单元测试
func TestCloneSliceN(t *testing.T) {
var cases = []struct {
name string
input []int
inputN int
expected [][]int
}{{"TestCloneSliceN_NonEmptySlice", []int{1, 2, 3}, 2, [][]int{{1, 2, 3}, {1, 2, 3}}}, {"TestCloneSliceN_EmptySlice", []int{}, 2, [][]int{{}, {}}}, {"TestCloneSliceN_NilSlice", nil, 2, nil}, {"TestCloneSliceN_NonEmptySlice_ZeroN", []int{1, 2, 3}, 0, [][]int{}}, {"TestCloneSliceN_EmptySlice_ZeroN", []int{}, 0, [][]int{}}, {"TestCloneSliceN_NilSlice_ZeroN", nil, 0, nil}, {"TestCloneSliceN_NonEmptySlice_NegativeN", []int{1, 2, 3}, -1, [][]int{}}, {"TestCloneSliceN_EmptySlice_NegativeN", []int{}, -1, [][]int{}}, {"TestCloneSliceN_NilSlice_NegativeN", nil, -1, nil}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
var actual = collection.CloneSliceN(c.input, c.inputN)
if actual == nil {
if c.expected != nil {
t.Fatalf("%s failed, expected: %v, actual: %v, inputN: %d, error: %s", c.name, c.expected, c.inputN, actual, "after clone, the expected is nil")
}
return
}
for a, i := range actual {
for b, v := range i {
if v != c.expected[a][b] {
t.Fatalf("%s failed, expected: %v, actual: %v, inputN: %d, error: %s", c.name, c.expected, c.inputN, actual, "after clone, the inputV of input is not equal")
}
}
}
})
}
}
func CloneMapN(m M, n int) []M
克隆 map 为 n 个 map 进行返回
示例代码:
func ExampleCloneMapN() {
var m = map[int]int{1: 1, 2: 2, 3: 3}
var result = collection.CloneMapN(m, 2)
fmt.Println(len(result))
}
查看 / 收起单元测试
func TestCloneMapN(t *testing.T) {
var cases = []struct {
name string
input map[int]int
inputN int
expected []map[int]int
}{{"TestCloneMapN_NonEmptyMap", map[int]int{1: 1, 2: 2, 3: 3}, 2, []map[int]int{{1: 1, 2: 2, 3: 3}, {1: 1, 2: 2, 3: 3}}}, {"TestCloneMapN_EmptyMap", map[int]int{}, 2, []map[int]int{{}, {}}}, {"TestCloneMapN_NilMap", nil, 2, nil}, {"TestCloneMapN_NonEmptyMap_ZeroN", map[int]int{1: 1, 2: 2, 3: 3}, 0, []map[int]int{}}, {"TestCloneMapN_EmptyMap_ZeroN", map[int]int{}, 0, []map[int]int{}}, {"TestCloneMapN_NilMap_ZeroN", nil, 0, nil}, {"TestCloneMapN_NonEmptyMap_NegativeN", map[int]int{1: 1, 2: 2, 3: 3}, -1, []map[int]int{}}, {"TestCloneMapN_EmptyMap_NegativeN", map[int]int{}, -1, []map[int]int{}}, {"TestCloneMapN_NilMap_NegativeN", nil, -1, nil}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
var actual = collection.CloneMapN(c.input, c.inputN)
if actual == nil {
if c.expected != nil {
t.Fatalf("%s failed, expected: %v, actual: %v, inputN: %d, error: %s", c.name, c.expected, actual, c.inputN, "after clone, the expected is nil")
}
return
}
for a, i := range actual {
for b, v := range i {
if v != c.expected[a][b] {
t.Fatalf("%s failed, expected: %v, actual: %v, inputN: %d, error: %s", c.name, c.expected, actual, c.inputN, "after clone, the inputV of map is not equal")
}
}
}
})
}
}
func CloneSlices(slices ...S) []S
克隆多个切片
示例代码:
func ExampleCloneSlices() {
var slice1 = []int{1, 2, 3}
var slice2 = []int{1, 2, 3}
var result = collection.CloneSlices(slice1, slice2)
fmt.Println(len(result))
}
查看 / 收起单元测试
func TestCloneSlices(t *testing.T) {
var cases = []struct {
name string
input [][]int
expected [][]int
}{{"TestCloneSlices_NonEmptySlices", [][]int{{1, 2, 3}, {1, 2, 3}}, [][]int{{1, 2, 3}, {1, 2, 3}}}, {"TestCloneSlices_EmptySlices", [][]int{{}, {}}, [][]int{{}, {}}}, {"TestCloneSlices_NilSlices", nil, nil}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
var actual = collection.CloneSlices(c.input...)
if len(actual) != len(c.expected) {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "after clone, the length of input is not equal")
}
for a, i := range actual {
for b, v := range i {
if v != c.expected[a][b] {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "after clone, the inputV of input is not equal")
}
}
}
})
}
}
func CloneMaps(maps ...M) []M
克隆多个 map
示例代码:
func ExampleCloneMaps() {
var m1 = map[int]int{1: 1, 2: 2, 3: 3}
var m2 = map[int]int{1: 1, 2: 2, 3: 3}
var result = collection.CloneMaps(m1, m2)
fmt.Println(len(result))
}
查看 / 收起单元测试
func TestCloneMaps(t *testing.T) {
var cases = []struct {
name string
input []map[int]int
expected []map[int]int
}{{"TestCloneMaps_NonEmptyMaps", []map[int]int{{1: 1, 2: 2, 3: 3}, {1: 1, 2: 2, 3: 3}}, []map[int]int{{1: 1, 2: 2, 3: 3}, {1: 1, 2: 2, 3: 3}}}, {"TestCloneMaps_EmptyMaps", []map[int]int{{}, {}}, []map[int]int{{}, {}}}, {"TestCloneMaps_NilMaps", nil, nil}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
var actual = collection.CloneMaps(c.input...)
if len(actual) != len(c.expected) {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "after clone, the length of maps is not equal")
}
for a, i := range actual {
for b, v := range i {
if v != c.expected[a][b] {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "after clone, the inputV of maps is not equal")
}
}
}
})
}
}
func InSlice(slice S, v V, handler ComparisonHandler[V]) bool
检查 v 是否被包含在 slice 中,当 handler 返回 true 时,表示 v 和 slice 中的某个元素相匹配
示例代码:
func ExampleInSlice() {
result := collection.InSlice([]int{1, 2, 3}, 2, func(source, target int) bool {
return source == target
})
fmt.Println(result)
}
查看 / 收起单元测试
func TestInSlice(t *testing.T) {
var cases = []struct {
name string
input []int
inputV int
expected bool
}{{"TestInSlice_NonEmptySliceIn", []int{1, 2, 3}, 1, true}, {"TestInSlice_NonEmptySliceNotIn", []int{1, 2, 3}, 4, false}, {"TestInSlice_EmptySlice", []int{}, 1, false}, {"TestInSlice_NilSlice", nil, 1, false}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
var actual = collection.InSlice(c.input, c.inputV, func(source, target int) bool {
return source == target
})
if actual != c.expected {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "after clone, the length of input is not equal")
}
})
}
}
func InComparableSlice(slice S, v V) bool
检查 v 是否被包含在 slice 中
示例代码:
func ExampleInComparableSlice() {
result := collection.InComparableSlice([]int{1, 2, 3}, 2)
fmt.Println(result)
}
查看 / 收起单元测试
func TestInComparableSlice(t *testing.T) {
var cases = []struct {
name string
input []int
inputV int
expected bool
}{{"TestInComparableSlice_NonEmptySliceIn", []int{1, 2, 3}, 1, true}, {"TestInComparableSlice_NonEmptySliceNotIn", []int{1, 2, 3}, 4, false}, {"TestInComparableSlice_EmptySlice", []int{}, 1, false}, {"TestInComparableSlice_NilSlice", nil, 1, false}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
var actual = collection.InComparableSlice(c.input, c.inputV)
if actual != c.expected {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "after clone, the length of input is not equal")
}
})
}
}
func AllInSlice(slice S, values []V, handler ComparisonHandler[V]) bool
检查 values 中的所有元素是否均被包含在 slice 中,当 handler 返回 true 时,表示 values 中的某个元素和 slice 中的某个元素相匹配
- 在所有 values 中的元素都被包含在 slice 中时,返回 true
- 当 values 长度为 0 或为 nil 时,将返回 true
示例代码:
func ExampleAllInSlice() {
result := collection.AllInSlice([]int{1, 2, 3}, []int{1, 2}, func(source, target int) bool {
return source == target
})
fmt.Println(result)
}
查看 / 收起单元测试
func TestAllInSlice(t *testing.T) {
var cases = []struct {
name string
input []int
inputV []int
expected bool
}{{"TestAllInSlice_NonEmptySliceIn", []int{1, 2, 3}, []int{1, 2}, true}, {"TestAllInSlice_NonEmptySliceNotIn", []int{1, 2, 3}, []int{1, 4}, false}, {"TestAllInSlice_EmptySlice", []int{}, []int{1, 2}, false}, {"TestAllInSlice_NilSlice", nil, []int{1, 2}, false}, {"TestAllInSlice_EmptyValueSlice", []int{1, 2, 3}, []int{}, true}, {"TestAllInSlice_NilValueSlice", []int{1, 2, 3}, nil, true}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
var actual = collection.AllInSlice(c.input, c.inputV, func(source, target int) bool {
return source == target
})
if actual != c.expected {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "after clone, the length of input is not equal")
}
})
}
}
func AllInComparableSlice(slice S, values []V) bool
检查 values 中的所有元素是否均被包含在 slice 中
- 在所有 values 中的元素都被包含在 slice 中时,返回 true
- 当 values 长度为 0 或为 nil 时,将返回 true
示例代码:
func ExampleAllInComparableSlice() {
result := collection.AllInComparableSlice([]int{1, 2, 3}, []int{1, 2})
fmt.Println(result)
}
查看 / 收起单元测试
func TestAllInComparableSlice(t *testing.T) {
var cases = []struct {
name string
input []int
inputV []int
expected bool
}{{"TestAllInComparableSlice_NonEmptySliceIn", []int{1, 2, 3}, []int{1, 2}, true}, {"TestAllInComparableSlice_NonEmptySliceNotIn", []int{1, 2, 3}, []int{1, 4}, false}, {"TestAllInComparableSlice_EmptySlice", []int{}, []int{1, 2}, false}, {"TestAllInComparableSlice_NilSlice", nil, []int{1, 2}, false}, {"TestAllInComparableSlice_EmptyValueSlice", []int{1, 2, 3}, []int{}, true}, {"TestAllInComparableSlice_NilValueSlice", []int{1, 2, 3}, nil, true}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
var actual = collection.AllInComparableSlice(c.input, c.inputV)
if actual != c.expected {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "not as expected")
}
})
}
}
func AnyInSlice(slice S, values []V, handler ComparisonHandler[V]) bool
检查 values 中的任意一个元素是否被包含在 slice 中,当 handler 返回 true 时,表示 value 中的某个元素和 slice 中的某个元素相匹配
- 当 values 中的任意一个元素被包含在 slice 中时,返回 true
示例代码:
func ExampleAnyInSlice() {
result := collection.AnyInSlice([]int{1, 2, 3}, []int{1, 2}, func(source, target int) bool {
return source == target
})
fmt.Println(result)
}
查看 / 收起单元测试
func TestAnyInSlice(t *testing.T) {
var cases = []struct {
name string
input []int
inputV []int
expected bool
}{{"TestAnyInSlice_NonEmptySliceIn", []int{1, 2, 3}, []int{1, 2}, true}, {"TestAnyInSlice_NonEmptySliceNotIn", []int{1, 2, 3}, []int{1, 4}, true}, {"TestAnyInSlice_EmptySlice", []int{}, []int{1, 2}, false}, {"TestAnyInSlice_NilSlice", nil, []int{1, 2}, false}, {"TestAnyInSlice_EmptyValueSlice", []int{1, 2, 3}, []int{}, false}, {"TestAnyInSlice_NilValueSlice", []int{1, 2, 3}, nil, false}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
var actual = collection.AnyInSlice(c.input, c.inputV, func(source, target int) bool {
return source == target
})
if actual != c.expected {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "after clone, the length of input is not equal")
}
})
}
}
func AnyInComparableSlice(slice S, values []V) bool
检查 values 中的任意一个元素是否被包含在 slice 中
- 当 values 中的任意一个元素被包含在 slice 中时,返回 true
示例代码:
func ExampleAnyInComparableSlice() {
result := collection.AnyInComparableSlice([]int{1, 2, 3}, []int{1, 2})
fmt.Println(result)
}
查看 / 收起单元测试
func TestAnyInComparableSlice(t *testing.T) {
var cases = []struct {
name string
input []int
inputV []int
expected bool
}{{"TestAnyInComparableSlice_NonEmptySliceIn", []int{1, 2, 3}, []int{1, 2}, true}, {"TestAnyInComparableSlice_NonEmptySliceNotIn", []int{1, 2, 3}, []int{1, 4}, true}, {"TestAnyInComparableSlice_EmptySlice", []int{}, []int{1, 2}, false}, {"TestAnyInComparableSlice_NilSlice", nil, []int{1, 2}, false}, {"TestAnyInComparableSlice_EmptyValueSlice", []int{1, 2, 3}, []int{}, false}, {"TestAnyInComparableSlice_NilValueSlice", []int{1, 2, 3}, nil, false}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
var actual = collection.AnyInComparableSlice(c.input, c.inputV)
if actual != c.expected {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "not as expected")
}
})
}
}
func InSlices(slices []S, v V, handler ComparisonHandler[V]) bool
通过将多个切片合并后检查 v 是否被包含在 slices 中,当 handler 返回 true 时,表示 v 和 slices 中的某个元素相匹配
- 当传入的 v 被包含在 slices 的任一成员中时,返回 true
示例代码:
func ExampleInSlices() {
result := collection.InSlices([][]int{{1, 2, 3}, {4, 5, 6}}, 2, func(source, target int) bool {
return source == target
})
fmt.Println(result)
}
查看 / 收起单元测试
func TestInSlices(t *testing.T) {
var cases = []struct {
name string
input [][]int
inputV int
expected bool
}{{"TestInSlices_NonEmptySliceIn", [][]int{{1, 2}, {3, 4}}, 1, true}, {"TestInSlices_NonEmptySliceNotIn", [][]int{{1, 2}, {3, 4}}, 5, false}, {"TestInSlices_EmptySlice", [][]int{{}, {}}, 1, false}, {"TestInSlices_NilSlice", nil, 1, false}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
var actual = collection.InSlices(c.input, c.inputV, func(source, target int) bool {
return source == target
})
if actual != c.expected {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "after clone, the length of input is not equal")
}
})
}
}
func InComparableSlices(slices []S, v V) bool
通过将多个切片合并后检查 v 是否被包含在 slices 中
- 当传入的 v 被包含在 slices 的任一成员中时,返回 true
示例代码:
func ExampleInComparableSlices() {
result := collection.InComparableSlices([][]int{{1, 2, 3}, {4, 5, 6}}, 2)
fmt.Println(result)
}
查看 / 收起单元测试
func TestInComparableSlices(t *testing.T) {
var cases = []struct {
name string
input [][]int
inputV int
expected bool
}{{"TestInComparableSlices_NonEmptySliceIn", [][]int{{1, 2}, {3, 4}}, 1, true}, {"TestInComparableSlices_NonEmptySliceNotIn", [][]int{{1, 2}, {3, 4}}, 5, false}, {"TestInComparableSlices_EmptySlice", [][]int{{}, {}}, 1, false}, {"TestInComparableSlices_NilSlice", nil, 1, false}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
var actual = collection.InComparableSlices(c.input, c.inputV)
if actual != c.expected {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "")
}
})
}
}
func AllInSlices(slices []S, values []V, handler ComparisonHandler[V]) bool
通过将多个切片合并后检查 values 中的所有元素是否被包含在 slices 中,当 handler 返回 true 时,表示 values 中的某个元素和 slices 中的某个元素相匹配
- 当 values 中的所有元素都被包含在 slices 的任一成员中时,返回 true
示例代码:
func ExampleAllInSlices() {
result := collection.AllInSlices([][]int{{1, 2, 3}, {4, 5, 6}}, []int{1, 2}, func(source, target int) bool {
return source == target
})
fmt.Println(result)
}
查看 / 收起单元测试
func TestAllInSlices(t *testing.T) {
var cases = []struct {
name string
input [][]int
inputValues []int
expected bool
}{{"TestAllInSlices_NonEmptySliceIn", [][]int{{1, 2}, {3, 4}}, []int{1, 2}, true}, {"TestAllInSlices_NonEmptySliceNotIn", [][]int{{1, 2}, {3, 4}}, []int{1, 5}, false}, {"TestAllInSlices_EmptySlice", [][]int{{}, {}}, []int{1, 2}, false}, {"TestAllInSlices_NilSlice", nil, []int{1, 2}, false}, {"TestAllInSlices_EmptyValueSlice", [][]int{{1, 2}, {3, 4}}, []int{}, true}, {"TestAllInSlices_NilValueSlice", [][]int{{1, 2}, {3, 4}}, nil, true}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
var actual = collection.AllInSlices(c.input, c.inputValues, func(source, target int) bool {
return source == target
})
if actual != c.expected {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "after clone, the length of input is not equal")
}
})
}
}
func AllInComparableSlices(slices []S, values []V) bool
通过将多个切片合并后检查 values 中的所有元素是否被包含在 slices 中
- 当 values 中的所有元素都被包含在 slices 的任一成员中时,返回 true
示例代码:
func ExampleAllInComparableSlices() {
result := collection.AllInComparableSlices([][]int{{1, 2, 3}, {4, 5, 6}}, []int{1, 2})
fmt.Println(result)
}
查看 / 收起单元测试
func TestAllInComparableSlices(t *testing.T) {
var cases = []struct {
name string
input [][]int
inputValues []int
expected bool
}{{"TestAllInComparableSlices_NonEmptySliceIn", [][]int{{1, 2}, {3, 4}}, []int{1, 2}, true}, {"TestAllInComparableSlices_NonEmptySliceNotIn", [][]int{{1, 2}, {3, 4}}, []int{1, 5}, false}, {"TestAllInComparableSlices_EmptySlice", [][]int{{}, {}}, []int{1, 2}, false}, {"TestAllInComparableSlices_NilSlice", nil, []int{1, 2}, false}, {"TestAllInComparableSlices_EmptyValueSlice", [][]int{{1, 2}, {3, 4}}, []int{}, true}, {"TestAllInComparableSlices_NilValueSlice", [][]int{{1, 2}, {3, 4}}, nil, true}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
var actual = collection.AllInComparableSlices(c.input, c.inputValues)
if actual != c.expected {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "")
}
})
}
}
func AnyInSlices(slices []S, values []V, handler ComparisonHandler[V]) bool
通过将多个切片合并后检查 values 中的任意一个元素是否被包含在 slices 中,当 handler 返回 true 时,表示 values 中的某个元素和 slices 中的某个元素相匹配
- 当 values 中的任意一个元素被包含在 slices 的任一成员中时,返回 true
示例代码:
func ExampleAnyInSlices() {
result := collection.AnyInSlices([][]int{{1, 2, 3}, {4, 5, 6}}, []int{1, 2}, func(source, target int) bool {
return source == target
})
fmt.Println(result)
}
查看 / 收起单元测试
func TestAnyInSlices(t *testing.T) {
var cases = []struct {
name string
slices [][]int
values []int
expected bool
}{{"TestAnyInSlices_NonEmptySliceIn", [][]int{{1, 2}, {3, 4}}, []int{1, 2}, true}, {"TestAnyInSlices_NonEmptySliceNotIn", [][]int{{1, 2}, {3, 4}}, []int{1, 5}, true}, {"TestAnyInSlices_EmptySlice", [][]int{{}, {}}, []int{1, 2}, false}, {"TestAnyInSlices_NilSlice", nil, []int{1, 2}, false}, {"TestAnyInSlices_EmptyValueSlice", [][]int{{1, 2}, {3, 4}}, []int{}, false}, {"TestAnyInSlices_NilValueSlice", [][]int{{1, 2}, {3, 4}}, nil, false}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
var actual = collection.AnyInSlices(c.slices, c.values, func(source, target int) bool {
return source == target
})
if actual != c.expected {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "after clone, the length of input is not equal")
}
})
}
}
func AnyInComparableSlices(slices []S, values []V) bool
通过将多个切片合并后检查 values 中的任意一个元素是否被包含在 slices 中
- 当 values 中的任意一个元素被包含在 slices 的任一成员中时,返回 true
示例代码:
func ExampleAnyInComparableSlices() {
result := collection.AnyInComparableSlices([][]int{{1, 2, 3}, {4, 5, 6}}, []int{1, 2})
fmt.Println(result)
}
查看 / 收起单元测试
func TestAnyInComparableSlices(t *testing.T) {
var cases = []struct {
name string
slices [][]int
values []int
expected bool
}{{"TestAnyInComparableSlices_NonEmptySliceIn", [][]int{{1, 2}, {3, 4}}, []int{1, 2}, true}, {"TestAnyInComparableSlices_NonEmptySliceNotIn", [][]int{{1, 2}, {3, 4}}, []int{1, 5}, true}, {"TestAnyInComparableSlices_EmptySlice", [][]int{{}, {}}, []int{1, 2}, false}, {"TestAnyInComparableSlices_NilSlice", nil, []int{1, 2}, false}, {"TestAnyInComparableSlices_EmptyValueSlice", [][]int{{1, 2}, {3, 4}}, []int{}, false}, {"TestAnyInComparableSlices_NilValueSlice", [][]int{{1, 2}, {3, 4}}, nil, false}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
var actual = collection.AnyInComparableSlices(c.slices, c.values)
if actual != c.expected {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "")
}
})
}
}
func InAllSlices(slices []S, v V, handler ComparisonHandler[V]) bool
检查 v 是否被包含在 slices 的每一项元素中,当 handler 返回 true 时,表示 v 和 slices 中的某个元素相匹配
- 当 v 被包含在 slices 的每一项元素中时,返回 true
示例代码:
func ExampleInAllSlices() {
result := collection.InAllSlices([][]int{{1, 2, 3}, {4, 5, 6}}, 2, func(source, target int) bool {
return source == target
})
fmt.Println(result)
}
查看 / 收起单元测试
func TestInAllSlices(t *testing.T) {
var cases = []struct {
name string
slices [][]int
value int
expected bool
}{{"TestInAllSlices_NonEmptySliceIn", [][]int{{1, 2}, {1, 3}}, 1, true}, {"TestInAllSlices_NonEmptySliceNotIn", [][]int{{1, 2}, {3, 4}}, 5, false}, {"TestInAllSlices_EmptySlice", [][]int{{}, {}}, 1, false}, {"TestInAllSlices_NilSlice", nil, 1, false}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
var actual = collection.InAllSlices(c.slices, c.value, func(source, target int) bool {
return source == target
})
if actual != c.expected {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "after clone, the length of input is not equal")
}
})
}
}
func InAllComparableSlices(slices []S, v V) bool
检查 v 是否被包含在 slices 的每一项元素中
- 当 v 被包含在 slices 的每一项元素中时,返回 true
示例代码:
func ExampleInAllComparableSlices() {
result := collection.InAllComparableSlices([][]int{{1, 2, 3}, {4, 5, 6}}, 2)
fmt.Println(result)
}
查看 / 收起单元测试
func TestInAllComparableSlices(t *testing.T) {
var cases = []struct {
name string
slices [][]int
value int
expected bool
}{{"TestInAllComparableSlices_NonEmptySliceIn", [][]int{{1, 2}, {1, 3}}, 1, true}, {"TestInAllComparableSlices_NonEmptySliceNotIn", [][]int{{1, 2}, {3, 4}}, 5, false}, {"TestInAllComparableSlices_EmptySlice", [][]int{{}, {}}, 1, false}, {"TestInAllComparableSlices_NilSlice", nil, 1, false}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
var actual = collection.InAllComparableSlices(c.slices, c.value)
if actual != c.expected {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "")
}
})
}
}
func AnyInAllSlices(slices []S, values []V, handler ComparisonHandler[V]) bool
检查 slices 中的每一个元素是否均包含至少任意一个 values 中的元素,当 handler 返回 true 时,表示 value 中的某个元素和 slices 中的某个元素相匹配
- 当 slices 中的每一个元素均包含至少任意一个 values 中的元素时,返回 true
示例代码:
func ExampleAnyInAllSlices() {
result := collection.AnyInAllSlices([][]int{{1, 2, 3}, {4, 5, 6}}, []int{1, 2}, func(source, target int) bool {
return source == target
})
fmt.Println(result)
}
查看 / 收起单元测试
func TestAnyInAllSlices(t *testing.T) {
var cases = []struct {
name string
slices [][]int
values []int
expected bool
}{{"TestAnyInAllSlices_NonEmptySliceIn", [][]int{{1, 2}, {1, 3}}, []int{1, 2}, true}, {"TestAnyInAllSlices_NonEmptySliceNotIn", [][]int{{1, 2}, {3, 4}}, []int{1, 5}, false}, {"TestAnyInAllSlices_EmptySlice", [][]int{{}, {}}, []int{1, 2}, false}, {"TestAnyInAllSlices_NilSlice", nil, []int{1, 2}, false}, {"TestAnyInAllSlices_EmptyValueSlice", [][]int{{1, 2}, {3, 4}}, []int{}, false}, {"TestAnyInAllSlices_NilValueSlice", [][]int{{1, 2}, {3, 4}}, nil, false}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
var actual = collection.AnyInAllSlices(c.slices, c.values, func(source, target int) bool {
return source == target
})
if actual != c.expected {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "after clone, the length of input is not equal")
}
})
}
}
func AnyInAllComparableSlices(slices []S, values []V) bool
检查 slices 中的每一个元素是否均包含至少任意一个 values 中的元素
- 当 slices 中的每一个元素均包含至少任意一个 values 中的元素时,返回 true
示例代码:
func ExampleAnyInAllComparableSlices() {
result := collection.AnyInAllComparableSlices([][]int{{1, 2, 3}, {4, 5, 6}}, []int{1, 2})
fmt.Println(result)
}
查看 / 收起单元测试
func TestAnyInAllComparableSlices(t *testing.T) {
var cases = []struct {
name string
slices [][]int
values []int
expected bool
}{{"TestAnyInAllComparableSlices_NonEmptySliceIn", [][]int{{1, 2}, {1, 3}}, []int{1, 2}, true}, {"TestAnyInAllComparableSlices_NonEmptySliceNotIn", [][]int{{1, 2}, {3, 4}}, []int{1, 5}, false}, {"TestAnyInAllComparableSlices_EmptySlice", [][]int{{}, {}}, []int{1, 2}, false}, {"TestAnyInAllComparableSlices_NilSlice", nil, []int{1, 2}, false}, {"TestAnyInAllComparableSlices_EmptyValueSlice", [][]int{{1, 2}, {3, 4}}, []int{}, false}, {"TestAnyInAllComparableSlices_NilValueSlice", [][]int{{1, 2}, {3, 4}}, nil, false}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
var actual = collection.AnyInAllComparableSlices(c.slices, c.values)
if actual != c.expected {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "")
}
})
}
}
func KeyInMap(m M, key K) bool
检查 m 中是否包含特定 key
示例代码:
func ExampleKeyInMap() {
result := collection.KeyInMap(map[string]int{"a": 1, "b": 2}, "a")
fmt.Println(result)
}
查看 / 收起单元测试
func TestKeyInMap(t *testing.T) {
var cases = []struct {
name string
m map[int]int
key int
expected bool
}{{"TestKeyInMap_NonEmptySliceIn", map[int]int{1: 1, 2: 2}, 1, true}, {"TestKeyInMap_NonEmptySliceNotIn", map[int]int{1: 1, 2: 2}, 3, false}, {"TestKeyInMap_EmptySlice", map[int]int{}, 1, false}, {"TestKeyInMap_NilSlice", nil, 1, false}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
var actual = collection.KeyInMap(c.m, c.key)
if actual != c.expected {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "after clone, the length of input is not equal")
}
})
}
}
func ValueInMap(m M, value V, handler ComparisonHandler[V]) bool
检查 m 中是否包含特定 value,当 handler 返回 true 时,表示 value 和 m 中的某个元素相匹配
示例代码:
func ExampleValueInMap() {
result := collection.ValueInMap(map[string]int{"a": 1, "b": 2}, 2, func(source, target int) bool {
return source == target
})
fmt.Println(result)
}
查看 / 收起单元测试
func TestValueInMap(t *testing.T) {
var cases = []struct {
name string
m map[int]int
value int
handler collection.ComparisonHandler[int]
expected bool
}{{"TestValueInMap_NonEmptySliceIn", map[int]int{1: 1, 2: 2}, 1, intComparisonHandler, true}, {"TestValueInMap_NonEmptySliceNotIn", map[int]int{1: 1, 2: 2}, 3, intComparisonHandler, false}, {"TestValueInMap_EmptySlice", map[int]int{}, 1, intComparisonHandler, false}, {"TestValueInMap_NilSlice", nil, 1, intComparisonHandler, false}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
var actual = collection.ValueInMap(c.m, c.value, c.handler)
if actual != c.expected {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "after clone, the length of input is not equal")
}
})
}
}
func AllKeyInMap(m M, keys ...K) bool
检查 m 中是否包含 keys 中所有的元素
示例代码:
func ExampleAllKeyInMap() {
result := collection.AllKeyInMap(map[string]int{"a": 1, "b": 2}, "a", "b")
fmt.Println(result)
}
查看 / 收起单元测试
func TestAllKeyInMap(t *testing.T) {
var cases = []struct {
name string
m map[int]int
keys []int
expected bool
}{{"TestAllKeyInMap_NonEmptySliceIn", map[int]int{1: 1, 2: 2}, []int{1, 2}, true}, {"TestAllKeyInMap_NonEmptySliceNotIn", map[int]int{1: 1, 2: 2}, []int{1, 3}, false}, {"TestAllKeyInMap_EmptySlice", map[int]int{}, []int{1, 2}, false}, {"TestAllKeyInMap_NilSlice", nil, []int{1, 2}, false}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
var actual = collection.AllKeyInMap(c.m, c.keys...)
if actual != c.expected {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "after clone, the length of input is not equal")
}
})
}
}
func AllValueInMap(m M, values []V, handler ComparisonHandler[V]) bool
检查 m 中是否包含 values 中所有的元素,当 handler 返回 true 时,表示 values 中的某个元素和 m 中的某个元素相匹配
示例代码:
func ExampleAllValueInMap() {
result := collection.AllValueInMap(map[string]int{"a": 1, "b": 2}, []int{1}, func(source, target int) bool {
return source == target
})
fmt.Println(result)
}
查看 / 收起单元测试
func TestAllValueInMap(t *testing.T) {
var cases = []struct {
name string
m map[int]int
values []int
expected bool
}{{"TestAllValueInMap_NonEmptySliceIn", map[int]int{1: 1, 2: 2}, []int{1, 2}, true}, {"TestAllValueInMap_NonEmptySliceNotIn", map[int]int{1: 1, 2: 2}, []int{1, 3}, false}, {"TestAllValueInMap_EmptySlice", map[int]int{}, []int{1, 2}, false}, {"TestAllValueInMap_NilSlice", nil, []int{1, 2}, false}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
var actual = collection.AllValueInMap(c.m, c.values, intComparisonHandler)
if actual != c.expected {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "after clone, the length of input is not equal")
}
})
}
}
func AnyKeyInMap(m M, keys ...K) bool
检查 m 中是否包含 keys 中任意一个元素
示例代码:
func ExampleAnyKeyInMap() {
result := collection.AnyKeyInMap(map[string]int{"a": 1, "b": 2}, "a", "b")
fmt.Println(result)
}
查看 / 收起单元测试
func TestAnyKeyInMap(t *testing.T) {
var cases = []struct {
name string
m map[int]int
keys []int
expected bool
}{{"TestAnyKeyInMap_NonEmptySliceIn", map[int]int{1: 1, 2: 2}, []int{1, 2}, true}, {"TestAnyKeyInMap_NonEmptySliceNotIn", map[int]int{1: 1, 2: 2}, []int{1, 3}, true}, {"TestAnyKeyInMap_EmptySlice", map[int]int{}, []int{1, 2}, false}, {"TestAnyKeyInMap_NilSlice", nil, []int{1, 2}, false}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
var actual = collection.AnyKeyInMap(c.m, c.keys...)
if actual != c.expected {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "not as expected")
}
})
}
}
func AnyValueInMap(m M, values []V, handler ComparisonHandler[V]) bool
检查 m 中是否包含 values 中任意一个元素,当 handler 返回 true 时,表示 values 中的某个元素和 m 中的某个元素相匹配
示例代码:
func ExampleAnyValueInMap() {
result := collection.AnyValueInMap(map[string]int{"a": 1, "b": 2}, []int{1}, func(source, target int) bool {
return source == target
})
fmt.Println(result)
}
查看 / 收起单元测试
func TestAnyValueInMap(t *testing.T) {
var cases = []struct {
name string
m map[int]int
values []int
expected bool
}{{"TestAnyValueInMap_NonEmptySliceIn", map[int]int{1: 1, 2: 2}, []int{1, 2}, true}, {"TestAnyValueInMap_NonEmptySliceNotIn", map[int]int{1: 1, 2: 2}, []int{1, 3}, true}, {"TestAnyValueInMap_EmptySlice", map[int]int{}, []int{1, 2}, false}, {"TestAnyValueInMap_NilSlice", nil, []int{1, 2}, false}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
var actual = collection.AnyValueInMap(c.m, c.values, intComparisonHandler)
if actual != c.expected {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "not as expected")
}
})
}
}
func AllKeyInMaps(maps []M, keys ...K) bool
检查 maps 中的每一个元素是否均包含 keys 中所有的元素
示例代码:
func ExampleAllKeyInMaps() {
result := collection.AllKeyInMaps([]map[string]int{{"a": 1, "b": 2}, {"a": 1, "b": 2}}, "a", "b")
fmt.Println(result)
}
查看 / 收起单元测试
func TestAllKeyInMaps(t *testing.T) {
var cases = []struct {
name string
maps []map[int]int
keys []int
expected bool
}{{"TestAllKeyInMaps_NonEmptySliceIn", []map[int]int{{1: 1, 2: 2}, {3: 3, 4: 4}}, []int{1, 2}, false}, {"TestAllKeyInMaps_NonEmptySliceNotIn", []map[int]int{{1: 1, 2: 2}, {3: 3, 4: 4}}, []int{1, 3}, false}, {"TestAllKeyInMaps_EmptySlice", []map[int]int{{1: 1, 2: 2}, {}}, []int{1, 2}, false}, {"TestAllKeyInMaps_NilSlice", []map[int]int{{}, {}}, []int{1, 2}, false}, {"TestAllKeyInMaps_EmptySlice", []map[int]int{}, []int{1, 2}, false}, {"TestAllKeyInMaps_NilSlice", nil, []int{1, 2}, false}, {"TestAllKeyInMaps_NonEmptySliceIn", []map[int]int{{1: 1, 2: 2, 3: 3}, {1: 1, 2: 2, 4: 4}}, []int{1, 2}, true}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
var actual = collection.AllKeyInMaps(c.maps, c.keys...)
if actual != c.expected {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "not as expected")
}
})
}
}
func AllValueInMaps(maps []M, values []V, handler ComparisonHandler[V]) bool
检查 maps 中的每一个元素是否均包含 value 中所有的元素,当 handler 返回 true 时,表示 value 中的某个元素和 maps 中的某个元素相匹配
示例代码:
func ExampleAllValueInMaps() {
result := collection.AllValueInMaps([]map[string]int{{"a": 1, "b": 2}, {"a": 1, "b": 2}}, []int{1}, func(source, target int) bool {
return source == target
})
fmt.Println(result)
}
查看 / 收起单元测试
func TestAllValueInMaps(t *testing.T) {
var cases = []struct {
name string
maps []map[int]int
values []int
expected bool
}{{"TestAllValueInMaps_NonEmptySliceIn", []map[int]int{{1: 1, 2: 2}, {3: 3, 4: 4}}, []int{1, 2}, false}, {"TestAllValueInMaps_NonEmptySliceNotIn", []map[int]int{{1: 1, 2: 2}, {3: 3, 4: 4}}, []int{1, 3}, false}, {"TestAllValueInMaps_EmptySlice", []map[int]int{{1: 1, 2: 2}, {}}, []int{1, 2}, false}, {"TestAllValueInMaps_NilSlice", []map[int]int{{}, {}}, []int{1, 2}, false}, {"TestAllValueInMaps_EmptySlice", []map[int]int{}, []int{1, 2}, false}, {"TestAllValueInMaps_NilSlice", nil, []int{1, 2}, false}, {"TestAllValueInMaps_NonEmptySliceIn", []map[int]int{{1: 1, 2: 2, 3: 3}, {1: 1, 2: 2, 4: 4}}, []int{1, 2}, true}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
var actual = collection.AllValueInMaps(c.maps, c.values, intComparisonHandler)
if actual != c.expected {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "not as expected")
}
})
}
}
func AnyKeyInMaps(maps []M, keys ...K) bool
检查 keys 中的任意一个元素是否被包含在 maps 中的任意一个元素中
- 当 keys 中的任意一个元素被包含在 maps 中的任意一个元素中时,返回 true
示例代码:
func ExampleAnyKeyInMaps() {
result := collection.AnyKeyInMaps([]map[string]int{{"a": 1, "b": 2}, {"a": 1, "b": 2}}, "a", "b")
fmt.Println(result)
}
查看 / 收起单元测试
func TestAnyKeyInMaps(t *testing.T) {
var cases = []struct {
name string
maps []map[int]int
keys []int
expected bool
}{{"TestAnyKeyInMaps_NonEmptySliceIn", []map[int]int{{1: 1, 2: 2}, {3: 3, 4: 4}}, []int{1, 2}, true}, {"TestAnyKeyInMaps_NonEmptySliceNotIn", []map[int]int{{1: 1, 2: 2}, {3: 3, 4: 4}}, []int{1, 3}, true}, {"TestAnyKeyInMaps_EmptySlice", []map[int]int{{1: 1, 2: 2}, {}}, []int{1, 2}, true}, {"TestAnyKeyInMaps_NilSlice", []map[int]int{{}, {}}, []int{1, 2}, false}, {"TestAnyKeyInMaps_EmptySlice", []map[int]int{}, []int{1, 2}, false}, {"TestAnyKeyInMaps_NilSlice", nil, []int{1, 2}, false}, {"TestAnyKeyInMaps_NonEmptySliceIn", []map[int]int{{1: 1, 2: 2, 3: 3}, {1: 1, 2: 2, 4: 4}}, []int{1, 2}, true}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
var actual = collection.AnyKeyInMaps(c.maps, c.keys...)
if actual != c.expected {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "not as expected")
}
})
}
}
func AnyValueInMaps(maps []M, values []V, handler ComparisonHandler[V]) bool
检查 maps 中的任意一个元素是否包含 value 中的任意一个元素,当 handler 返回 true 时,表示 value 中的某个元素和 maps 中的某个元素相匹配
- 当 maps 中的任意一个元素包含 value 中的任意一个元素时,返回 true
示例代码:
func ExampleAnyValueInMaps() {
result := collection.AnyValueInMaps([]map[string]int{{"a": 1, "b": 2}, {"a": 1, "b": 2}}, []int{1}, func(source, target int) bool {
return source == target
})
fmt.Println(result)
}
查看 / 收起单元测试
func TestAnyValueInMaps(t *testing.T) {
var cases = []struct {
name string
maps []map[int]int
values []int
expected bool
}{{"TestAnyValueInMaps_NonEmptySliceIn", []map[int]int{{1: 1, 2: 2}, {3: 3, 4: 4}}, []int{1, 2}, false}, {"TestAnyValueInMaps_NonEmptySliceNotIn", []map[int]int{{1: 1, 2: 2}, {3: 3, 4: 4}}, []int{1, 3}, true}, {"TestAnyValueInMaps_EmptySlice", []map[int]int{{1: 1, 2: 2}, {}}, []int{1, 2}, false}, {"TestAnyValueInMaps_NilSlice", []map[int]int{{}, {}}, []int{1, 2}, false}, {"TestAnyValueInMaps_EmptySlice", []map[int]int{}, []int{1, 2}, false}, {"TestAnyValueInMaps_NilSlice", nil, []int{1, 2}, false}, {"TestAnyValueInMaps_NonEmptySliceIn", []map[int]int{{1: 1, 2: 2, 3: 3}, {1: 1, 2: 2, 4: 4}}, []int{1, 2}, true}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
var actual = collection.AnyValueInMaps(c.maps, c.values, intComparisonHandler)
if actual != c.expected {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "not as expected")
}
})
}
}
func KeyInAllMaps(maps []M, key K) bool
检查 key 是否被包含在 maps 的每一个元素中
示例代码:
func ExampleKeyInAllMaps() {
result := collection.KeyInAllMaps([]map[string]int{{"a": 1, "b": 2}, {"a": 1, "b": 2}}, "a")
fmt.Println(result)
}
查看 / 收起单元测试
func TestKeyInAllMaps(t *testing.T) {
var cases = []struct {
name string
maps []map[int]int
key int
expected bool
}{{"TestKeyInAllMaps_NonEmptySliceIn", []map[int]int{{1: 1, 2: 2}, {3: 3, 4: 4}}, 1, false}, {"TestKeyInAllMaps_NonEmptySliceNotIn", []map[int]int{{1: 1, 2: 2}, {3: 3, 4: 4}}, 3, false}, {"TestKeyInAllMaps_EmptySlice", []map[int]int{{1: 1, 2: 2}, {}}, 1, false}, {"TestKeyInAllMaps_NilSlice", []map[int]int{{}, {}}, 1, false}, {"TestKeyInAllMaps_EmptySlice", []map[int]int{}, 1, false}, {"TestKeyInAllMaps_NilSlice", nil, 1, false}, {"TestKeyInAllMaps_NonEmptySliceIn", []map[int]int{{1: 1, 2: 2, 3: 3}, {1: 1, 2: 2, 4: 4}}, 1, true}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
var actual = collection.KeyInAllMaps(c.maps, c.key)
if actual != c.expected {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "not as expected")
}
})
}
}
func AnyKeyInAllMaps(maps []M, keys []K) bool
检查 maps 中的每一个元素是否均包含 keys 中任意一个元素
- 当 maps 中的每一个元素均包含 keys 中任意一个元素时,返回 true
示例代码:
func ExampleAnyKeyInAllMaps() {
result := collection.AnyKeyInAllMaps([]map[string]int{{"a": 1, "b": 2}, {"a": 1, "b": 2}}, []string{"a"})
fmt.Println(result)
}
查看 / 收起单元测试
func TestAnyKeyInAllMaps(t *testing.T) {
var cases = []struct {
name string
maps []map[int]int
keys []int
expected bool
}{{"TestAnyKeyInAllMaps_NonEmptySliceIn", []map[int]int{{1: 1, 2: 2}, {3: 3, 4: 4}}, []int{1, 2}, false}, {"TestAnyKeyInAllMaps_NonEmptySliceNotIn", []map[int]int{{1: 1, 2: 2}, {3: 3, 4: 4}}, []int{1, 3}, true}, {"TestAnyKeyInAllMaps_EmptySlice", []map[int]int{{1: 1, 2: 2}, {}}, []int{1, 2}, false}, {"TestAnyKeyInAllMaps_NilSlice", []map[int]int{{}, {}}, []int{1, 2}, false}, {"TestAnyKeyInAllMaps_EmptySlice", []map[int]int{}, []int{1, 2}, false}, {"TestAnyKeyInAllMaps_NilSlice", nil, []int{1, 2}, false}, {"TestAnyKeyInAllMaps_NonEmptySliceIn", []map[int]int{{1: 1, 2: 2, 3: 3}, {1: 1, 2: 2, 4: 4}}, []int{1, 2}, true}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
var actual = collection.AnyKeyInAllMaps(c.maps, c.keys)
if actual != c.expected {
t.Fatalf("%s failed, expected: %v, actual: %v", c.name, c.expected, actual)
}
})
}
}
func ConvertSliceToAny(s S) []any
将切片转换为任意类型的切片
示例代码:
func ExampleConvertSliceToAny() {
result := collection.ConvertSliceToAny([]int{1, 2, 3})
fmt.Println(reflect.TypeOf(result).String(), len(result))
}
查看 / 收起单元测试
func TestConvertSliceToAny(t *testing.T) {
var cases = []struct {
name string
input []int
expected []interface{}
}{{name: "TestConvertSliceToAny_NonEmpty", input: []int{1, 2, 3}, expected: []any{1, 2, 3}}, {name: "TestConvertSliceToAny_Empty", input: []int{}, expected: []any{}}, {name: "TestConvertSliceToAny_Nil", input: nil, expected: nil}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
actual := collection.ConvertSliceToAny(c.input)
if len(actual) != len(c.expected) {
t.Errorf("expected: %v, actual: %v", c.expected, actual)
}
for i := 0; i < len(actual); i++ {
av, ev := actual[i], c.expected[i]
if reflect.TypeOf(av).Kind() != reflect.TypeOf(ev).Kind() {
t.Errorf("expected: %v, actual: %v", c.expected, actual)
}
if av != ev {
t.Errorf("expected: %v, actual: %v", c.expected, actual)
}
}
})
}
}
func ConvertSliceToIndexMap(s S) map[int]V
将切片转换为索引为键的映射
示例代码:
func ExampleConvertSliceToIndexMap() {
slice := []int{1, 2, 3}
result := collection.ConvertSliceToIndexMap(slice)
for i, v := range slice {
fmt.Println(result[i], v)
}
}
查看 / 收起单元测试
func TestConvertSliceToIndexMap(t *testing.T) {
var cases = []struct {
name string
input []int
expected map[int]int
}{{name: "TestConvertSliceToIndexMap_NonEmpty", input: []int{1, 2, 3}, expected: map[int]int{0: 1, 1: 2, 2: 3}}, {name: "TestConvertSliceToIndexMap_Empty", input: []int{}, expected: map[int]int{}}, {name: "TestConvertSliceToIndexMap_Nil", input: nil, expected: nil}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
actual := collection.ConvertSliceToIndexMap(c.input)
if len(actual) != len(c.expected) {
t.Errorf("expected: %v, actual: %v", c.expected, actual)
}
for k, v := range actual {
if c.expected[k] != v {
t.Errorf("expected: %v, actual: %v", c.expected, actual)
}
}
})
}
}
func ConvertSliceToIndexOnlyMap(s S) map[int]struct
将切片转换为索引为键的映射
示例代码:
func ExampleConvertSliceToIndexOnlyMap() {
slice := []int{1, 2, 3}
result := collection.ConvertSliceToIndexOnlyMap(slice)
expected := map[int]bool{0: true, 1: true, 2: true}
for k := range result {
fmt.Println(expected[k])
}
}
查看 / 收起单元测试
func TestConvertSliceToIndexOnlyMap(t *testing.T) {
var cases = []struct {
name string
input []int
expected map[int]struct{}
}{{name: "TestConvertSliceToIndexOnlyMap_NonEmpty", input: []int{1, 2, 3}, expected: map[int]struct{}{0: {}, 1: {}, 2: {}}}, {name: "TestConvertSliceToIndexOnlyMap_Empty", input: []int{}, expected: map[int]struct{}{}}, {name: "TestConvertSliceToIndexOnlyMap_Nil", input: nil, expected: nil}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
actual := collection.ConvertSliceToIndexOnlyMap(c.input)
if len(actual) != len(c.expected) {
t.Errorf("expected: %v, actual: %v", c.expected, actual)
}
for k, v := range actual {
if _, ok := c.expected[k]; !ok {
t.Errorf("expected: %v, actual: %v", c.expected, actual)
}
if v != struct{}{} {
t.Errorf("expected: %v, actual: %v", c.expected, actual)
}
}
})
}
}
func ConvertSliceToMap(s S) map[V]struct
将切片转换为值为键的映射
示例代码:
func ExampleConvertSliceToMap() {
slice := []int{1, 2, 3}
result := collection.ConvertSliceToMap(slice)
fmt.Println(collection.AllKeyInMap(result, slice...))
}
查看 / 收起单元测试
func TestConvertSliceToMap(t *testing.T) {
var cases = []struct {
name string
input []int
expected map[int]struct{}
}{{name: "TestConvertSliceToMap_NonEmpty", input: []int{1, 2, 3}, expected: map[int]struct{}{1: {}, 2: {}, 3: {}}}, {name: "TestConvertSliceToMap_Empty", input: []int{}, expected: map[int]struct{}{}}, {name: "TestConvertSliceToMap_Nil", input: nil, expected: nil}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
actual := collection.ConvertSliceToMap(c.input)
if len(actual) != len(c.expected) {
t.Errorf("expected: %v, actual: %v", c.expected, actual)
}
for k, v := range actual {
if _, ok := c.expected[k]; !ok {
t.Errorf("expected: %v, actual: %v", c.expected, actual)
}
if v != struct{}{} {
t.Errorf("expected: %v, actual: %v", c.expected, actual)
}
}
})
}
}
func ConvertSliceToBoolMap(s S) map[V]bool
将切片转换为值为键的映射
示例代码:
func ExampleConvertSliceToBoolMap() {
slice := []int{1, 2, 3}
result := collection.ConvertSliceToBoolMap(slice)
for _, v := range slice {
fmt.Println(v, result[v])
}
}
查看 / 收起单元测试
func TestConvertSliceToBoolMap(t *testing.T) {
var cases = []struct {
name string
input []int
expected map[int]bool
}{{name: "TestConvertSliceToBoolMap_NonEmpty", input: []int{1, 2, 3}, expected: map[int]bool{1: true, 2: true, 3: true}}, {name: "TestConvertSliceToBoolMap_Empty", input: []int{}, expected: map[int]bool{}}, {name: "TestConvertSliceToBoolMap_Nil", input: nil, expected: nil}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
actual := collection.ConvertSliceToBoolMap(c.input)
if len(actual) != len(c.expected) {
t.Errorf("expected: %v, actual: %v", c.expected, actual)
}
for k, v := range actual {
if c.expected[k] != v {
t.Errorf("expected: %v, actual: %v", c.expected, actual)
}
}
})
}
}
func ConvertMapKeysToSlice(m M) []K
将映射的键转换为切片
示例代码:
func ExampleConvertMapKeysToSlice() {
result := collection.ConvertMapKeysToSlice(map[int]int{1: 1, 2: 2, 3: 3})
for i, v := range result {
fmt.Println(i, v)
}
}
查看 / 收起单元测试
func TestConvertMapKeysToSlice(t *testing.T) {
var cases = []struct {
name string
input map[int]int
expected []int
}{{name: "TestConvertMapKeysToSlice_NonEmpty", input: map[int]int{1: 1, 2: 2, 3: 3}, expected: []int{1, 2, 3}}, {name: "TestConvertMapKeysToSlice_Empty", input: map[int]int{}, expected: []int{}}, {name: "TestConvertMapKeysToSlice_Nil", input: nil, expected: nil}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
actual := collection.ConvertMapKeysToSlice(c.input)
if len(actual) != len(c.expected) {
t.Errorf("expected: %v, actual: %v", c.expected, actual)
}
var matchCount = 0
for _, av := range actual {
for _, ev := range c.expected {
if av == ev {
matchCount++
}
}
}
if matchCount != len(actual) {
t.Errorf("expected: %v, actual: %v", c.expected, actual)
}
})
}
}
func ConvertMapValuesToSlice(m M) []V
将映射的值转换为切片
示例代码:
func ExampleConvertMapValuesToSlice() {
result := collection.ConvertMapValuesToSlice(map[int]int{1: 1, 2: 2, 3: 3})
expected := map[int]bool{1: true, 2: true, 3: true}
for _, v := range result {
fmt.Println(expected[v])
}
}
查看 / 收起单元测试
func TestConvertMapValuesToSlice(t *testing.T) {
var cases = []struct {
name string
input map[int]int
expected []int
}{{name: "TestConvertMapValuesToSlice_NonEmpty", input: map[int]int{1: 1, 2: 2, 3: 3}, expected: []int{1, 2, 3}}, {name: "TestConvertMapValuesToSlice_Empty", input: map[int]int{}, expected: []int{}}, {name: "TestConvertMapValuesToSlice_Nil", input: nil, expected: nil}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
actual := collection.ConvertMapValuesToSlice(c.input)
if len(actual) != len(c.expected) {
t.Errorf("expected: %v, actual: %v", c.expected, actual)
}
var matchCount = 0
for _, av := range actual {
for _, ev := range c.expected {
if av == ev {
matchCount++
}
}
}
if matchCount != len(actual) {
t.Errorf("expected: %v, actual: %v", c.expected, actual)
}
})
}
}
func InvertMap(m M) N
将映射的键和值互换
示例代码:
func ExampleInvertMap() {
result := collection.InvertMap(map[int]string{1: "a", 2: "b", 3: "c"})
fmt.Println(collection.AllKeyInMap(result, "a", "b", "c"))
}
查看 / 收起单元测试
func TestInvertMap(t *testing.T) {
var cases = []struct {
name string
input map[int]string
expected map[string]int
}{{name: "TestInvertMap_NonEmpty", input: map[int]string{1: "1", 2: "2", 3: "3"}, expected: map[string]int{"1": 1, "2": 2, "3": 3}}, {name: "TestInvertMap_Empty", input: map[int]string{}, expected: map[string]int{}}, {name: "TestInvertMap_Nil", input: nil, expected: nil}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
actual := collection.InvertMap[map[int]string, map[string]int](c.input)
if len(actual) != len(c.expected) {
t.Errorf("expected: %v, actual: %v", c.expected, actual)
}
for k, v := range actual {
if c.expected[k] != v {
t.Errorf("expected: %v, actual: %v", c.expected, actual)
}
}
})
}
}
func ConvertMapValuesToBool(m M) N
将映射的值转换为布尔值
示例代码:
func ExampleConvertMapValuesToBool() {
result := collection.ConvertMapValuesToBool(map[int]int{1: 1})
fmt.Println(result)
}
查看 / 收起单元测试
func TestConvertMapValuesToBool(t *testing.T) {
var cases = []struct {
name string
input map[int]int
expected map[int]bool
}{{name: "TestConvertMapValuesToBool_NonEmpty", input: map[int]int{1: 1, 2: 2, 3: 3}, expected: map[int]bool{1: true, 2: true, 3: true}}, {name: "TestConvertMapValuesToBool_Empty", input: map[int]int{}, expected: map[int]bool{}}, {name: "TestConvertMapValuesToBool_Nil", input: nil, expected: nil}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
actual := collection.ConvertMapValuesToBool[map[int]int, map[int]bool](c.input)
if len(actual) != len(c.expected) {
t.Errorf("expected: %v, actual: %v", c.expected, actual)
}
for k, v := range actual {
if c.expected[k] != v {
t.Errorf("expected: %v, actual: %v", c.expected, actual)
}
}
})
}
}
func ReverseSlice(s *S)
将切片反转
示例代码:
func ExampleReverseSlice() {
var s = []int{1, 2, 3}
collection.ReverseSlice(&s)
fmt.Println(s)
}
查看 / 收起单元测试
func TestReverseSlice(t *testing.T) {
var cases = []struct {
name string
input []int
expected []int
}{{name: "TestReverseSlice_NonEmpty", input: []int{1, 2, 3}, expected: []int{3, 2, 1}}, {name: "TestReverseSlice_Empty", input: []int{}, expected: []int{}}, {name: "TestReverseSlice_Nil", input: nil, expected: nil}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
collection.ReverseSlice(&c.input)
if len(c.input) != len(c.expected) {
t.Errorf("expected: %v, actual: %v", c.expected, c.input)
}
for i := 0; i < len(c.input); i++ {
av, ev := c.input[i], c.expected[i]
if reflect.TypeOf(av).Kind() != reflect.TypeOf(ev).Kind() {
t.Errorf("expected: %v, actual: %v", c.expected, c.input)
}
if av != ev {
t.Errorf("expected: %v, actual: %v", c.expected, c.input)
}
}
})
}
}
func ClearSlice(slice *S)
清空切片
示例代码:
func ExampleClearSlice() {
slice := []int{1, 2, 3, 4, 5}
ClearSlice(&slice)
fmt.Println(slice)
}
查看 / 收起单元测试
func TestClearSlice(t *testing.T) {
var cases = []struct {
name string
input []int
expected []int
}{{"TestClearSlice_NonEmptySlice", []int{1, 2, 3}, []int{}}, {"TestClearSlice_EmptySlice", []int{}, []int{}}, {"TestClearSlice_NilSlice", nil, nil}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
collection.ClearSlice(&c.input)
if len(c.input) != len(c.expected) {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, c.input, "after clear, the length of input is not equal")
}
for i := 0; i < len(c.input); i++ {
if c.input[i] != c.expected[i] {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, c.input, "after clear, the inputV of input is not equal")
}
}
})
}
}
func ClearMap(m M)
清空 map
示例代码:
func ExampleClearMap() {
m := map[int]int{1: 1, 2: 2, 3: 3}
ClearMap(m)
fmt.Println(m)
}
查看 / 收起单元测试
func TestClearMap(t *testing.T) {
var cases = []struct {
name string
input map[int]int
expected map[int]int
}{{"TestClearMap_NonEmptyMap", map[int]int{1: 1, 2: 2, 3: 3}, map[int]int{}}, {"TestClearMap_EmptyMap", map[int]int{}, map[int]int{}}, {"TestClearMap_NilMap", nil, nil}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
collection.ClearMap(c.input)
if len(c.input) != len(c.expected) {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, c.input, "after clear, the length of map is not equal")
}
for k, v := range c.input {
if v != c.expected[k] {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, c.input, "after clear, the inputV of map is not equal")
}
}
})
}
}
func DropSliceByIndices(slice *S, indices ...int)
删除切片中特定索引的元素
示例代码:
func ExampleDropSliceByIndices() {
slice := []int{1, 2, 3, 4, 5}
DropSliceByIndices(&slice, 1, 3)
fmt.Println(slice)
}
查看 / 收起单元测试
func TestDropSliceByIndices(t *testing.T) {
var cases = []struct {
name string
input []int
indices []int
expected []int
}{{"TestDropSliceByIndices_NonEmptySlice", []int{1, 2, 3, 4, 5}, []int{1, 3}, []int{1, 3, 5}}, {"TestDropSliceByIndices_EmptySlice", []int{}, []int{1, 3}, []int{}}, {"TestDropSliceByIndices_NilSlice", nil, []int{1, 3}, nil}, {"TestDropSliceByIndices_NonEmptySlice", []int{1, 2, 3, 4, 5}, []int{}, []int{1, 2, 3, 4, 5}}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
collection.DropSliceByIndices(&c.input, c.indices...)
if len(c.input) != len(c.expected) {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, c.input, "after drop, the length of input is not equal")
}
for i := 0; i < len(c.input); i++ {
if c.input[i] != c.expected[i] {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, c.input, "after drop, the inputV of input is not equal")
}
}
})
}
}
func DropSliceByCondition(slice *S, condition func (v V) bool)
删除切片中符合条件的元素
- condition 的返回值为 true 时,将会删除该元素
示例代码:
func ExampleDropSliceByCondition() {
slice := []int{1, 2, 3, 4, 5}
DropSliceByCondition(&slice, func(v int) bool {
return v%2 == 0
})
fmt.Println(slice)
}
查看 / 收起单元测试
func TestDropSliceByCondition(t *testing.T) {
var cases = []struct {
name string
input []int
condition func(v int) bool
expected []int
}{{"TestDropSliceByCondition_NonEmptySlice", []int{1, 2, 3, 4, 5}, func(v int) bool {
return v%2 == 0
}, []int{1, 3, 5}}, {"TestDropSliceByCondition_EmptySlice", []int{}, func(v int) bool {
return v%2 == 0
}, []int{}}, {"TestDropSliceByCondition_NilSlice", nil, func(v int) bool {
return v%2 == 0
}, nil}, {"TestDropSliceByCondition_NonEmptySlice", []int{1, 2, 3, 4, 5}, func(v int) bool {
return v%2 == 1
}, []int{2, 4}}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
collection.DropSliceByCondition(&c.input, c.condition)
if len(c.input) != len(c.expected) {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, c.input, "after drop, the length of input is not equal")
}
for i := 0; i < len(c.input); i++ {
if c.input[i] != c.expected[i] {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, c.input, "after drop, the inputV of input is not equal")
}
}
})
}
}
func DropSliceOverlappingElements(slice *S, anotherSlice []V, comparisonHandler ComparisonHandler[V])
删除切片中与另一个切片重叠的元素
示例代码:
func ExampleDropSliceOverlappingElements() {
slice := []int{1, 2, 3, 4, 5}
DropSliceOverlappingElements(&slice, []int{1, 3, 5}, func(source, target int) bool {
return source == target
})
fmt.Println(slice)
}
查看 / 收起单元测试
func TestDropSliceOverlappingElements(t *testing.T) {
var cases = []struct {
name string
input []int
anotherSlice []int
comparisonHandler collection.ComparisonHandler[int]
expected []int
expectedAnother []int
expectedComparison []int
}{{"TestDropSliceOverlappingElements_NonEmptySlice", []int{1, 2, 3, 4, 5}, []int{3, 4, 5, 6, 7}, func(v1, v2 int) bool {
return v1 == v2
}, []int{1, 2}, []int{6, 7}, []int{3, 4, 5}}, {"TestDropSliceOverlappingElements_EmptySlice", []int{}, []int{3, 4, 5, 6, 7}, func(v1, v2 int) bool {
return v1 == v2
}, []int{}, []int{3, 4, 5, 6, 7}, []int{}}, {"TestDropSliceOverlappingElements_NilSlice", nil, []int{3, 4, 5, 6, 7}, func(v1, v2 int) bool {
return v1 == v2
}, nil, []int{3, 4, 5, 6, 7}, nil}, {"TestDropSliceOverlappingElements_NonEmptySlice", []int{1, 2, 3, 4, 5}, []int{}, func(v1, v2 int) bool {
return v1 == v2
}, []int{1, 2, 3, 4, 5}, []int{}, []int{}}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
collection.DropSliceOverlappingElements(&c.input, c.anotherSlice, c.comparisonHandler)
if len(c.input) != len(c.expected) {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, c.input, "after drop, the length of input is not equal")
}
for i := 0; i < len(c.input); i++ {
if c.input[i] != c.expected[i] {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, c.input, "after drop, the inputV of input is not equal")
}
}
})
}
}
func DeduplicateSliceInPlace(s *S)
去除切片中的重复元素
示例代码:
func ExampleDeduplicateSliceInPlace() {
slice := []int{1, 2, 3, 4, 5, 5, 4, 3, 2, 1}
DeduplicateSliceInPlace(&slice)
fmt.Println(slice)
}
查看 / 收起单元测试
func TestDeduplicateSliceInPlace(t *testing.T) {
var cases = []struct {
name string
input []int
expected []int
}{{name: "TestDeduplicateSliceInPlace_NonEmpty", input: []int{1, 2, 3, 1, 2, 3}, expected: []int{1, 2, 3}}, {name: "TestDeduplicateSliceInPlace_Empty", input: []int{}, expected: []int{}}, {name: "TestDeduplicateSliceInPlace_Nil", input: nil, expected: nil}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
collection.DeduplicateSliceInPlace(&c.input)
if len(c.input) != len(c.expected) {
t.Errorf("expected: %v, actual: %v", c.expected, c.input)
}
for i := 0; i < len(c.input); i++ {
av, ev := c.input[i], c.expected[i]
if av != ev {
t.Errorf("expected: %v, actual: %v", c.expected, c.input)
}
}
})
}
}
func DeduplicateSlice(s S) S
去除切片中的重复元素,返回新切片
示例代码:
func ExampleDeduplicateSlice() {
slice := []int{1, 2, 3, 4, 5, 5, 4, 3, 2, 1}
fmt.Println(DeduplicateSlice(slice))
}
查看 / 收起单元测试
func TestDeduplicateSlice(t *testing.T) {
var cases = []struct {
name string
input []int
expected []int
}{{name: "TestDeduplicateSlice_NonEmpty", input: []int{1, 2, 3, 1, 2, 3}, expected: []int{1, 2, 3}}, {name: "TestDeduplicateSlice_Empty", input: []int{}, expected: []int{}}, {name: "TestDeduplicateSlice_Nil", input: nil, expected: nil}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
actual := collection.DeduplicateSlice(c.input)
if len(actual) != len(c.expected) {
t.Errorf("expected: %v, actual: %v", c.expected, actual)
}
for i := 0; i < len(actual); i++ {
av, ev := actual[i], c.expected[i]
if av != ev {
t.Errorf("expected: %v, actual: %v", c.expected, actual)
}
}
})
}
}
func DeduplicateSliceInPlaceWithCompare(s *S, compare func (a V) bool)
去除切片中的重复元素,使用自定义的比较函数
示例代码:
func ExampleDeduplicateSliceInPlaceWithCompare() {
slice := []int{1, 2, 3, 4, 5, 5, 4, 3, 2, 1}
DeduplicateSliceInPlaceWithCompare(&slice, func(a, b int) bool {
return a == b
})
fmt.Println(slice)
}
查看 / 收起单元测试
func TestDeduplicateSliceInPlaceWithCompare(t *testing.T) {
var cases = []struct {
name string
input []int
expected []int
}{{name: "TestDeduplicateSliceInPlaceWithCompare_NonEmpty", input: []int{1, 2, 3, 1, 2, 3}, expected: []int{1, 2, 3}}, {name: "TestDeduplicateSliceInPlaceWithCompare_Empty", input: []int{}, expected: []int{}}, {name: "TestDeduplicateSliceInPlaceWithCompare_Nil", input: nil, expected: nil}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
collection.DeduplicateSliceInPlaceWithCompare(&c.input, func(a, b int) bool {
return a == b
})
if len(c.input) != len(c.expected) {
t.Errorf("expected: %v, actual: %v", c.expected, c.input)
}
for i := 0; i < len(c.input); i++ {
av, ev := c.input[i], c.expected[i]
if av != ev {
t.Errorf("expected: %v, actual: %v", c.expected, c.input)
}
}
})
}
}
func DeduplicateSliceWithCompare(s S, compare func (a V) bool) S
去除切片中的重复元素,使用自定义的比较函数,返回新的切片
示例代码:
func ExampleDeduplicateSliceWithCompare() {
slice := []int{1, 2, 3, 4, 5, 5, 4, 3, 2, 1}
fmt.Println(DeduplicateSliceWithCompare(slice, func(a, b int) bool {
return a == b
}))
}
查看 / 收起单元测试
func TestDeduplicateSliceWithCompare(t *testing.T) {
var cases = []struct {
name string
input []int
expected []int
}{{name: "TestDeduplicateSliceWithCompare_NonEmpty", input: []int{1, 2, 3, 1, 2, 3}, expected: []int{1, 2, 3}}, {name: "TestDeduplicateSliceWithCompare_Empty", input: []int{}, expected: []int{}}, {name: "TestDeduplicateSliceWithCompare_Nil", input: nil, expected: nil}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
actual := collection.DeduplicateSliceWithCompare(c.input, func(a, b int) bool {
return a == b
})
if len(actual) != len(c.expected) {
t.Errorf("expected: %v, actual: %v", c.expected, actual)
}
for i := 0; i < len(actual); i++ {
av, ev := actual[i], c.expected[i]
if av != ev {
t.Errorf("expected: %v, actual: %v", c.expected, actual)
}
}
})
}
}
func FilterOutByIndices(slice S, indices ...int) S
过滤切片中特定索引的元素,返回过滤后的切片
示例代码:
func ExampleFilterOutByIndices() {
var slice = []int{1, 2, 3, 4, 5}
var result = collection.FilterOutByIndices(slice, 1, 3)
fmt.Println(result)
}
查看 / 收起单元测试
func TestFilterOutByIndices(t *testing.T) {
var cases = []struct {
name string
input []int
indices []int
expected []int
}{{"TestFilterOutByIndices_NonEmptySlice", []int{1, 2, 3, 4, 5}, []int{1, 3}, []int{1, 3, 5}}, {"TestFilterOutByIndices_EmptySlice", []int{}, []int{1, 3}, []int{}}, {"TestFilterOutByIndices_NilSlice", nil, []int{1, 3}, nil}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
actual := collection.FilterOutByIndices(c.input, c.indices...)
if len(actual) != len(c.expected) {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "after filter, the length of input is not equal")
}
for i := 0; i < len(actual); i++ {
if actual[i] != c.expected[i] {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "after filter, the inputV of input is not equal")
}
}
})
}
}
func FilterOutByCondition(slice S, condition func (v V) bool) S
过滤切片中符合条件的元素,返回过滤后的切片
- condition 的返回值为 true 时,将会过滤掉该元素
示例代码:
func ExampleFilterOutByCondition() {
var slice = []int{1, 2, 3, 4, 5}
var result = collection.FilterOutByCondition(slice, func(v int) bool {
return v%2 == 0
})
fmt.Println(result)
}
查看 / 收起单元测试
func TestFilterOutByCondition(t *testing.T) {
var cases = []struct {
name string
input []int
condition func(int) bool
expected []int
}{{"TestFilterOutByCondition_NonEmptySlice", []int{1, 2, 3, 4, 5}, func(v int) bool {
return v%2 == 0
}, []int{1, 3, 5}}, {"TestFilterOutByCondition_EmptySlice", []int{}, func(v int) bool {
return v%2 == 0
}, []int{}}, {"TestFilterOutByCondition_NilSlice", nil, func(v int) bool {
return v%2 == 0
}, nil}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
actual := collection.FilterOutByCondition(c.input, c.condition)
if len(actual) != len(c.expected) {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "after filter, the length of input is not equal")
}
for i := 0; i < len(actual); i++ {
if actual[i] != c.expected[i] {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "after filter, the inputV of input is not equal")
}
}
})
}
}
func FilterOutByKey(m M, key K) M
过滤 map 中特定的 key,返回过滤后的 map
示例代码:
func ExampleFilterOutByKey() {
var m = map[string]int{"a": 1, "b": 2, "c": 3}
var result = collection.FilterOutByKey(m, "b")
fmt.Println(result)
}
查看 / 收起单元测试
func TestFilterOutByKey(t *testing.T) {
var cases = []struct {
name string
input map[int]int
key int
expected map[int]int
}{{"TestFilterOutByKey_NonEmptyMap", map[int]int{1: 1, 2: 2, 3: 3}, 1, map[int]int{2: 2, 3: 3}}, {"TestFilterOutByKey_EmptyMap", map[int]int{}, 1, map[int]int{}}, {"TestFilterOutByKey_NilMap", nil, 1, nil}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
actual := collection.FilterOutByKey(c.input, c.key)
if len(actual) != len(c.expected) {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "after filter, the length of map is not equal")
}
for k, v := range actual {
if v != c.expected[k] {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "after filter, the inputV of map is not equal")
}
}
})
}
}
func FilterOutByValue(m M, value V, handler ComparisonHandler[V]) M
过滤 map 中特定的 value,返回过滤后的 map
示例代码:
func ExampleFilterOutByValue() {
var m = map[string]int{"a": 1, "b": 2, "c": 3}
var result = collection.FilterOutByValue(m, 2, func(source, target int) bool {
return source == target
})
fmt.Println(len(result))
}
查看 / 收起单元测试
func TestFilterOutByValue(t *testing.T) {
var cases = []struct {
name string
input map[int]int
value int
expected map[int]int
}{{"TestFilterOutByValue_NonEmptyMap", map[int]int{1: 1, 2: 2, 3: 3}, 1, map[int]int{2: 2, 3: 3}}, {"TestFilterOutByValue_EmptyMap", map[int]int{}, 1, map[int]int{}}, {"TestFilterOutByValue_NilMap", nil, 1, nil}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
actual := collection.FilterOutByValue(c.input, c.value, func(source, target int) bool {
return source == target
})
if len(actual) != len(c.expected) {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "after filter, the length of map is not equal")
}
for k, v := range actual {
if v != c.expected[k] {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "after filter, the inputV of map is not equal")
}
}
})
}
}
func FilterOutByKeys(m M, keys ...K) M
过滤 map 中多个 key,返回过滤后的 map
示例代码:
func ExampleFilterOutByKeys() {
var m = map[string]int{"a": 1, "b": 2, "c": 3}
var result = collection.FilterOutByKeys(m, "a", "c")
fmt.Println(result)
}
查看 / 收起单元测试
func TestFilterOutByKeys(t *testing.T) {
var cases = []struct {
name string
input map[int]int
keys []int
expected map[int]int
}{{"TestFilterOutByKeys_NonEmptyMap", map[int]int{1: 1, 2: 2, 3: 3}, []int{1, 3}, map[int]int{2: 2}}, {"TestFilterOutByKeys_EmptyMap", map[int]int{}, []int{1, 3}, map[int]int{}}, {"TestFilterOutByKeys_NilMap", nil, []int{1, 3}, nil}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
actual := collection.FilterOutByKeys(c.input, c.keys...)
if len(actual) != len(c.expected) {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "after filter, the length of map is not equal")
}
for k, v := range actual {
if v != c.expected[k] {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "after filter, the inputV of map is not equal")
}
}
})
}
}
func FilterOutByValues(m M, values []V, handler ComparisonHandler[V]) M
过滤 map 中多个 values,返回过滤后的 map
示例代码:
func ExampleFilterOutByValues() {
var m = map[string]int{"a": 1, "b": 2, "c": 3}
var result = collection.FilterOutByValues(m, []int{1}, func(source, target int) bool {
return source == target
})
for i, s := range []string{"a", "b", "c"} {
fmt.Println(i, result[s])
}
}
查看 / 收起单元测试
func TestFilterOutByValues(t *testing.T) {
var cases = []struct {
name string
input map[int]int
values []int
expected map[int]int
}{{"TestFilterOutByValues_NonEmptyMap", map[int]int{1: 1, 2: 2, 3: 3}, []int{1, 3}, map[int]int{2: 2}}, {"TestFilterOutByValues_EmptyMap", map[int]int{}, []int{1, 3}, map[int]int{}}, {"TestFilterOutByValues_NilMap", nil, []int{1, 3}, nil}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
actual := collection.FilterOutByValues(c.input, c.values, func(source, target int) bool {
return source == target
})
if len(actual) != len(c.expected) {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "after filter, the length of map is not equal")
}
for k, v := range actual {
if v != c.expected[k] {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "after filter, the inputV of map is not equal")
}
}
})
}
}
func FilterOutByMap(m M, condition func (k K, v V) bool) M
过滤 map 中符合条件的元素,返回过滤后的 map
- condition 的返回值为 true 时,将会过滤掉该元素
示例代码:
func ExampleFilterOutByMap() {
var m = map[string]int{"a": 1, "b": 2, "c": 3}
var result = collection.FilterOutByMap(m, func(k string, v int) bool {
return k == "a" || v == 3
})
fmt.Println(result)
}
查看 / 收起单元测试
func TestFilterOutByMap(t *testing.T) {
var cases = []struct {
name string
input map[int]int
filter map[int]int
expected map[int]int
}{{"TestFilterOutByMap_NonEmptyMap", map[int]int{1: 1, 2: 2, 3: 3}, map[int]int{1: 1, 3: 3}, map[int]int{2: 2}}, {"TestFilterOutByMap_EmptyMap", map[int]int{}, map[int]int{1: 1, 3: 3}, map[int]int{}}, {"TestFilterOutByMap_NilMap", nil, map[int]int{1: 1, 3: 3}, nil}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
actual := collection.FilterOutByMap(c.input, func(k int, v int) bool {
return c.filter[k] == v
})
if len(actual) != len(c.expected) {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "after filter, the length of map is not equal")
}
for k, v := range actual {
if v != c.expected[k] {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "after filter, the inputV of map is not equal")
}
}
})
}
}
func FindLoopedNextInSlice(slice S, i int) (next int, value V)
返回 i 的下一个数组成员,当 i 达到数组长度时从 0 开始
- 当 i 为负数时将返回第一个元素
示例代码:
func ExampleFindLoopedNextInSlice() {
next, v := collection.FindLoopedNextInSlice([]int{1, 2, 3}, 1)
fmt.Println(next, v)
}
查看 / 收起单元测试
func TestFindLoopedNextInSlice(t *testing.T) {
var cases = []struct {
name string
input []int
i int
expected int
}{{"TestFindLoopedNextInSlice_NonEmptySlice", []int{1, 2, 3}, 1, 2}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
actual, _ := collection.FindLoopedNextInSlice(c.input, c.i)
if actual != c.expected {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "the next index of input is not equal")
}
})
}
}
func FindLoopedPrevInSlice(slice S, i int) (prev int, value V)
返回 i 的上一个数组成员,当 i 为 0 时从数组末尾开始
- 当 i 为负数时将返回最后一个元素
示例代码:
func ExampleFindLoopedPrevInSlice() {
prev, v := collection.FindLoopedPrevInSlice([]int{1, 2, 3}, 1)
fmt.Println(prev, v)
}
查看 / 收起单元测试
func TestFindLoopedPrevInSlice(t *testing.T) {
var cases = []struct {
name string
input []int
i int
expected int
}{{"TestFindLoopedPrevInSlice_NonEmptySlice", []int{1, 2, 3}, 1, 0}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
actual, _ := collection.FindLoopedPrevInSlice(c.input, c.i)
if actual != c.expected {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "the prev index of input is not equal")
}
})
}
}
func FindCombinationsInSliceByRange(s S, minSize int, maxSize int) []S
获取给定数组的所有组合,且每个组合的成员数量限制在指定范围内
示例代码:
func ExampleFindCombinationsInSliceByRange() {
result := collection.FindCombinationsInSliceByRange([]int{1, 2, 3}, 1, 2)
fmt.Println(len(result))
}
查看 / 收起单元测试
func TestFindCombinationsInSliceByRange(t *testing.T) {
var cases = []struct {
name string
input []int
minSize int
maxSize int
expected [][]int
}{{"TestFindCombinationsInSliceByRange_NonEmptySlice", []int{1, 2, 3}, 1, 2, [][]int{{1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}}}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
actual := collection.FindCombinationsInSliceByRange(c.input, c.minSize, c.maxSize)
if len(actual) != len(c.expected) {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "the length of input is not equal")
}
})
}
}
func FindFirstOrDefaultInSlice(slice S, defaultValue V) V
判断切片中是否存在元素,返回第一个元素,不存在则返回默认值
示例代码:
func ExampleFindFirstOrDefaultInSlice() {
result := collection.FindFirstOrDefaultInSlice([]int{1, 2, 3}, 0)
fmt.Println(result)
}
查看 / 收起单元测试
func TestFindFirstOrDefaultInSlice(t *testing.T) {
var cases = []struct {
name string
input []int
expected int
}{{"TestFindFirstOrDefaultInSlice_NonEmptySlice", []int{1, 2, 3}, 1}, {"TestFindFirstOrDefaultInSlice_EmptySlice", []int{}, 0}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
actual := collection.FindFirstOrDefaultInSlice(c.input, 0)
if actual != c.expected {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "the first element of input is not equal")
}
})
}
}
func FindOrDefaultInSlice(slice S, defaultValue V, handler func (v V) bool) (t V)
判断切片中是否存在某个元素,返回第一个匹配的索引和元素,不存在则返回默认值
示例代码:
func ExampleFindOrDefaultInSlice() {
result := collection.FindOrDefaultInSlice([]int{1, 2, 3}, 0, func(v int) bool {
return v == 2
})
fmt.Println(result)
}
查看 / 收起单元测试
func TestFindOrDefaultInSlice(t *testing.T) {
var cases = []struct {
name string
input []int
expected int
}{{"TestFindOrDefaultInSlice_NonEmptySlice", []int{1, 2, 3}, 2}, {"TestFindOrDefaultInSlice_EmptySlice", []int{}, 0}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
actual := collection.FindOrDefaultInSlice(c.input, 0, func(v int) bool {
return v == 2
})
if actual != c.expected {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "the element of input is not equal")
}
})
}
}
func FindOrDefaultInComparableSlice(slice S, v V, defaultValue V) (t V)
判断切片中是否存在某个元素,返回第一个匹配的索引和元素,不存在则返回默认值
示例代码:
func ExampleFindOrDefaultInComparableSlice() {
result := collection.FindOrDefaultInComparableSlice([]int{1, 2, 3}, 2, 0)
fmt.Println(result)
}
查看 / 收起单元测试
func TestFindOrDefaultInComparableSlice(t *testing.T) {
var cases = []struct {
name string
input []int
expected int
}{{"TestFindOrDefaultInComparableSlice_NonEmptySlice", []int{1, 2, 3}, 2}, {"TestFindOrDefaultInComparableSlice_EmptySlice", []int{}, 0}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
actual := collection.FindOrDefaultInComparableSlice(c.input, 2, 0)
if actual != c.expected {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "the element of input is not equal")
}
})
}
}
func FindInSlice(slice S, handler func (v V) bool) (i int, t V)
判断切片中是否存在某个元素,返回第一个匹配的索引和元素,不存在则索引返回 -1
示例代码:
func ExampleFindInSlice() {
_, result := collection.FindInSlice([]int{1, 2, 3}, func(v int) bool {
return v == 2
})
fmt.Println(result)
}
查看 / 收起单元测试
func TestFindInSlice(t *testing.T) {
var cases = []struct {
name string
input []int
expected int
}{{"TestFindInSlice_NonEmptySlice", []int{1, 2, 3}, 2}, {"TestFindInSlice_EmptySlice", []int{}, 0}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
_, actual := collection.FindInSlice(c.input, func(v int) bool {
return v == 2
})
if actual != c.expected {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "the element of input is not equal")
}
})
}
}
func FindIndexInSlice(slice S, handler func (v V) bool) int
判断切片中是否存在某个元素,返回第一个匹配的索引,不存在则索引返回 -1
示例代码:
func ExampleFindIndexInSlice() {
result := collection.FindIndexInSlice([]int{1, 2, 3}, func(v int) bool {
return v == 2
})
fmt.Println(result)
}
查看 / 收起单元测试
func TestFindIndexInSlice(t *testing.T) {
var cases = []struct {
name string
input []int
expected int
}{{"TestFindIndexInSlice_NonEmptySlice", []int{1, 2, 3}, 1}, {"TestFindIndexInSlice_EmptySlice", []int{}, -1}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
actual := collection.FindIndexInSlice(c.input, func(v int) bool {
return v == 2
})
if actual != c.expected {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "the index of input is not equal")
}
})
}
}
func FindInComparableSlice(slice S, v V) (i int, t V)
判断切片中是否存在某个元素,返回第一个匹配的索引和元素,不存在则索引返回 -1
示例代码:
func ExampleFindInComparableSlice() {
index, result := collection.FindInComparableSlice([]int{1, 2, 3}, 2)
fmt.Println(index, result)
}
查看 / 收起单元测试
func TestFindInComparableSlice(t *testing.T) {
var cases = []struct {
name string
input []int
expected int
}{{"TestFindInComparableSlice_NonEmptySlice", []int{1, 2, 3}, 2}, {"TestFindInComparableSlice_EmptySlice", []int{}, 0}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
_, actual := collection.FindInComparableSlice(c.input, 2)
if actual != c.expected {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "the element of input is not equal")
}
})
}
}
func FindIndexInComparableSlice(slice S, v V) int
判断切片中是否存在某个元素,返回第一个匹配的索引,不存在则索引返回 -1
示例代码:
func ExampleFindIndexInComparableSlice() {
result := collection.FindIndexInComparableSlice([]int{1, 2, 3}, 2)
fmt.Println(result)
}
查看 / 收起单元测试
func TestFindIndexInComparableSlice(t *testing.T) {
var cases = []struct {
name string
input []int
expected int
}{{"TestFindIndexInComparableSlice_NonEmptySlice", []int{1, 2, 3}, 1}, {"TestFindIndexInComparableSlice_EmptySlice", []int{}, -1}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
actual := collection.FindIndexInComparableSlice(c.input, 2)
if actual != c.expected {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "the index of input is not equal")
}
})
}
}
func FindMinimumInComparableSlice(slice S) (result V)
获取切片中的最小值
示例代码:
func ExampleFindMinimumInComparableSlice() {
result := collection.FindMinimumInComparableSlice([]int{1, 2, 3})
fmt.Println(result)
}
查看 / 收起单元测试
func TestFindMinimumInComparableSlice(t *testing.T) {
var cases = []struct {
name string
input []int
expected int
}{{"TestFindMinimumInComparableSlice_NonEmptySlice", []int{1, 2, 3}, 1}, {"TestFindMinimumInComparableSlice_EmptySlice", []int{}, 0}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
actual := collection.FindMinimumInComparableSlice(c.input)
if actual != c.expected {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "the minimum of input is not equal")
}
})
}
}
func FindMinimumInSlice(slice S, handler OrderedValueGetter[V, N]) (result V)
获取切片中的最小值
示例代码:
func ExampleFindMinimumInSlice() {
result := collection.FindMinimumInSlice([]int{1, 2, 3}, func(v int) int {
return v
})
fmt.Println(result)
}
查看 / 收起单元测试
func TestFindMinimumInSlice(t *testing.T) {
var cases = []struct {
name string
input []int
expected int
}{{"TestFindMinimumInSlice_NonEmptySlice", []int{1, 2, 3}, 1}, {"TestFindMinimumInSlice_EmptySlice", []int{}, 0}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
actual := collection.FindMinimumInSlice(c.input, func(v int) int {
return v
})
if actual != c.expected {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "the minimum of input is not equal")
}
})
}
}
func FindMaximumInComparableSlice(slice S) (result V)
获取切片中的最大值
示例代码:
func ExampleFindMaximumInComparableSlice() {
result := collection.FindMaximumInComparableSlice([]int{1, 2, 3})
fmt.Println(result)
}
查看 / 收起单元测试
func TestFindMaximumInComparableSlice(t *testing.T) {
var cases = []struct {
name string
input []int
expected int
}{{"TestFindMaximumInComparableSlice_NonEmptySlice", []int{1, 2, 3}, 3}, {"TestFindMaximumInComparableSlice_EmptySlice", []int{}, 0}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
actual := collection.FindMaximumInComparableSlice(c.input)
if actual != c.expected {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "the maximum of input is not equal")
}
})
}
}
func FindMaximumInSlice(slice S, handler OrderedValueGetter[V, N]) (result V)
获取切片中的最大值
示例代码:
func ExampleFindMaximumInSlice() {
result := collection.FindMaximumInSlice([]int{1, 2, 3}, func(v int) int {
return v
})
fmt.Println(result)
}
查看 / 收起单元测试
func TestFindMaximumInSlice(t *testing.T) {
var cases = []struct {
name string
input []int
expected int
}{{"TestFindMaximumInSlice_NonEmptySlice", []int{1, 2, 3}, 3}, {"TestFindMaximumInSlice_EmptySlice", []int{}, 0}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
actual := collection.FindMaximumInSlice(c.input, func(v int) int {
return v
})
if actual != c.expected {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "the maximum of input is not equal")
}
})
}
}
func FindMin2MaxInComparableSlice(slice S) (min V, max V)
获取切片中的最小值和最大值
示例代码:
func ExampleFindMin2MaxInComparableSlice() {
minimum, maximum := collection.FindMin2MaxInComparableSlice([]int{1, 2, 3})
fmt.Println(minimum, maximum)
}
查看 / 收起单元测试
func TestFindMin2MaxInComparableSlice(t *testing.T) {
var cases = []struct {
name string
input []int
expectedMin int
expectedMax int
}{{"TestFindMin2MaxInComparableSlice_NonEmptySlice", []int{1, 2, 3}, 1, 3}, {"TestFindMin2MaxInComparableSlice_EmptySlice", []int{}, 0, 0}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
minimum, maximum := collection.FindMin2MaxInComparableSlice(c.input)
if minimum != c.expectedMin || maximum != c.expectedMax {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expectedMin, minimum, "the minimum of input is not equal")
}
})
}
}
func FindMin2MaxInSlice(slice S, handler OrderedValueGetter[V, N]) (min V, max V)
获取切片中的最小值和最大值
示例代码:
func ExampleFindMin2MaxInSlice() {
minimum, maximum := collection.FindMin2MaxInSlice([]int{1, 2, 3}, func(v int) int {
return v
})
fmt.Println(minimum, maximum)
}
查看 / 收起单元测试
func TestFindMin2MaxInSlice(t *testing.T) {
var cases = []struct {
name string
input []int
expectedMin int
expectedMax int
}{{"TestFindMin2MaxInSlice_NonEmptySlice", []int{1, 2, 3}, 1, 3}, {"TestFindMin2MaxInSlice_EmptySlice", []int{}, 0, 0}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
minimum, maximum := collection.FindMin2MaxInSlice(c.input, func(v int) int {
return v
})
if minimum != c.expectedMin || maximum != c.expectedMax {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expectedMin, minimum, "the minimum of input is not equal")
}
})
}
}
func FindMinFromComparableMap(m M) (result V)
获取 map 中的最小值
示例代码:
func ExampleFindMinFromComparableMap() {
result := collection.FindMinFromComparableMap(map[int]int{1: 1, 2: 2, 3: 3})
fmt.Println(result)
}
查看 / 收起单元测试
func TestFindMinFromComparableMap(t *testing.T) {
var cases = []struct {
name string
input map[int]int
expected int
}{{"TestFindMinFromComparableMap_NonEmptyMap", map[int]int{1: 1, 2: 2, 3: 3}, 1}, {"TestFindMinFromComparableMap_EmptyMap", map[int]int{}, 0}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
actual := collection.FindMinFromComparableMap(c.input)
if actual != c.expected {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "the minimum of input is not equal")
}
})
}
}
func FindMinFromMap(m M, handler OrderedValueGetter[V, N]) (result V)
获取 map 中的最小值
示例代码:
func ExampleFindMinFromMap() {
result := collection.FindMinFromMap(map[int]int{1: 1, 2: 2, 3: 3}, func(v int) int {
return v
})
fmt.Println(result)
}
查看 / 收起单元测试
func TestFindMinFromMap(t *testing.T) {
var cases = []struct {
name string
input map[int]int
expected int
}{{"TestFindMinFromMap_NonEmptyMap", map[int]int{1: 1, 2: 2, 3: 3}, 1}, {"TestFindMinFromMap_EmptyMap", map[int]int{}, 0}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
actual := collection.FindMinFromMap(c.input, func(v int) int {
return v
})
if actual != c.expected {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "the minimum of input is not equal")
}
})
}
}
func FindMaxFromComparableMap(m M) (result V)
获取 map 中的最大值
示例代码:
func ExampleFindMaxFromComparableMap() {
result := collection.FindMaxFromComparableMap(map[int]int{1: 1, 2: 2, 3: 3})
fmt.Println(result)
}
查看 / 收起单元测试
func TestFindMaxFromComparableMap(t *testing.T) {
var cases = []struct {
name string
input map[int]int
expected int
}{{"TestFindMaxFromComparableMap_NonEmptyMap", map[int]int{1: 1, 2: 2, 3: 3}, 3}, {"TestFindMaxFromComparableMap_EmptyMap", map[int]int{}, 0}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
actual := collection.FindMaxFromComparableMap(c.input)
if actual != c.expected {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "the maximum of input is not equal")
}
})
}
}
func FindMaxFromMap(m M, handler OrderedValueGetter[V, N]) (result V)
获取 map 中的最大值
示例代码:
func ExampleFindMaxFromMap() {
result := collection.FindMaxFromMap(map[int]int{1: 1, 2: 2, 3: 3}, func(v int) int {
return v
})
fmt.Println(result)
}
查看 / 收起单元测试
func TestFindMaxFromMap(t *testing.T) {
var cases = []struct {
name string
input map[int]int
expected int
}{{"TestFindMaxFromMap_NonEmptyMap", map[int]int{1: 1, 2: 2, 3: 3}, 3}, {"TestFindMaxFromMap_EmptyMap", map[int]int{}, 0}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
actual := collection.FindMaxFromMap(c.input, func(v int) int {
return v
})
if actual != c.expected {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, actual, "the maximum of input is not equal")
}
})
}
}
func FindMin2MaxFromComparableMap(m M) (min V, max V)
获取 map 中的最小值和最大值
示例代码:
func ExampleFindMin2MaxFromComparableMap() {
minimum, maximum := collection.FindMin2MaxFromComparableMap(map[int]int{1: 1, 2: 2, 3: 3})
fmt.Println(minimum, maximum)
}
查看 / 收起单元测试
func TestFindMin2MaxFromComparableMap(t *testing.T) {
var cases = []struct {
name string
input map[int]int
expectedMin int
expectedMax int
}{{"TestFindMin2MaxFromComparableMap_NonEmptyMap", map[int]int{1: 1, 2: 2, 3: 3}, 1, 3}, {"TestFindMin2MaxFromComparableMap_EmptyMap", map[int]int{}, 0, 0}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
minimum, maximum := collection.FindMin2MaxFromComparableMap(c.input)
if minimum != c.expectedMin || maximum != c.expectedMax {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expectedMin, minimum, "the minimum of input is not equal")
}
})
}
}
func FindMin2MaxFromMap(m M) (min V, max V)
获取 map 中的最小值和最大值
示例代码:
func ExampleFindMin2MaxFromMap() {
minimum, maximum := collection.FindMin2MaxFromMap(map[int]int{1: 1, 2: 2, 3: 3})
fmt.Println(minimum, maximum)
}
查看 / 收起单元测试
func TestFindMin2MaxFromMap(t *testing.T) {
var cases = []struct {
name string
input map[int]int
expectedMin int
expectedMax int
}{{"TestFindMin2MaxFromMap_NonEmptyMap", map[int]int{1: 1, 2: 2, 3: 3}, 1, 3}, {"TestFindMin2MaxFromMap_EmptyMap", map[int]int{}, 0, 0}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
minimum, maximum := collection.FindMin2MaxFromMap(c.input)
if minimum != c.expectedMin || maximum != c.expectedMax {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expectedMin, minimum, "the minimum of input is not equal")
}
})
}
}
func SwapSlice(slice *S, i int, j int)
将切片中的两个元素进行交换
示例代码:
func ExampleSwapSlice() {
var s = []int{1, 2, 3}
collection.SwapSlice(&s, 0, 1)
fmt.Println(s)
}
查看 / 收起单元测试
func TestSwapSlice(t *testing.T) {
var cases = []struct {
name string
slice []int
i int
j int
expect []int
}{{"TestSwapSliceNonEmpty", []int{1, 2, 3}, 0, 1, []int{2, 1, 3}}, {"TestSwapSliceEmpty", []int{}, 0, 0, []int{}}, {"TestSwapSliceIndexOutOfBound", []int{1, 2, 3}, 0, 3, []int{1, 2, 3}}, {"TestSwapSliceNil", nil, 0, 0, nil}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
collection.SwapSlice(&c.slice, c.i, c.j)
for i, v := range c.slice {
if v != c.expect[i] {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expect, c.slice, "the slice is not equal")
}
}
})
}
}
func MappingFromSlice(slice S, handler func (value V) N) NS
将切片中的元素进行转换
示例代码:
func ExampleMappingFromSlice() {
result := collection.MappingFromSlice([]int{1, 2, 3}, func(value int) int {
return value + 1
})
fmt.Println(result)
}
查看 / 收起单元测试
func TestMappingFromSlice(t *testing.T) {
var cases = []struct {
name string
input []int
expected []int
}{{"TestMappingFromSlice_NonEmptySlice", []int{1, 2, 3}, []int{2, 3, 4}}, {"TestMappingFromSlice_EmptySlice", []int{}, []int{}}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
result := collection.MappingFromSlice[[]int, []int](c.input, func(value int) int {
return value + 1
})
if len(result) != len(c.expected) {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, result, "the length of input is not equal")
}
for i := 0; i < len(result); i++ {
if result[i] != c.expected[i] {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, result, "the value of input is not equal")
}
}
})
}
}
func MappingFromMap(m M, handler func (value V) N) NM
将 map 中的元素进行转换
示例代码:
func ExampleMappingFromMap() {
result := collection.MappingFromMap(map[int]int{1: 1, 2: 2, 3: 3}, func(value int) int {
return value + 1
})
fmt.Println(result)
}
查看 / 收起单元测试
func TestMappingFromMap(t *testing.T) {
var cases = []struct {
name string
input map[int]int
expected map[int]int
}{{"TestMappingFromMap_NonEmptyMap", map[int]int{1: 1, 2: 2, 3: 3}, map[int]int{1: 2, 2: 3, 3: 4}}, {"TestMappingFromMap_EmptyMap", map[int]int{}, map[int]int{}}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
result := collection.MappingFromMap[map[int]int, map[int]int](c.input, func(value int) int {
return value + 1
})
if len(result) != len(c.expected) {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, result, "the length of input is not equal")
}
for k, v := range result {
if v != c.expected[k] {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, result, "the value of input is not equal")
}
}
})
}
}
func MergeSlices(slices ...S) (result S)
合并切片
示例代码:
func ExampleMergeSlices() {
fmt.Println(collection.MergeSlices([]int{1, 2, 3}, []int{4, 5, 6}))
}
查看 / 收起单元测试
func TestMergeSlices(t *testing.T) {
var cases = []struct {
name string
input [][]int
expected []int
}{{"TestMergeSlices_NonEmptySlice", [][]int{{1, 2, 3}, {4, 5, 6}}, []int{1, 2, 3, 4, 5, 6}}, {"TestMergeSlices_EmptySlice", [][]int{{}, {}}, []int{}}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
result := collection.MergeSlices(c.input...)
if len(result) != len(c.expected) {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, result, "the length of input is not equal")
}
for i := 0; i < len(result); i++ {
if result[i] != c.expected[i] {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, result, "the value of input is not equal")
}
}
})
}
}
func MergeMaps(maps ...M) (result M)
合并 map,当多个 map 中存在相同的 key 时,后面的 map 中的 key 将会覆盖前面的 map 中的 key
示例代码:
func ExampleMergeMaps() {
m := collection.MergeMaps(map[int]int{1: 1, 2: 2, 3: 3}, map[int]int{4: 4, 5: 5, 6: 6})
fmt.Println(len(m))
}
查看 / 收起单元测试
func TestMergeMaps(t *testing.T) {
var cases = []struct {
name string
input []map[int]int
expected int
}{{"TestMergeMaps_NonEmptyMap", []map[int]int{{1: 1, 2: 2, 3: 3}, {4: 4, 5: 5, 6: 6}}, 6}, {"TestMergeMaps_EmptyMap", []map[int]int{{}, {}}, 0}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
result := collection.MergeMaps(c.input...)
if len(result) != c.expected {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, result, "the length of input is not equal")
}
})
}
}
func MergeMapsWithSkip(maps ...M) (result M)
合并 map,当多个 map 中存在相同的 key 时,后面的 map 中的 key 将会被跳过
示例代码:
func ExampleMergeMapsWithSkip() {
m := collection.MergeMapsWithSkip(map[int]int{1: 1}, map[int]int{1: 2})
fmt.Println(m[1])
}
查看 / 收起单元测试
func TestMergeMapsWithSkip(t *testing.T) {
var cases = []struct {
name string
input []map[int]int
expected int
}{{"TestMergeMapsWithSkip_NonEmptyMap", []map[int]int{{1: 1}, {1: 2}}, 1}, {"TestMergeMapsWithSkip_EmptyMap", []map[int]int{{}, {}}, 0}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
result := collection.MergeMapsWithSkip(c.input...)
if len(result) != c.expected {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, result, "the length of input is not equal")
}
})
}
}
func ChooseRandomSliceElementRepeatN(slice S, n int) (result []V)
返回 slice 中的 n 个可重复随机元素
- 当 slice 长度为 0 或 n 小于等于 0 时将会返回 nil
示例代码:
func ExampleChooseRandomSliceElementRepeatN() {
result := collection.ChooseRandomSliceElementRepeatN([]int{1}, 10)
fmt.Println(result)
}
查看 / 收起单元测试
func TestChooseRandomSliceElementRepeatN(t *testing.T) {
var cases = []struct {
name string
input []int
expected int
}{{"TestChooseRandomSliceElementRepeatN_NonEmptySlice", []int{1, 2, 3}, 3}, {"TestChooseRandomSliceElementRepeatN_EmptySlice", []int{}, 0}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
result := collection.ChooseRandomSliceElementRepeatN(c.input, 3)
if len(result) != c.expected {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, result, "the length of input is not equal")
}
})
}
}
func ChooseRandomIndexRepeatN(slice S, n int) (result []int)
返回 slice 中的 n 个可重复随机元素的索引
- 当 slice 长度为 0 或 n 小于等于 0 时将会返回 nil
示例代码:
func ExampleChooseRandomIndexRepeatN() {
result := collection.ChooseRandomIndexRepeatN([]int{1}, 10)
fmt.Println(result)
}
查看 / 收起单元测试
func TestChooseRandomIndexRepeatN(t *testing.T) {
var cases = []struct {
name string
input []int
expected int
}{{"TestChooseRandomIndexRepeatN_NonEmptySlice", []int{1, 2, 3}, 3}, {"TestChooseRandomIndexRepeatN_EmptySlice", []int{}, 0}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
result := collection.ChooseRandomIndexRepeatN(c.input, 3)
if len(result) != c.expected {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, result, "the length of input is not equal")
}
})
}
}
func ChooseRandomSliceElement(slice S) (v V)
返回 slice 中随机一个元素,当 slice 长度为 0 时将会得到 V 的零值
示例代码:
func ExampleChooseRandomSliceElement() {
result := collection.ChooseRandomSliceElement([]int{1})
fmt.Println(result)
}
查看 / 收起单元测试
func TestChooseRandomSliceElement(t *testing.T) {
var cases = []struct {
name string
input []int
expected map[int]bool
}{{"TestChooseRandomSliceElement_NonEmptySlice", []int{1, 2, 3}, map[int]bool{1: true, 2: true, 3: true}}, {"TestChooseRandomSliceElement_EmptySlice", []int{}, map[int]bool{0: true}}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
result := collection.ChooseRandomSliceElement(c.input)
if !c.expected[result] {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, result, "the length of input is not equal")
}
})
}
}
func ChooseRandomIndex(slice S) (index int)
返回 slice 中随机一个元素的索引,当 slice 长度为 0 时将会得到 -1
示例代码:
func ExampleChooseRandomIndex() {
result := collection.ChooseRandomIndex([]int{1})
fmt.Println(result)
}
查看 / 收起单元测试
func TestChooseRandomIndex(t *testing.T) {
var cases = []struct {
name string
input []int
expected map[int]bool
}{{"TestChooseRandomIndex_NonEmptySlice", []int{1, 2, 3}, map[int]bool{0: true, 1: true, 2: true}}, {"TestChooseRandomIndex_EmptySlice", []int{}, map[int]bool{-1: true}}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
result := collection.ChooseRandomIndex(c.input)
if !c.expected[result] {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, result, "the length of input is not equal")
}
})
}
}
func ChooseRandomSliceElementN(slice S, n int) (result []V)
返回 slice 中的 n 个不可重复的随机元素
- 当 slice 长度为 0 或 n 大于 slice 长度或小于 0 时将会发生 panic
示例代码:
func ExampleChooseRandomSliceElementN() {
result := collection.ChooseRandomSliceElementN([]int{1}, 1)
fmt.Println(result)
}
查看 / 收起单元测试
func TestChooseRandomSliceElementN(t *testing.T) {
var cases = []struct {
name string
input []int
}{{"TestChooseRandomSliceElementN_NonEmptySlice", []int{1, 2, 3}}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
actual := collection.ChooseRandomSliceElementN(c.input, 3)
if !collection.AllInComparableSlice(actual, c.input) {
t.Fatalf("%s failed, actual: %v, error: %s", c.name, actual, "the length of input is not equal")
}
})
}
}
func ChooseRandomIndexN(slice S, n int) (result []int)
获取切片中的 n 个随机元素的索引
- 如果 n 大于切片长度或小于 0 时将会发生 panic
示例代码:
func ExampleChooseRandomIndexN() {
result := collection.ChooseRandomIndexN([]int{1}, 1)
fmt.Println(result)
}
查看 / 收起单元测试
func TestChooseRandomIndexN(t *testing.T) {
var cases = []struct {
name string
input []int
expected map[int]bool
}{{"TestChooseRandomIndexN_NonEmptySlice", []int{1, 2, 3}, map[int]bool{0: true, 1: true, 2: true}}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
result := collection.ChooseRandomIndexN(c.input, 3)
if !c.expected[result[0]] {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, result, "the length of input is not equal")
}
})
}
}
func ChooseRandomMapKeyRepeatN(m M, n int) (result []K)
获取 map 中的 n 个随机 key,允许重复
- 如果 n 大于 map 长度或小于 0 时将会发生 panic
示例代码:
func ExampleChooseRandomMapKeyRepeatN() {
result := collection.ChooseRandomMapKeyRepeatN(map[int]int{1: 1}, 1)
fmt.Println(result)
}
查看 / 收起单元测试
func TestChooseRandomMapKeyRepeatN(t *testing.T) {
var cases = []struct {
name string
input map[int]int
expected map[int]bool
}{{"TestChooseRandomMapKeyRepeatN_NonEmptyMap", map[int]int{1: 1, 2: 2, 3: 3}, map[int]bool{1: true, 2: true, 3: true}}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
result := collection.ChooseRandomMapKeyRepeatN(c.input, 3)
if !c.expected[result[0]] {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, result, "the length of input is not equal")
}
})
}
}
func ChooseRandomMapValueRepeatN(m M, n int) (result []V)
获取 map 中的 n 个随机 n,允许重复
- 如果 n 大于 map 长度或小于 0 时将会发生 panic
示例代码:
func ExampleChooseRandomMapValueRepeatN() {
result := collection.ChooseRandomMapValueRepeatN(map[int]int{1: 1}, 1)
fmt.Println(result)
}
查看 / 收起单元测试
func TestChooseRandomMapValueRepeatN(t *testing.T) {
var cases = []struct {
name string
input map[int]int
expected map[int]bool
}{{"TestChooseRandomMapValueRepeatN_NonEmptyMap", map[int]int{1: 1, 2: 2, 3: 3}, map[int]bool{1: true, 2: true, 3: true}}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
result := collection.ChooseRandomMapValueRepeatN(c.input, 3)
if !c.expected[result[0]] {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, result, "the length of input is not equal")
}
})
}
}
func ChooseRandomMapKeyAndValueRepeatN(m M, n int) M
获取 map 中的 n 个随机 key 和 v,允许重复
- 如果 n 大于 map 长度或小于 0 时将会发生 panic
示例代码:
func ExampleChooseRandomMapKeyAndValueRepeatN() {
result := collection.ChooseRandomMapKeyAndValueRepeatN(map[int]int{1: 1}, 1)
fmt.Println(result)
}
查看 / 收起单元测试
func TestChooseRandomMapKeyAndValueRepeatN(t *testing.T) {
var cases = []struct {
name string
input map[int]int
expected map[int]bool
}{{"TestChooseRandomMapKeyAndValueRepeatN_NonEmptyMap", map[int]int{1: 1, 2: 2, 3: 3}, map[int]bool{1: true, 2: true, 3: true}}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
result := collection.ChooseRandomMapKeyAndValueRepeatN(c.input, 3)
if !c.expected[result[1]] {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, result, "the length of input is not equal")
}
})
}
}
func ChooseRandomMapKey(m M) (k K)
获取 map 中的随机 key
示例代码:
func ExampleChooseRandomMapKey() {
result := collection.ChooseRandomMapKey(map[int]int{1: 1})
fmt.Println(result)
}
查看 / 收起单元测试
func TestChooseRandomMapKey(t *testing.T) {
var cases = []struct {
name string
input map[int]int
expected map[int]bool
}{{"TestChooseRandomMapKey_NonEmptyMap", map[int]int{1: 1, 2: 2, 3: 3}, map[int]bool{1: true, 2: true, 3: true}}, {"TestChooseRandomMapKey_EmptyMap", map[int]int{}, map[int]bool{0: true}}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
result := collection.ChooseRandomMapKey(c.input)
if !c.expected[result] {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, result, "the length of input is not equal")
}
})
}
}
func ChooseRandomMapValue(m M) (v V)
获取 map 中的随机 value
示例代码:
func ExampleChooseRandomMapValue() {
result := collection.ChooseRandomMapValue(map[int]int{1: 1})
fmt.Println(result)
}
查看 / 收起单元测试
func TestChooseRandomMapValue(t *testing.T) {
var cases = []struct {
name string
input map[int]int
expected map[int]bool
}{{"TestChooseRandomMapValue_NonEmptyMap", map[int]int{1: 1, 2: 2, 3: 3}, map[int]bool{1: true, 2: true, 3: true}}, {"TestChooseRandomMapValue_EmptyMap", map[int]int{}, map[int]bool{0: true}}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
result := collection.ChooseRandomMapValue(c.input)
if !c.expected[result] {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, result, "the length of input is not equal")
}
})
}
}
func ChooseRandomMapKeyN(m M, n int) (result []K)
获取 map 中的 inputN 个随机 key
- 如果 inputN 大于 map 长度或小于 0 时将会发生 panic
示例代码:
func ExampleChooseRandomMapKeyN() {
result := collection.ChooseRandomMapKeyN(map[int]int{1: 1}, 1)
fmt.Println(result)
}
func ChooseRandomMapValueN(m M, n int) (result []V)
获取 map 中的 n 个随机 value
- 如果 n 大于 map 长度或小于 0 时将会发生 panic
示例代码:
func ExampleChooseRandomMapValueN() {
result := collection.ChooseRandomMapValueN(map[int]int{1: 1}, 1)
fmt.Println(result)
}
查看 / 收起单元测试
func TestChooseRandomMapValueN(t *testing.T) {
var cases = []struct {
name string
input map[int]int
expected map[int]bool
}{{"TestChooseRandomMapValueN_NonEmptyMap", map[int]int{1: 1, 2: 2, 3: 3}, map[int]bool{1: true, 2: true, 3: true}}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
result := collection.ChooseRandomMapValueN(c.input, 3)
if !c.expected[result[0]] {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, result, "the length of input is not equal")
}
})
}
}
func ChooseRandomMapKeyAndValue(m M) (k K, v V)
获取 map 中的随机 key 和 v
示例代码:
func ExampleChooseRandomMapKeyAndValue() {
k, v := collection.ChooseRandomMapKeyAndValue(map[int]int{1: 1})
fmt.Println(k, v)
}
查看 / 收起单元测试
func TestChooseRandomMapKeyAndValue(t *testing.T) {
var cases = []struct {
name string
input map[int]int
expected map[int]bool
}{{"TestChooseRandomMapKeyAndValue_NonEmptyMap", map[int]int{1: 1, 2: 2, 3: 3}, map[int]bool{1: true, 2: true, 3: true}}, {"TestChooseRandomMapKeyAndValue_EmptyMap", map[int]int{}, map[int]bool{0: true}}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
k, v := collection.ChooseRandomMapKeyAndValue(c.input)
if !c.expected[k] || !c.expected[v] {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, k, "the length of input is not equal")
}
})
}
}
func ChooseRandomMapKeyAndValueN(m M, n int) M
获取 map 中的 inputN 个随机 key 和 v
- 如果 n 大于 map 长度或小于 0 时将会发生 panic
示例代码:
func ExampleChooseRandomMapKeyAndValueN() {
result := collection.ChooseRandomMapKeyAndValueN(map[int]int{1: 1}, 1)
fmt.Println(result)
}
查看 / 收起单元测试
func TestChooseRandomMapKeyAndValueN(t *testing.T) {
var cases = []struct {
name string
input map[int]int
expected map[int]bool
}{{"TestChooseRandomMapKeyAndValueN_NonEmptyMap", map[int]int{1: 1, 2: 2, 3: 3}, map[int]bool{1: true, 2: true, 3: true}}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
kvm := collection.ChooseRandomMapKeyAndValueN(c.input, 1)
for k := range kvm {
if !c.expected[k] {
t.Fatalf("%s failed, expected: %v, actual: %v, error: %s", c.name, c.expected, k, "the length of input is not equal")
}
}
})
}
}
func DescBy(a Sort, b Sort) bool
返回降序比较结果
示例代码:
func ExampleDescBy() {
var slice = []int{1, 2, 3}
sort.Slice(slice, func(i, j int) bool {
return collection.DescBy(slice[i], slice[j])
})
fmt.Println(slice)
}
查看 / 收起单元测试
func TestDescBy(t *testing.T) {
var cases = []struct {
name string
input []int
expected []int
}{{"TestDescBy_NonEmptySlice", []int{1, 2, 3}, []int{3, 2, 1}}, {"TestDescBy_EmptySlice", []int{}, []int{}}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
sort.Slice(c.input, func(i, j int) bool {
return collection.DescBy(c.input[i], c.input[j])
})
for i, v := range c.input {
if v != c.expected[i] {
t.Fatalf("%s failed, expected: %v, actual: %v", c.name, c.expected, c.input)
}
}
})
}
}
func AscBy(a Sort, b Sort) bool
返回升序比较结果
示例代码:
func ExampleAscBy() {
var slice = []int{1, 2, 3}
sort.Slice(slice, func(i, j int) bool {
return collection.AscBy(slice[i], slice[j])
})
fmt.Println(slice)
}
查看 / 收起单元测试
func TestAscBy(t *testing.T) {
var cases = []struct {
name string
input []int
expected []int
}{{"TestAscBy_NonEmptySlice", []int{1, 2, 3}, []int{1, 2, 3}}, {"TestAscBy_EmptySlice", []int{}, []int{}}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
sort.Slice(c.input, func(i, j int) bool {
return collection.AscBy(c.input[i], c.input[j])
})
for i, v := range c.input {
if v != c.expected[i] {
t.Fatalf("%s failed, expected: %v, actual: %v", c.name, c.expected, c.input)
}
}
})
}
}
func Desc(slice *S, getter func (index int) Sort)
对切片进行降序排序
示例代码:
func ExampleDesc() {
var slice = []int{1, 2, 3}
collection.Desc(&slice, func(index int) int {
return slice[index]
})
fmt.Println(slice)
}
查看 / 收起单元测试
func TestDesc(t *testing.T) {
var cases = []struct {
name string
input []int
expected []int
}{{"TestDesc_NonEmptySlice", []int{1, 2, 3}, []int{3, 2, 1}}, {"TestDesc_EmptySlice", []int{}, []int{}}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
collection.Desc(&c.input, func(index int) int {
return c.input[index]
})
for i, v := range c.input {
if v != c.expected[i] {
t.Fatalf("%s failed, expected: %v, actual: %v", c.name, c.expected, c.input)
}
}
})
}
}
func DescByClone(slice S, getter func (index int) Sort) S
对切片进行降序排序,返回排序后的切片
示例代码:
func ExampleDescByClone() {
var slice = []int{1, 2, 3}
result := collection.DescByClone(slice, func(index int) int {
return slice[index]
})
fmt.Println(result)
}
查看 / 收起单元测试
func TestDescByClone(t *testing.T) {
var cases = []struct {
name string
input []int
expected []int
}{{"TestDescByClone_NonEmptySlice", []int{1, 2, 3}, []int{3, 2, 1}}, {"TestDescByClone_EmptySlice", []int{}, []int{}}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
result := collection.DescByClone(c.input, func(index int) int {
return c.input[index]
})
for i, v := range result {
if v != c.expected[i] {
t.Fatalf("%s failed, expected: %v, actual: %v", c.name, c.expected, result)
}
}
})
}
}
func Asc(slice *S, getter func (index int) Sort)
对切片进行升序排序
示例代码:
func ExampleAsc() {
var slice = []int{1, 2, 3}
collection.Asc(&slice, func(index int) int {
return slice[index]
})
fmt.Println(slice)
}
查看 / 收起单元测试
func TestAsc(t *testing.T) {
var cases = []struct {
name string
input []int
expected []int
}{{"TestAsc_NonEmptySlice", []int{1, 2, 3}, []int{1, 2, 3}}, {"TestAsc_EmptySlice", []int{}, []int{}}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
collection.Asc(&c.input, func(index int) int {
return c.input[index]
})
for i, v := range c.input {
if v != c.expected[i] {
t.Fatalf("%s failed, expected: %v, actual: %v", c.name, c.expected, c.input)
}
}
})
}
}
func AscByClone(slice S, getter func (index int) Sort) S
对切片进行升序排序,返回排序后的切片
示例代码:
func ExampleAscByClone() {
var slice = []int{1, 2, 3}
result := collection.AscByClone(slice, func(index int) int {
return slice[index]
})
fmt.Println(result)
}
查看 / 收起单元测试
func TestAscByClone(t *testing.T) {
var cases = []struct {
name string
input []int
expected []int
}{{"TestAscByClone_NonEmptySlice", []int{1, 2, 3}, []int{1, 2, 3}}, {"TestAscByClone_EmptySlice", []int{}, []int{}}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
result := collection.AscByClone(c.input, func(index int) int {
return c.input[index]
})
for i, v := range result {
if v != c.expected[i] {
t.Fatalf("%s failed, expected: %v, actual: %v", c.name, c.expected, result)
}
}
})
}
}
func Shuffle(slice *S)
对切片进行随机排序
示例代码:
func ExampleShuffle() {
var slice = []int{1, 2, 3}
collection.Shuffle(&slice)
fmt.Println(len(slice))
}
查看 / 收起单元测试
func TestShuffle(t *testing.T) {
var cases = []struct {
name string
input []int
expected int
}{{"TestShuffle_NonEmptySlice", []int{1, 2, 3}, 3}, {"TestShuffle_EmptySlice", []int{}, 0}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
collection.Shuffle(&c.input)
if len(c.input) != c.expected {
t.Fatalf("%s failed, expected: %v, actual: %v", c.name, c.expected, c.input)
}
})
}
}
func ShuffleByClone(slice S) S
对切片进行随机排序,返回排序后的切片
示例代码:
func ExampleShuffleByClone() {
var slice = []int{1, 2, 3}
result := collection.ShuffleByClone(slice)
fmt.Println(len(result))
}
查看 / 收起单元测试
func TestShuffleByClone(t *testing.T) {
var cases = []struct {
name string
input []int
expected int
}{{"TestShuffleByClone_NonEmptySlice", []int{1, 2, 3}, 3}, {"TestShuffleByClone_EmptySlice", []int{}, 0}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
result := collection.ShuffleByClone(c.input)
if len(result) != c.expected {
t.Fatalf("%s failed, expected: %v, actual: %v", c.name, c.expected, result)
}
})
}
}
ComparisonHandler STRUCT
type ComparisonHandler[V any] func(source V) bool
OrderedValueGetter STRUCT
type OrderedValueGetter[V any, N generic.Ordered] func(v V) N