jsoniter_int_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. // +build go1.8
  2. package jsoniter
  3. import (
  4. "bytes"
  5. "encoding/json"
  6. "fmt"
  7. "github.com/stretchr/testify/require"
  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(ConfigDefault, ",")
  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(ConfigDefault, 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(ConfigDefault, 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(ConfigDefault, 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(ConfigDefault, 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(ConfigDefault, 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(ConfigDefault, 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(ConfigDefault, 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(ConfigDefault, input)
  105. iter.ReadInt64()
  106. should.NotNil(iter.Error)
  107. }
  108. func Test_wrap_int(t *testing.T) {
  109. should := require.New(t)
  110. str, err := MarshalToString(WrapInt64(100))
  111. should.Nil(err)
  112. should.Equal("100", str)
  113. }
  114. func Test_write_uint8(t *testing.T) {
  115. vals := []uint8{0, 1, 11, 111, 255}
  116. for _, val := range vals {
  117. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  118. should := require.New(t)
  119. buf := &bytes.Buffer{}
  120. stream := NewStream(ConfigDefault, buf, 4096)
  121. stream.WriteUint8(val)
  122. stream.Flush()
  123. should.Nil(stream.Error)
  124. should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
  125. })
  126. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  127. should := require.New(t)
  128. buf := &bytes.Buffer{}
  129. stream := NewStream(ConfigDefault, buf, 4096)
  130. stream.WriteVal(val)
  131. stream.Flush()
  132. should.Nil(stream.Error)
  133. should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
  134. })
  135. }
  136. should := require.New(t)
  137. buf := &bytes.Buffer{}
  138. stream := NewStream(ConfigDefault, buf, 3)
  139. stream.WriteRaw("a")
  140. stream.WriteUint8(100) // should clear buffer
  141. stream.Flush()
  142. should.Nil(stream.Error)
  143. should.Equal("a100", buf.String())
  144. }
  145. func Test_write_int8(t *testing.T) {
  146. vals := []int8{0, 1, -1, 99, 0x7f, -0x80}
  147. for _, val := range vals {
  148. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  149. should := require.New(t)
  150. buf := &bytes.Buffer{}
  151. stream := NewStream(ConfigDefault, buf, 4096)
  152. stream.WriteInt8(val)
  153. stream.Flush()
  154. should.Nil(stream.Error)
  155. should.Equal(strconv.FormatInt(int64(val), 10), buf.String())
  156. })
  157. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  158. should := require.New(t)
  159. buf := &bytes.Buffer{}
  160. stream := NewStream(ConfigDefault, buf, 4096)
  161. stream.WriteVal(val)
  162. stream.Flush()
  163. should.Nil(stream.Error)
  164. should.Equal(strconv.FormatInt(int64(val), 10), buf.String())
  165. })
  166. }
  167. should := require.New(t)
  168. buf := &bytes.Buffer{}
  169. stream := NewStream(ConfigDefault, buf, 4)
  170. stream.WriteRaw("a")
  171. stream.WriteInt8(-100) // should clear buffer
  172. stream.Flush()
  173. should.Nil(stream.Error)
  174. should.Equal("a-100", buf.String())
  175. }
  176. func Test_write_uint16(t *testing.T) {
  177. vals := []uint16{0, 1, 11, 111, 255, 0xfff, 0xffff}
  178. for _, val := range vals {
  179. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  180. should := require.New(t)
  181. buf := &bytes.Buffer{}
  182. stream := NewStream(ConfigDefault, buf, 4096)
  183. stream.WriteUint16(val)
  184. stream.Flush()
  185. should.Nil(stream.Error)
  186. should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
  187. })
  188. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  189. should := require.New(t)
  190. buf := &bytes.Buffer{}
  191. stream := NewStream(ConfigDefault, buf, 4096)
  192. stream.WriteVal(val)
  193. stream.Flush()
  194. should.Nil(stream.Error)
  195. should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
  196. })
  197. }
  198. should := require.New(t)
  199. buf := &bytes.Buffer{}
  200. stream := NewStream(ConfigDefault, buf, 5)
  201. stream.WriteRaw("a")
  202. stream.WriteUint16(10000) // should clear buffer
  203. stream.Flush()
  204. should.Nil(stream.Error)
  205. should.Equal("a10000", buf.String())
  206. }
  207. func Test_write_int16(t *testing.T) {
  208. vals := []int16{0, 1, 11, 111, 255, 0xfff, 0x7fff, -0x8000}
  209. for _, val := range vals {
  210. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  211. should := require.New(t)
  212. buf := &bytes.Buffer{}
  213. stream := NewStream(ConfigDefault, buf, 4096)
  214. stream.WriteInt16(val)
  215. stream.Flush()
  216. should.Nil(stream.Error)
  217. should.Equal(strconv.FormatInt(int64(val), 10), buf.String())
  218. })
  219. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  220. should := require.New(t)
  221. buf := &bytes.Buffer{}
  222. stream := NewStream(ConfigDefault, buf, 4096)
  223. stream.WriteVal(val)
  224. stream.Flush()
  225. should.Nil(stream.Error)
  226. should.Equal(strconv.FormatInt(int64(val), 10), buf.String())
  227. })
  228. }
  229. should := require.New(t)
  230. buf := &bytes.Buffer{}
  231. stream := NewStream(ConfigDefault, buf, 6)
  232. stream.WriteRaw("a")
  233. stream.WriteInt16(-10000) // should clear buffer
  234. stream.Flush()
  235. should.Nil(stream.Error)
  236. should.Equal("a-10000", buf.String())
  237. }
  238. func Test_write_uint32(t *testing.T) {
  239. vals := []uint32{0, 1, 11, 111, 255, 999999, 0xfff, 0xffff, 0xfffff, 0xffffff, 0xfffffff, 0xffffffff}
  240. for _, val := range vals {
  241. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  242. should := require.New(t)
  243. buf := &bytes.Buffer{}
  244. stream := NewStream(ConfigDefault, buf, 4096)
  245. stream.WriteUint32(val)
  246. stream.Flush()
  247. should.Nil(stream.Error)
  248. should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
  249. })
  250. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  251. should := require.New(t)
  252. buf := &bytes.Buffer{}
  253. stream := NewStream(ConfigDefault, buf, 4096)
  254. stream.WriteVal(val)
  255. stream.Flush()
  256. should.Nil(stream.Error)
  257. should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
  258. })
  259. }
  260. should := require.New(t)
  261. buf := &bytes.Buffer{}
  262. stream := NewStream(ConfigDefault, buf, 10)
  263. stream.WriteRaw("a")
  264. stream.WriteUint32(0xffffffff) // should clear buffer
  265. stream.Flush()
  266. should.Nil(stream.Error)
  267. should.Equal("a4294967295", buf.String())
  268. }
  269. func Test_write_int32(t *testing.T) {
  270. vals := []int32{0, 1, 11, 111, 255, 999999, 0xfff, 0xffff, 0xfffff, 0xffffff, 0xfffffff, 0x7fffffff, -0x80000000}
  271. for _, val := range vals {
  272. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  273. should := require.New(t)
  274. buf := &bytes.Buffer{}
  275. stream := NewStream(ConfigDefault, buf, 4096)
  276. stream.WriteInt32(val)
  277. stream.Flush()
  278. should.Nil(stream.Error)
  279. should.Equal(strconv.FormatInt(int64(val), 10), buf.String())
  280. })
  281. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  282. should := require.New(t)
  283. buf := &bytes.Buffer{}
  284. stream := NewStream(ConfigDefault, buf, 4096)
  285. stream.WriteVal(val)
  286. stream.Flush()
  287. should.Nil(stream.Error)
  288. should.Equal(strconv.FormatInt(int64(val), 10), buf.String())
  289. })
  290. }
  291. should := require.New(t)
  292. buf := &bytes.Buffer{}
  293. stream := NewStream(ConfigDefault, buf, 11)
  294. stream.WriteRaw("a")
  295. stream.WriteInt32(-0x7fffffff) // should clear buffer
  296. stream.Flush()
  297. should.Nil(stream.Error)
  298. should.Equal("a-2147483647", buf.String())
  299. }
  300. func Test_write_uint64(t *testing.T) {
  301. vals := []uint64{0, 1, 11, 111, 255, 999999, 0xfff, 0xffff, 0xfffff, 0xffffff, 0xfffffff, 0xffffffff,
  302. 0xfffffffff, 0xffffffffff, 0xfffffffffff, 0xffffffffffff, 0xfffffffffffff, 0xffffffffffffff,
  303. 0xfffffffffffffff, 0xffffffffffffffff}
  304. for _, val := range vals {
  305. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  306. should := require.New(t)
  307. buf := &bytes.Buffer{}
  308. stream := NewStream(ConfigDefault, buf, 4096)
  309. stream.WriteUint64(val)
  310. stream.Flush()
  311. should.Nil(stream.Error)
  312. should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
  313. })
  314. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  315. should := require.New(t)
  316. buf := &bytes.Buffer{}
  317. stream := NewStream(ConfigDefault, buf, 4096)
  318. stream.WriteVal(val)
  319. stream.Flush()
  320. should.Nil(stream.Error)
  321. should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
  322. })
  323. }
  324. should := require.New(t)
  325. buf := &bytes.Buffer{}
  326. stream := NewStream(ConfigDefault, buf, 10)
  327. stream.WriteRaw("a")
  328. stream.WriteUint64(0xffffffff) // should clear buffer
  329. stream.Flush()
  330. should.Nil(stream.Error)
  331. should.Equal("a4294967295", buf.String())
  332. }
  333. func Test_write_int64(t *testing.T) {
  334. vals := []int64{0, 1, 11, 111, 255, 999999, 0xfff, 0xffff, 0xfffff, 0xffffff, 0xfffffff, 0xffffffff,
  335. 0xfffffffff, 0xffffffffff, 0xfffffffffff, 0xffffffffffff, 0xfffffffffffff, 0xffffffffffffff,
  336. 0xfffffffffffffff, 0x7fffffffffffffff, -0x8000000000000000}
  337. for _, val := range vals {
  338. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  339. should := require.New(t)
  340. buf := &bytes.Buffer{}
  341. stream := NewStream(ConfigDefault, buf, 4096)
  342. stream.WriteInt64(val)
  343. stream.Flush()
  344. should.Nil(stream.Error)
  345. should.Equal(strconv.FormatInt(val, 10), buf.String())
  346. })
  347. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  348. should := require.New(t)
  349. buf := &bytes.Buffer{}
  350. stream := NewStream(ConfigDefault, buf, 4096)
  351. stream.WriteVal(val)
  352. stream.Flush()
  353. should.Nil(stream.Error)
  354. should.Equal(strconv.FormatInt(val, 10), buf.String())
  355. })
  356. }
  357. should := require.New(t)
  358. buf := &bytes.Buffer{}
  359. stream := NewStream(ConfigDefault, buf, 10)
  360. stream.WriteRaw("a")
  361. stream.WriteInt64(0xffffffff) // should clear buffer
  362. stream.Flush()
  363. should.Nil(stream.Error)
  364. should.Equal("a4294967295", buf.String())
  365. }
  366. func Test_write_val_int(t *testing.T) {
  367. should := require.New(t)
  368. buf := &bytes.Buffer{}
  369. stream := NewStream(ConfigDefault, buf, 4096)
  370. stream.WriteVal(1001)
  371. stream.Flush()
  372. should.Nil(stream.Error)
  373. should.Equal("1001", buf.String())
  374. }
  375. func Test_write_val_int_ptr(t *testing.T) {
  376. should := require.New(t)
  377. buf := &bytes.Buffer{}
  378. stream := NewStream(ConfigDefault, buf, 4096)
  379. val := 1001
  380. stream.WriteVal(&val)
  381. stream.Flush()
  382. should.Nil(stream.Error)
  383. should.Equal("1001", buf.String())
  384. }
  385. func Test_json_number(t *testing.T) {
  386. should := require.New(t)
  387. var arr []json.Number
  388. err := Unmarshal([]byte(`[1]`), &arr)
  389. should.Nil(err)
  390. should.Equal(json.Number("1"), arr[0])
  391. str, err := MarshalToString(arr)
  392. should.Nil(err)
  393. should.Equal(`[1]`, str)
  394. }
  395. func Benchmark_jsoniter_encode_int(b *testing.B) {
  396. stream := NewStream(ConfigDefault, ioutil.Discard, 64)
  397. for n := 0; n < b.N; n++ {
  398. stream.n = 0
  399. stream.WriteUint64(0xffffffff)
  400. }
  401. }
  402. func Benchmark_itoa(b *testing.B) {
  403. for n := 0; n < b.N; n++ {
  404. strconv.FormatInt(0xffffffff, 10)
  405. }
  406. }
  407. func Benchmark_jsoniter_int(b *testing.B) {
  408. iter := NewIterator(ConfigDefault)
  409. input := []byte(`100`)
  410. for n := 0; n < b.N; n++ {
  411. iter.ResetBytes(input)
  412. iter.ReadInt64()
  413. }
  414. }
  415. func Benchmark_json_int(b *testing.B) {
  416. for n := 0; n < b.N; n++ {
  417. result := int64(0)
  418. json.Unmarshal([]byte(`-100`), &result)
  419. }
  420. }