forked from klokare/evo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathposition_test.go
134 lines (127 loc) · 3.39 KB
/
position_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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package evo
import "testing"
func TestPositionString(t *testing.T) {
p := Position{} // Given even an empty position
if p.String() == "" {
t.Error("String() should return non-empty string")
}
}
func TestPositionCompare(t *testing.T) {
var cases = []struct {
Desc string
A, B Position
Expected int8
}{
{
Desc: "equal positions",
A: Position{Layer: 1.0, X: 0.5, Y: -0.5, Z: 0.25},
B: Position{Layer: 1.0, X: 0.5, Y: -0.5, Z: 0.25},
Expected: 0,
},
{
Desc: "lower layer",
A: Position{Layer: 0.0, X: 0.5, Y: -0.5, Z: 0.25},
B: Position{Layer: 1.0, X: 0.5, Y: -0.5, Z: 0.25},
Expected: -1,
},
{
Desc: "higher layer",
A: Position{Layer: 1.0, X: 0.5, Y: -0.5, Z: 0.25},
B: Position{Layer: 0.0, X: 0.5, Y: -0.5, Z: 0.25},
Expected: 1,
},
{
Desc: "lower X",
A: Position{Layer: 1.0, X: 0.0, Y: -0.5, Z: 0.25},
B: Position{Layer: 1.0, X: 0.5, Y: -0.5, Z: 0.25},
Expected: -1,
},
{
Desc: "higher X",
A: Position{Layer: 1.0, X: 1.0, Y: -0.5, Z: 0.25},
B: Position{Layer: 1.0, X: 0.5, Y: -0.5, Z: 0.25},
Expected: 1,
},
{
Desc: "lower Y",
A: Position{Layer: 1.0, X: 0.5, Y: -0.8, Z: 0.25},
B: Position{Layer: 1.0, X: 0.5, Y: -0.5, Z: 0.25},
Expected: -1,
},
{
Desc: "higher Y",
A: Position{Layer: 1.0, X: 0.5, Y: -0.2, Z: 0.25},
B: Position{Layer: 1.0, X: 0.5, Y: -0.5, Z: 0.25},
Expected: 1,
},
{
Desc: "lower Z",
A: Position{Layer: 1.0, X: 0.5, Y: -0.5, Z: 0.15},
B: Position{Layer: 1.0, X: 0.5, Y: -0.5, Z: 0.25},
Expected: -1,
},
{
Desc: "higher Z",
A: Position{Layer: 1.0, X: 0.5, Y: -0.5, Z: 0.35},
B: Position{Layer: 1.0, X: 0.5, Y: -0.5, Z: 0.25},
Expected: 1,
},
}
for _, c := range cases {
t.Run(c.Desc, func(t *testing.T) {
actual := c.A.Compare(c.B)
if c.Expected != actual {
t.Errorf("incorrect comparison value: expected %d, actual %d", c.Expected, actual)
}
})
}
}
func TestMidpoint(t *testing.T) {
var cases = []struct {
Desc string
Positions []Position
Expected Position
}{
{
Desc: "one position",
Positions: []Position{
{Layer: 1.0, X: 1.0, Y: 1.0, Z: 1.0},
},
Expected: Position{Layer: 1.0, X: 1.0, Y: 1.0, Z: 1.0},
},
{
Desc: "two positions",
Positions: []Position{
{Layer: 1.0, X: 3.0, Y: 0.0, Z: 2.0},
{Layer: 3.0, X: 1.0, Y: 4.0, Z: 2.0},
},
Expected: Position{Layer: 2.0, X: 2.0, Y: 2.0, Z: 2.0},
},
{
Desc: "three positions",
Positions: []Position{
{Layer: 1.0, X: 1.0, Y: 8.0, Z: 3.0},
{Layer: 3.0, X: 8.0, Y: 3.0, Z: 1.0},
{Layer: 8.0, X: 3.0, Y: 1.0, Z: 8.0},
},
Expected: Position{Layer: 4.0, X: 4.0, Y: 4.0, Z: 4.0},
},
}
for _, c := range cases {
t.Run(c.Desc, func(t *testing.T) {
actual := Midpoint(c.Positions...)
if c.Expected.Layer != actual.Layer {
t.Errorf("incorrect layer: expected %f, actual %f", c.Expected.Layer, actual.Layer)
}
if c.Expected.X != actual.X {
t.Errorf("incorrect x: expected %f, actual %f", c.Expected.X, actual.X)
}
if c.Expected.Y != actual.Y {
t.Errorf("incorrect y: expected %f, actual %f", c.Expected.Y, actual.Y)
}
if c.Expected.Z != actual.Z {
t.Errorf("incorrect z: expected %f, actual %f", c.Expected.Z, actual.Z)
}
})
}
}