forked from paulmach/orb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ewkb_test.go
156 lines (131 loc) · 2.88 KB
/
ewkb_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
package ewkb
import (
"bytes"
"encoding/binary"
"encoding/hex"
"io/ioutil"
"testing"
"github.com/paulmach/orb"
"github.com/paulmach/orb/encoding/internal/wkbcommon"
)
func TestMarshal(t *testing.T) {
for _, g := range orb.AllGeometries {
_, err := Marshal(g, 0, binary.BigEndian)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
}
func TestMustMarshal(t *testing.T) {
for _, g := range orb.AllGeometries {
MustMarshal(g, 0, binary.BigEndian)
}
}
func MustDecodeHex(s string) []byte {
b, err := hex.DecodeString(s)
if err != nil {
panic(err)
}
return b
}
func BenchmarkEncode_Point(b *testing.B) {
g := orb.Point{1, 2}
e := NewEncoder(ioutil.Discard)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
err := e.Encode(g)
if err != nil {
b.Fatalf("unexpected error: %v", err)
}
}
}
func BenchmarkEncode_LineString(b *testing.B) {
g := orb.LineString{
{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5},
{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5},
}
e := NewEncoder(ioutil.Discard)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
err := e.Encode(g)
if err != nil {
b.Fatalf("unexpected error: %v", err)
}
}
}
func compare(t testing.TB, e orb.Geometry, s int, b []byte) {
t.Helper()
// Decoder
g, srid, err := NewDecoder(bytes.NewReader(b)).Decode()
if err != nil {
t.Fatalf("decoder: read error: %v", err)
}
if !orb.Equal(g, e) {
t.Errorf("decoder: incorrect geometry: %v != %v", g, e)
}
if srid != s {
t.Errorf("decoder: incorrect srid: %v != %v", srid, s)
}
// Umarshal
g, srid, err = Unmarshal(b)
if err != nil {
t.Fatalf("unmarshal: read error: %v", err)
}
if !orb.Equal(g, e) {
t.Errorf("unmarshal: incorrect geometry: %v != %v", g, e)
}
if srid != s {
t.Errorf("decoder: incorrect srid: %v != %v", srid, s)
}
// Marshal
var data []byte
if b[0] == 0 {
data, err = Marshal(g, srid, binary.BigEndian)
} else {
data, err = Marshal(g, srid, binary.LittleEndian)
}
if err != nil {
t.Fatalf("marshal error: %v", err)
}
if !bytes.Equal(data, b) {
t.Logf("%v", data)
t.Logf("%v", b)
t.Errorf("marshal: incorrent encoding")
}
// Encode
buf := bytes.NewBuffer(nil)
en := NewEncoder(buf)
if b[0] == 0 {
en.SetByteOrder(binary.BigEndian)
} else {
en.SetByteOrder(binary.LittleEndian)
}
en.SetSRID(s)
err = en.Encode(e)
if err != nil {
t.Errorf("encode error: %v", err)
}
if !bytes.Equal(data, buf.Bytes()) {
t.Logf("%v", data)
t.Logf("%v", b)
t.Errorf("encode: incorrent encoding")
}
// pass in srid
buf.Reset()
en.SetSRID(10101)
err = en.Encode(e, s)
if err != nil {
t.Errorf("encode with srid error: %v", err)
}
if !bytes.Equal(data, buf.Bytes()) {
t.Logf("%v", data)
t.Logf("%v", b)
t.Errorf("encode with srid: incorrent encoding")
}
// preallocation
if l := wkbcommon.GeomLength(e, s != 0); len(data) != l {
t.Errorf("prealloc length: %v != %v", len(data), l)
}
}