Skip to content

Commit

Permalink
feat: collection 包新增 Equel 命名前缀的用于比较切片和 map 元素是否相同的函数,新增 Loop 命名前缀的用于…
Browse files Browse the repository at this point in the history
…遍历切片和 map 元素的函数
  • Loading branch information
kercylan98 committed Jan 19, 2024
1 parent c4605cc commit 756f823
Show file tree
Hide file tree
Showing 9 changed files with 1,421 additions and 6 deletions.
56 changes: 56 additions & 0 deletions utils/collection/contains.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,61 @@
package collection

// EqualSlice 检查两个切片是否相等,当 handler 返回 true 时,表示 slice1 中的某个元素和 slice2 中的某个元素相匹配
// - 当两个切片的容量不同时,不会影响最终的比较结果
func EqualSlice[S ~[]V, V any](slice1 S, slice2 S, handler ComparisonHandler[V]) bool {
if len(slice1) != len(slice2) {
return false
}
for i, v1 := range slice1 {
if !handler(v1, slice2[i]) {
return false
}
}
return true
}

// EqualComparableSlice 检查两个切片的值是否相同
// - 当两个切片的容量不同时,不会影响最终的比较结果
func EqualComparableSlice[S ~[]V, V comparable](slice1 S, slice2 S) bool {
if len(slice1) != len(slice2) {
return false
}
for i, v1 := range slice1 {
if v1 != slice2[i] {
return false
}
}
return true
}

// EqualMap 检查两个 map 是否相等,当 handler 返回 true 时,表示 map1 中的某个元素和 map2 中的某个元素相匹配
// - 当两个 map 的容量不同时,不会影响最终的比较结果
func EqualMap[M ~map[K]V, K comparable, V any](map1 M, map2 M, handler ComparisonHandler[V]) bool {
if len(map1) != len(map2) {
return false
}
for k, v1 := range map1 {
if !handler(v1, map2[k]) {
return false
}
}
return true
}

// EqualComparableMap 检查两个 map 的值是否相同
// - 当两个 map 的容量不同时,不会影响最终的比较结果
func EqualComparableMap[M ~map[K]V, K comparable, V comparable](map1 M, map2 M) bool {
if len(map1) != len(map2) {
return false
}
for k, v1 := range map1 {
if v1 != map2[k] {
return false
}
}
return true
}

// InSlice 检查 v 是否被包含在 slice 中,当 handler 返回 true 时,表示 v 和 slice 中的某个元素相匹配
func InSlice[S ~[]V, V any](slice S, v V, handler ComparisonHandler[V]) bool {
if len(slice) == 0 {
Expand Down
52 changes: 52 additions & 0 deletions utils/collection/contains_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,58 @@ import (
"github.com/kercylan98/minotaur/utils/collection"
)

func ExampleEqualSlice() {
s1 := []int{1, 2, 3}
s2 := []int{1}
s3 := []int{1, 2, 3}
fmt.Println(collection.EqualSlice(s1, s2, func(source, target int) bool {
return source == target
}))
fmt.Println(collection.EqualSlice(s1, s3, func(source, target int) bool {
return source == target
}))
// Output:
// false
// true
}

func ExampleEqualComparableSlice() {
s1 := []int{1, 2, 3}
s2 := []int{1}
s3 := []int{1, 2, 3}
fmt.Println(collection.EqualComparableSlice(s1, s2))
fmt.Println(collection.EqualComparableSlice(s1, s3))
// Output:
// false
// true
}

func ExampleEqualMap() {
m1 := map[string]int{"a": 1, "b": 2}
m2 := map[string]int{"a": 1}
m3 := map[string]int{"a": 1, "b": 2}
fmt.Println(collection.EqualMap(m1, m2, func(source, target int) bool {
return source == target
}))
fmt.Println(collection.EqualMap(m1, m3, func(source, target int) bool {
return source == target
}))
// Output:
// false
// true
}

func ExampleEqualComparableMap() {
m1 := map[string]int{"a": 1, "b": 2}
m2 := map[string]int{"a": 1}
m3 := map[string]int{"a": 1, "b": 2}
fmt.Println(collection.EqualComparableMap(m1, m2))
fmt.Println(collection.EqualComparableMap(m1, m3))
// Output:
// false
// true
}

func ExampleInSlice() {
result := collection.InSlice([]int{1, 2, 3}, 2, func(source, target int) bool {
return source == target
Expand Down
100 changes: 100 additions & 0 deletions utils/collection/contains_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,106 @@ var intComparisonHandler = func(source, target int) bool {
return source == target
}

func TestEqualSlice(t *testing.T) {
var cases = []struct {
name string
input []int
inputV []int
expected bool
}{
{"TestEqualSlice_NonEmptySliceEqual", []int{1, 2, 3}, []int{1, 2, 3}, true},
{"TestEqualSlice_NonEmptySliceNotEqual", []int{1, 2, 3}, []int{1, 2}, false},
{"TestEqualSlice_EmptySlice", []int{}, []int{}, true},
{"TestEqualSlice_NilSlice", nil, nil, true},
}

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
var actual = collection.EqualSlice(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, "not as expected")
}
})
}
}

func TestEqualComparableSlice(t *testing.T) {
var cases = []struct {
name string
input []int
inputV []int
expected bool
}{
{"TestEqualComparableSlice_NonEmptySliceEqual", []int{1, 2, 3}, []int{1, 2, 3}, true},
{"TestEqualComparableSlice_NonEmptySliceNotEqual", []int{1, 2, 3}, []int{1, 2}, false},
{"TestEqualComparableSlice_EmptySlice", []int{}, []int{}, true},
{"TestEqualComparableSlice_NilSlice", nil, nil, true},
}

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
var actual = collection.EqualComparableSlice(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 TestEqualMap(t *testing.T) {
var cases = []struct {
name string
input map[int]int
inputV map[int]int
expected bool
}{
{"TestEqualMap_NonEmptyMapEqual", map[int]int{1: 1, 2: 2}, map[int]int{1: 1, 2: 2}, true},
{"TestEqualMap_NonEmptyMapNotEqual", map[int]int{1: 1, 2: 2}, map[int]int{1: 1}, false},
{"TestEqualMap_EmptyMap", map[int]int{}, map[int]int{}, true},
{"TestEqualMap_NilMap", nil, nil, true},
}

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
var actual = collection.EqualMap(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, "not as expected")
}
})
}
}

func TestEqualComparableMap(t *testing.T) {
var cases = []struct {
name string
input map[int]int
inputV map[int]int
expected bool
}{
{"TestEqualComparableMap_NonEmptyMapEqual", map[int]int{1: 1, 2: 2}, map[int]int{1: 1, 2: 2}, true},
{"TestEqualComparableMap_NonEmptyMapNotEqual", map[int]int{1: 1, 2: 2}, map[int]int{1: 1}, false},
{"TestEqualComparableMap_EmptyMap", map[int]int{}, map[int]int{}, true},
{"TestEqualComparableMap_NilMap", nil, nil, true},
}

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
var actual = collection.EqualComparableMap(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 TestInSlice(t *testing.T) {
var cases = []struct {
name string
Expand Down
Loading

0 comments on commit 756f823

Please sign in to comment.