1// Code generated by Makefile, DO NOT EDIT.
2
3/*
4 * Copyright 2021 ByteDance Inc.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19package {{PACKAGE}}
20
21import (
22 `encoding/hex`
23 `fmt`
24 `math`
25 `strings`
26 `testing`
27 `unsafe`
28
29 `github.com/bytedance/sonic/internal/native/types`
30 `github.com/bytedance/sonic/internal/rt`
31 `github.com/davecgh/go-spew/spew`
32 `github.com/stretchr/testify/assert`
33 `github.com/stretchr/testify/require`
34)
35
36func TestNative_Value(t *testing.T) {
37 var v types.JsonState
38 s := ` -12345`
39 p := (*rt.GoString)(unsafe.Pointer(&s))
40 x := value(p.Ptr, p.Len, 0, &v, 0)
41 assert.Equal(t, 9, x)
42 assert.Equal(t, types.V_INTEGER, v.Vt)
43 assert.Equal(t, int64(-12345), v.Iv)
44 assert.Equal(t, 3, v.Ep)
45}
46
47func TestNative_Value_OutOfBound(t *testing.T) {
48 var v types.JsonState
49 mem := []byte{'"', '"'}
50 s := rt.Mem2Str(mem[:1])
51 p := (*rt.GoString)(unsafe.Pointer(&s))
52 x := value(p.Ptr, p.Len, 0, &v, 0)
53 assert.Equal(t, 1, x)
54 assert.Equal(t, -int(types.ERR_EOF), int(v.Vt))
55}
56
57func TestNative_Quote(t *testing.T) {
58 s := "hello\b\f\n\r\t\\\"\u666fworld"
59 d := make([]byte, 256)
60 dp := (*rt.GoSlice)(unsafe.Pointer(&d))
61 sp := (*rt.GoString)(unsafe.Pointer(&s))
62 rv := quote(sp.Ptr, sp.Len, dp.Ptr, &dp.Len, 0)
63 if rv < 0 {
64 require.NoError(t, types.ParsingError(-rv))
65 }
66 assert.Equal(t, len(s), rv)
67 assert.Equal(t, 35, len(d))
68 assert.Equal(t, `hello\u0008\u000c\n\r\t\\\"景world`, string(d))
69}
70
71func TestNative_QuoteNoMem(t *testing.T) {
72 s := "hello\b\f\n\r\t\\\"\u666fworld"
73 d := make([]byte, 10)
74 dp := (*rt.GoSlice)(unsafe.Pointer(&d))
75 sp := (*rt.GoString)(unsafe.Pointer(&s))
76 rv := quote(sp.Ptr, sp.Len, dp.Ptr, &dp.Len, 0)
77 assert.Equal(t, -6, rv)
78 assert.Equal(t, 5, len(d))
79 assert.Equal(t, `hello`, string(d))
80}
81
82func TestNative_DoubleQuote(t *testing.T) {
83 s := "hello\b\f\n\r\t\\\"\u666fworld"
84 d := make([]byte, 256)
85 dp := (*rt.GoSlice)(unsafe.Pointer(&d))
86 sp := (*rt.GoString)(unsafe.Pointer(&s))
87 rv := quote(sp.Ptr, sp.Len, dp.Ptr, &dp.Len, types.F_DOUBLE_UNQUOTE)
88 if rv < 0 {
89 require.NoError(t, types.ParsingError(-rv))
90 }
91 assert.Equal(t, len(s), rv)
92 assert.Equal(t, 44, len(d))
93 assert.Equal(t, `hello\\u0008\\u000c\\n\\r\\t\\\\\\\"景world`, string(d))
94}
95
96func TestNative_Unquote(t *testing.T) {
97 s := `hello\b\f\n\r\t\\\"\u2333world`
98 d := make([]byte, 0, len(s))
99 ep := -1
100 dp := (*rt.GoSlice)(unsafe.Pointer(&d))
101 sp := (*rt.GoString)(unsafe.Pointer(&s))
102 rv := unquote(sp.Ptr, sp.Len, dp.Ptr, &ep, 0)
103 if rv < 0 {
104 require.NoError(t, types.ParsingError(-rv))
105 }
106 dp.Len = rv
107 assert.Equal(t, -1, ep)
108 assert.Equal(t, "hello\b\f\n\r\t\\\"\u2333world", string(d))
109}
110
111func TestNative_UnquoteError(t *testing.T) {
112 s := `asdf\`
113 d := make([]byte, 0, len(s))
114 ep := -1
115 dp := (*rt.GoSlice)(unsafe.Pointer(&d))
116 sp := (*rt.GoString)(unsafe.Pointer(&s))
117 rv := unquote(sp.Ptr, sp.Len, dp.Ptr, &ep, 0)
118 assert.Equal(t, -int(types.ERR_EOF), rv)
119 assert.Equal(t, 5, ep)
120 s = `asdf\gqwer`
121 d = make([]byte, 0, len(s))
122 ep = -1
123 dp = (*rt.GoSlice)(unsafe.Pointer(&d))
124 sp = (*rt.GoString)(unsafe.Pointer(&s))
125 rv = unquote(sp.Ptr, sp.Len, dp.Ptr, &ep, 0)
126 assert.Equal(t, -int(types.ERR_INVALID_ESCAPE), rv)
127 assert.Equal(t, 5, ep)
128 s = `asdf\u1gggqwer`
129 d = make([]byte, 0, len(s))
130 ep = -1
131 dp = (*rt.GoSlice)(unsafe.Pointer(&d))
132 sp = (*rt.GoString)(unsafe.Pointer(&s))
133 rv = unquote(sp.Ptr, sp.Len, dp.Ptr, &ep, 0)
134 assert.Equal(t, -int(types.ERR_INVALID_CHAR), rv)
135 assert.Equal(t, 7, ep)
136 s = `asdf\ud800qwer`
137 d = make([]byte, 0, len(s))
138 ep = -1
139 dp = (*rt.GoSlice)(unsafe.Pointer(&d))
140 sp = (*rt.GoString)(unsafe.Pointer(&s))
141 rv = unquote(sp.Ptr, sp.Len, dp.Ptr, &ep, 0)
142 assert.Equal(t, -int(types.ERR_INVALID_UNICODE), rv)
143 assert.Equal(t, 6, ep)
144 s = `asdf\\ud800qwer`
145 d = make([]byte, 0, len(s))
146 ep = -1
147 dp = (*rt.GoSlice)(unsafe.Pointer(&d))
148 sp = (*rt.GoString)(unsafe.Pointer(&s))
149 rv = unquote(sp.Ptr, sp.Len, dp.Ptr, &ep, types.F_DOUBLE_UNQUOTE)
150 assert.Equal(t, -int(types.ERR_INVALID_UNICODE), rv)
151 assert.Equal(t, 7, ep)
152 s = `asdf\ud800\ud800qwer`
153 d = make([]byte, 0, len(s))
154 ep = -1
155 dp = (*rt.GoSlice)(unsafe.Pointer(&d))
156 sp = (*rt.GoString)(unsafe.Pointer(&s))
157 rv = unquote(sp.Ptr, sp.Len, dp.Ptr, &ep, 0)
158 assert.Equal(t, -int(types.ERR_INVALID_UNICODE), rv)
159 assert.Equal(t, 12, ep)
160 s = `asdf\\ud800\\ud800qwer`
161 d = make([]byte, 0, len(s))
162 ep = -1
163 dp = (*rt.GoSlice)(unsafe.Pointer(&d))
164 sp = (*rt.GoString)(unsafe.Pointer(&s))
165 rv = unquote(sp.Ptr, sp.Len, dp.Ptr, &ep, types.F_DOUBLE_UNQUOTE)
166 assert.Equal(t, -int(types.ERR_INVALID_UNICODE), rv)
167 assert.Equal(t, 14, ep)
168}
169
170func TestNative_DoubleUnquote(t *testing.T) {
171 s := `hello\\b\\f\\n\\r\\t\\\\\\\"\\u2333world`
172 d := make([]byte, 0, len(s))
173 ep := -1
174 dp := (*rt.GoSlice)(unsafe.Pointer(&d))
175 sp := (*rt.GoString)(unsafe.Pointer(&s))
176 rv := unquote(sp.Ptr, sp.Len, dp.Ptr, &ep, types.F_DOUBLE_UNQUOTE)
177 if rv < 0 {
178 require.NoError(t, types.ParsingError(-rv))
179 }
180 dp.Len = rv
181 assert.Equal(t, -1, ep)
182 assert.Equal(t, "hello\b\f\n\r\t\\\"\u2333world", string(d))
183}
184
185func TestNative_UnquoteUnicodeReplacement(t *testing.T) {
186 s := `hello\ud800world`
187 d := make([]byte, 0, len(s))
188 ep := -1
189 dp := (*rt.GoSlice)(unsafe.Pointer(&d))
190 sp := (*rt.GoString)(unsafe.Pointer(&s))
191 rv := unquote(sp.Ptr, sp.Len, dp.Ptr, &ep, types.F_UNICODE_REPLACE)
192 if rv < 0 {
193 require.NoError(t, types.ParsingError(-rv))
194 }
195 dp.Len = rv
196 assert.Equal(t, -1, ep)
197 assert.Equal(t, "hello\ufffdworld", string(d))
198 s = `hello\ud800\ud800world`
199 d = make([]byte, 0, len(s))
200 ep = -1
201 dp = (*rt.GoSlice)(unsafe.Pointer(&d))
202 sp = (*rt.GoString)(unsafe.Pointer(&s))
203 rv = unquote(sp.Ptr, sp.Len, dp.Ptr, &ep, types.F_UNICODE_REPLACE)
204 if rv < 0 {
205 require.NoError(t, types.ParsingError(-rv))
206 }
207 dp.Len = rv
208 assert.Equal(t, -1, ep)
209 assert.Equal(t, "hello\ufffd\ufffdworld", string(d))
210}
211
212func TestNative_HTMLEscape(t *testing.T) {
213 s := "hello\u2029\u2028<&>world"
214 d := make([]byte, 256)
215 dp := (*rt.GoSlice)(unsafe.Pointer(&d))
216 sp := (*rt.GoString)(unsafe.Pointer(&s))
217 rv := html_escape(sp.Ptr, sp.Len, dp.Ptr, &dp.Len)
218 if rv < 0 {
219 require.NoError(t, types.ParsingError(-rv))
220 }
221 assert.Equal(t, len(s), rv)
222 assert.Equal(t, 40, len(d))
223 assert.Equal(t, `hello\u2029\u2028\u003c\u0026\u003eworld`, string(d))
224}
225
226func TestNative_HTMLEscapeNoMem(t *testing.T) {
227 s := "hello\u2029\u2028<&>world"
228 d := make([]byte, 10)
229 dp := (*rt.GoSlice)(unsafe.Pointer(&d))
230 sp := (*rt.GoString)(unsafe.Pointer(&s))
231 rv := html_escape(sp.Ptr, sp.Len, dp.Ptr, &dp.Len)
232 assert.Equal(t, -6, rv)
233 assert.Equal(t, 5, len(d))
234 assert.Equal(t, `hello`, string(d))
235}
236
237func TestNative_Vstring(t *testing.T) {
238 var v types.JsonState
239 i := 0
240 s := `test"test\n2"`
241 vstring(&s, &i, &v, 0)
242 assert.Equal(t, 5, i)
243 assert.Equal(t, -1, v.Ep)
244 assert.Equal(t, int64(0), v.Iv)
245 vstring(&s, &i, &v, 0)
246 assert.Equal(t, 13, i)
247 assert.Equal(t, 9, v.Ep)
248 assert.Equal(t, int64(5), v.Iv)
249}
250
251func TestNative_Vstring_ValidUnescapedChars(t *testing.T) {
252 var v types.JsonState
253 valid := uint64(types.F_VALIDATE_STRING)
254 i := 0
255 s := "test\x1f\""
256 vstring(&s, &i, &v, valid)
257 assert.Equal(t, -int(types.ERR_INVALID_CHAR), int(v.Vt))
258}
259
260func TestNative_VstringEscapeEOF(t *testing.T) {
261 var v types.JsonState
262 i := 0
263 s := `xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\"xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"x`
264 vstring(&s, &i, &v, 0)
265 assert.Equal(t, 95, i)
266 assert.Equal(t, 63, v.Ep)
267 assert.Equal(t, int64(0), v.Iv)
268}
269
270func TestNative_VstringHangUpOnRandomData(t *testing.T) {
271 v, e := hex.DecodeString(
272 "228dc61efd54ef80a908fb6026b7f2d5f92a257ba8b347c995f259eb8685376a" +
273 "8c4500262d9c308b3f3ec2577689cf345d9f86f9b5d18d3e463bec5c22df2d2e" +
274 "4506010eba1dae7278",
275 )
276 assert.Nil(t, e)
277 p := 1
278 s := rt.Mem2Str(v)
279 var js types.JsonState
280 vstring(&s, &p, &js, 0)
281 fmt.Printf("js: %s\n", spew.Sdump(js))
282}
283
284func TestNative_Vnumber(t *testing.T) {
285 var v types.JsonState
286 i := 0
287 s := "1234"
288 vnumber(&s, &i, &v)
289 assert.Equal(t, 4, i)
290 assert.Equal(t, 0, v.Ep)
291 assert.Equal(t, int64(1234), v.Iv)
292 assert.Equal(t, types.V_INTEGER, v.Vt)
293 i = 0
294 s = "1.234"
295 vnumber(&s, &i, &v)
296 assert.Equal(t, 5, i)
297 assert.Equal(t, 0, v.Ep)
298 assert.Equal(t, 1.234, v.Dv)
299 assert.Equal(t, types.V_DOUBLE, v.Vt)
300 i = 0
301 s = "1.234e5"
302 vnumber(&s, &i, &v)
303 assert.Equal(t, 7, i)
304 assert.Equal(t, 0, v.Ep)
305 assert.Equal(t, 1.234e5, v.Dv)
306 assert.Equal(t, types.V_DOUBLE, v.Vt)
307 i = 0
308 s = "0.0125"
309 vnumber(&s, &i, &v)
310 assert.Equal(t, 6, i)
311 assert.Equal(t, 0, v.Ep)
312 assert.Equal(t, 0.0125, v.Dv)
313 assert.Equal(t, types.V_DOUBLE, v.Vt)
314 i = 0
315 s = "100000000000000000000"
316 vnumber(&s, &i, &v)
317 assert.Equal(t, 21, i)
318 assert.Equal(t, 0, v.Ep)
319 assert.Equal(t, 100000000000000000000.0, v.Dv)
320 assert.Equal(t, types.V_DOUBLE, v.Vt)
321 i = 0
322 s = "999999999999999900000"
323 vnumber(&s, &i, &v)
324 assert.Equal(t, 21, i)
325 assert.Equal(t, 0, v.Ep)
326 assert.Equal(t, 999999999999999900000.0, v.Dv)
327 assert.Equal(t, types.V_DOUBLE, v.Vt)
328 i = 0
329 s = "-1.234"
330 vnumber(&s, &i, &v)
331 assert.Equal(t, 6, i)
332 assert.Equal(t, 0, v.Ep)
333 assert.Equal(t, -1.234, v.Dv)
334 assert.Equal(t, types.V_DOUBLE, v.Vt)
335}
336
337func TestNative_Vsigned(t *testing.T) {
338 var v types.JsonState
339 i := 0
340 s := "1234"
341 vsigned(&s, &i, &v)
342 assert.Equal(t, 4, i)
343 assert.Equal(t, 0, v.Ep)
344 assert.Equal(t, int64(1234), v.Iv)
345 assert.Equal(t, types.V_INTEGER, v.Vt)
346 i = 0
347 s = "-1234"
348 vsigned(&s, &i, &v)
349 assert.Equal(t, 5, i)
350 assert.Equal(t, 0, v.Ep)
351 assert.Equal(t, int64(-1234), v.Iv)
352 assert.Equal(t, types.V_INTEGER, v.Vt)
353 i = 0
354 s = "9223372036854775807"
355 vsigned(&s, &i, &v)
356 assert.Equal(t, 19, i)
357 assert.Equal(t, 0, v.Ep)
358 assert.Equal(t, int64(math.MaxInt64), v.Iv)
359 assert.Equal(t, types.V_INTEGER, v.Vt)
360 i = 0
361 s = "-9223372036854775808"
362 vsigned(&s, &i, &v)
363 assert.Equal(t, 20, i)
364 assert.Equal(t, 0, v.Ep)
365 assert.Equal(t, int64(math.MinInt64), v.Iv)
366 assert.Equal(t, types.V_INTEGER, v.Vt)
367 i = 0
368 s = "9223372036854775808"
369 vsigned(&s, &i, &v)
370 assert.Equal(t, 18, i)
371 assert.Equal(t, 0, v.Ep)
372 assert.Equal(t, types.ValueType(-int(types.ERR_INTEGER_OVERFLOW)), v.Vt)
373 i = 0
374 s = "-9223372036854775809"
375 vsigned(&s, &i, &v)
376 assert.Equal(t, 19, i)
377 assert.Equal(t, 0, v.Ep)
378 assert.Equal(t, types.ValueType(-int(types.ERR_INTEGER_OVERFLOW)), v.Vt)
379 i = 0
380 s = "1.234"
381 vsigned(&s, &i, &v)
382 assert.Equal(t, 1, i)
383 assert.Equal(t, 0, v.Ep)
384 assert.Equal(t, types.ValueType(-int(types.ERR_INVALID_NUMBER_FMT)), v.Vt)
385 i = 0
386 s = "0.0125"
387 vsigned(&s, &i, &v)
388 assert.Equal(t, 1, i)
389 assert.Equal(t, 0, v.Ep)
390 assert.Equal(t, types.ValueType(-int(types.ERR_INVALID_NUMBER_FMT)), v.Vt)
391 i = 0
392 s = "-1234e5"
393 vsigned(&s, &i, &v)
394 assert.Equal(t, 5, i)
395 assert.Equal(t, 0, v.Ep)
396 assert.Equal(t, types.ValueType(-int(types.ERR_INVALID_NUMBER_FMT)), v.Vt)
397 i = 0
398 s = "-1234e-5"
399 vsigned(&s, &i, &v)
400 assert.Equal(t, 5, i)
401 assert.Equal(t, 0, v.Ep)
402 assert.Equal(t, types.ValueType(-int(types.ERR_INVALID_NUMBER_FMT)), v.Vt)
403}
404
405func TestNative_Vunsigned(t *testing.T) {
406 var v types.JsonState
407 i := 0
408 s := "1234"
409 vunsigned(&s, &i, &v)
410 assert.Equal(t, 4, i)
411 assert.Equal(t, 0, v.Ep)
412 assert.Equal(t, int64(1234), v.Iv)
413 assert.Equal(t, types.V_INTEGER, v.Vt)
414 i = 0
415 s = "18446744073709551615"
416 vunsigned(&s, &i, &v)
417 assert.Equal(t, 20, i)
418 assert.Equal(t, 0, v.Ep)
419 assert.Equal(t, ^int64(0), v.Iv)
420 assert.Equal(t, types.V_INTEGER, v.Vt)
421 i = 0
422 s = "18446744073709551616"
423 vunsigned(&s, &i, &v)
424 assert.Equal(t, 19, i)
425 assert.Equal(t, 0, v.Ep)
426 assert.Equal(t, types.ValueType(-int(types.ERR_INTEGER_OVERFLOW)), v.Vt)
427 i = 0
428 s = "-1234"
429 vunsigned(&s, &i, &v)
430 assert.Equal(t, 0, i)
431 assert.Equal(t, 0, v.Ep)
432 assert.Equal(t, types.ValueType(-int(types.ERR_INVALID_NUMBER_FMT)), v.Vt)
433 i = 0
434 s = "1.234"
435 vunsigned(&s, &i, &v)
436 assert.Equal(t, 1, i)
437 assert.Equal(t, 0, v.Ep)
438 assert.Equal(t, types.ValueType(-int(types.ERR_INVALID_NUMBER_FMT)), v.Vt)
439 i = 0
440 s = "0.0125"
441 vunsigned(&s, &i, &v)
442 assert.Equal(t, 1, i)
443 assert.Equal(t, 0, v.Ep)
444 assert.Equal(t, types.ValueType(-int(types.ERR_INVALID_NUMBER_FMT)), v.Vt)
445 i = 0
446 s = "1234e5"
447 vunsigned(&s, &i, &v)
448 assert.Equal(t, 4, i)
449 assert.Equal(t, 0, v.Ep)
450 assert.Equal(t, types.ValueType(-int(types.ERR_INVALID_NUMBER_FMT)), v.Vt)
451 i = 0
452 s = "-1234e5"
453 vunsigned(&s, &i, &v)
454 assert.Equal(t, 0, i)
455 assert.Equal(t, 0, v.Ep)
456 assert.Equal(t, types.ValueType(-int(types.ERR_INVALID_NUMBER_FMT)), v.Vt)
457 i = 0
458 s = "-1.234e5"
459 vunsigned(&s, &i, &v)
460 assert.Equal(t, 0, i)
461 assert.Equal(t, 0, v.Ep)
462 assert.Equal(t, types.ValueType(-int(types.ERR_INVALID_NUMBER_FMT)), v.Vt)
463 i = 0
464 s = "-1.234e-5"
465 vunsigned(&s, &i, &v)
466 assert.Equal(t, 0, i)
467 assert.Equal(t, 0, v.Ep)
468 assert.Equal(t, types.ValueType(-int(types.ERR_INVALID_NUMBER_FMT)), v.Vt)
469}
470
471func TestNative_SkipOne(t *testing.T) {
472 p := 0
473 s := ` {"asdf": [null, true, false, 1, 2.0, -3]}, 1234.5`
474 q := skip_one(&s, &p, &types.StateMachine{}, uint64(0))
475 assert.Equal(t, 42, p)
476 assert.Equal(t, 1, q)
477 p = 0
478 s = `1 2.5 -3 "asdf\nqwer" true false null {} []`
479 q = skip_one(&s, &p, &types.StateMachine{}, uint64(0))
480 assert.Equal(t, 1, p)
481 assert.Equal(t, 0, q)
482 q = skip_one(&s, &p, &types.StateMachine{}, uint64(0))
483 assert.Equal(t, 5, p)
484 assert.Equal(t, 2, q)
485 q = skip_one(&s, &p, &types.StateMachine{}, uint64(0))
486 assert.Equal(t, 8, p)
487 assert.Equal(t, 6, q)
488 q = skip_one(&s, &p, &types.StateMachine{}, uint64(0))
489 assert.Equal(t, 21, p)
490 assert.Equal(t, 9, q)
491 q = skip_one(&s, &p, &types.StateMachine{}, uint64(0))
492 assert.Equal(t, 26, p)
493 assert.Equal(t, 22, q)
494 q = skip_one(&s, &p, &types.StateMachine{}, uint64(0))
495 assert.Equal(t, 32, p)
496 assert.Equal(t, 27, q)
497 q = skip_one(&s, &p, &types.StateMachine{}, uint64(0))
498 assert.Equal(t, 37, p)
499 assert.Equal(t, 33, q)
500 q = skip_one(&s, &p, &types.StateMachine{}, uint64(0))
501 assert.Equal(t, 40, p)
502 assert.Equal(t, 38, q)
503 q = skip_one(&s, &p, &types.StateMachine{}, uint64(0))
504 assert.Equal(t, 43, p)
505 assert.Equal(t, 41, q)
506}
507
508func TestNative_SkipOne_Error(t *testing.T) {
509 for _, s := range([]string{
510 "-", "+", "0.", "0. ", "+1", "0.0e ", "9e+", "0e-",
511 "tru", "fals", "nul", "trux", "fals ",
512 `"asdf`, `"\\\"`,
513 }) {
514 p := 0
515 q := skip_one(&s, &p, &types.StateMachine{}, uint64(0))
516 assert.True(t, q < 0)
517 }
518}
519
520func TestNative_SkipArray(t *testing.T) {
521 p := 0
522 s := `null, true, false, 1, 2.0, -3, {"asdf": "wqer"}],`
523 skip_array(&s, &p, &types.StateMachine{}, uint64(0))
524 assert.Equal(t, p, 48)
525}
526
527func TestNative_SkipObject(t *testing.T) {
528 p := 0
529 s := `"asdf": "wqer"},`
530 skip_object(&s, &p, &types.StateMachine{}, uint64(0))
531 assert.Equal(t, p, 15)
532}
533
534func TestNative_SkipNumber(t *testing.T) {
535 p := 0
536 s := `-1.23e+12`
537 q := skip_number(&s, &p)
538 assert.Equal(t, 9, p)
539 assert.Equal(t, 0, q)
540}
541
542func TestNative_SkipNumberInJson(t *testing.T) {
543 p := 0x13
544 s := "{\"h\":\"1.00000\",\"i\":true,\"pass3\":1}"
545 q := skip_number(&s, &p)
546 assert.Equal(t, 0x13, p)
547 assert.Equal(t, -2, q)
548}
549
550func TestNative_SkipOneFast(t *testing.T) {
551 p := 0
552 s := ` {"asdf": [null, true, false, 1, 2.0, -3]}, 1234.5`
553 q := skip_one_fast(&s, &p)
554 assert.Equal(t, 42, p)
555 assert.Equal(t, 1, q)
556 p = 0
557 s = `1, 2.5, -3, "asdf\nqwer", true, false, null, {}, [],`
558 q = skip_one_fast(&s, &p)
559 assert.Equal(t, 1, p)
560 assert.Equal(t, 0, q)
561 p += 1
562 q = skip_one_fast(&s, &p)
563 assert.Equal(t, 6, p)
564 assert.Equal(t, 3, q)
565 p += 1
566 q = skip_one_fast(&s, &p)
567 assert.Equal(t, 10, p)
568 assert.Equal(t, 8, q)
569 p += 1
570 q = skip_one_fast(&s, &p)
571 assert.Equal(t, 24, p)
572 assert.Equal(t, 12, q)
573 p += 1
574 q = skip_one_fast(&s, &p)
575 assert.Equal(t, 30, p)
576 assert.Equal(t, 26, q)
577 p += 1
578 q = skip_one_fast(&s, &p)
579 assert.Equal(t, 37, p)
580 assert.Equal(t, 32, q)
581 p += 1
582 q = skip_one_fast(&s, &p)
583 assert.Equal(t, 43, p)
584 assert.Equal(t, 39, q)
585 p += 1
586 q = skip_one_fast(&s, &p)
587 assert.Equal(t, 47, p)
588 assert.Equal(t, 45, q)
589 p += 1
590 q = skip_one_fast(&s, &p)
591 assert.Equal(t, 51, p)
592 assert.Equal(t, 49, q)
593}
594
595func TestNative_SkipOneFast_Error(t *testing.T) {
596 for _, s := range([]string{
597 "{{", "[{", "{{}",
598 `"asdf`, `"\\\"`,
599 }) {
600 p := 0
601 q := skip_one_fast(&s, &p)
602 assert.True(t, q < 0)
603 }
604}
605
606func TestNative_GetByPath(t *testing.T) {
607 s := `{"asdf": [null, true, false, 1, 2.0, -3]}, 1234.5`
608 p := 0
609 path := []interface{}{"asdf", 4}
610 ret := get_by_path(&s, &p, &path, types.NewStateMachine())
611 assert.Equal(t, strings.Index(s, "2.0"), ret)
612}
613
614func BenchmarkNative_SkipOneFast(b *testing.B) {
615 b.ResetTimer()
616 for i:=0; i<b.N; i++ {
617 s := `{"asdf": [null, true, false, 1, 2.0, -3]}, 1234.5`
618 p := 0
619 _ = skip_one_fast(&s, &p)
620 }
621}
622
623func BenchmarkNative_GetByPath(b *testing.B) {
624 b.ResetTimer()
625 for i:=0; i<b.N; i++ {
626 s := `{"asdf": [null, true, false, 1, 2.0, -3]}, 1234.5`
627 p := 0
628 path := []interface{}{"asdf", 3}
629 sm := types.NewStateMachine()
630 _ = get_by_path(&s, &p, &path, sm)
631 types.FreeStateMachine(sm)
632 }
633}
View as plain text