-
Notifications
You must be signed in to change notification settings - Fork 9
/
coordseq_test.go
175 lines (157 loc) · 4.36 KB
/
coordseq_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
package geos_test
import (
"fmt"
"math"
"runtime"
"testing"
"github.com/alecthomas/assert/v2"
"github.com/twpayne/go-geos"
)
func TestCoordSeqAliasing(t *testing.T) {
coords := geos.NewContext().NewCoordSeqFromCoords([][]float64{{0, 1}, {2, 3}}).ToCoords()
coords[0] = append(coords[0], 4)
assert.Equal(t, []float64{2, 3}, coords[1])
}
func TestCoordSeqEmpty(t *testing.T) {
defer runtime.GC() // Exercise finalizers.
c := geos.NewContext()
s := c.NewCoordSeq(0, 2)
assert.Equal(t, 0, s.Size())
assert.Equal(t, 2, s.Dimensions())
assert.Equal(t, nil, s.ToCoords())
}
func TestCoordSeqIsCCW(t *testing.T) {
for _, tc := range []struct {
name string
coords [][]float64
expected bool
expectedErrPre13_2 bool
}{
{
name: "ccw",
coords: [][]float64{{0, 0}, {1, 0}, {1, 1}, {0, 0}},
expected: true,
},
{
name: "cw",
coords: [][]float64{{0, 0}, {0, 1}, {1, 1}, {0, 0}},
expected: false,
},
{
name: "short",
coords: [][]float64{{0, 0}, {1, 0}, {1, 1}},
expected: false,
expectedErrPre13_2: true,
},
} {
t.Run(tc.name, func(t *testing.T) {
defer runtime.GC() // Exercise finalizers.
s := geos.NewContext().NewCoordSeqFromCoords(tc.coords)
if geos.VersionCompare(3, 12, 0) < 0 && tc.expectedErrPre13_2 {
assert.Panics(t, func() {
s.IsCCW()
})
} else {
assert.Equal(t, tc.expected, s.IsCCW())
}
})
}
}
func TestCoordSeqMethods(t *testing.T) {
defer runtime.GC() // Exercise finalizers.
c := geos.NewContext()
s := c.NewCoordSeq(2, 3)
assert.Equal(t, 2, s.Size())
assert.Equal(t, 3, s.Dimensions())
assert.Equal(t, 0.0, s.X(0))
assert.Equal(t, 0.0, s.Y(0))
assert.True(t, math.IsNaN(s.Z(0)))
s.SetZ(0, 0)
s.SetX(1, 1)
s.SetY(1, 2)
s.SetZ(1, 3)
assert.Equal(t, 1.0, s.X(1))
assert.Equal(t, 2.0, s.Y(1))
assert.Equal(t, 3.0, s.Z(1))
assert.Equal(t, 1.0, s.Ordinate(1, 0))
assert.Equal(t, 2.0, s.Ordinate(1, 1))
assert.Equal(t, 3.0, s.Ordinate(1, 2))
assert.Equal(t, [][]float64{{0, 0, 0}, {1, 2, 3}}, s.ToCoords())
clone := s.Clone()
assert.Equal(t, 1.0, clone.X(1))
assert.Equal(t, 2.0, clone.Y(1))
clone.SetOrdinate(0, 0, -1.0)
clone.SetOrdinate(0, 1, -2.0)
assert.Equal(t, -1.0, clone.X(0))
assert.Equal(t, -2.0, clone.Y(0))
assert.Equal(t, [][]float64{{-1, -2, 0}, {1, 2, 3}}, clone.ToCoords())
assert.Equal(t, 3, clone.Dimensions())
assert.Equal(t, 3.0, clone.Z(1))
clone.SetOrdinate(0, 2, -3.0)
assert.Equal(t, -3.0, clone.Z(0))
}
func TestCoordSeqPanics(t *testing.T) {
c := geos.NewContext()
s := c.NewCoordSeq(1, 2)
assert.Panics(t, func() { s.X(-1) })
assert.NotPanics(t, func() { s.X(0) })
assert.Panics(t, func() { s.X(1) })
assert.Panics(t, func() { s.Y(-1) })
assert.NotPanics(t, func() { s.Y(0) })
assert.Panics(t, func() { s.Y(1) })
assert.Panics(t, func() { s.Z(-1) })
assert.Panics(t, func() { s.Z(0) })
assert.Panics(t, func() { s.Z(1) })
assert.Panics(t, func() { s.SetX(-1, 0) })
assert.NotPanics(t, func() { s.SetX(0, 0) })
assert.Panics(t, func() { s.SetX(1, 0) })
assert.Panics(t, func() { s.SetY(-1, 0) })
assert.NotPanics(t, func() { s.SetY(0, 0) })
assert.Panics(t, func() { s.SetY(1, 0) })
assert.Panics(t, func() { s.SetZ(-1, 0) })
assert.Panics(t, func() { s.SetZ(0, 0) })
assert.Panics(t, func() { s.SetZ(1, 0) })
for idx := -1; idx <= 1; idx++ {
for dim := -1; dim <= 4; dim++ {
t.Run(fmt.Sprintf("idx_%d_dim_%d", idx, dim), func(t *testing.T) {
if idx == 0 && 0 <= dim && dim < 2 {
assert.NotPanics(t, func() { s.Ordinate(idx, dim) })
assert.NotPanics(t, func() { s.SetOrdinate(idx, dim, 0) })
} else {
assert.Panics(t, func() { s.Ordinate(idx, dim) })
assert.Panics(t, func() { s.SetOrdinate(idx, dim, 0) })
}
})
}
}
}
func TestCoordSeqCoordsMethods(t *testing.T) {
for _, tc := range []struct {
name string
coords [][]float64
}{
{
name: "point_2d",
coords: [][]float64{{1, 2}},
},
{
name: "point_3d",
coords: [][]float64{{1, 2, 3}},
},
{
name: "linestring_2d",
coords: [][]float64{{1, 2}, {3, 4}},
},
{
name: "linestring_3d",
coords: [][]float64{{1, 2, 3}, {4, 5, 6}},
},
} {
t.Run(tc.name, func(t *testing.T) {
defer runtime.GC() // Exercise finalizers.
c := geos.NewContext()
s := c.NewCoordSeqFromCoords(tc.coords)
assert.Equal(t, tc.coords, s.ToCoords())
})
}
}