Skip to content

Commit

Permalink
Merge pull request #27 from ccpwcn/dev
Browse files Browse the repository at this point in the history
新增切片包含操作
  • Loading branch information
ccpwcn authored Aug 15, 2023
2 parents 7ad2722 + 1654db4 commit e80dc7b
Show file tree
Hide file tree
Showing 3 changed files with 211 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
- Empty 判断Set元素是否是空的
- [x] Slice切片相关
- SlicePagination 切片分页,返回一个分好页的二维数组,可指定页大小
- Contains 包含某个元素
- ContainsAll 包含指定元素
- ContainsAny 包含任意元素
- [x] 结构体相关操作
- JoinStructsField 将任意结构体数组中的指定字段的值使用英文逗号拼接成一个字符串,例如:用户列表中,所有用户ID拼成一个字符串
- PickStructsField 将任意结构体数组中的指定字段的值提取出来形成一个保持原类型的数组,例如:用户列表中,所有用户ID提取成一个用户ID数组
Expand Down
32 changes: 32 additions & 0 deletions slice.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package kgo

import "reflect"

type ChsSort interface {
}

// SlicePagination 对一个切片进行内存分页
func SlicePagination[T any](data []T, pageSize int) (paged [][]T) {
if data == nil {
Expand All @@ -26,3 +31,30 @@ func SlicePagination[T any](data []T, pageSize int) (paged [][]T) {
}
return paged
}

func Contains[T any](data []T, element T) bool {
for _, datum := range data {
if reflect.DeepEqual(datum, element) {
return true
}
}
return false
}

func ContainsAll[T any](data []T, elements []T) bool {
for _, element := range elements {
if !Contains(data, element) {
return false
}
}
return true
}

func ContainsAny[T any](data []T, elements []T) bool {
for _, element := range elements {
if Contains(data, element) {
return true
}
}
return false
}
176 changes: 176 additions & 0 deletions slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,179 @@ func TestSlicePagination(t *testing.T) {
})
}
}

func TestContainsString(t *testing.T) {
type args[T any] struct {
data []T
element T
}
type testCase[T any] struct {
name string
args args[T]
want bool
}
tests := []testCase[string]{
{name: "stringsContains", args: args[string]{data: []string{"abc", "def", "xyz"}, element: "abc"}, want: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Contains(tt.args.data, tt.args.element); got != tt.want {
t.Errorf("Contains() = %v, want %v", got, tt.want)
}
})
}
}

func TestContainsInteger(t *testing.T) {
type args[T any] struct {
data []T
element T
}
type testCase[T any] struct {
name string
args args[T]
want bool
}
tests := []testCase[int]{
{name: "integerContains", args: args[int]{data: []int{1, 2, 3}, element: 1}, want: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Contains(tt.args.data, tt.args.element); got != tt.want {
t.Errorf("Contains() = %v, want %v", got, tt.want)
}
})
}
}

func TestContainsFloat(t *testing.T) {
type args[T any] struct {
data []T
element T
}
type testCase[T any] struct {
name string
args args[T]
want bool
}
tests := []testCase[float64]{
{name: "floatContains", args: args[float64]{data: []float64{1.1, 2.2, 3.3}, element: 1.1}, want: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Contains(tt.args.data, tt.args.element); got != tt.want {
t.Errorf("Contains() = %v, want %v", got, tt.want)
}
})
}
}

func TestContainsAllStrings(t *testing.T) {
type args[T any] struct {
data []T
elements []T
}
type testCase[T any] struct {
name string
args args[T]
want bool
}
tests := []testCase[string]{
{name: "stringsContainsAll", args: args[string]{data: []string{"abc", "def", "xyz"}, elements: []string{"abc", "def"}}, want: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ContainsAll(tt.args.data, tt.args.elements); got != tt.want {
t.Errorf("ContainsAll() = %v, want %v", got, tt.want)
}
})
}
}

func TestContainsAllIntegers(t *testing.T) {
type args[T any] struct {
data []T
elements []T
}
type testCase[T any] struct {
name string
args args[T]
want bool
}
tests := []testCase[int]{
{name: "integerContainsAll", args: args[int]{data: []int{1, 2, 3}, elements: []int{1, 2}}, want: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ContainsAll(tt.args.data, tt.args.elements); got != tt.want {
t.Errorf("ContainsAll() = %v, want %v", got, tt.want)
}
})
}
}

func TestContainsAllFloats(t *testing.T) {
type args[T any] struct {
data []T
elements []T
}
type testCase[T any] struct {
name string
args args[T]
want bool
}
tests := []testCase[float64]{
{name: "floatContainsAll", args: args[float64]{data: []float64{1.1, 2.2, 3.3}, elements: []float64{1.1, 2.2}}, want: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ContainsAll(tt.args.data, tt.args.elements); got != tt.want {
t.Errorf("ContainsAll() = %v, want %v", got, tt.want)
}
})
}
}

func TestContainsAnyString(t *testing.T) {
type args[T any] struct {
data []T
elements []T
}
type testCase[T any] struct {
name string
args args[T]
want bool
}
tests := []testCase[string]{
{name: "stringsContainsAny", args: args[string]{data: []string{"abc", "def", "xyz"}, elements: []string{"abc", "opq"}}, want: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ContainsAny(tt.args.data, tt.args.elements); got != tt.want {
t.Errorf("ContainsAny() = %v, want %v", got, tt.want)
}
})
}
}

func TestContainsAnyInteger(t *testing.T) {
type args[T any] struct {
data []T
elements []T
}
type testCase[T any] struct {
name string
args args[T]
want bool
}
tests := []testCase[int]{
{name: "integerContainsAny", args: args[int]{data: []int{1, 2, 3, 4}, elements: []int{10, 1, 2, 300}}, want: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ContainsAny(tt.args.data, tt.args.elements); got != tt.want {
t.Errorf("ContainsAny() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit e80dc7b

Please sign in to comment.