jsoniter_int_test.go 12 KB

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