jsoniter_int_test.go 11 KB

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