diff options
author | Artem Sidorenko <artem@posteo.de> | 2017-07-03 00:20:49 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2017-07-03 00:20:48 +0200 |
commit | 34c566773a1364077e1397daece85b22948dc721 (patch) | |
tree | f38c7405225de9b4f861450ec3c69003746d94d1 /tpl/math/math_test.go | |
parent | 41805dca9e40e9b0952e04d06074e6fc91140495 (diff) |
tpl/math: Add log function
It might be very useful for building tag clouds.
Diffstat (limited to 'tpl/math/math_test.go')
-rw-r--r-- | tpl/math/math_test.go | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/tpl/math/math_test.go b/tpl/math/math_test.go index 40bed5539..49ac053fd 100644 --- a/tpl/math/math_test.go +++ b/tpl/math/math_test.go @@ -15,6 +15,7 @@ package math import ( "fmt" + "math" "testing" "github.com/stretchr/testify/assert" @@ -142,6 +143,42 @@ func TestDoArithmetic(t *testing.T) { } } +func TestLog(t *testing.T) { + t.Parallel() + + ns := New() + + for i, test := range []struct { + a interface{} + expect interface{} + }{ + {1, float64(0)}, + {3, float64(1.0986)}, + {0, float64(math.Inf(-1))}, + {1.0, float64(0)}, + {3.1, float64(1.1314)}, + {"abc", false}, + } { + errMsg := fmt.Sprintf("[%d] %v", i, test) + + result, err := ns.Log(test.a) + + if b, ok := test.expect.(bool); ok && !b { + require.Error(t, err, errMsg) + continue + } + + // we compare only 4 digits behind point if its a real float + // otherwise we usually get different float values on the last positions + if result != math.Inf(-1) { + result = float64(int(result*10000)) / 10000 + } + + require.NoError(t, err, errMsg) + assert.Equal(t, test.expect, result, errMsg) + } +} + func TestMod(t *testing.T) { t.Parallel() |