forked from paulmach/orb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_test.go
226 lines (195 loc) · 4.97 KB
/
json_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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package orb
import (
"encoding/json"
"testing"
)
func TestPointJSON(t *testing.T) {
p1 := Point{1, 2.1}
data, err := json.Marshal(p1)
if err != nil {
t.Errorf("should marshal just fine: %v", err)
}
if string(data) != "[1,2.1]" {
t.Errorf("incorrect json: %v", string(data))
}
var p2 Point
err = json.Unmarshal(data, &p2)
if err != nil {
t.Errorf("should unmarshal just fine: %v", err)
}
if !p1.Equal(p2) {
t.Errorf("not equal: %v", p2)
}
}
func TestLineStringJSON(t *testing.T) {
ls1 := LineString{{1.5, 2.5}, {3.5, 4.5}, {5.5, 6.5}}
data, err := json.Marshal(ls1)
if err != nil {
t.Fatalf("should marshal just fine: %v", err)
}
if string(data) != "[[1.5,2.5],[3.5,4.5],[5.5,6.5]]" {
t.Errorf("incorrect data: %v", string(data))
}
var ls2 LineString
err = json.Unmarshal(data, &ls2)
if err != nil {
t.Fatalf("should unmarshal just fine: %v", err)
}
if !ls1.Equal(ls2) {
t.Errorf("unmarshal not equal: %v", ls2)
}
// empty line
ls1 = LineString{}
data, err = json.Marshal(ls1)
if err != nil {
t.Errorf("should marshal just fine: %v", err)
}
if string(data) != "[]" {
t.Errorf("incorrect json: %v", string(data))
}
}
var pointData = []byte(`[-81.60540868,41.51522539]`)
var lineStringShortData = []byte(`[
[-81.60540868,41.51522539],[-81.6049915,41.51523057],
[-81.60499258,41.51528156],[-81.60506867,41.51528061],
[-81.60540868,41.51522539]
]`)
var lineStringLongData = []byte(`[
[-81.60540868,41.51522539],[-81.6049915,41.51523057],
[-81.60499258,41.51528156],[-81.60506867,41.51528061],
[-81.60507145,41.5154027],[-81.60500219,41.51540351],
[-81.60500345,41.51545786],[-81.60529819,41.51545416],
[-81.60530133,41.51559117],[-81.60487193,41.51559662],
[-81.60486062,41.51508837],[-81.60521401,41.515084],
[-81.60521123,41.51495707],[-81.60480788,41.51496219],
[-81.6047998,41.51460138],[-81.60496455,41.51459936],
[-81.60496778,41.51474909],[-81.6053979,41.51474371],
[-81.60540868,41.51522539]
]`)
var polygonShortData = append(append([]byte{'['}, lineStringShortData...), byte(']'))
var polygonLongData = append(append([]byte{'['}, lineStringLongData...), byte(']'))
func BenchmarkPointMarshalJSON(b *testing.B) {
p := Point{}
if err := json.Unmarshal(pointData, &p); err != nil {
b.Fatalf("unable to unmarshal: %v", err)
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := json.Marshal(p)
if err != nil {
b.Fatalf("marshal error: %v", err)
}
}
}
func BenchmarkPointUnmarshalJSON(b *testing.B) {
p := Point{}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
err := json.Unmarshal(pointData, &p)
if err != nil {
b.Fatalf("marshal error: %v", err)
}
}
}
func BenchmarkLineStringMarshalJSON_short(b *testing.B) {
ls := LineString{}
if err := json.Unmarshal(lineStringShortData, &ls); err != nil {
b.Fatalf("unable to unmarshal: %v", err)
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := json.Marshal(ls)
if err != nil {
b.Fatalf("marshal error: %v", err)
}
}
}
func BenchmarkLineStringUnmarshalJSON_short(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
ls := LineString{}
err := json.Unmarshal(lineStringShortData, &ls)
if err != nil {
b.Fatalf("marshal error: %v", err)
}
}
}
func BenchmarkLineStringMarshalJSON_long(b *testing.B) {
ls := LineString{}
if err := json.Unmarshal(lineStringLongData, &ls); err != nil {
b.Fatalf("unable to unmarshal: %v", err)
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := json.Marshal(ls)
if err != nil {
b.Fatalf("marshal error: %v", err)
}
}
}
func BenchmarkLineStringUnmarshalJSON_long(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
ls := LineString{}
err := json.Unmarshal(lineStringLongData, &ls)
if err != nil {
b.Fatalf("marshal error: %v", err)
}
}
}
func BenchmarkPolygonMarshalJSON_short(b *testing.B) {
p := Polygon{}
if err := json.Unmarshal(polygonShortData, &p); err != nil {
b.Fatalf("unable to unmarshal: %v", err)
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := json.Marshal(p)
if err != nil {
b.Fatalf("marshal error: %v", err)
}
}
}
func BenchmarkPolygonUnmarshalJSON_short(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
p := Polygon{}
err := json.Unmarshal(polygonShortData, &p)
if err != nil {
b.Fatalf("marshal error: %v", err)
}
}
}
func BenchmarkPolygonMarshalJSON_long(b *testing.B) {
p := Polygon{}
if err := json.Unmarshal(polygonLongData, &p); err != nil {
b.Fatalf("unable to unmarshal: %v", err)
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := json.Marshal(p)
if err != nil {
b.Fatalf("marshal error: %v", err)
}
}
}
func BenchmarkPolygonUnmarshalJSON_long(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
p := Polygon{}
err := json.Unmarshal(polygonLongData, &p)
if err != nil {
b.Fatalf("marshal error: %v", err)
}
}
}