Skip to content

Commit

Permalink
✨ feat: mathutil - add new util func InRange(), OutRange(), InUintRan…
Browse files Browse the repository at this point in the history
…ge()

- update for CompInt64(), CompFloat()
  • Loading branch information
inhere committed Jun 1, 2023
1 parent 94d4ac5 commit 90843e9
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 28 deletions.
4 changes: 3 additions & 1 deletion basefn/extfunc.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package basefn

import "fmt"
import (
"fmt"
)

// DataSize format bytes number friendly. eg: 1024 => 1KB, 1024*1024 => 1MB
//
Expand Down
66 changes: 41 additions & 25 deletions mathutil/check.go
Original file line number Diff line number Diff line change
@@ -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:
//
Expand Down Expand Up @@ -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
}
25 changes: 25 additions & 0 deletions mathutil/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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))
}
8 changes: 8 additions & 0 deletions testutil/assert/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@ 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.
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
Expand Down
4 changes: 2 additions & 2 deletions testutil/assert/asserts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down

0 comments on commit 90843e9

Please sign in to comment.