From 90843e921f06343e0050feb982f4affce8a87dca Mon Sep 17 00:00:00 2001 From: Inhere Date: Thu, 1 Jun 2023 15:20:24 +0800 Subject: [PATCH] :sparkles: feat: mathutil - add new util func InRange(), OutRange(), InUintRange() - update for CompInt64(), CompFloat() --- basefn/extfunc.go | 4 ++- mathutil/check.go | 66 ++++++++++++++++++++++------------- mathutil/check_test.go | 25 +++++++++++++ testutil/assert/assertions.go | 8 +++++ testutil/assert/asserts.go | 4 +-- 5 files changed, 79 insertions(+), 28 deletions(-) diff --git a/basefn/extfunc.go b/basefn/extfunc.go index 31efac180..15be452cf 100644 --- a/basefn/extfunc.go +++ b/basefn/extfunc.go @@ -1,6 +1,8 @@ package basefn -import "fmt" +import ( + "fmt" +) // DataSize format bytes number friendly. eg: 1024 => 1KB, 1024*1024 => 1MB // diff --git a/mathutil/check.go b/mathutil/check.go index 45c499275..2f2aa91a1 100644 --- a/mathutil/check.go +++ b/mathutil/check.go @@ -1,6 +1,8 @@ package mathutil -// Compare intX,floatX value by given op. returns `srcVal op(=,!=,<,<=,>,>=) dstVal` +import "github.com/gookit/goutil/comdef" + +// Compare any intX,floatX value by given op. returns `srcVal op(=,!=,<,<=,>,>=) dstVal` // // Usage: // @@ -42,40 +44,54 @@ func Compare(srcVal, dstVal any, op string) (ok bool) { return CompInt64(srcInt, dstInt, op) } -// CompInt64 compare int64, returns the srcI64 op dstI64 -func CompInt64(srcI64, dstI64 int64, op string) (ok bool) { +// CompInt compare int,uint value. returns `srcVal op(=,!=,<,<=,>,>=) dstVal` +func CompInt[T comdef.Xint](srcVal, dstVal T, op string) (ok bool) { + return CompIntOrFloat(srcVal, dstVal, op) +} + +// CompInt64 compare int64 value. returns `srcVal op(=,!=,<,<=,>,>=) dstVal` +func CompInt64(srcVal, dstVal int64, op string) bool { + return CompIntOrFloat(srcVal, dstVal, op) +} + +// CompFloat compare float64,float32 value. returns `srcVal op(=,!=,<,<=,>,>=) dstVal` +func CompFloat[T comdef.Float](srcVal, dstVal T, op string) (ok bool) { + return CompIntOrFloat(srcVal, dstVal, op) +} + +// CompIntOrFloat compare intX,uintX,floatX value. returns `srcVal op(=,!=,<,<=,>,>=) dstVal` +func CompIntOrFloat[T comdef.XintOrFloat](srcVal, dstVal T, op string) (ok bool) { switch op { case "<", "lt": - ok = srcI64 < dstI64 + ok = srcVal < dstVal case "<=", "lte": - ok = srcI64 <= dstI64 + ok = srcVal <= dstVal case ">", "gt": - ok = srcI64 > dstI64 + ok = srcVal > dstVal case ">=", "gte": - ok = srcI64 >= dstI64 + ok = srcVal >= dstVal case "=", "eq": - ok = srcI64 == dstI64 + ok = srcVal == dstVal case "!=", "ne", "neq": - ok = srcI64 != dstI64 + ok = srcVal != dstVal } return } -// CompFloat compare float64 -func CompFloat(srcF64, dstF64 float64, op string) (ok bool) { - switch op { - case "<", "lt": - ok = srcF64 < dstF64 - case "<=", "lte": - ok = srcF64 <= dstF64 - case ">", "gt": - ok = srcF64 > dstF64 - case ">=", "gte": - ok = srcF64 >= dstF64 - case "=", "eq": - ok = srcF64 == dstF64 - case "!=", "ne", "neq": - ok = srcF64 != dstF64 +// InRange check if val in int/float range [min, max] +func InRange[T comdef.IntOrFloat](val, min, max T) bool { + return val >= min && val <= max +} + +// OutRange check if val not in int/float range [min, max] +func OutRange[T comdef.IntOrFloat](val, min, max T) bool { + return val < min || val > max +} + +// InUintRange check if val in unit range [min, max] +func InUintRange[T comdef.Uint](val, min, max T) bool { + if max == 0 { + return val >= min } - return + return val >= min && val <= max } diff --git a/mathutil/check_test.go b/mathutil/check_test.go index ff5a94b66..b4de3b40f 100644 --- a/mathutil/check_test.go +++ b/mathutil/check_test.go @@ -21,6 +21,7 @@ func TestCompare(t *testing.T) { {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}, @@ -40,4 +41,28 @@ func TestCompare(t *testing.T) { 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)) + + assert.True(t, mathutil.CompInt64(2, 3, comdef.OpLt)) +} + +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)) } diff --git a/testutil/assert/assertions.go b/testutil/assert/assertions.go index 5a2894651..cf54d456f 100644 --- a/testutil/assert/assertions.go +++ b/testutil/assert/assertions.go @@ -4,6 +4,8 @@ package assert type Assertions struct { t TestingT ok bool // last assert result + // prefix message for each assert TODO + Msg string } // New makes a new Assertions object for the specified TestingT. @@ -11,6 +13,12 @@ func New(t TestingT) *Assertions { return &Assertions{t: t} } +// WithMsg set with prefix message. +func (as *Assertions) WithMsg(msg string) *Assertions { + as.Msg = msg + return as +} + // IsOk for last check func (as *Assertions) IsOk() bool { return as.ok diff --git a/testutil/assert/asserts.go b/testutil/assert/asserts.go index 9419723e4..ae991980d 100644 --- a/testutil/assert/asserts.go +++ b/testutil/assert/asserts.go @@ -507,7 +507,7 @@ func NotEq(t TestingT, want, give any, fmtAndArgs ...any) bool { // Lt asserts that the give(intX) should not be less than max func Lt(t TestingT, give, max int, fmtAndArgs ...any) bool { gInt, err := mathutil.ToInt(give) - if err == nil && gInt <= max { + if err == nil && gInt < max { return true } @@ -524,7 +524,7 @@ func Lte(t TestingT, give, max int, fmtAndArgs ...any) bool { // Gt asserts that the give(intX) should not be greater than min func Gt(t TestingT, give, min int, fmtAndArgs ...any) bool { gInt, err := mathutil.ToInt(give) - if err == nil && gInt >= min { + if err == nil && gInt > min { return true }