-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlearn_test.go
343 lines (292 loc) · 7.59 KB
/
learn_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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package json
import (
"encoding/json"
"github.com/json-iterator/go"
"github.com/mailru/easyjson"
"github.com/zuoxinyu/jzon"
"reflect"
"testing"
)
var str = []byte(` {
"someSillyObj": { "nice": "waste of time" }, "Nested": { "Amazing"
: "yeah i know" },
"bad": "missing", "Food":
"i dont believe it wow", "EmptyList": [],
"Tags" : { "a": "lol", "b": "yay" } ,
"SurpriseMe": [ {}, "", "wow" ],
"SomeList": [ "yay", "huge suuuuuccess", "its big", "wow", "im amazed"],
"Name":"world" } `)
var str1 = []byte(` {
"bad": "missing", "Food":
"i dont believe it wow", "EmptyList": [],
"Tags" : { "a": "lol", "b": "yay" } ,
"SomeList": [ "yay", "huge suuuuuccess", "its big", "wow", "im amazed"
, " but with loads", "of", "strings", "in", "the", "list", "a", "b", "c",
"d", "e", "f", "g", "h" ] } `)
var str2 = []byte(` {
"someSillyObj": {}, "EmptyList": [],
"Tags" : { "a": "lol", "b": "yay", "c": "d", "e": "f", "g": "h", "i": "j" } ,
"Name":"world" } `)
var str3 = []byte(` {
"SurpriseMe": [ {}, "", "wow", "lots of",
["crazy stuff in", {"suddenly": "an object"}], {"here":"today"} ],
"Name":"world" } `)
var strOnlyMap = []byte(`
{ "Tags" : { "a": "lol", "b": "yay" }
}
`)
var strWithList = []byte(` {
"someSillyObj": { "nice": "waste of time" }, "Nested": { "Amazing"
: "yeah i know" },
"bad": "missing", "Food":
"i dont believe it wow", "EmptyList": [],
"SomeList": [ "yay", "huge suuuuuccess", "its big", "wow", "im amazed"],
"Name":"world"
} `)
func TestNumberInt(t *testing.T) {
var i int
iData := []byte(" 100 ")
t.Log(ReportPlan(&i))
t.Logf("before: %d", i)
Unmarshal(iData, &i)
t.Logf("want 100, got %d", i)
if i != 100 {
t.Fail()
}
}
func TestNilPtr(t *testing.T) {
var dst *string
Unmarshal([]byte(`"hi"`), dst)
}
func TestQuickScan(t *testing.T) {
ids := quickScan(str)
for _, id := range ids {
if id[2] == 1 {
t.Log(id, "fancy", string(str[id[0]:id[1]]))
} else {
t.Log(id, "raw", string(str[id[0]:id[1]]))
}
}
}
func TestInterface(t *testing.T) {
interfaceStr := []byte(`["hello"]`)
var oldJson, newJson interface{}
json.Unmarshal(interfaceStr, &oldJson)
Unmarshal(interfaceStr, &newJson)
t.Logf("old: \n%#v", oldJson)
t.Logf("new: \n%#v", newJson)
if !reflect.DeepEqual(oldJson, newJson) {
t.Error("different outcomes")
}
}
func TestCorrectnessMixed(t *testing.T) {
for _, str := range [][]byte{str, str1, str2, str3} {
obj := &testType{SomeList: []string{"im already here"}, Tags: map[string]string{"temp": "temp"}}
err := Unmarshal(str, obj)
obj2 := &testType{SomeList: []string{"im already here"}, Tags: map[string]string{"temp": "temp"}}
err2 := json.Unmarshal(str, obj2)
t.Logf("obj: \n%#v, err: %s", obj, err)
t.Logf("obj2: \n%#v, err2: %s", obj2, err2)
t.Logf("obj.Nested: \n%#v, err: %s", obj.Nested, err)
t.Logf("obj2.Nested: \n%#v, err2: %s", obj2.Nested, err2)
if !reflect.DeepEqual(obj, obj2) || !reflect.DeepEqual(err, err2) {
t.Error("different outcomes")
}
}
}
type BrokenObject struct {
// SomeTime time.Time
// SomeNumber int
// SomeChannel chan int
// SomeFunc func()
}
func TestReporting(t *testing.T) {
var obj *testType
d := ReportPlan(&obj)
t.Log(d.String())
{
var obj ***[][]string
d := ReportPlan(&obj)
t.Log(d.String())
}
{
var obj string
d := ReportPlan(&obj)
t.Log(d.String())
}
}
type simpleType struct {
Name string
}
func TestSimple(t *testing.T) {
var dst simpleType
src := []byte(`{"name": "dan"}`)
if err := Unmarshal(src, &dst); err != nil {
t.Error(err)
}
if dst.Name != "dan" {
t.Errorf(`name = %#v, expected "dan"`, dst.Name)
}
}
type nestedType struct {
ParentName string
SimpleType *simpleType
}
func TestNested(t *testing.T) {
var dst nestedType
src := []byte(`{"parentname": "libfor", "simpletype": {"name": "dan"}}`)
if err := Unmarshal(src, &dst); err != nil {
t.Error(err)
}
if dst.ParentName != "libfor" {
t.Errorf(`name = %#v, expected "libfor"`, dst.ParentName)
}
if dst.SimpleType == nil {
t.FailNow()
}
if dst.SimpleType.Name != "dan" {
t.Errorf(`simple.name = %#v, expected "dan"`, dst.SimpleType.Name)
}
}
func TestCorrectness3(t *testing.T) {
obj := &testType{SomeList: []string{"im already here"}, Tags: map[string]string{"temp": "temp"}}
err := Unmarshal(str3, obj)
obj2 := &testType{SomeList: []string{"im already here"}, Tags: map[string]string{"temp": "temp"}}
err2 := json.Unmarshal(str3, obj2)
t.Logf("obj: \n%#v, err: %s", obj, err)
t.Logf("obj2: \n%#v, err2: %s", obj2, err2)
t.Logf("obj.Nested: \n%#v, err: %s", obj.Nested, err)
t.Logf("obj2.Nested: \n%#v, err2: %s", obj2.Nested, err2)
if !reflect.DeepEqual(obj, obj2) || !reflect.DeepEqual(err, err2) {
t.Error("different outcomes")
}
}
var jsoni = jsoniter.ConfigFastest
func TestEasyUnmarshal(t *testing.T) {
obj := &easyType{SomeList: []string{"im already here"}, Tags: map[string]string{"temp": "temp"}}
err := easyjson.Unmarshal(str, obj)
t.Logf("obj: %#v, err: %s", obj, err)
if obj.Name != "world" {
t.Error("name is not right")
}
}
func BenchmarkQuickScan(b *testing.B) {
for i := 0; i < b.N; i++ {
quickScan(str)
}
}
func BenchmarkSerially_Libfor(b *testing.B) {
for i := 0; i < b.N; i++ {
t := &testType{SomeList: []string{"already in"}}
Unmarshal(strWithList, t)
}
}
func BenchmarkSerially_StdlibJson(b *testing.B) {
for i := 0; i < b.N; i++ {
t := &testType{SomeList: []string{"already in"}}
json.Unmarshal(strWithList, t)
}
}
func BenchmarkSerially_Iterjson(b *testing.B) {
for i := 0; i < b.N; i++ {
t := &testType{SomeList: []string{"already in"}}
jsoni.Unmarshal(strWithList, t)
}
}
func BenchmarkSerially_Easyjson(b *testing.B) {
for i := 0; i < b.N; i++ {
t := &easyType{SomeList: []string{"already in"}}
easyjson.Unmarshal(strWithList, t)
}
}
func BenchmarkSerially_Jzon(b *testing.B) {
for i := 0; i < b.N; i++ {
t := &testType{SomeList: []string{"already in"}}
jzon.Deserialize(strWithList, t)
}
}
func BenchmarkParallel_Libfor(b *testing.B) {
b.RunParallel(
func(pb *testing.PB) {
var t *testType
for pb.Next() {
t = &testType{}
Unmarshal(str, t)
t = &testType{}
Unmarshal(str1, t)
t = &testType{}
Unmarshal(str2, t)
t = &testType{}
Unmarshal(str3, t)
}
},
)
}
func BenchmarkParallel_StdlibJson(b *testing.B) {
b.RunParallel(
func(pb *testing.PB) {
var t *testType
for pb.Next() {
t = &testType{}
json.Unmarshal(str, t)
t = &testType{}
json.Unmarshal(str1, t)
t = &testType{}
json.Unmarshal(str2, t)
t = &testType{}
json.Unmarshal(str3, t)
}
},
)
}
func BenchmarkParallel_Iterjson(b *testing.B) {
b.RunParallel(
func(pb *testing.PB) {
var t *testType
for pb.Next() {
t = &testType{}
jsoni.Unmarshal(str, t)
t = &testType{}
jsoni.Unmarshal(str1, t)
t = &testType{}
jsoni.Unmarshal(str2, t)
t = &testType{}
jsoni.Unmarshal(str3, t)
}
},
)
}
func BenchmarkParallel_Easyjson(b *testing.B) {
b.RunParallel(
func(pb *testing.PB) {
var t *easyType
for pb.Next() {
t = &easyType{}
easyjson.Unmarshal(str, t)
t = &easyType{}
easyjson.Unmarshal(str1, t)
t = &easyType{}
easyjson.Unmarshal(str2, t)
t = &easyType{}
easyjson.Unmarshal(str3, t)
}
},
)
}
func BenchmarkParallel_Jzon(b *testing.B) {
b.RunParallel(
func(pb *testing.PB) {
var t *testType
for pb.Next() {
t = &testType{}
jzon.Deserialize(str, t)
t = &testType{}
jzon.Deserialize(str1, t)
t = &testType{}
jzon.Deserialize(str2, t)
t = &testType{}
jzon.Deserialize(str3, t)
}
},
)
}