1
16
17 package decoder
18
19 import (
20 `fmt`
21 `reflect`
22 `testing`
23
24 `github.com/bytedance/sonic/internal/native/types`
25 `github.com/davecgh/go-spew/spew`
26 `github.com/stretchr/testify/assert`
27 `github.com/stretchr/testify/require`
28 )
29
30
31 func decodeValueStub(st *_Stack, sp string, ic int, vp *interface{}, df uint64) (int, types.ParsingError)
32
33 func decodeValue(k *_Stack, s string, i int, f uint64) (p int, v interface{}, e types.ParsingError) {
34 p, e = decodeValueStub(k, s, i, &v, f)
35 return
36 }
37
38 func decodeGeneric(s string, i int, f uint64) (p int, v interface{}, e types.ParsingError) {
39 t := newStack()
40 p, e = decodeValueStub(t, s, i, &v, f)
41 freeStack(t)
42 return
43 }
44
45 func TestGeneric_DecodeInterface(t *testing.T) {
46 s := `[null, true, false, 1234, -1.25e-8, "hello\nworld", [], {"asdf": [1, 2.5, "qwer", null, true, false, [], {"zxcv": "fghj"}], "qwer": 7777}]`
47 i, v, err := decodeGeneric(s, 0, 0)
48 assert.Equal(t, len(s), i)
49 if err != 0 {
50 require.NoError(t, err)
51 }
52 fmt.Print("v: ")
53 spew.Dump(v)
54 fmt.Printf("type: %s\n", reflect.TypeOf(v))
55 }
56
57 func BenchmarkGeneric_DecodeGeneric(b *testing.B) {
58 t := newStack()
59 _, _, _ = decodeValue(t, TwitterJson, 0, 0)
60 b.SetBytes(int64(len(TwitterJson)))
61 b.ResetTimer()
62 for i := 0; i < b.N; i++ {
63 _, _, _ = decodeValue(t, TwitterJson, 0, 0)
64 }
65 freeStack(t)
66 }
67
68 func BenchmarkGeneric_Parallel_DecodeGeneric(b *testing.B) {
69 _, _, _ = decodeGeneric(TwitterJson, 0, 0)
70 b.SetBytes(int64(len(TwitterJson)))
71 b.ResetTimer()
72 b.RunParallel(func(pb *testing.PB) {
73 for pb.Next() {
74 _, _, _ = decodeGeneric(TwitterJson, 0, 0)
75 }
76 })
77 }
78
View as plain text