From 19c5910485242838d6678c2aacd8501f7e646a53 Mon Sep 17 00:00:00 2001 From: Cameron Moore Date: Sat, 23 Sep 2017 20:07:55 -0500 Subject: 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 --- tpl/math/math_test.go | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) (limited to 'tpl/math/math_test.go') diff --git a/tpl/math/math_test.go b/tpl/math/math_test.go index 49ac053fd..4d14a58cc 100644 --- a/tpl/math/math_test.go +++ b/tpl/math/math_test.go @@ -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) + } +} -- cgit v1.2.3