-
Notifications
You must be signed in to change notification settings - Fork 0
/
factory_test.go
146 lines (130 loc) · 2.59 KB
/
factory_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
package geom
import (
"testing"
"github.com/vistarmedia/geom/geos-go"
"github.com/vistarmedia/geom/geos-go/handle"
)
var fact = NewFactory(handle.NewPooledHandleProvider())
func TestNewEmptyPoint(t *testing.T) {
p := fact.NewEmptyPoint()
if p.Type() != POINT {
t.Errorf("Unexpected geom type: %d", p.Type())
}
if isEmpty, err := p.IsEmpty(); err != nil {
t.Error(err)
} else if !isEmpty {
t.Error("Point is not empty")
}
}
func TestNewEmptyPolygon(t *testing.T) {
p := fact.NewEmptyPolygon()
if p.Type() != POLYGON {
t.Errorf("Unexpected geom type: %d", p.Type())
}
if isEmpty, err := p.IsEmpty(); err != nil {
t.Error(err)
} else if !isEmpty {
t.Error("Polygon is not empty")
}
}
func TestNewPoint(t *testing.T) {
p, err := fact.NewPoint(Coord{4, 2})
if err != nil {
t.Error(err)
}
if p.Type() != POINT {
t.Errorf("Unexpected geom type: %d", p.Type())
}
}
func TestNewLinearRing(t *testing.T) {
coords := []Coord{
{2, 2},
{2, 4},
{4, 4},
{4, 2},
{2, 2},
}
ring, err := fact.NewLinearRing(coords)
if err != nil {
t.Error(err)
}
if ring.Type() != LINEARRING {
t.Errorf("Unexpected geom type: %d", ring.Type())
}
// LinearRings dont have an area
if ring.Area() != 0 {
t.Errorf("Expected area of 0, got %f", ring.Area())
}
}
func TestNewLinearRingInvalid(t *testing.T) {
// Not a closed ring
coords := []Coord{{2, 2}, {2, 4}}
_, err := fact.NewLinearRing(coords)
if err != geos.ErrGeos {
t.Errorf("Expected GEOS error, got %v", err)
}
}
func TestNewPolygon(t *testing.T) {
shell := []Coord{
{2, 2},
{2, 4},
{4, 4},
{4, 2},
{2, 2},
}
poly, err := fact.NewPolygon(shell)
if err != nil {
t.Error(err)
}
if poly.Type() != POLYGON {
t.Errorf("Unexpected geom type: %d", poly.Type())
}
if poly.Area() != 4 {
t.Errorf("Expected area of 4, got %f", poly.Area())
}
}
func TestNewPolygonWithHoles(t *testing.T) {
//10x10 rect
shell := []Coord{
{0, 0},
{10, 0},
{10, 10},
{0, 10},
{0, 0},
}
//2x2 rect
hole1 := []Coord{
{2, 2},
{2, 4},
{4, 4},
{4, 2},
{2, 2},
}
//3x3 rect
hole2 := []Coord{
{6, 6},
{6, 9},
{9, 9},
{9, 6},
{6, 6},
}
poly, err := fact.NewPolygon(shell, hole1, hole2)
if err != nil {
t.Error(err)
}
if poly.Type() != POLYGON {
t.Errorf("Unexpected geom type: %d", poly.Type())
}
// 10x10 - 3x3 - 2x2 = 87
if poly.Area() != 87 {
t.Errorf("Expected area of 87, got %f", poly.Area())
}
}
func TestNewPolygonInvalid(t *testing.T) {
// not a ring
shell := []Coord{{2, 2}, {2, 4}}
_, err := fact.NewPolygon(shell)
if err != geos.ErrGeos {
t.Errorf("Expected GEOS error, got %v", err)
}
}