jsoniter_int_test.go 13 KB

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