-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgridutils_test.go
65 lines (57 loc) · 1.76 KB
/
gridutils_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package egriden
import (
"testing"
"github.com/greenthepear/imggg"
)
func TestGridUtilities(t *testing.T) {
lv := NewBaseLevel("test")
l1 := lv.CreateSimpleGridLayerOnTop("test", 20, 12, 6, Sparse, 0, 50)
if l1.Anchor.Y != 50 {
t.Errorf("Anchor is not 50, instead %v!", l1.Anchor.Y)
}
// Testing associated cells with the anchor
shouldbe := [...]int{-3, -2, -2, -1, -1, 0, 0, 1, 1, 2}
for i, s := range shouldbe {
x, y := screenXYtoGrid(*l1, 0, float64(i*10))
if y != s {
t.Errorf(`Wrong screen XY to grid conversion with yoffset %v!
is: %v %v
should be: %v %v`,
int(l1.Anchor.Y), x, y, x, s)
}
ax, ay := snapScreenXYtoCellAnchor(*l1, 0, float64(i*10))
if ay != float64(s*l1.cellDimensions.Height) {
t.Errorf(`Wrong anchor point calculation!
is: %v %v
should be: %v %v`,
ax, ay, ax, float64(s*l1.cellDimensions.Height))
}
}
l2 := lv.CreateGridLayerOnTop("test2", GridLayerParameters{
GridDimensions: Dimensions{5, 5},
CellDimensions: Dimensions{5, 5},
PaddingVector: imggg.Pt(2.0, 1.0),
Anchor: imggg.Pt(-5.0, -5.0),
})
type gapTest struct {
screenpos imggg.Point[float64]
gridpos imggg.Point[int]
outsideGap bool
}
forTest := [...]gapTest{
{imggg.Pt(0.0, 0.0), imggg.Pt(0, 0), true},
{imggg.Pt(1.0, 0.0), imggg.Pt(0, 0), false},
{imggg.Pt(2.0, 0.0), imggg.Pt(1, 0), true},
{imggg.Pt(-1.0, -1.0), imggg.Pt(0, 0), true},
}
for _, e := range forTest {
c, b := l2.CellAtScreenPosWithPadding(e.screenpos.X, e.screenpos.Y)
cx, cy := c.XY()
if c != l2.CellAtScreenPos(e.screenpos.X, e.screenpos.Y) ||
cx != e.gridpos.X || cy != e.gridpos.Y || b != e.outsideGap {
t.Errorf(`Wrong cell for padding layer! (%v)
returned: %v, %v
target: %v, %v`, e.screenpos, c.Coordinate, b, e.gridpos, e.outsideGap)
}
}
}