refactor: move packages to pkg directory

This commit is contained in:
2021-10-12 19:31:15 +02:00
parent de9dae9bd6
commit e436775c2c
7 changed files with 7 additions and 7 deletions

12
pkg/util/util.go Normal file
View File

@ -0,0 +1,12 @@
package util
// MapRange Linear mapping between two ranges of values
func MapRange(x, xmin, xmax, ymin, ymax float64) int {
Xrange := xmax - xmin
Yrange := ymax - ymin
XYratio := Xrange / Yrange
y := (x-xmin)/XYratio + ymin
return int(y)
}

32
pkg/util/util_test.go Normal file
View File

@ -0,0 +1,32 @@
package util
import "testing"
func TestRangeMapping(t *testing.T) {
cases := []struct {
x, xmin, xmax, ymin, ymax float64
expected int
}{
// Test positive
{-100, -100, 100, 0, 1000, 0},
{0, -100, 100, 0, 1000, 500},
{100, -100, 100, 0, 1000, 1000},
// Test negative
{0, 0, 100, 0, 1000, 0},
{50, 0, 100, 0, 1000, 500},
{100, 0, 100, 0, 1000, 1000},
// Reverse
{0, 100, 0, 0, 1000, 1000},
{50, 100, 0, 0, 1000, 500},
{100, 100, 0, 0, 1000, 0},
}
for _, c := range cases {
val := MapRange(c.x, c.xmin, c.xmax, c.ymin, c.ymax)
if val != c.expected {
t.Errorf("MapRange(%.0f, %.0f, %.0f, %.0f, %.0f): %d, wants %d", c.x, c.xmin, c.xmax, c.ymin, c.ymax, val, c.expected)
}
}
}