wp-go/helper/func_test.go

100 lines
1.5 KiB
Go
Raw Normal View History

2022-09-08 02:54:37 +00:00
package helper
import (
"fmt"
"reflect"
"testing"
)
type x struct {
Id uint64
}
func c(x []*x) (r []uint64) {
for i := 0; i < len(x); i++ {
r = append(r, x[i].Id)
}
return
}
func getX() (r []*x) {
for i := 0; i < 10; i++ {
r = append(r, &x{
uint64(i),
})
}
return
}
func BenchmarkOr(b *testing.B) {
y := getX()
b.ResetTimer()
for i := 0; i < b.N; i++ {
c(y)
}
}
2023-01-21 11:31:23 +00:00
func BenchmarkStructColumnToSlice(b *testing.B) {
2022-09-08 02:54:37 +00:00
y := getX()
fmt.Println(y)
b.ResetTimer()
//b.N = 2
for i := 0; i < 1; i++ {
2023-01-21 11:31:23 +00:00
StructColumnToSlice[int, *x](y, "Id")
2022-09-08 02:54:37 +00:00
}
}
2023-01-21 11:31:23 +00:00
func TestStructColumnToSlice(t *testing.T) {
2022-09-08 02:54:37 +00:00
type args struct {
arr []x
field string
}
tests := []struct {
name string
args args
wantR []uint64
}{
{name: "test1", args: args{
arr: []x{
{Id: 1},
{2},
{4},
{6},
},
field: "Id",
}, wantR: []uint64{1, 2, 4, 6}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2023-01-21 11:31:23 +00:00
if gotR := StructColumnToSlice[uint64, x](tt.args.arr, tt.args.field); !reflect.DeepEqual(gotR, tt.wantR) {
t.Errorf("StructColumnToSlice() = %v, want %v", gotR, tt.wantR)
2022-09-08 02:54:37 +00:00
}
})
}
}
2022-09-15 14:35:32 +00:00
2023-01-21 11:31:23 +00:00
func TestToAny(t *testing.T) {
type args struct {
v int
}
tests := []struct {
name string
args args
want any
}{
{
name: "t1",
args: args{v: 1},
want: any(1),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ToAny(tt.args.v); !reflect.DeepEqual(got, tt.want) {
t.Errorf("ToAny() = %v, want %v", got, tt.want)
}
})
}
}