forked from twpayne/go-geom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
polygon_test.go
117 lines (108 loc) · 2.73 KB
/
polygon_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
package geom
import (
"fmt"
"strconv"
"testing"
"github.com/stretchr/testify/assert"
)
// Polygon implements interface T.
var _ T = &Polygon{}
func ExampleNewPolygon() {
unitSquare := NewPolygon(XY).MustSetCoords([][]Coord{
{{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}},
})
fmt.Printf("unitSquare.Area() == %f", unitSquare.Area())
// Output: unitSquare.Area() == 1.000000
}
type expectedPolygon struct {
layout Layout
stride int
flatCoords []float64
ends []int
coords [][]Coord
bounds *Bounds
}
func (g *Polygon) assertEquals(t *testing.T, e *expectedPolygon) {
assert.NoError(t, g.verify())
assert.Equal(t, e.layout, g.Layout())
assert.Equal(t, e.stride, g.Stride())
assert.Equal(t, e.flatCoords, g.FlatCoords())
assert.Equal(t, e.ends, g.Ends())
assert.Nil(t, g.Endss())
assert.Equal(t, e.coords, g.Coords())
assert.Equal(t, e.bounds, g.Bounds())
assert.Equal(t, len(e.coords), g.NumLinearRings())
for i, c := range e.coords {
assert.Equal(t, NewLinearRing(g.Layout()).MustSetCoords(c), g.LinearRing(i))
}
}
func TestPolygon(t *testing.T) {
for i, tc := range []struct {
p *Polygon
expected *expectedPolygon
}{
{
p: NewPolygon(XY).MustSetCoords([][]Coord{{{1, 2}, {3, 4}, {5, 6}}, {{7, 8}, {9, 10}, {11, 12}}}),
expected: &expectedPolygon{
layout: XY,
stride: 2,
coords: [][]Coord{{{1, 2}, {3, 4}, {5, 6}}, {{7, 8}, {9, 10}, {11, 12}}},
flatCoords: []float64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
ends: []int{6, 12},
bounds: NewBounds(XY).Set(1, 2, 11, 12),
},
},
} {
t.Run(strconv.Itoa(i), func(t *testing.T) {
tc.p.assertEquals(t, tc.expected)
assert.False(t, aliases(tc.p.FlatCoords(), tc.p.Clone().FlatCoords()))
})
}
}
func TestPolygonStrideMismatch(t *testing.T) {
for i, tc := range []struct {
l Layout
cs [][]Coord
expected error
}{
{
l: XY,
cs: nil,
expected: nil,
},
{
l: XY,
cs: [][]Coord{},
expected: nil,
},
{
l: XY,
cs: [][]Coord{{{1, 2}, {}}},
expected: ErrStrideMismatch{Got: 0, Want: 2},
},
{
l: XY,
cs: [][]Coord{{{1, 2}, {1}}},
expected: ErrStrideMismatch{Got: 1, Want: 2},
},
{
l: XY,
cs: [][]Coord{{{1, 2}, {3, 4}}},
expected: nil,
},
{
l: XY,
cs: [][]Coord{{{1, 2}, {3, 4, 5}}},
expected: ErrStrideMismatch{Got: 3, Want: 2},
},
} {
t.Run(strconv.Itoa(i), func(t *testing.T) {
_, err := NewPolygon(tc.l).SetCoords(tc.cs)
assert.Equal(t, tc.expected, err)
})
}
}
func TestPolygonSetSRID(t *testing.T) {
assert.Equal(t, 4326, NewPolygon(NoLayout).SetSRID(4326).SRID())
assert.Equal(t, 4326, Must(SetSRID(NewPolygon(NoLayout), 4326)).SRID())
}