Skip to content

Commit

Permalink
eval
Browse files Browse the repository at this point in the history
  • Loading branch information
wxiaoguang committed Apr 5, 2023
1 parent 376396a commit 094e651
Show file tree
Hide file tree
Showing 15 changed files with 502 additions and 158 deletions.
49 changes: 0 additions & 49 deletions modules/base/tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,55 +141,6 @@ func FileSize(s int64) string {
return humanize.IBytes(uint64(s))
}

// Subtract deals with subtraction of all types of number.
func Subtract(left, right interface{}) interface{} {
var rleft, rright int64
var fleft, fright float64
isInt := true
switch v := left.(type) {
case int:
rleft = int64(v)
case int8:
rleft = int64(v)
case int16:
rleft = int64(v)
case int32:
rleft = int64(v)
case int64:
rleft = v
case float32:
fleft = float64(v)
isInt = false
case float64:
fleft = v
isInt = false
}

switch v := right.(type) {
case int:
rright = int64(v)
case int8:
rright = int64(v)
case int16:
rright = int64(v)
case int32:
rright = int64(v)
case int64:
rright = v
case float32:
fright = float64(v)
isInt = false
case float64:
fright = v
isInt = false
}

if isInt {
return rleft - rright
}
return fleft + float64(rleft) - (fright + float64(rright))
}

// EllipsisString returns a truncated short string,
// it appends '...' in the end of the length of string is too large.
func EllipsisString(str string, length int) string {
Expand Down
39 changes: 0 additions & 39 deletions modules/base/tool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,45 +114,6 @@ func TestFileSize(t *testing.T) {
assert.Equal(t, "2.0 EiB", FileSize(size))
}

func TestSubtract(t *testing.T) {
toFloat64 := func(n interface{}) float64 {
switch v := n.(type) {
case int:
return float64(v)
case int8:
return float64(v)
case int16:
return float64(v)
case int32:
return float64(v)
case int64:
return float64(v)
case float32:
return float64(v)
case float64:
return v
default:
return 0.0
}
}
values := []interface{}{
int(-3),
int8(14),
int16(81),
int32(-156),
int64(1528),
float32(3.5),
float64(-15.348),
}
for _, left := range values {
for _, right := range values {
expected := toFloat64(left) - toFloat64(right)
sub := Subtract(left, right)
assert.InDelta(t, expected, sub, 1e-3)
}
}
}

func TestEllipsisString(t *testing.T) {
assert.Equal(t, "...", EllipsisString("foobar", 0))
assert.Equal(t, "...", EllipsisString("foobar", 1))
Expand Down
Loading

0 comments on commit 094e651

Please sign in to comment.