File: polygon_test.go

package info (click to toggle)
golang-github-golang-geo 0.0~git20170112.0.f819552-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 832 kB
  • sloc: makefile: 2
file content (199 lines) | stat: -rw-r--r-- 5,872 bytes parent folder | download
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
/*
Copyright 2015 Google Inc. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package s2

import (
	"testing"
)

func TestPolygonEmptyAndFull(t *testing.T) {
	emptyPolygon := &Polygon{}

	if !emptyPolygon.IsEmpty() {
		t.Errorf("empty polygon should be empty")
	}
	if emptyPolygon.IsFull() {
		t.Errorf("empty polygon should not be full")
	}
	/*
		// TODO(roberts): Uncomment when Polygon finishes the Shape interface.
		if emptyPolygon.ContainsOrigin() {
			t.Errorf("emptyPolygon.ContainsOrigin() = true, want false")
		}
		if got, want := emptyPolygon.NumEdges(), 0; got != want {
			t.Errorf("emptyPolygon.NumEdges() = %v, want %v", got, want)
		}
	*/
	if got := emptyPolygon.dimension(); got != polygonGeometry {
		t.Errorf("emptyPolygon.dimension() = %v, want %v", got, polygonGeometry)
	}
	if got, want := emptyPolygon.numChains(), 0; got != want {
		t.Errorf("emptyPolygon.numChains() = %v, want %v", got, want)
	}

	fullPolygon := FullPolygon()
	if fullPolygon.IsEmpty() {
		t.Errorf("full polygon should not be emtpy")
	}
	if !fullPolygon.IsFull() {
		t.Errorf("full polygon should be full")
	}
	/*
		// TODO(roberts): Uncomment when Polygon finishes the Shape interface.
		if !fullPolygon.ContainsOrigin() {
			t.Errorf("fullPolygon.ContainsOrigin() = false, want true")
		}
		if got, want := fullPolygon.NumEdges(), 0; got != want {
			t.Errorf("fullPolygon.NumEdges() = %v, want %v", got, want)
		}
	*/
	if got := fullPolygon.dimension(); got != polygonGeometry {
		t.Errorf("emptyPolygon.dimension() = %v, want %v", got, polygonGeometry)
	}
	if got, want := fullPolygon.numChains(), 0; got != want {
		t.Errorf("emptyPolygon.numChains() = %v, want %v", got, want)
	}
}

func TestPolygonShape(t *testing.T) {
	// TODO(roberts): Once Polygon implements Shape uncomment this test.
	/*
		p := &Polygon{}
		shape := Shape(p)
		if p.NumVertices() != shape.NumEdges() {
			t.Errorf("the number of vertices in a polygon should equal the number of edges")
		}
		if p.NumLoops() != shape.numChains() {
			t.Errorf("the number of loops in a polygon should equal the number of chains")
		}
		e := 0
		for i, l := range p.loops {
			if e != shape.chainStart(i) {
				t.Errorf("the edge if of the start of loop(%d) should equal the sum of vertices so far in the polygon. got %d, want %d", i, shape.chainStart(i), e)
			}
			for j := 0; j < len(l.Vertices()); j++ {
				v0, v1 := shape.Edge(e)
				// TODO(roberts): Update once Loop implements orientedVertex.
				//if l.orientedVertex(j) != v0 {
				if l.Vertex(j) != v0 {
					t.Errorf("l.Vertex(%d) = %v, want %v", j, l.Vertex(j), v0)
				}
				// TODO(roberts): Update once Loop implements orientedVertex.
				//if l.orientedVertex(j+1) != v1 {
				if l.Vertex(j+1) != v1 {
					t.Errorf("l.Vertex(%d) = %v, want %v", j+1, l.Vertex(j+1), v1)
				}
				e++
			}
			if e != shape.chainStart(i+1) {
				t.Errorf("the edge id of the start of the next loop(%d+1) should equal the sum of vertices so far in the polygon. got %d, want %d", i, shape.chainStart(i+1), e)
			}
		}
		if shape.dimension() != polygonGeometry {
			t.Errorf("polygon.dimension() = %v, want %v", shape.dimension() , polygonGeometry)
		}
		if !shape.HasInterior() {
			t.Errorf("polygons should always have interiors")
		}
	*/
}

func TestPolygonLoop(t *testing.T) {
	full := FullPolygon()
	if full.NumLoops() != 1 {
		t.Errorf("full polygon should have one loop")
	}

	l := &Loop{}
	p1 := PolygonFromLoops([]*Loop{l})
	if p1.NumLoops() != 1 {
		t.Errorf("polygon with one loop should have one loop")
	}
	if p1.Loop(0) != l {
		t.Errorf("polygon with one loop should return it")
	}

	// TODO: When multiple loops are supported, add more test cases.
}

func TestPolygonParent(t *testing.T) {
	p1 := PolygonFromLoops([]*Loop{&Loop{}})
	tests := []struct {
		p    *Polygon
		have int
		want int
		ok   bool
	}{
		{FullPolygon(), 0, -1, false},
		{p1, 0, -1, false},

		// TODO: When multiple loops are supported, add more test cases to
		// more fully show the parent levels.
	}

	for _, test := range tests {
		if got, ok := test.p.Parent(test.have); ok != test.ok || got != test.want {
			t.Errorf("%v.Parent(%d) = %d,%v, want %d,%v", test.p, test.have, got, ok, test.want, test.ok)
		}
	}
}

func TestPolygonLastDescendant(t *testing.T) {
	p1 := PolygonFromLoops([]*Loop{&Loop{}})

	tests := []struct {
		p    *Polygon
		have int
		want int
	}{
		{FullPolygon(), 0, 0},
		{FullPolygon(), -1, 0},

		{p1, 0, 0},
		{p1, -1, 0},

		// TODO: When multiple loops are supported, add more test cases.
	}

	for _, test := range tests {
		if got := test.p.LastDescendant(test.have); got != test.want {
			t.Errorf("%v.LastDescendant(%d) = %d, want %d", test.p, test.have, got, test.want)
		}
	}
}

func TestPolygonLoopIsHoleAndLoopSign(t *testing.T) {
	if FullPolygon().loopIsHole(0) {
		t.Errorf("the full polygons only loop should not be a hole")
	}
	if FullPolygon().loopSign(0) != 1 {
		t.Errorf("the full polygons only loop should be postitive")
	}

	loop := LoopFromPoints(parsePoints("30:20, 40:20, 39:43, 33:35"))
	p := PolygonFromLoops([]*Loop{loop})

	if p.loopIsHole(0) {
		t.Errorf("first loop in a polygon should not start out as a hole")
	}
	if p.loopSign(0) != 1 {
		t.Errorf("first loop in a polygon should start out as positive")
	}

	// TODO: When multiple loops are supported, add more test cases to
	// more fully show the parent levels.
}