You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ntool/nmath/check_test.go

68 lines
1.7 KiB
Go

1 year ago
package nmath_test
import (
"git.noahlan.cn/noahlan/ntool/ndef"
"git.noahlan.cn/noahlan/ntool/nmath"
"git.noahlan.cn/noahlan/ntool/ntest/assert"
"testing"
)
func TestCompare(t *testing.T) {
tests := []struct {
x, y any
op string
}{
{2, 2, ndef.OpEq},
{2, 3, ndef.OpNeq},
{2, 3, ndef.OpLt},
{2, 3, ndef.OpLte},
{2, 2, ndef.OpLte},
{2, 1, ndef.OpGt},
{2, 2, ndef.OpGte},
{2, 1, ndef.OpGte},
{2, "1", ndef.OpGte},
{2.2, 2.2, ndef.OpEq},
{2.2, 3.1, ndef.OpNeq},
{2.3, 3.2, ndef.OpLt},
{2.3, 3.3, ndef.OpLte},
{2.3, 2.3, ndef.OpLte},
{2.3, 1.3, ndef.OpGt},
{2.3, 2.3, ndef.OpGte},
{2.3, 1.3, ndef.OpGte},
}
for _, test := range tests {
assert.True(t, nmath.Compare(test.x, test.y, test.op))
}
assert.False(t, nmath.Compare(2, 3, ndef.OpGt))
assert.False(t, nmath.Compare(nil, 3, ndef.OpGt))
assert.False(t, nmath.Compare(2, nil, ndef.OpGt))
assert.False(t, nmath.Compare("abc", 3, ndef.OpGt))
assert.False(t, nmath.Compare(2, "def", ndef.OpGt))
assert.True(t, nmath.CompInt64(2, 3, ndef.OpLt))
}
func TestInRange(t *testing.T) {
assert.True(t, nmath.InRange(1, 1, 2))
assert.True(t, nmath.InRange(1, 1, 1))
assert.False(t, nmath.InRange(1, 2, 1))
assert.False(t, nmath.InRange(1, 2, 2))
assert.True(t, nmath.InRange(1.1, 1.1, 2.2))
assert.True(t, nmath.InRange(1.1, 1.1, 1.1))
assert.False(t, nmath.InRange(1.1, 2.2, 1.1))
// test for OutRange()
assert.False(t, nmath.OutRange(1, 1, 2))
assert.False(t, nmath.OutRange(1, 1, 1))
assert.True(t, nmath.OutRange(1, 2, 10))
// test for InUintRange()
assert.True(t, nmath.InUintRange[uint](1, 1, 2))
assert.True(t, nmath.InUintRange[uint](1, 1, 1))
assert.True(t, nmath.InUintRange[uint](1, 1, 0))
assert.False(t, nmath.InUintRange[uint](1, 2, 1))
}