forked from ugorji/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalues_test.go
418 lines (347 loc) · 11.3 KB
/
values_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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
// comment this out // + build testing
// Copyright (c) 2012-2020 Ugorji Nwoke. All rights reserved.
// Use of this source code is governed by a MIT license found in the LICENSE file.
package codec
// This file contains values used by tests and benchmarks.
// The benchmarks will test performance against other libraries
// (encoding/json, json-iterator, bson, gob, etc).
// Consequently, we only use values that will parse well in all engines,
// and only leverage features that work across multiple libraries for a truer comparison.
// For example,
// - JSON/BSON do not like maps with keys that are not strings,
// so we only use maps with string keys here.
// - _struct options are not honored by other libraries,
// so we don't use them in this file.
import (
"math"
"strconv"
"strings"
)
// func init() {
// rt := reflect.TypeOf((*TestStruc)(nil)).Elem()
// defTypeInfos.get(rt2id(rt), rt)
// }
const numStrUi64T = 32 // use 8, prefer 32, test with 1024
type wrapSliceUint64 []uint64
type wrapSliceString []string
type wrapUint64 uint64
type wrapString string
type wrapUint64Slice []wrapUint64
type wrapStringSlice []wrapString
// some other types
type stringUint64T struct {
S string
U uint64
}
type AnonInTestStruc struct {
AS string
AI64 int64
AI16 int16
AUi64 uint64
ASslice []string
AI64slice []int64
AUi64slice []uint64
AF64slice []float64
AF32slice []float32
// AMI32U32 map[int32]uint32
// AMU32F64 map[uint32]float64 // json/bson do not like it
AMSU16 map[string]uint16
// use these to test 0-len or nil slices/maps/arrays
AI64arr0 [0]int64
AI64slice0 []int64
AUi64sliceN []uint64
AMSU16N map[string]uint16
AMSU16E map[string]uint16
}
// testSimpleFields is a sub-set of TestStrucCommon
type testSimpleFields struct {
S string
I64 int64
I8 int8
Ui64 uint64
Ui8 uint8
F64 float64
F32 float32
B bool
Sslice []string
I16slice []int16
Ui64slice []uint64
Ui8slice []uint8
Bslice []bool
Iptrslice []*int64
WrapSliceInt64 wrapSliceUint64
WrapSliceString wrapSliceString
Msi64 map[string]int64
}
type TestStrucCommon struct {
S string
I64 int64
I32 int32
I16 int16
I8 int8
I64n int64
I32n int32
I16n int16
I8n int8
Ui64 uint64
Ui32 uint32
Ui16 uint16
Ui8 uint8
F64 float64
F32 float32
B bool
By uint8 // byte: msgp doesn't like byte
Sslice []string
I64slice []int64
I16slice []int16
Ui64slice []uint64
Ui8slice []uint8
Bslice []bool
Byslice []byte
BytesSlice [][]byte
Iptrslice []*int64
WrapSliceInt64 wrapSliceUint64
WrapSliceString wrapSliceString
Msi64 map[string]int64
Msbytes map[string][]byte
Simplef testSimpleFields
SstrUi64T []stringUint64T
MstrUi64T map[string]*stringUint64T
AnonInTestStruc
NotAnon AnonInTestStruc
// R Raw // Testing Raw must be explicitly turned on, so use standalone test
// Rext RawExt // Testing RawExt is tricky, so use standalone test
Nmap map[string]bool //don't set this, so we can test for nil
Nslice []byte //don't set this, so we can test for nil
Nint64 *int64 //don't set this, so we can test for nil
}
type TestStruc struct {
// _struct struct{} `json:",omitempty"` //set omitempty for every field
TestStrucCommon
Mtsptr map[string]*TestStruc
Mts map[string]TestStruc
Its []*TestStruc
Nteststruc *TestStruc
}
func populateTestStrucCommon(ts *TestStrucCommon, n int, bench, useInterface, useStringKeyOnly bool) {
var i64a, i64b, i64c, i64d int64 = 64, 6464, 646464, 64646464
// if bench, do not use uint64 values > math.MaxInt64, as bson, etc cannot decode them
var a = AnonInTestStruc{
// There's more leeway in altering this.
AS: strRpt(n, "A-String"),
AI64: -64646464,
AI16: 1616,
AUi64: 64646464,
// (U+1D11E)G-clef character may be represented in json as "\uD834\uDD1E".
// single reverse solidus character may be represented in json as "\u005C".
// include these in ASslice below.
ASslice: []string{
strRpt(n, "Aone"),
strRpt(n, "Atwo"),
strRpt(n, "Athree"),
strRpt(n, "Afour.reverse_solidus.\u005c"),
strRpt(n, "Afive.Gclef.\U0001d11E\"ugorji\"done.")},
AI64slice: []int64{
0, 1, -1, -22, 333, -4444, 55555, -666666,
// msgpack ones
-48, -32, -24, -8, 32, 127, 192, 255,
// standard ones
0, -1, 1,
math.MaxInt8, math.MaxInt8 + 4, math.MaxInt8 - 4,
math.MaxInt16, math.MaxInt16 + 4, math.MaxInt16 - 4,
math.MaxInt32, math.MaxInt32 + 4, math.MaxInt32 - 4,
math.MaxInt64, math.MaxInt64 - 4,
math.MinInt8, math.MinInt8 + 4, math.MinInt8 - 4,
math.MinInt16, math.MinInt16 + 4, math.MinInt16 - 4,
math.MinInt32, math.MinInt32 + 4, math.MinInt32 - 4,
math.MinInt64, math.MinInt64 + 4,
},
AUi64slice: []uint64{
0, 1, 22, 333, 4444, 55555, 666666,
// standard ones
math.MaxUint8, math.MaxUint8 + 4, math.MaxUint8 - 4,
math.MaxUint16, math.MaxUint16 + 4, math.MaxUint16 - 4,
math.MaxUint32, math.MaxUint32 + 4, math.MaxUint32 - 4,
},
AMSU16: map[string]uint16{strRpt(n, "1"): 1, strRpt(n, "22"): 2, strRpt(n, "333"): 3, strRpt(n, "4444"): 4},
// Note: +/- inf, NaN, and other non-representable numbers should not be explicitly tested here
AF64slice: []float64{
11.11e-11, -11.11e+11,
2.222e+12, -2.222e-12,
-555.55e-5, 555.55e+5,
666.66e-6, -666.66e+6,
7777.7777e-7, -7777.7777e-7,
-8888.8888e+8, 8888.8888e+8,
-99999.9999e+9, 99999.9999e+9,
// these below are hairy enough to need strconv.ParseFloat
33.33e-33, -33.33e+33,
44.44e+44, -44.44e-44,
// standard ones
0, -1, 1,
// math.Inf(1), math.Inf(-1),
math.Pi, math.Phi, math.E,
math.MaxFloat64, math.SmallestNonzeroFloat64,
},
AF32slice: []float32{
11.11e-1, -11.11e+1,
2.222e+2, -2.222e-2,
-55.55e-5, 55.55e+5,
66.66e-6, -66.66e+6,
777.777e-7, -777.777e-7,
-8.88e+8, 8.88e-8,
-99999.9999e+9, 99999.9999e+9,
// these below are hairy enough to need strconv.ParseFloat
33.33e-33, -33.33e+33,
// standard ones
0, -1, 1,
// math.Float32frombits(0x7FF00000), math.Float32frombits(0xFFF00000), //+inf and -inf
math.MaxFloat32, math.SmallestNonzeroFloat32,
},
AI64slice0: []int64{},
AUi64sliceN: nil,
AMSU16N: nil,
AMSU16E: map[string]uint16{},
}
if !bench {
a.AUi64slice = append(a.AUi64slice, math.MaxUint64, math.MaxUint64-4)
}
*ts = TestStrucCommon{
S: strRpt(n, `some really really cool names that are nigerian and american like "ugorji melody nwoke" - get it? `),
// set the numbers close to the limits
I8: math.MaxInt8 * 2 / 3, // 8,
I8n: math.MinInt8 * 2 / 3, // 8,
I16: math.MaxInt16 * 2 / 3, // 16,
I16n: math.MinInt16 * 2 / 3, // 16,
I32: math.MaxInt32 * 2 / 3, // 32,
I32n: math.MinInt32 * 2 / 3, // 32,
I64: math.MaxInt64 * 2 / 3, // 64,
I64n: math.MinInt64 * 2 / 3, // 64,
Ui64: math.MaxUint64 * 2 / 3, // 64
Ui32: math.MaxUint32 * 2 / 3, // 32
Ui16: math.MaxUint16 * 2 / 3, // 16
Ui8: math.MaxUint8 * 2 / 3, // 8
F32: 3.402823e+38, // max representable float32 without losing precision
F64: 3.40281991833838838338e+53,
B: true,
By: 5,
Sslice: []string{strRpt(n, "one"), strRpt(n, "two"), strRpt(n, "three")},
I64slice: []int64{1111, 2222, 3333},
I16slice: []int16{44, 55, 66},
Ui64slice: []uint64{12121212, 34343434, 56565656},
Ui8slice: []uint8{210, 211, 212},
Bslice: []bool{true, false, true, false},
Byslice: []byte{13, 14, 15},
BytesSlice: [][]byte{
[]byte(strRpt(n, "one")),
[]byte(strRpt(n, "two")),
[]byte(strRpt(n, "\"three\"")),
},
Msi64: map[string]int64{
strRpt(n, "one"): 1,
strRpt(n, "two"): 2,
strRpt(n, "\"three\""): 3,
},
Msbytes: map[string][]byte{
strRpt(n, "one"): []byte(strRpt(n, "one")),
strRpt(n, "two"): []byte(strRpt(n, "two")),
strRpt(n, "\"three\""): []byte(strRpt(n, "\"three\"")),
},
WrapSliceInt64: []uint64{4, 16, 64, 256},
WrapSliceString: []string{strRpt(n, "4"), strRpt(n, "16"), strRpt(n, "64"), strRpt(n, "256")},
// R: Raw([]byte("goodbye")),
// Rext: RawExt{ 120, []byte("hello"), }, // MARKER: don't set this - it's hard to test
// make Simplef same as top-level
// MARKER: should this have slightly different values???
Simplef: testSimpleFields{
S: strRpt(n, `some really really cool names that are nigerian and american like "ugorji melody nwoke" - get it? `),
// set the numbers close to the limits
I8: math.MaxInt8 * 2 / 3, // 8,
I64: math.MaxInt64 * 2 / 3, // 64,
Ui64: math.MaxUint64 * 2 / 3, // 64
Ui8: math.MaxUint8 * 2 / 3, // 8
F32: 3.402823e+38, // max representable float32 without losing precision
F64: 3.40281991833838838338e+53,
B: true,
Sslice: []string{strRpt(n, "one"), strRpt(n, "two"), strRpt(n, "three")},
I16slice: []int16{44, 55, 66},
Ui64slice: []uint64{12121212, 34343434, 56565656},
Ui8slice: []uint8{210, 211, 212},
Bslice: []bool{true, false, true, false},
Msi64: map[string]int64{
strRpt(n, "one"): 1,
strRpt(n, "two"): 2,
strRpt(n, "\"three\""): 3,
},
WrapSliceInt64: []uint64{4, 16, 64, 256},
WrapSliceString: []string{strRpt(n, "4"), strRpt(n, "16"), strRpt(n, "64"), strRpt(n, "256")},
},
SstrUi64T: make([]stringUint64T, numStrUi64T), // {{"1", 1}, {"2", 2}, {"3", 3}, {"4", 4}},
MstrUi64T: make(map[string]*stringUint64T, numStrUi64T),
AnonInTestStruc: a,
NotAnon: a,
}
for i := uint64(0); i < numStrUi64T; i++ {
ss := strings.Repeat(strconv.FormatUint(i, 10), int(i)) // 4)
ts.SstrUi64T[i] = stringUint64T{S: ss, U: i}
ts.MstrUi64T[ss] = &ts.SstrUi64T[i]
}
if bench {
ts.Ui64 = math.MaxInt64 * 2 / 3
ts.Simplef.Ui64 = ts.Ui64
}
//For benchmarks, some things will not work.
if !bench {
//json and bson require string keys in maps
//ts.M = map[interface{}]interface{}{
// true: "true",
// int8(9): false,
//}
//gob cannot encode nil in element in array (encodeArray: nil element)
ts.Iptrslice = []*int64{nil, &i64a, nil, &i64b, nil, &i64c, nil, &i64d, nil}
// ts.Iptrslice = nil
}
if !useStringKeyOnly {
var _ byte = 0 // so this empty branch doesn't flag a warning
// ts.AnonInTestStruc.AMU32F64 = map[uint32]float64{1: 1, 2: 2, 3: 3} // Json/Bson barf
}
}
func newTestStruc(depth, n int, bench, useInterface, useStringKeyOnly bool) (ts *TestStruc) {
ts = &TestStruc{}
populateTestStrucCommon(&ts.TestStrucCommon, n, bench, useInterface, useStringKeyOnly)
if depth > 0 {
depth--
if ts.Mtsptr == nil {
ts.Mtsptr = make(map[string]*TestStruc)
}
if ts.Mts == nil {
ts.Mts = make(map[string]TestStruc)
}
ts.Mtsptr[strRpt(n, "0")] = newTestStruc(depth, n, bench, useInterface, useStringKeyOnly)
ts.Mts[strRpt(n, "0")] = *(ts.Mtsptr[strRpt(n, "0")])
ts.Its = append(ts.Its, ts.Mtsptr[strRpt(n, "0")])
}
return
}
var testStrRptMap = make(map[int]map[string]string)
func strRpt(n int, s string) string {
if false {
// fmt.Printf(">>>> calling strings.Repeat on n: %d, key: %s\n", n, s)
return strings.Repeat(s, n)
}
m1, ok := testStrRptMap[n]
if !ok {
// fmt.Printf(">>>> making new map for n: %v\n", n)
m1 = make(map[string]string)
testStrRptMap[n] = m1
}
v1, ok := m1[s]
if !ok {
// fmt.Printf(">>>> creating new entry for key: %s\n", s)
v1 = strings.Repeat(s, n)
m1[s] = v1
}
return v1
}
// func wstrRpt(n int, s string) wrapBytes {
// return wrapBytes(bytes.Repeat([]byte(s), n))
// }