-
Notifications
You must be signed in to change notification settings - Fork 108
/
helpers_test.go
217 lines (200 loc) · 4.89 KB
/
helpers_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
package clip
import (
"testing"
"github.com/paulmach/orb"
)
func TestGeometry(t *testing.T) {
bound := orb.Bound{Min: orb.Point{-1, -1}, Max: orb.Point{1, 1}}
for _, g := range orb.AllGeometries {
Geometry(bound, g)
}
cases := []struct {
name string
input orb.Geometry
output orb.Geometry
}{
{
name: "only one multipoint in bound",
input: orb.MultiPoint{{0, 0}, {5, 5}},
output: orb.Point{0, 0},
},
{
name: "only one multilinestring in bound",
input: orb.MultiLineString{
{{0, 0}, {5, 5}},
{{6, 6}, {7, 7}},
},
output: orb.LineString{{0, 0}, {1, 1}},
},
{
name: "only one multipolygon in bound",
input: orb.MultiPolygon{
{{{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}}},
{{{2, 2}, {3, 2}, {3, 3}, {2, 3}, {2, 2}}},
},
output: orb.Polygon{{{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}}},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
result := Geometry(bound, tc.input)
if !orb.Equal(result, tc.output) {
t.Errorf("not equal")
t.Logf("%v", result)
t.Logf("%v", tc.output)
}
})
}
}
func TestRing(t *testing.T) {
cases := []struct {
name string
bound orb.Bound
input orb.Ring
output orb.Ring
}{
{
name: "regular clip",
bound: orb.Bound{Min: orb.Point{0, 0}, Max: orb.Point{1.5, 1.5}},
input: orb.Ring{
{1, 1}, {2, 1}, {2, 2}, {1, 2}, {1, 1},
},
output: orb.Ring{
{1, 1}, {1.5, 1}, {1.5, 1.5}, {1, 1.5}, {1, 1},
},
},
{
name: "bound to the top",
bound: orb.Bound{Min: orb.Point{-1, 3}, Max: orb.Point{3, 4}},
input: orb.Ring{
{1, 1}, {2, 1}, {2, 2}, {1, 2}, {1, 1},
},
output: orb.Ring{},
},
{
name: "bound in lower left",
bound: orb.Bound{Min: orb.Point{-1, -1}, Max: orb.Point{0, 0}},
input: orb.Ring{
{1, 1}, {2, 1}, {2, 2}, {1, 2}, {1, 1},
},
output: orb.Ring{},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
result := Ring(tc.bound, tc.input)
if !result.Equal(tc.output) {
t.Errorf("not equal")
t.Logf("%v", result)
t.Logf("%v", tc.output)
}
})
}
}
func TestMultiLineString(t *testing.T) {
bound := orb.Bound{Min: orb.Point{0, 0}, Max: orb.Point{2, 2}}
cases := []struct {
name string
open bool
input orb.MultiLineString
output orb.MultiLineString
}{
{
name: "regular closed bound clip",
input: orb.MultiLineString{
{{1, 1}, {2, 1}, {2, 2}, {3, 3}},
},
output: orb.MultiLineString{
{{1, 1}, {2, 1}, {2, 2}, {2, 2}},
},
},
{
name: "open bound clip",
open: true,
input: orb.MultiLineString{
{{1, 1}, {2, 1}, {2, 2}, {3, 3}},
},
output: orb.MultiLineString{
{{1, 1}, {2, 1}},
},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
result := MultiLineString(bound, tc.input, OpenBound(tc.open))
if !result.Equal(tc.output) {
t.Errorf("not equal")
t.Logf("%v", result)
t.Logf("%v", tc.output)
}
})
}
}
func TestBound(t *testing.T) {
cases := []struct {
name string
b1 orb.Bound
b2 orb.Bound
rs orb.Bound
}{
{
name: "normal intersection",
b1: orb.Bound{Min: orb.Point{0, 1}, Max: orb.Point{3, 4}},
b2: orb.Bound{Min: orb.Point{1, 2}, Max: orb.Point{4, 5}},
rs: orb.Bound{Min: orb.Point{1, 2}, Max: orb.Point{3, 4}},
},
{
name: "1 contains 2",
b1: orb.Bound{Min: orb.Point{0, 1}, Max: orb.Point{3, 4}},
b2: orb.Bound{Min: orb.Point{1, 2}, Max: orb.Point{2, 3}},
rs: orb.Bound{Min: orb.Point{1, 2}, Max: orb.Point{2, 3}},
},
{
name: "no overlap",
b1: orb.Bound{Min: orb.Point{0, 1}, Max: orb.Point{3, 4}},
b2: orb.Bound{Min: orb.Point{4, 5}, Max: orb.Point{5, 6}},
rs: orb.Bound{Min: orb.Point{1, 1}, Max: orb.Point{0, 0}}, // empty
},
{
name: "same bound",
b1: orb.Bound{Min: orb.Point{0, 1}, Max: orb.Point{3, 4}},
b2: orb.Bound{Min: orb.Point{0, 1}, Max: orb.Point{3, 4}},
rs: orb.Bound{Min: orb.Point{0, 1}, Max: orb.Point{3, 4}},
},
{
name: "1 is empty",
b1: orb.Bound{Min: orb.Point{1, 1}, Max: orb.Point{0, 0}},
b2: orb.Bound{Min: orb.Point{0, 1}, Max: orb.Point{3, 4}},
rs: orb.Bound{Min: orb.Point{0, 1}, Max: orb.Point{3, 4}},
},
{
name: "both are empty",
b1: orb.Bound{Min: orb.Point{1, 1}, Max: orb.Point{0, 0}},
b2: orb.Bound{Min: orb.Point{1, 1}, Max: orb.Point{0, 0}},
rs: orb.Bound{Min: orb.Point{1, 1}, Max: orb.Point{0, 0}},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
r1 := Bound(tc.b1, tc.b2)
r2 := Bound(tc.b1, tc.b2)
if tc.rs.IsEmpty() && (!r1.IsEmpty() || !r2.IsEmpty()) {
t.Errorf("should be empty")
t.Logf("%v", r1)
t.Logf("%v", r2)
}
if !tc.rs.IsEmpty() {
if !r1.Equal(tc.rs) {
t.Errorf("r1 not equal")
t.Logf("%v", r1)
t.Logf("%v", tc.rs)
}
if !r2.Equal(tc.rs) {
t.Errorf("r2 not equal")
t.Logf("%v", r2)
t.Logf("%v", tc.rs)
}
}
})
}
}