-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
encoding_test.go
128 lines (102 loc) · 2.92 KB
/
encoding_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
package main
import (
"math"
"testing"
"github.com/qedus/osmpbf"
"github.com/stretchr/testify/assert"
)
func TestEncodingSimple(t *testing.T) {
var node = &osmpbf.Node{ID: 100, Lat: -50, Lon: 77}
var expectedBytes = []byte{0xc0, 0x49, 0x0, 0x0, 0x0, 0x0, 0x40, 0x53, 0x40, 0x0, 0x0, 0x0}
var expectedLatlon = map[string]string{"lon": "77.0000000", "lat": "-50.0000000"}
// encode
var stringid, byteval = nodeToBytes(node)
assert.Equal(t, "100", stringid)
assert.Equal(t, expectedBytes, byteval)
// decode
var latlon = bytesToLatLon(byteval)
assert.Equal(t, expectedLatlon, latlon)
}
func TestEncodingFloatPrecision(t *testing.T) {
var node = &osmpbf.Node{ID: 100, Lat: -50.555555555, Lon: 77.777777777}
var expectedBytes = []byte{0xc0, 0x49, 0x47, 0x1c, 0x71, 0xc5, 0x40, 0x53, 0x71, 0xc7, 0x1c, 0x70}
var expectedLatlon = map[string]string{"lon": "77.7777778", "lat": "-50.5555556"}
// encode
var stringid, byteval = nodeToBytes(node)
assert.Equal(t, "100", stringid)
assert.Equal(t, expectedBytes, byteval)
// decode
var latlon = bytesToLatLon(byteval)
assert.Equal(t, expectedLatlon, latlon)
}
func TestEncodingBitmaskValues(t *testing.T) {
var tags = map[string]string{"entrance": "main", "wheelchair": "yes"}
var node = &osmpbf.Node{ID: 100, Lat: -50, Lon: 77, Tags: tags}
var expectedBytes = []byte{0xc0, 0x49, 0x0, 0x0, 0x0, 0x0, 0x40, 0x53, 0x40, 0x0, 0x0, 0x0, 0xa0}
var expectedLatlon = map[string]string{"lon": "77.0000000", "lat": "-50.0000000", "entrance": "2", "wheelchair": "2"}
// encode
var stringid, byteval = nodeToBytes(node)
assert.Equal(t, "100", stringid)
assert.Equal(t, expectedBytes, byteval)
// decode
var latlon = bytesToLatLon(byteval)
assert.Equal(t, expectedLatlon, latlon)
}
func TestEncodingAndDecodingIdsToBytes(t *testing.T) {
var ids = []int64{0, 100, 100000, 100000000, math.MaxInt64}
// encode
var encoded = idSliceToBytes(ids)
// assert.Equal(t, "100", stringid)
// assert.Equal(t, expectedBytes, byteval)
// decode
var decoded = bytesToIDSlice(encoded)
assert.Equal(t, decoded, ids)
}
func BenchmarkBytesToLatLon(b *testing.B) {
node := &osmpbf.Node{
ID: 123,
Lat: 12.1234,
Lon: -122.1234,
Tags: map[string]string{
"entrance": "main",
},
}
_, data := nodeToBytes(node)
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
bytesToLatLon(data)
}
}
func BenchmarkNodeToBytes(b *testing.B) {
node := &osmpbf.Node{
ID: 123,
Lat: 12.1234,
Lon: -122.1234,
Tags: map[string]string{
"entrance": "main",
},
}
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
nodeToBytes(node)
}
}
func BenchmarkIdSliceToBytes(b *testing.B) {
ids := make([]int64, 100)
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
idSliceToBytes(ids)
}
}
func BenchmarkBytesToIDSlice(b *testing.B) {
ids := make([]int64, 100)
data := idSliceToBytes(ids)
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
bytesToIDSlice(data)
}
}