1 package test
2
3 import (
4 "bytes"
5 "fmt"
6 "github.com/json-iterator/go"
7 "github.com/stretchr/testify/require"
8 "strconv"
9 "testing"
10 )
11
12 func init() {
13 unmarshalCases = append(unmarshalCases, unmarshalCase{
14 ptr: (*struct {
15 F1 int8
16 F2 int16
17 F3 int32
18 F4 int64
19 F5 int
20 F6 uint8
21 F7 uint16
22 F8 uint32
23 F9 uint64
24 F10 uint
25 F11 float32
26 F12 float64
27 F13 uintptr
28 })(nil),
29 input: `{
30 "f1":null,
31 "f2":null,
32 "f3":null,
33 "f4":null,
34 "f5":null,
35 "f6":null,
36 "f7":null,
37 "f8":null,
38 "f9":null,
39 "f10":null,
40 "f11":null,
41 "f12":null,
42 "f13":null
43 }`,
44 })
45 }
46
47 func Test_int8(t *testing.T) {
48 inputs := []string{`127`, `-128`}
49 for _, input := range inputs {
50 t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
51 should := require.New(t)
52 iter := jsoniter.ParseString(jsoniter.ConfigDefault, input)
53 expected, err := strconv.ParseInt(input, 10, 8)
54 should.Nil(err)
55 should.Equal(int8(expected), iter.ReadInt8())
56 })
57 }
58 }
59
60 func Test_read_int16(t *testing.T) {
61 inputs := []string{`32767`, `-32768`}
62 for _, input := range inputs {
63 t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
64 should := require.New(t)
65 iter := jsoniter.ParseString(jsoniter.ConfigDefault, input)
66 expected, err := strconv.ParseInt(input, 10, 16)
67 should.Nil(err)
68 should.Equal(int16(expected), iter.ReadInt16())
69 })
70 }
71 }
72
73 func Test_read_int32(t *testing.T) {
74 inputs := []string{`1`, `12`, `123`, `1234`, `12345`, `123456`, `2147483647`, `-2147483648`}
75 for _, input := range inputs {
76 t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
77 should := require.New(t)
78 iter := jsoniter.ParseString(jsoniter.ConfigDefault, input)
79 expected, err := strconv.ParseInt(input, 10, 32)
80 should.Nil(err)
81 should.Equal(int32(expected), iter.ReadInt32())
82 })
83 t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
84 should := require.New(t)
85 iter := jsoniter.Parse(jsoniter.ConfigDefault, bytes.NewBufferString(input), 2)
86 expected, err := strconv.ParseInt(input, 10, 32)
87 should.Nil(err)
88 should.Equal(int32(expected), iter.ReadInt32())
89 })
90 }
91 }
92
93 func Test_read_int_overflow(t *testing.T) {
94 should := require.New(t)
95 inputArr := []string{"123451", "-123451"}
96 for _, s := range inputArr {
97 iter := jsoniter.ParseString(jsoniter.ConfigDefault, s)
98 iter.ReadInt8()
99 should.NotNil(iter.Error)
100
101 iterU := jsoniter.ParseString(jsoniter.ConfigDefault, s)
102 iterU.ReadUint8()
103 should.NotNil(iterU.Error)
104
105 }
106
107 inputArr = []string{"12345678912", "-12345678912"}
108 for _, s := range inputArr {
109 iter := jsoniter.ParseString(jsoniter.ConfigDefault, s)
110 iter.ReadInt16()
111 should.NotNil(iter.Error)
112
113 iterUint := jsoniter.ParseString(jsoniter.ConfigDefault, s)
114 iterUint.ReadUint16()
115 should.NotNil(iterUint.Error)
116 }
117
118 inputArr = []string{"3111111111", "-3111111111", "1234232323232323235678912", "-1234567892323232323212"}
119 for _, s := range inputArr {
120 iter := jsoniter.ParseString(jsoniter.ConfigDefault, s)
121 iter.ReadInt32()
122 should.NotNil(iter.Error)
123
124 iterUint := jsoniter.ParseString(jsoniter.ConfigDefault, s)
125 iterUint.ReadUint32()
126 should.NotNil(iterUint.Error)
127 }
128
129 inputArr = []string{"9223372036854775811", "-9523372036854775807", "1234232323232323235678912", "-1234567892323232323212"}
130 for _, s := range inputArr {
131 iter := jsoniter.ParseString(jsoniter.ConfigDefault, s)
132 iter.ReadInt64()
133 should.NotNil(iter.Error)
134
135 iterUint := jsoniter.ParseString(jsoniter.ConfigDefault, s)
136 iterUint.ReadUint64()
137 should.NotNil(iterUint.Error)
138 }
139 }
140
141 func Test_read_int64(t *testing.T) {
142 inputs := []string{`1`, `12`, `123`, `1234`, `12345`, `123456`, `9223372036854775807`, `-9223372036854775808`}
143 for _, input := range inputs {
144 t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
145 should := require.New(t)
146 iter := jsoniter.ParseString(jsoniter.ConfigDefault, input)
147 expected, err := strconv.ParseInt(input, 10, 64)
148 should.Nil(err)
149 should.Equal(expected, iter.ReadInt64())
150 })
151 t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
152 should := require.New(t)
153 iter := jsoniter.Parse(jsoniter.ConfigDefault, bytes.NewBufferString(input), 2)
154 expected, err := strconv.ParseInt(input, 10, 64)
155 should.Nil(err)
156 should.Equal(expected, iter.ReadInt64())
157 })
158 }
159 }
160
161 func Test_write_uint8(t *testing.T) {
162 vals := []uint8{0, 1, 11, 111, 255}
163 for _, val := range vals {
164 t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
165 should := require.New(t)
166 buf := &bytes.Buffer{}
167 stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4096)
168 stream.WriteUint8(val)
169 stream.Flush()
170 should.Nil(stream.Error)
171 should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
172 })
173 t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
174 should := require.New(t)
175 buf := &bytes.Buffer{}
176 stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4096)
177 stream.WriteVal(val)
178 stream.Flush()
179 should.Nil(stream.Error)
180 should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
181 })
182 }
183 should := require.New(t)
184 buf := &bytes.Buffer{}
185 stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 3)
186 stream.WriteRaw("a")
187 stream.WriteUint8(100)
188 stream.Flush()
189 should.Nil(stream.Error)
190 should.Equal("a100", buf.String())
191 }
192
193 func Test_write_int8(t *testing.T) {
194 vals := []int8{0, 1, -1, 99, 0x7f, -0x80}
195 for _, val := range vals {
196 t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
197 should := require.New(t)
198 buf := &bytes.Buffer{}
199 stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4096)
200 stream.WriteInt8(val)
201 stream.Flush()
202 should.Nil(stream.Error)
203 should.Equal(strconv.FormatInt(int64(val), 10), buf.String())
204 })
205 t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
206 should := require.New(t)
207 buf := &bytes.Buffer{}
208 stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4096)
209 stream.WriteVal(val)
210 stream.Flush()
211 should.Nil(stream.Error)
212 should.Equal(strconv.FormatInt(int64(val), 10), buf.String())
213 })
214 }
215 should := require.New(t)
216 buf := &bytes.Buffer{}
217 stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4)
218 stream.WriteRaw("a")
219 stream.WriteInt8(-100)
220 stream.Flush()
221 should.Nil(stream.Error)
222 should.Equal("a-100", buf.String())
223 }
224
225 func Test_write_uint16(t *testing.T) {
226 vals := []uint16{0, 1, 11, 111, 255, 0xfff, 0xffff}
227 for _, val := range vals {
228 t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
229 should := require.New(t)
230 buf := &bytes.Buffer{}
231 stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4096)
232 stream.WriteUint16(val)
233 stream.Flush()
234 should.Nil(stream.Error)
235 should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
236 })
237 t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
238 should := require.New(t)
239 buf := &bytes.Buffer{}
240 stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4096)
241 stream.WriteVal(val)
242 stream.Flush()
243 should.Nil(stream.Error)
244 should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
245 })
246 }
247 should := require.New(t)
248 buf := &bytes.Buffer{}
249 stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 5)
250 stream.WriteRaw("a")
251 stream.WriteUint16(10000)
252 stream.Flush()
253 should.Nil(stream.Error)
254 should.Equal("a10000", buf.String())
255 }
256
257 func Test_write_int16(t *testing.T) {
258 vals := []int16{0, 1, 11, 111, 255, 0xfff, 0x7fff, -0x8000}
259 for _, val := range vals {
260 t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
261 should := require.New(t)
262 buf := &bytes.Buffer{}
263 stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4096)
264 stream.WriteInt16(val)
265 stream.Flush()
266 should.Nil(stream.Error)
267 should.Equal(strconv.FormatInt(int64(val), 10), buf.String())
268 })
269 t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
270 should := require.New(t)
271 buf := &bytes.Buffer{}
272 stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4096)
273 stream.WriteVal(val)
274 stream.Flush()
275 should.Nil(stream.Error)
276 should.Equal(strconv.FormatInt(int64(val), 10), buf.String())
277 })
278 }
279 should := require.New(t)
280 buf := &bytes.Buffer{}
281 stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 6)
282 stream.WriteRaw("a")
283 stream.WriteInt16(-10000)
284 stream.Flush()
285 should.Nil(stream.Error)
286 should.Equal("a-10000", buf.String())
287 }
288
289 func Test_write_uint32(t *testing.T) {
290 vals := []uint32{0, 1, 11, 111, 255, 999999, 0xfff, 0xffff, 0xfffff, 0xffffff, 0xfffffff, 0xffffffff}
291 for _, val := range vals {
292 t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
293 should := require.New(t)
294 buf := &bytes.Buffer{}
295 stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4096)
296 stream.WriteUint32(val)
297 stream.Flush()
298 should.Nil(stream.Error)
299 should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
300 })
301 t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
302 should := require.New(t)
303 buf := &bytes.Buffer{}
304 stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4096)
305 stream.WriteVal(val)
306 stream.Flush()
307 should.Nil(stream.Error)
308 should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
309 })
310 }
311 should := require.New(t)
312 buf := &bytes.Buffer{}
313 stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 10)
314 stream.WriteRaw("a")
315 stream.WriteUint32(0xffffffff)
316 stream.Flush()
317 should.Nil(stream.Error)
318 should.Equal("a4294967295", buf.String())
319 }
320
321 func Test_write_int32(t *testing.T) {
322 vals := []int32{0, 1, 11, 111, 255, 999999, 0xfff, 0xffff, 0xfffff, 0xffffff, 0xfffffff, 0x7fffffff, -0x80000000}
323 for _, val := range vals {
324 t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
325 should := require.New(t)
326 buf := &bytes.Buffer{}
327 stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4096)
328 stream.WriteInt32(val)
329 stream.Flush()
330 should.Nil(stream.Error)
331 should.Equal(strconv.FormatInt(int64(val), 10), buf.String())
332 })
333 t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
334 should := require.New(t)
335 buf := &bytes.Buffer{}
336 stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4096)
337 stream.WriteVal(val)
338 stream.Flush()
339 should.Nil(stream.Error)
340 should.Equal(strconv.FormatInt(int64(val), 10), buf.String())
341 })
342 }
343 should := require.New(t)
344 buf := &bytes.Buffer{}
345 stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 11)
346 stream.WriteRaw("a")
347 stream.WriteInt32(-0x7fffffff)
348 stream.Flush()
349 should.Nil(stream.Error)
350 should.Equal("a-2147483647", buf.String())
351 }
352
353 func Test_write_uint64(t *testing.T) {
354 vals := []uint64{0, 1, 11, 111, 255, 999999, 0xfff, 0xffff, 0xfffff, 0xffffff, 0xfffffff, 0xffffffff,
355 0xfffffffff, 0xffffffffff, 0xfffffffffff, 0xffffffffffff, 0xfffffffffffff, 0xffffffffffffff,
356 0xfffffffffffffff, 0xffffffffffffffff}
357 for _, val := range vals {
358 t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
359 should := require.New(t)
360 buf := &bytes.Buffer{}
361 stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4096)
362 stream.WriteUint64(val)
363 stream.Flush()
364 should.Nil(stream.Error)
365 should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
366 })
367 t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
368 should := require.New(t)
369 buf := &bytes.Buffer{}
370 stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4096)
371 stream.WriteVal(val)
372 stream.Flush()
373 should.Nil(stream.Error)
374 should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
375 })
376 }
377 should := require.New(t)
378 buf := &bytes.Buffer{}
379 stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 10)
380 stream.WriteRaw("a")
381 stream.WriteUint64(0xffffffff)
382 stream.Flush()
383 should.Nil(stream.Error)
384 should.Equal("a4294967295", buf.String())
385 }
386
387 func Test_write_int64(t *testing.T) {
388 vals := []int64{0, 1, 11, 111, 255, 999999, 0xfff, 0xffff, 0xfffff, 0xffffff, 0xfffffff, 0xffffffff,
389 0xfffffffff, 0xffffffffff, 0xfffffffffff, 0xffffffffffff, 0xfffffffffffff, 0xffffffffffffff,
390 0xfffffffffffffff, 0x7fffffffffffffff, -0x8000000000000000}
391 for _, val := range vals {
392 t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
393 should := require.New(t)
394 buf := &bytes.Buffer{}
395 stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4096)
396 stream.WriteInt64(val)
397 stream.Flush()
398 should.Nil(stream.Error)
399 should.Equal(strconv.FormatInt(val, 10), buf.String())
400 })
401 t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
402 should := require.New(t)
403 buf := &bytes.Buffer{}
404 stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4096)
405 stream.WriteVal(val)
406 stream.Flush()
407 should.Nil(stream.Error)
408 should.Equal(strconv.FormatInt(val, 10), buf.String())
409 })
410 }
411 should := require.New(t)
412 buf := &bytes.Buffer{}
413 stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 10)
414 stream.WriteRaw("a")
415 stream.WriteInt64(0xffffffff)
416 stream.Flush()
417 should.Nil(stream.Error)
418 should.Equal("a4294967295", buf.String())
419 }
420
View as plain text