forked from tidwall/geojson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrect_test.go
100 lines (88 loc) · 2.64 KB
/
rect_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
package geojson
import (
"math/rand"
"testing"
"github.com/tidwall/geojson/geometry"
)
func TestRect(t *testing.T) {
rect := RO(10, 20, 30, 40)
expect(t, !rect.Empty())
expect(t, string(rect.AppendJSON(nil)) ==
`{"type":"Polygon","coordinates":[[[10,20],[30,20],[30,40],[10,40],[10,20]]]}`)
expect(t, rect.String() == string(rect.AppendJSON(nil)))
// expect(t, !rect.Contains(NewString("")))
// expect(t, !rect.Within(NewString("")))
// expect(t, !rect.Intersects(NewString("")))
// expect(t, rect.Distance(NewString("")) == 0)
expect(t, rect.Rect() == R(10, 20, 30, 40))
expect(t, rect.Center() == P(20, 30))
var g Object
rect.ForEach(func(o Object) bool {
expect(t, g == nil)
g = o
return true
})
expect(t, g == rect)
expect(t, rect.NumPoints() == 2)
expect(t, !(&Point{}).Contains(rect))
expect(t, !(&Rect{}).Contains(rect))
expect(t, !(&LineString{}).Contains(rect))
expect(t, !(&Polygon{}).Contains(rect))
expect(t, !(&Point{}).Intersects(rect))
expect(t, !(&Rect{}).Intersects(rect))
expect(t, !(&LineString{}).Intersects(rect))
expect(t, !(&Polygon{}).Intersects(rect))
expect(t, (&Point{}).Distance(rect) != 0)
expect(t, (&Rect{}).Distance(rect) != 0)
expect(t, (&LineString{}).Distance(rect) != 0)
expect(t, (&Polygon{}).Distance(rect) != 0)
}
func TestRectPoly(t *testing.T) {
rect := RO(10, 20, 30, 40)
json := rect.JSON()
expect(t, json ==
`{"type":"Polygon","coordinates":[[[10,20],[30,20],[30,40],[10,40],[10,20]]]}`)
opts := *DefaultParseOptions
opts.AllowRects = true
o, err := Parse(json, &opts)
expect(t, err == nil)
rect2, ok := o.(*Rect)
expect(t, ok)
expect(t, rect2.base == rect.base)
opts.AllowRects = false
o, err = Parse(json, &opts)
expect(t, err == nil)
poly, ok := o.(*Polygon)
expect(t, ok)
json2 := poly.JSON()
expect(t, json == json2)
}
func TestRectValid(t *testing.T) {
json := `{"type":"Polygon","coordinates":[[[10,200],[30,200],[30,40],[10,40],[10,200]]]}`
expectJSON(t, json, nil)
expectJSONOpts(t, json, errCoordinatesInvalid, &ParseOptions{RequireValid: true})
}
func BenchmarkRectValid(b *testing.B) {
rects := make([]*Rect, b.N)
for i := 0; i < b.N; i++ {
min := geometry.Point{
X: rand.Float64()*400 - 200, // some are out of bounds
Y: rand.Float64()*200 - 100, // some are out of bounds
}
max := geometry.Point{
X: rand.Float64()*400 - 200, // some are out of bounds
Y: rand.Float64()*200 - 100, // some are out of bounds
}
if min.X > max.X {
min.X, max.X = max.X, min.X
}
if min.Y > max.Y {
min.Y, max.Y = max.Y, min.Y
}
rects[i] = NewRect(geometry.Rect{Min: min, Max: max})
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
rects[i].Valid()
}
}