Files
open-im-server/pkg/utils/utils_v2_test.go
T

85 lines
1.4 KiB
Go
Raw Normal View History

2023-02-02 17:54:02 +08:00
package utils
2023-02-02 15:42:14 +08:00
import (
"fmt"
"testing"
)
func TestDistinct(t *testing.T) {
arr := []int{1, 1, 1, 4, 4, 5, 2, 3, 3, 3, 6}
fmt.Println(Distinct(arr))
}
func TestDeleteAt(t *testing.T) {
arr := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
2023-02-02 17:54:02 +08:00
fmt.Println(Delete(arr, 0, 1, -1, -2))
fmt.Println(Delete(arr))
fmt.Println(Delete(arr, 1))
2023-02-02 15:42:14 +08:00
}
func TestSliceToMap(t *testing.T) {
type Item struct {
ID string
Name string
}
list := []Item{
{ID: "111", Name: "111"},
{ID: "222", Name: "222"},
{ID: "333", Name: "333"},
}
m := SliceToMap(list, func(t Item) string {
return t.ID
})
fmt.Printf("%+v\n", m)
}
func TestIndexOf(t *testing.T) {
arr := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
2023-02-27 14:44:14 +08:00
fmt.Println(IndexOf(3, arr...))
2023-02-02 15:42:14 +08:00
}
2023-02-02 16:08:08 +08:00
func TestSort(t *testing.T) {
arr := []int{1, 1, 1, 4, 4, 5, 2, 3, 3, 3, 6}
fmt.Println(Sort(arr, false))
2023-02-02 17:49:19 +08:00
}
func TestBothExist(t *testing.T) {
arr1 := []int{1, 1, 1, 4, 4, 5, 2, 3, 3, 3, 6}
arr2 := []int{6, 1, 3}
arr3 := []int{5, 1, 3, 6}
fmt.Println(BothExist(arr1, arr2, arr3))
}
func TestCompleteAny(t *testing.T) {
type Item struct {
ID int
Value string
}
ids := []int{1, 2, 3, 4, 5, 6, 7, 8}
var list []Item
for _, id := range ids {
list = append(list, Item{
ID: id,
Value: fmt.Sprintf("%d", id*1000),
})
}
2023-02-02 17:54:02 +08:00
DeleteAt(&list, -1)
DeleteAt(&ids, -1)
2023-02-02 17:49:19 +08:00
2023-02-06 11:55:10 +08:00
ok := Complete(ids, Slice(list, func(t Item) int {
2023-02-02 17:49:19 +08:00
return t.ID
2023-02-06 11:55:10 +08:00
}))
2023-02-02 17:49:19 +08:00
fmt.Printf("%+v\n", ok)
2023-02-02 16:08:08 +08:00
}