-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate_test.go
More file actions
244 lines (223 loc) · 7.32 KB
/
Copy pathvalidate_test.go
File metadata and controls
244 lines (223 loc) · 7.32 KB
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
package crdt
import (
"encoding/json"
"strings"
"testing"
)
func closedSquare(scale float64) [][]float64 {
return [][]float64{
{0, 0}, {scale, 0}, {scale, scale}, {0, scale}, {0, 0},
}
}
func TestValidatePolygonRings(t *testing.T) {
tests := []struct {
name string
rings [][][]float64
wantErr string
}{
{"valid square", [][][]float64{closedSquare(10)}, ""},
{"no rings", nil, "at least one ring"},
{"too few positions", [][][]float64{{{0, 0}, {1, 1}, {0, 0}}}, "at least 4"},
{"unclosed", [][][]float64{{{0, 0}, {10, 0}, {10, 10}, {0, 10}}}, "not closed"},
{"cw exterior", [][][]float64{{{0, 0}, {0, 10}, {10, 10}, {10, 0}, {0, 0}}}, "counter-clockwise"},
{"zero area", [][][]float64{{{0, 0}, {5, 0}, {10, 0}, {0, 0}}}, "OGC validity"},
{"bowtie", [][][]float64{{{0, 0}, {10, 10}, {10, 0}, {0, 10}, {0, 0}}}, "OGC validity"},
{
"valid hole",
[][][]float64{
{{0, 0}, {20, 0}, {20, 20}, {0, 20}, {0, 0}},
{{5, 5}, {5, 15}, {15, 15}, {15, 5}, {5, 5}},
},
"",
},
{
"ccw hole",
[][][]float64{
{{0, 0}, {20, 0}, {20, 20}, {0, 20}, {0, 0}},
{{5, 5}, {15, 5}, {15, 15}, {5, 15}, {5, 5}},
},
"clockwise",
},
{
"hole outside exterior",
[][][]float64{
{{0, 0}, {10, 0}, {10, 10}, {0, 10}, {0, 0}},
{{20, 20}, {20, 25}, {25, 25}, {25, 20}, {20, 20}},
},
"OGC validity",
},
{
"hole crossing exterior",
[][][]float64{
{{0, 0}, {10, 0}, {10, 10}, {0, 10}, {0, 0}},
{{5, -1}, {5, 5}, {8, 5}, {8, -1}, {5, -1}},
},
"OGC validity",
},
{
"nested holes",
[][][]float64{
{{0, 0}, {20, 0}, {20, 20}, {0, 20}, {0, 0}},
{{2, 2}, {2, 18}, {18, 18}, {18, 2}, {2, 2}},
{{5, 5}, {5, 10}, {10, 10}, {10, 5}, {5, 5}},
},
"OGC validity",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidatePolygonRings(tt.rings)
if tt.wantErr == "" {
if err != nil {
t.Fatalf("expected valid, got %v", err)
}
return
}
if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
t.Fatalf("expected error containing %q, got %v", tt.wantErr, err)
}
})
}
}
func TestValidatePolygonRingsMalformedArityNeverPanics(t *testing.T) {
t.Parallel()
cases := [][][][]float64{
{{{}, {1, 0}, {1, 1}, {}}},
{{{0}, {1, 0}, {1, 1}, {0}}},
{{{0, 0}, {1}, {1, 1}, {0, 0}}},
}
for _, rings := range cases {
assertNoPanic(t, func() {
if err := ValidatePolygonRings(rings); err == nil {
t.Fatalf("malformed positions were accepted: %v", rings)
}
})
}
}
func TestValidateGeometryViewRejectsOverlappingMultiPolygon(t *testing.T) {
t.Parallel()
raw := json.RawMessage(`{
"type":"MultiPolygon",
"coordinates":[
[[[0,0],[10,0],[10,10],[0,10],[0,0]]],
[[[5,5],[15,5],[15,15],[5,15],[5,5]]]
]
}`)
if err := validateGeometryView(raw); err == nil || !strings.Contains(err.Error(), "OGC validity") {
t.Fatalf("overlapping multipolygon error = %v", err)
}
}
// Regression (review finding A8): tolerances scale with the geometry, so a
// tiny-but-real parcel in degrees and a huge ring in projected meters both
// validate.
func TestValidatePolygonRings_ScaleIndependence(t *testing.T) {
// ~1 m² triangle expressed in degrees (~1e-5 degrees per meter).
small := [][][]float64{{
{-122.40000, 37.70000},
{-122.39999, 37.70000},
{-122.399995, 37.70001},
{-122.40000, 37.70000},
}}
if err := ValidatePolygonRings(small); err != nil {
t.Fatalf("small degree-scale parcel flagged invalid: %v", err)
}
// The same shape in UTM-like meters at large offsets.
large := [][][]float64{{
{500000, 4000000},
{500100, 4000000},
{500050, 4000100},
{500000, 4000000},
}}
if err := ValidatePolygonRings(large); err != nil {
t.Fatalf("projected-meter parcel flagged invalid: %v", err)
}
}
// Regression (review finding A3): repairs honor the bitmask exactly.
func TestRepairPolygonRings_HonorsBitmask(t *testing.T) {
// Clockwise, unclosed, with a duplicate vertex.
ring := [][]float64{{0, 0}, {0, 10}, {0, 10}, {10, 10}, {10, 0}}
closed := RepairPolygonRings([][][]float64{ring}, RepairCloseRings)
got := closed[0]
if !samePositionXY(got[0], got[len(got)-1]) {
t.Fatal("RepairCloseRings should close the ring")
}
if len(got) != 6 {
t.Fatalf("RepairCloseRings must not deduplicate: %v", got)
}
if signedAreaXY(got) > 0 {
t.Fatal("RepairCloseRings must not change orientation")
}
dedup := RepairPolygonRings([][][]float64{ring}, RepairRemoveDuplicateVertices)
if len(dedup[0]) != 4 {
t.Fatalf("RepairRemoveDuplicateVertices: %v", dedup[0])
}
if samePositionXY(dedup[0][0], dedup[0][len(dedup[0])-1]) {
t.Fatal("RepairRemoveDuplicateVertices must not close the ring")
}
oriented := RepairPolygonRings([][][]float64{ring}, RepairNormalizeOrientation)
if signedAreaXY(oriented[0]) < 0 {
t.Fatal("RepairNormalizeOrientation should rewind the exterior CCW")
}
full := RepairPolygonRings([][][]float64{ring}, RepairBasicPolygon)
if err := ValidatePolygonRings(full); err != nil {
t.Fatalf("fully repaired ring should validate: %v", err)
}
}
func TestRepairPolygonRings_DropsDegenerateRings(t *testing.T) {
rings := [][][]float64{
closedSquare(20),
{{5, 5}, {6, 6}, {5, 5}}, // degenerate hole
}
repaired := RepairPolygonRings(rings, RepairDropDegenerateRings)
if len(repaired) != 1 {
t.Fatalf("expected degenerate hole dropped, got %d rings", len(repaired))
}
// A degenerate exterior drops the whole polygon.
repaired = RepairPolygonRings([][][]float64{{{0, 0}, {1, 1}, {0, 0}}}, RepairDropDegenerateRings)
if len(repaired) != 0 {
t.Fatalf("expected empty polygon, got %v", repaired)
}
}
func TestRepairPolygonRings_PreservesZ(t *testing.T) {
ring := [][]float64{{0, 0, 5}, {0, 10, 6}, {10, 10, 7}, {10, 0, 8}}
repaired := RepairPolygonRings([][][]float64{ring}, RepairBasicPolygon)
for _, position := range repaired[0] {
if len(position) != 3 {
t.Fatalf("altitude lost during repair: %v", repaired[0])
}
}
}
func TestDocumentExportTopologyPolicy(t *testing.T) {
// A concurrent-merge shape that self-intersects exports fine by default
// but fails under ValidateOnExport.
bowtie := json.RawMessage(`{"type":"Polygon","coordinates":[[[0,0],[10,10],[10,0],[0,10],[0,0]]]}`)
lenient := NewDocument("test-document", "site-a")
mustApply(t, lenient, InsertFeature{FeatureID: "f", Geometry: bowtie})
if _, err := lenient.FeatureCollectionJSON(); err != nil {
t.Fatalf("lenient export should pass: %v", err)
}
strict := NewDocument("test-document", "site-a", WithTopologyPolicy(ValidateOnExport))
mustApply(t, strict, InsertFeature{FeatureID: "f", Geometry: bowtie})
if _, err := strict.FeatureCollectionJSON(); err == nil {
t.Fatal("ValidateOnExport should reject a self-intersecting polygon")
}
}
func TestDocumentPolygonRepairView(t *testing.T) {
doc := NewDocument("test-document", "site-a", WithPolygonRepair(RepairBasicPolygon))
// Clockwise exterior; the view repairs orientation without touching
// CRDT state.
mustApply(t, doc, InsertFeature{
FeatureID: "f",
Geometry: json.RawMessage(`{"type":"Polygon","coordinates":[[[0,0],[0,10],[10,10],[10,0],[0,0]]]}`),
})
feature, _ := doc.Feature("f")
var geom struct {
Coordinates [][][]float64 `json:"coordinates"`
}
if err := json.Unmarshal(feature.Geometry, &geom); err != nil {
t.Fatal(err)
}
if signedAreaXY(geom.Coordinates[0]) < 0 {
t.Fatalf("repair view should normalize orientation: %v", geom.Coordinates[0])
}
}