forked from paulmach/orb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bound_test.go
255 lines (223 loc) · 5.16 KB
/
bound_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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package orb
import (
"testing"
)
func TestBoundExtend(t *testing.T) {
bound := Bound{Min: Point{0, 0}, Max: Point{3, 5}}
if r := bound.Extend(Point{2, 1}); !r.Equal(bound) {
t.Errorf("extend incorrect: %v != %v", r, bound)
}
answer := Bound{Min: Point{0, -1}, Max: Point{6, 5}}
if r := bound.Extend(Point{6, -1}); !r.Equal(answer) {
t.Errorf("extend incorrect: %v != %v", r, answer)
}
}
func TestBoundUnion(t *testing.T) {
b1 := Bound{Min: Point{0, 0}, Max: Point{1, 1}}
b2 := Bound{Min: Point{0, 0}, Max: Point{2, 2}}
expected := Bound{Min: Point{0, 0}, Max: Point{2, 2}}
if b := b1.Union(b2); !b.Equal(expected) {
t.Errorf("union incorrect: %v != %v", b, expected)
}
if b := b2.Union(b1); !b.Equal(expected) {
t.Errorf("union incorrect: %v != %v", b, expected)
}
}
func TestBoundContains(t *testing.T) {
bound := Bound{Min: Point{-2, -1}, Max: Point{2, 1}}
cases := []struct {
name string
point Point
result bool
}{
{
name: "middle",
point: Point{0, 0},
result: true,
},
{
name: "left border",
point: Point{-1, 0},
result: true,
},
{
name: "ne corner",
point: Point{2, 1},
result: true,
},
{
name: "above",
point: Point{0, 3},
result: false,
},
{
name: "below",
point: Point{0, -3},
result: false,
},
{
name: "left",
point: Point{-3, 0},
result: false,
},
{
name: "right",
point: Point{3, 0},
result: false,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
v := bound.Contains(tc.point)
if v != tc.result {
t.Errorf("incorrect contains: %v != %v", v, tc.result)
}
})
}
}
func TestBoundIntersects(t *testing.T) {
bound := Bound{Min: Point{0, 2}, Max: Point{1, 3}}
cases := []struct {
name string
bound Bound
result bool
}{
{
name: "outside, top right",
bound: Bound{Min: Point{5, 7}, Max: Point{6, 8}},
result: false,
},
{
name: "outside, top left",
bound: Bound{Min: Point{-6, 7}, Max: Point{-5, 8}},
result: false,
},
{
name: "outside, above",
bound: Bound{Min: Point{0, 7}, Max: Point{0.5, 8}},
result: false,
},
{
name: "over the middle",
bound: Bound{Min: Point{0, 0.5}, Max: Point{1, 4}},
result: true,
},
{
name: "over the left",
bound: Bound{Min: Point{-1, 2}, Max: Point{1, 4}},
result: true,
},
{
name: "completely inside",
bound: Bound{Min: Point{0.3, 2.3}, Max: Point{0.6, 2.6}},
result: true,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
v := bound.Intersects(tc.bound)
if v != tc.result {
t.Errorf("incorrect result: %v != %v", v, tc.result)
}
})
}
a := Bound{Min: Point{7, 6}, Max: Point{8, 7}}
b := Bound{Min: Point{6.1, 6.1}, Max: Point{8.1, 8.1}}
if !a.Intersects(b) || !b.Intersects(a) {
t.Errorf("expected to intersect")
}
a = Bound{Min: Point{1, 2}, Max: Point{4, 3}}
b = Bound{Min: Point{2, 1}, Max: Point{3, 4}}
if !a.Intersects(b) || !b.Intersects(a) {
t.Errorf("expected to intersect")
}
}
func TestBoundIsEmpty(t *testing.T) {
cases := []struct {
name string
bound Bound
result bool
}{
{
name: "regular bound",
bound: Bound{Min: Point{5, 7}, Max: Point{6, 8}},
result: false,
},
{
name: "single point",
bound: Bound{Min: Point{5, 7}, Max: Point{6, 8}},
result: false,
},
{
name: "horizontal bar",
bound: Bound{Min: Point{5, 7}, Max: Point{6, 8}},
result: false,
},
{
name: "vertical bar",
bound: Bound{Min: Point{5, 7}, Max: Point{6, 8}},
result: false,
},
{
name: "vertical bar",
bound: Bound{Min: Point{5, 7}, Max: Point{6, 8}},
result: false,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
v := tc.bound.IsEmpty()
if v != tc.result {
t.Errorf("incorrect result: %v != %v", v, tc.result)
}
})
}
// negative/malformed area
bound := Bound{Min: Point{1, 1}, Max: Point{0, 2}}
if !bound.IsEmpty() {
t.Error("expected true, got false")
}
// negative/malformed area
bound = Bound{Min: Point{1, 3}, Max: Point{1, 2}}
if !bound.IsEmpty() {
t.Error("expected true, got false")
}
}
func TestBoundPad(t *testing.T) {
bound := Bound{Min: Point{1, 1}, Max: Point{2, 2}}
bound = bound.Pad(0.5)
if !bound.Min.Equal(Point{0.5, 0.5}) {
t.Errorf("incorrect min: %v", bound.Min)
}
if !bound.Max.Equal(Point{2.5, 2.5}) {
t.Errorf("incorrect min: %v", bound.Max)
}
}
func TestBoundCenter(t *testing.T) {
bound := Bound{Min: Point{1, 1}, Max: Point{2, 2}}
if c := bound.Center(); !c.Equal(Point{1.5, 1.5}) {
t.Errorf("incorrect center: %v", c)
}
}
func TestBoundIsZero(t *testing.T) {
bound := Bound{Min: Point{1, 2}, Max: Point{1, 2}}
if bound.IsZero() {
t.Error("expected false, got true")
}
bound = Bound{}
if !bound.IsZero() {
t.Error("expected true, got false")
}
}
func TestBoundToRing(t *testing.T) {
bound := Bound{Min: Point{1, 1}, Max: Point{2, 2}}
if bound.ToRing().Orientation() != CCW {
t.Errorf("orientation should be ccw")
}
}
func TestBoundToPolygon(t *testing.T) {
bound := Bound{Min: Point{1, 1}, Max: Point{2, 2}}
if bound.ToPolygon()[0].Orientation() != CCW {
t.Errorf("orientation should be ccw")
}
}