2017-05-12 20:12:23 -04:00
|
|
|
package util
|
2016-07-06 21:54:00 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2018-04-05 03:42:38 -04:00
|
|
|
"github.com/blend/go-sdk/assert"
|
2016-07-06 21:54:00 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMinAndMax(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
values := []float64{1.0, 2.0, 3.0, 4.0}
|
2016-07-29 21:24:25 -04:00
|
|
|
min, max := Math.MinAndMax(values...)
|
2016-07-06 21:54:00 -04:00
|
|
|
assert.Equal(1.0, min)
|
|
|
|
assert.Equal(4.0, max)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMinAndMaxReversed(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
values := []float64{4.0, 2.0, 3.0, 1.0}
|
2016-07-29 21:24:25 -04:00
|
|
|
min, max := Math.MinAndMax(values...)
|
2016-07-06 21:54:00 -04:00
|
|
|
assert.Equal(1.0, min)
|
|
|
|
assert.Equal(4.0, max)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMinAndMaxEmpty(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
values := []float64{}
|
2016-07-29 21:24:25 -04:00
|
|
|
min, max := Math.MinAndMax(values...)
|
2016-07-06 21:54:00 -04:00
|
|
|
assert.Equal(0.0, min)
|
|
|
|
assert.Equal(0.0, max)
|
|
|
|
}
|
|
|
|
|
2016-07-11 21:48:51 -04:00
|
|
|
func TestGetRoundToForDelta(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2016-07-29 21:24:25 -04:00
|
|
|
assert.Equal(100.0, Math.GetRoundToForDelta(1001.00))
|
|
|
|
assert.Equal(10.0, Math.GetRoundToForDelta(101.00))
|
|
|
|
assert.Equal(1.0, Math.GetRoundToForDelta(11.00))
|
2016-07-11 21:48:51 -04:00
|
|
|
}
|
2016-07-14 15:30:57 -04:00
|
|
|
|
2016-07-28 19:36:30 -04:00
|
|
|
func TestRoundUp(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
2016-07-29 21:24:25 -04:00
|
|
|
assert.Equal(0.5, Math.RoundUp(0.49, 0.1))
|
|
|
|
assert.Equal(1.0, Math.RoundUp(0.51, 1.0))
|
|
|
|
assert.Equal(0.4999, Math.RoundUp(0.49988, 0.0001))
|
2018-04-15 15:35:39 -04:00
|
|
|
assert.Equal(0.123, Math.RoundUp(0.123, 0))
|
2016-07-28 19:36:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRoundDown(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
2016-07-29 21:24:25 -04:00
|
|
|
assert.Equal(0.5, Math.RoundDown(0.51, 0.1))
|
|
|
|
assert.Equal(1.0, Math.RoundDown(1.01, 1.0))
|
|
|
|
assert.Equal(0.5001, Math.RoundDown(0.50011, 0.0001))
|
2018-04-15 15:35:39 -04:00
|
|
|
assert.Equal(0.123, Math.RoundDown(0.123, 0))
|
2016-07-14 15:30:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPercentDifference(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2016-07-29 21:24:25 -04:00
|
|
|
assert.Equal(0.5, Math.PercentDifference(1.0, 1.5))
|
|
|
|
assert.Equal(-0.5, Math.PercentDifference(2.0, 1.0))
|
2016-07-14 15:30:57 -04:00
|
|
|
}
|
2016-07-28 16:22:18 -04:00
|
|
|
|
2016-07-28 19:36:30 -04:00
|
|
|
func TestNormalize(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
values := []float64{10, 9, 8, 7, 6}
|
2016-07-29 21:24:25 -04:00
|
|
|
normalized := Math.Normalize(values...)
|
2018-09-05 11:45:19 -04:00
|
|
|
assert.Len(normalized, 5)
|
2016-07-28 19:36:30 -04:00
|
|
|
assert.Equal(0.25, normalized[0])
|
2016-07-28 21:56:45 -04:00
|
|
|
assert.Equal(0.1499, normalized[4])
|
2016-07-28 19:36:30 -04:00
|
|
|
}
|
|
|
|
|
2016-07-28 16:22:18 -04:00
|
|
|
var (
|
|
|
|
_degreesToRadians = map[float64]float64{
|
|
|
|
0: 0, // !_2pi b/c no irrational nums in floats.
|
|
|
|
45: _pi4,
|
|
|
|
90: _pi2,
|
|
|
|
135: _3pi4,
|
|
|
|
180: _pi,
|
|
|
|
225: _5pi4,
|
|
|
|
270: _3pi2,
|
|
|
|
315: _7pi4,
|
|
|
|
}
|
|
|
|
|
|
|
|
_compassToRadians = map[float64]float64{
|
|
|
|
0: _pi2,
|
|
|
|
45: _pi4,
|
|
|
|
90: 0, // !_2pi b/c no irrational nums in floats.
|
|
|
|
135: _7pi4,
|
|
|
|
180: _3pi2,
|
|
|
|
225: _5pi4,
|
|
|
|
270: _pi,
|
|
|
|
315: _3pi4,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDegreesToRadians(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
for d, r := range _degreesToRadians {
|
2016-07-29 21:24:25 -04:00
|
|
|
assert.Equal(r, Math.DegreesToRadians(d))
|
2016-07-28 16:22:18 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPercentToRadians(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
for d, r := range _degreesToRadians {
|
2016-07-29 21:24:25 -04:00
|
|
|
assert.Equal(r, Math.PercentToRadians(d/360.0))
|
2016-07-28 16:22:18 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRadiansToDegrees(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
for d, r := range _degreesToRadians {
|
2016-07-29 21:24:25 -04:00
|
|
|
assert.Equal(d, Math.RadiansToDegrees(r))
|
2016-07-28 16:22:18 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRadianAdd(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2016-07-29 21:24:25 -04:00
|
|
|
assert.Equal(_pi, Math.RadianAdd(_pi2, _pi2))
|
|
|
|
assert.Equal(_3pi2, Math.RadianAdd(_pi2, _pi))
|
|
|
|
assert.Equal(_pi, Math.RadianAdd(_pi, _2pi))
|
|
|
|
assert.Equal(_pi, Math.RadianAdd(_pi, -_2pi))
|
2016-07-28 16:22:18 -04:00
|
|
|
}
|
2016-09-01 01:11:52 -04:00
|
|
|
|
2016-09-05 16:26:12 -04:00
|
|
|
func TestRotateCoordinate90(t *testing.T) {
|
2016-09-01 01:11:52 -04:00
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
cx, cy := 10, 10
|
2016-09-05 16:26:12 -04:00
|
|
|
x, y := 5, 10
|
2016-09-01 01:11:52 -04:00
|
|
|
|
2016-09-05 16:26:12 -04:00
|
|
|
rx, ry := Math.RotateCoordinate(cx, cy, x, y, Math.DegreesToRadians(90))
|
2016-09-01 01:11:52 -04:00
|
|
|
assert.Equal(10, rx)
|
2016-09-05 16:26:12 -04:00
|
|
|
assert.Equal(5, ry)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRotateCoordinate45(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
cx, cy := 10, 10
|
|
|
|
x, y := 5, 10
|
|
|
|
|
|
|
|
rx, ry := Math.RotateCoordinate(cx, cy, x, y, Math.DegreesToRadians(45))
|
|
|
|
assert.Equal(7, rx)
|
|
|
|
assert.Equal(7, ry)
|
2016-09-01 01:11:52 -04:00
|
|
|
}
|