-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils_test.go
76 lines (72 loc) · 2.2 KB
/
utils_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package gocast
import (
"reflect"
"testing"
)
func TestEmpty(t *testing.T) {
var tests = []struct {
src any
target bool
}{
{src: 120, target: false},
{src: int8(120), target: false},
{src: int16(120), target: false},
{src: int32(120), target: false},
{src: int64(120), target: false},
{src: uint(121), target: false},
{src: uint8(121), target: false},
{src: uint16(121), target: false},
{src: uint32(121), target: false},
{src: uint64(121), target: false},
{src: uintptr(121), target: false},
{src: float32(122.), target: false},
{src: float64(122.), target: false},
{src: "123", target: false},
{src: "", target: true},
{src: nil, target: true},
{src: func() *struct{ s string } { return nil }(), target: true},
{src: any(nil), target: true},
{src: any(func() *struct{ s string } { return nil }()), target: true},
{src: []byte("125."), target: false},
{src: []byte(""), target: true},
{src: true, target: false},
{src: false, target: true},
{src: []any{}, target: true},
{src: []any{1}, target: false},
{src: []int{1}, target: false},
{src: []int8{1}, target: false},
{src: []int16{1}, target: false},
{src: []int32{1}, target: false},
{src: []int64{1}, target: false},
{src: []uint{1}, target: false},
{src: []uint8{1}, target: false},
{src: []uint16{1}, target: false},
{src: []uint32{1}, target: false},
{src: []uint64{1}, target: false},
{src: []uintptr{1}, target: false},
{src: []float32{1}, target: false},
{src: []float64{1}, target: false},
{src: []bool{}, target: true},
{src: []string{}, target: true},
}
t.Run("IsEmpty", func(t *testing.T) {
for _, test := range tests {
if v := IsEmpty(test.src); v != test.target {
t.Errorf("target must be equal %v != %v", test.src, test.target)
}
}
})
t.Run("IsEmptyByReflection", func(t *testing.T) {
for _, test := range tests {
if v := IsEmptyByReflection(reflect.ValueOf(test.src)); v != test.target {
t.Errorf("target must be equal %v != %v", test.src, test.target)
}
}
})
}
func BenchmarkIsEmpty(b *testing.B) {
values := []any{120, int64(122), "123", "120.0", "120.", []byte("125."), true, false}
for n := 0; n < b.N; n++ {
_ = IsEmpty(values[n%len(values)])
}
}