jsoniter_int_test.go 13 KB

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