tpl: Add math.Ceil, Floor, and Round

Ceil and Floor are frontends for the stdlib math functions. The Round
implementation is essentially the same thing except that the Go stdlib
doesn't include a Round implementation in a stable release yet.  I've
included the Round function slated for Go 1.10.

Fixes #3883
This commit is contained in:
Cameron Moore
2017-09-23 20:07:55 -05:00
committed by Bjørn Erik Pedersen
parent 80c7ea60a0
commit 19c5910485
3 changed files with 190 additions and 0 deletions

View File

@@ -143,6 +143,72 @@ func TestDoArithmetic(t *testing.T) {
}
}
func TestCeil(t *testing.T) {
t.Parallel()
ns := New()
for i, test := range []struct {
x interface{}
expect interface{}
}{
{0.1, 1.0},
{0.5, 1.0},
{1.1, 2.0},
{1.5, 2.0},
{-0.1, 0.0},
{-0.5, 0.0},
{-1.1, -1.0},
{-1.5, -1.0},
{"abc", false},
} {
errMsg := fmt.Sprintf("[%d] %v", i, test)
result, err := ns.Ceil(test.x)
if b, ok := test.expect.(bool); ok && !b {
require.Error(t, err, errMsg)
continue
}
require.NoError(t, err, errMsg)
assert.Equal(t, test.expect, result, errMsg)
}
}
func TestFloor(t *testing.T) {
t.Parallel()
ns := New()
for i, test := range []struct {
x interface{}
expect interface{}
}{
{0.1, 0.0},
{0.5, 0.0},
{1.1, 1.0},
{1.5, 1.0},
{-0.1, -1.0},
{-0.5, -1.0},
{-1.1, -2.0},
{-1.5, -2.0},
{"abc", false},
} {
errMsg := fmt.Sprintf("[%d] %v", i, test)
result, err := ns.Floor(test.x)
if b, ok := test.expect.(bool); ok && !b {
require.Error(t, err, errMsg)
continue
}
require.NoError(t, err, errMsg)
assert.Equal(t, test.expect, result, errMsg)
}
}
func TestLog(t *testing.T) {
t.Parallel()
@@ -255,3 +321,36 @@ func TestModBool(t *testing.T) {
assert.Equal(t, test.expect, result, errMsg)
}
}
func TestRound(t *testing.T) {
t.Parallel()
ns := New()
for i, test := range []struct {
x interface{}
expect interface{}
}{
{0.1, 0.0},
{0.5, 1.0},
{1.1, 1.0},
{1.5, 2.0},
{-0.1, -0.0},
{-0.5, -1.0},
{-1.1, -1.0},
{-1.5, -2.0},
{"abc", false},
} {
errMsg := fmt.Sprintf("[%d] %v", i, test)
result, err := ns.Round(test.x)
if b, ok := test.expect.(bool); ok && !b {
require.Error(t, err, errMsg)
continue
}
require.NoError(t, err, errMsg)
assert.Equal(t, test.expect, result, errMsg)
}
}