-
Notifications
You must be signed in to change notification settings - Fork 191
/
check_test.go
86 lines (72 loc) · 2.43 KB
/
check_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
77
78
79
80
81
82
83
84
85
86
package mathutil_test
import (
"testing"
"github.com/gookit/goutil/comdef"
"github.com/gookit/goutil/mathutil"
"github.com/gookit/goutil/testutil/assert"
)
func TestIsNumeric(t *testing.T) {
assert.True(t, mathutil.IsNumeric('3'))
assert.False(t, mathutil.IsNumeric('a'))
}
func TestCompare(t *testing.T) {
tests := []struct {
x, y any
op string
}{
{2, 2, comdef.OpEq},
{2, 3, comdef.OpNeq},
{2, 3, comdef.OpLt},
{2, 3, comdef.OpLte},
{2, 2, comdef.OpLte},
{2, 1, comdef.OpGt},
{2, 2, comdef.OpGte},
{2, 1, comdef.OpGte},
{2, "1", comdef.OpGte},
{2.2, 2.2, comdef.OpEq},
{2.2, 3.1, comdef.OpNeq},
{2.3, 3.2, comdef.OpLt},
{2.3, 3.3, comdef.OpLte},
{2.3, 2.3, comdef.OpLte},
{2.3, 1.3, comdef.OpGt},
{2.3, 2.3, comdef.OpGte},
{2.3, 1.3, comdef.OpGte},
}
for _, test := range tests {
assert.True(t, mathutil.Compare(test.x, test.y, test.op))
}
assert.False(t, mathutil.Compare(2, 3, comdef.OpGt))
assert.False(t, mathutil.Compare(nil, 3, comdef.OpGt))
assert.False(t, mathutil.Compare(2, nil, comdef.OpGt))
assert.False(t, mathutil.Compare("abc", 3, comdef.OpGt))
assert.False(t, mathutil.Compare(2, "def", comdef.OpGt))
// float64
assert.False(t, mathutil.Compare(2.4, "def", comdef.OpGt))
// float32
assert.True(t, mathutil.Compare(float32(2.3), float32(2.1), comdef.OpGt))
assert.False(t, mathutil.Compare(float32(2.3), float32(2.1), "<"))
assert.False(t, mathutil.Compare(float32(2.3), "invalid", "<"))
assert.True(t, mathutil.CompInt(2, 3, comdef.OpLt))
// int64
assert.True(t, mathutil.CompInt64(int64(2), 3, comdef.OpLt))
assert.True(t, mathutil.CompInt64(int64(22), 3, comdef.OpGt))
assert.False(t, mathutil.CompInt64(int64(2), 3, comdef.OpGt))
}
func TestInRange(t *testing.T) {
assert.True(t, mathutil.InRange(1, 1, 2))
assert.True(t, mathutil.InRange(1, 1, 1))
assert.False(t, mathutil.InRange(1, 2, 1))
assert.False(t, mathutil.InRange(1, 2, 2))
assert.True(t, mathutil.InRange(1.1, 1.1, 2.2))
assert.True(t, mathutil.InRange(1.1, 1.1, 1.1))
assert.False(t, mathutil.InRange(1.1, 2.2, 1.1))
// test for OutRange()
assert.False(t, mathutil.OutRange(1, 1, 2))
assert.False(t, mathutil.OutRange(1, 1, 1))
assert.True(t, mathutil.OutRange(1, 2, 10))
// test for InUintRange()
assert.True(t, mathutil.InUintRange[uint](1, 1, 2))
assert.True(t, mathutil.InUintRange[uint](1, 1, 1))
assert.True(t, mathutil.InUintRange[uint](1, 1, 0))
assert.False(t, mathutil.InUintRange[uint](1, 2, 1))
}