Skip to content

Commit 82d4069

Browse files
committed
add Equal function and some tests
1 parent 0a0fa36 commit 82d4069

File tree

5 files changed

+124
-0
lines changed

5 files changed

+124
-0
lines changed

clone.go

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import (
66

77
// Clone will make a deep copy of the geometry.
88
func Clone(g Geometry) Geometry {
9+
if g == nil {
10+
return nil
11+
}
12+
913
switch g := g.(type) {
1014
case Point:
1115
return g

clone_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package orb
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func TestClone(t *testing.T) {
9+
for _, g := range AllGeometries {
10+
// this closure is necessary if tests are run in parallel, maybe
11+
func(geom Geometry) {
12+
t.Run(fmt.Sprintf("%T", g), func(t *testing.T) {
13+
// should not panic
14+
Clone(geom)
15+
})
16+
}(g)
17+
}
18+
}

equal.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package orb
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
// Equal returns if the two geometrires are equal.
8+
func Equal(g1, g2 Geometry) bool {
9+
if g1 == nil || g2 == nil {
10+
return g1 == g2
11+
}
12+
13+
if g1.GeoJSONType() != g2.GeoJSONType() {
14+
return false
15+
}
16+
17+
switch g1 := g1.(type) {
18+
case Point:
19+
return g1.Equal(g2.(Point))
20+
case MultiPoint:
21+
return g1.Equal(g2.(MultiPoint))
22+
case LineString:
23+
return g1.Equal(g2.(LineString))
24+
case MultiLineString:
25+
return g1.Equal(g2.(MultiLineString))
26+
case Ring:
27+
return g1.Equal(g2.(Ring))
28+
case Polygon:
29+
return g1.Equal(g2.(Polygon))
30+
case MultiPolygon:
31+
return g1.Equal(g2.(MultiPolygon))
32+
case Collection:
33+
return g1.Equal(g2.(Collection))
34+
case Bound:
35+
return g1.Equal(g2.(Bound))
36+
}
37+
38+
panic(fmt.Sprintf("geometry type not supported: %T", g1))
39+
}

equal_test.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package orb
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func TestEqual(t *testing.T) {
9+
for _, g := range AllGeometries {
10+
// this closure is necessary if tests are run in parallel, maybe
11+
func(geom Geometry) {
12+
t.Run(fmt.Sprintf("%T", g), func(t *testing.T) {
13+
if !Equal(geom, geom) {
14+
t.Errorf("%T not equal", g)
15+
}
16+
})
17+
}(g)
18+
}
19+
}

geometry.go

+44
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,34 @@ func (mp MultiPolygon) private() {}
3535
func (b Bound) private() {}
3636
func (c Collection) private() {}
3737

38+
// AllGeometries lists all possible types and values that a geometry
39+
// interface can be. It should be used only for testing to verify
40+
// function that accept a Geometry will work in all cases.
41+
var AllGeometries = []Geometry{
42+
nil,
43+
Point{},
44+
MultiPoint{},
45+
LineString{},
46+
MultiLineString{},
47+
Ring{},
48+
Polygon{},
49+
MultiPolygon{},
50+
Bound{},
51+
Collection{},
52+
53+
// nil values
54+
MultiPoint(nil),
55+
LineString(nil),
56+
MultiLineString(nil),
57+
Ring(nil),
58+
Polygon(nil),
59+
MultiPolygon(nil),
60+
Collection(nil),
61+
62+
// Collection of Collection
63+
Collection{Collection{Point{}}},
64+
}
65+
3866
// A Collection is a collection of geometries that is also a Geometry.
3967
type Collection []Geometry
4068

@@ -65,6 +93,22 @@ func (c Collection) Bound() Bound {
6593
return r
6694
}
6795

96+
// Equal compares two collections. Returns true if lengths are the same
97+
// and all the sub geometries are the same and in the same order.
98+
func (c Collection) Equal(collection Collection) bool {
99+
if len(c) != len(collection) {
100+
return false
101+
}
102+
103+
for i, g := range c {
104+
if !Equal(g, collection[i]) {
105+
return false
106+
}
107+
}
108+
109+
return true
110+
}
111+
68112
// Clone returns a deep copy of the collection.
69113
func (c Collection) Clone() Collection {
70114
if c == nil {

0 commit comments

Comments
 (0)