jsoniter_int_test.go 12 KB

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