github.com/zerosnake0/jzon@v0.0.9-0.20230801092939-1b135cb83f7f/val_decoder_native_array_test.go (about) 1 package jzon 2 3 import ( 4 "io" 5 "runtime" 6 "runtime/debug" 7 "testing" 8 9 "github.com/stretchr/testify/require" 10 ) 11 12 func TestValDecoder_Native_Array(t *testing.T) { 13 f := func(t *testing.T, data string, ex error, p1, p2 interface{}) { 14 checkDecodeWithStandard(t, DefaultDecoderConfig, data, ex, p1, p2) 15 } 16 t.Run("nil pointer", func(t *testing.T) { 17 f(t, "null", ErrNilPointerReceiver, nil, nil) 18 }) 19 t.Run("eof", func(t *testing.T) { 20 arr1 := [...]int{1, 2} 21 arr2 := [...]int{1, 2} 22 f(t, "", io.EOF, &arr1, &arr2) 23 }) 24 t.Run("null", func(t *testing.T) { 25 arr1 := [...]int{1, 2} 26 arr2 := [...]int{1, 2} 27 f(t, "null", nil, &arr1, &arr2) 28 }) 29 t.Run("invalid first byte", func(t *testing.T) { 30 arr1 := [...]int{1, 2} 31 arr2 := [...]int{1, 2} 32 f(t, "+", UnexpectedByteError{}, &arr1, &arr2) 33 }) 34 t.Run("eof after bracket", func(t *testing.T) { 35 arr1 := [...]int{1, 2} 36 arr2 := [...]int{1, 2} 37 f(t, "[", io.EOF, &arr1, &arr2) 38 }) 39 t.Run("empty", func(t *testing.T) { 40 arr1 := [...]int{1, 2} 41 arr2 := [...]int{1, 2} 42 f(t, " [ ] ", nil, &arr1, &arr2) 43 }) 44 t.Run("element error", func(t *testing.T) { 45 arr1 := [...]int{1, 2} 46 arr2 := [...]int{1, 2} 47 f(t, ` [ " `, InvalidDigitError{}, &arr1, &arr2) 48 }) 49 t.Run("null element", func(t *testing.T) { 50 arr1 := [...]int{1, 2} 51 arr2 := [...]int{1, 2} 52 f(t, ` [ null ] `, nil, &arr1, &arr2) 53 }) 54 t.Run("eof after element", func(t *testing.T) { 55 arr1 := [...]int{1, 2} 56 arr2 := [...]int{1, 2} 57 f(t, ` [ 2 `, io.EOF, &arr1, &arr2) 58 }) 59 t.Run("lesser element", func(t *testing.T) { 60 arr1 := [...]int{1, 2} 61 arr2 := [...]int{1, 2} 62 f(t, ` [ 2 ] `, nil, &arr1, &arr2) 63 }) 64 t.Run("invalid comma", func(t *testing.T) { 65 arr1 := [...]int{1, 2} 66 arr2 := [...]int{1, 2} 67 f(t, ` [ 2 [ `, UnexpectedByteError{}, &arr1, &arr2) 68 }) 69 t.Run("more element error", func(t *testing.T) { 70 arr1 := [...]int{1, 2} 71 arr2 := [...]int{1, 2} 72 f(t, ` [ 2 , 3 , `, io.EOF, &arr1, &arr2) 73 }) 74 t.Run("more element", func(t *testing.T) { 75 arr1 := [...]int{1, 2} 76 arr2 := [...]int{1, 2} 77 f(t, ` [ 2 , 3 , "test"]`, nil, &arr1, &arr2) 78 }) 79 t.Run("big element", func(t *testing.T) { 80 type st struct { 81 A int 82 B string 83 C []byte 84 } 85 t.Run("empty", func(t *testing.T) { 86 arr1 := [...]st{{ 87 A: 1, 88 B: "a", 89 C: []byte("b"), 90 }, { 91 A: 2, 92 B: "c", 93 C: []byte("d"), 94 }} 95 arr2 := [...]st{{ 96 A: 1, 97 B: "a", 98 C: []byte("b"), 99 }, { 100 A: 2, 101 B: "c", 102 C: []byte("d"), 103 }} 104 f(t, ` [ ] `, nil, &arr1, &arr2) 105 }) 106 t.Run("non empty", func(t *testing.T) { 107 arr1 := [...]st{{ 108 A: 1, 109 B: "a", 110 C: []byte("b"), 111 }, { 112 A: 2, 113 B: "c", 114 C: []byte("d"), 115 }} 116 arr2 := [...]st{{ 117 A: 1, 118 B: "a", 119 C: []byte("b"), 120 }, { 121 A: 2, 122 B: "c", 123 C: []byte("d"), 124 }} 125 f(t, ` [ { } ] `, nil, &arr1, &arr2) 126 }) 127 128 }) 129 debug.FreeOSMemory() 130 } 131 132 func TestValDecoder_Native_Array_Memory(t *testing.T) { 133 t.Run("test1", func(t *testing.T) { 134 f := func(i int) [1]*int { 135 pi := &i 136 runtime.SetFinalizer(pi, func(_ *int) { 137 t.Logf("finalizing") 138 }) 139 return [1]*int{pi} 140 } 141 arr := f(1) 142 err := Unmarshal([]byte(`[]`), &arr) 143 require.NoError(t, err) 144 debug.FreeOSMemory() 145 t.Logf("please check if the memory has been freed") 146 }) 147 t.Run("test2", func(t *testing.T) { 148 type st struct { 149 p *int 150 } 151 f := func(i int) [1]*st { 152 pi := &i 153 runtime.SetFinalizer(pi, func(_ *int) { 154 t.Logf("finalizing") 155 }) 156 return [1]*st{{p: pi}} 157 } 158 arr := f(1) 159 err := Unmarshal([]byte(`[]`), &arr) 160 require.NoError(t, err) 161 debug.FreeOSMemory() 162 t.Logf("please check if the memory has been freed") 163 }) 164 }