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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkStructColumn(b *testing.B) {
|
|
|
|
y := getX()
|
|
|
|
fmt.Println(y)
|
|
|
|
b.ResetTimer()
|
|
|
|
//b.N = 2
|
|
|
|
for i := 0; i < 1; i++ {
|
|
|
|
StructColumn[int, *x](y, "Id")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStructColumn(t *testing.T) {
|
|
|
|
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) {
|
|
|
|
if gotR := StructColumn[uint64, x](tt.args.arr, tt.args.field); !reflect.DeepEqual(gotR, tt.wantR) {
|
|
|
|
t.Errorf("StructColumn() = %v, want %v", gotR, tt.wantR)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-09-15 14:35:32 +00:00
|
|
|
|
2022-09-26 13:25:41 +00:00
|
|
|
func TestToInterface(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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-09-27 13:52:15 +00:00
|
|
|
|
2022-10-13 03:09:52 +00:00
|
|
|
func TestRandNum(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
start int
|
|
|
|
end int
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "t1",
|
|
|
|
args: args{
|
|
|
|
start: 1,
|
|
|
|
end: 2,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
got := RandNum(tt.args.start, tt.args.end)
|
|
|
|
if got > tt.args.end || got < tt.args.start {
|
|
|
|
t.Errorf("RandNum() = %v, range error", got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-10-25 12:05:44 +00:00
|
|
|
|
2022-11-04 02:38:59 +00:00
|
|
|
func TestSimpleSort(t *testing.T) {
|
2022-10-27 02:02:01 +00:00
|
|
|
type xy struct {
|
|
|
|
x int
|
|
|
|
y int
|
|
|
|
}
|
|
|
|
|
2022-10-25 12:05:44 +00:00
|
|
|
type args struct {
|
2022-10-27 02:02:01 +00:00
|
|
|
arr []xy
|
|
|
|
fn func(i, j xy) bool
|
2022-10-25 12:05:44 +00:00
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
2022-10-27 02:02:01 +00:00
|
|
|
wantR []xy
|
2022-10-25 12:05:44 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "t1",
|
|
|
|
args: args{
|
2022-10-27 02:02:01 +00:00
|
|
|
arr: []xy{
|
|
|
|
{1, 2},
|
|
|
|
{3, 4},
|
|
|
|
{1, 3},
|
|
|
|
{2, 1},
|
|
|
|
{1, 6},
|
2022-10-25 12:05:44 +00:00
|
|
|
},
|
2022-10-27 02:02:01 +00:00
|
|
|
fn: func(i, j xy) bool {
|
|
|
|
if i.x < j.x {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if i.x == j.x && i.y > i.y {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantR: []xy{
|
|
|
|
{1, 2},
|
|
|
|
{1, 3},
|
|
|
|
{1, 6},
|
|
|
|
{2, 1},
|
|
|
|
{3, 4},
|
2022-10-25 12:05:44 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2022-11-04 02:38:59 +00:00
|
|
|
if SimpleSort(tt.args.arr, tt.args.fn); !reflect.DeepEqual(tt.args.arr, tt.wantR) {
|
|
|
|
t.Errorf("SimpleSort() = %v, want %v", tt.args.arr, tt.wantR)
|
2022-10-25 12:05:44 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-11-05 12:59:49 +00:00
|
|
|
|
|
|
|
func TestMin(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
a []int
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "t1",
|
|
|
|
args: args{a: []int{1, 2, 3}},
|
|
|
|
want: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
if got := Min(tt.args.a...); got != tt.want {
|
|
|
|
t.Errorf("Min() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMax(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
a []int
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "t1",
|
|
|
|
args: args{a: []int{1, 2, 3}},
|
|
|
|
want: 3,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
if got := Max(tt.args.a...); got != tt.want {
|
|
|
|
t.Errorf("Max() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-11-06 04:04:41 +00:00
|
|
|
|
2022-11-06 04:12:57 +00:00
|
|
|
func TestSum(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
a []int
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "t1",
|
|
|
|
args: args{a: RangeSlice(1, 10, 1)},
|
|
|
|
want: 55,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
if got := Sum(tt.args.a...); got != tt.want {
|
|
|
|
t.Errorf("Sum() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-11-17 06:08:26 +00:00
|
|
|
|
|
|
|
func TestNumberToString(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
n float64
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "t1",
|
|
|
|
args: args{n: 111},
|
|
|
|
want: "111",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "t2",
|
|
|
|
args: args{n: 111.222222},
|
|
|
|
want: "111.222222",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
if got := NumberToString(tt.args.n); got != tt.want {
|
|
|
|
t.Errorf("NumberToString() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|