diff options
author | Cameron Moore <moorereason@gmail.com> | 2017-09-23 20:07:55 -0500 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2017-09-24 10:06:14 +0200 |
commit | 19c5910485242838d6678c2aacd8501f7e646a53 (patch) | |
tree | 3430c91d257dae583dcd2556b68c8a8818ddbcf1 /tpl/math/math_test.go | |
parent | 80c7ea60a0e0f488563a6b7311f3d4c23457aac7 (diff) |
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
Diffstat (limited to 'tpl/math/math_test.go')
-rw-r--r-- | tpl/math/math_test.go | 99 |
1 files changed, 99 insertions, 0 deletions
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) + } +} |