jsoniter_int_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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_int32(t *testing.T) {
  18. inputs := []string{`1`, `12`, `123`, `1234`, `12345`, `123456`, `2147483647`}
  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, 32)
  24. should.Nil(err)
  25. should.Equal(int32(expected), iter.ReadInt32())
  26. })
  27. t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
  28. should := require.New(t)
  29. iter := Parse(bytes.NewBufferString(input), 2)
  30. expected, err := strconv.ParseInt(input, 10, 32)
  31. should.Nil(err)
  32. should.Equal(int32(expected), iter.ReadInt32())
  33. })
  34. }
  35. }
  36. func Test_read_int32_overflow(t *testing.T) {
  37. should := require.New(t)
  38. input := "123456789123456789"
  39. iter := ParseString(input)
  40. iter.ReadInt32()
  41. should.NotNil(iter.Error)
  42. }
  43. func Test_read_int64(t *testing.T) {
  44. inputs := []string{`1`, `12`, `123`, `1234`, `12345`, `123456`, `9223372036854775807`}
  45. for _, input := range inputs {
  46. t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
  47. should := require.New(t)
  48. iter := ParseString(input)
  49. expected, err := strconv.ParseInt(input, 10, 64)
  50. should.Nil(err)
  51. should.Equal(expected, iter.ReadInt64())
  52. })
  53. t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
  54. should := require.New(t)
  55. iter := Parse(bytes.NewBufferString(input), 2)
  56. expected, err := strconv.ParseInt(input, 10, 64)
  57. should.Nil(err)
  58. should.Equal(expected, iter.ReadInt64())
  59. })
  60. }
  61. }
  62. func Test_read_int64_overflow(t *testing.T) {
  63. should := require.New(t)
  64. input := "123456789123456789"
  65. iter := ParseString(input)
  66. iter.ReadInt64()
  67. should.NotNil(iter.Error)
  68. }
  69. func Test_write_uint8(t *testing.T) {
  70. vals := []uint8{0, 1, 11, 111, 255}
  71. for _, val := range vals {
  72. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  73. should := require.New(t)
  74. buf := &bytes.Buffer{}
  75. stream := NewStream(buf, 4096)
  76. stream.WriteUint8(val)
  77. stream.Flush()
  78. should.Nil(stream.Error)
  79. should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
  80. })
  81. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  82. should := require.New(t)
  83. buf := &bytes.Buffer{}
  84. stream := NewStream(buf, 4096)
  85. stream.WriteVal(val)
  86. stream.Flush()
  87. should.Nil(stream.Error)
  88. should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
  89. })
  90. }
  91. should := require.New(t)
  92. buf := &bytes.Buffer{}
  93. stream := NewStream(buf, 3)
  94. stream.WriteRaw("a")
  95. stream.WriteUint8(100) // should clear buffer
  96. stream.Flush()
  97. should.Nil(stream.Error)
  98. should.Equal("a100", buf.String())
  99. }
  100. func Test_write_int8(t *testing.T) {
  101. vals := []int8{0, 1, -1, 99, 0x7f, -0x7f}
  102. for _, val := range vals {
  103. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  104. should := require.New(t)
  105. buf := &bytes.Buffer{}
  106. stream := NewStream(buf, 4096)
  107. stream.WriteInt8(val)
  108. stream.Flush()
  109. should.Nil(stream.Error)
  110. should.Equal(strconv.FormatInt(int64(val), 10), buf.String())
  111. })
  112. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  113. should := require.New(t)
  114. buf := &bytes.Buffer{}
  115. stream := NewStream(buf, 4096)
  116. stream.WriteVal(val)
  117. stream.Flush()
  118. should.Nil(stream.Error)
  119. should.Equal(strconv.FormatInt(int64(val), 10), buf.String())
  120. })
  121. }
  122. should := require.New(t)
  123. buf := &bytes.Buffer{}
  124. stream := NewStream(buf, 4)
  125. stream.WriteRaw("a")
  126. stream.WriteInt8(-100) // should clear buffer
  127. stream.Flush()
  128. should.Nil(stream.Error)
  129. should.Equal("a-100", buf.String())
  130. }
  131. func Test_write_uint16(t *testing.T) {
  132. vals := []uint16{0, 1, 11, 111, 255, 0xfff, 0xffff}
  133. for _, val := range vals {
  134. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  135. should := require.New(t)
  136. buf := &bytes.Buffer{}
  137. stream := NewStream(buf, 4096)
  138. stream.WriteUint16(val)
  139. stream.Flush()
  140. should.Nil(stream.Error)
  141. should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
  142. })
  143. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  144. should := require.New(t)
  145. buf := &bytes.Buffer{}
  146. stream := NewStream(buf, 4096)
  147. stream.WriteVal(val)
  148. stream.Flush()
  149. should.Nil(stream.Error)
  150. should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
  151. })
  152. }
  153. should := require.New(t)
  154. buf := &bytes.Buffer{}
  155. stream := NewStream(buf, 5)
  156. stream.WriteRaw("a")
  157. stream.WriteUint16(10000) // should clear buffer
  158. stream.Flush()
  159. should.Nil(stream.Error)
  160. should.Equal("a10000", buf.String())
  161. }
  162. func Test_write_int16(t *testing.T) {
  163. vals := []int16{0, 1, 11, 111, 255, 0xfff, 0x7fff, -0x7fff}
  164. for _, val := range vals {
  165. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  166. should := require.New(t)
  167. buf := &bytes.Buffer{}
  168. stream := NewStream(buf, 4096)
  169. stream.WriteInt16(val)
  170. stream.Flush()
  171. should.Nil(stream.Error)
  172. should.Equal(strconv.FormatInt(int64(val), 10), buf.String())
  173. })
  174. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  175. should := require.New(t)
  176. buf := &bytes.Buffer{}
  177. stream := NewStream(buf, 4096)
  178. stream.WriteVal(val)
  179. stream.Flush()
  180. should.Nil(stream.Error)
  181. should.Equal(strconv.FormatInt(int64(val), 10), buf.String())
  182. })
  183. }
  184. should := require.New(t)
  185. buf := &bytes.Buffer{}
  186. stream := NewStream(buf, 6)
  187. stream.WriteRaw("a")
  188. stream.WriteInt16(-10000) // should clear buffer
  189. stream.Flush()
  190. should.Nil(stream.Error)
  191. should.Equal("a-10000", buf.String())
  192. }
  193. func Test_write_uint32(t *testing.T) {
  194. vals := []uint32{0, 1, 11, 111, 255, 999999, 0xfff, 0xffff, 0xfffff, 0xffffff, 0xfffffff, 0xffffffff}
  195. for _, val := range vals {
  196. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  197. should := require.New(t)
  198. buf := &bytes.Buffer{}
  199. stream := NewStream(buf, 4096)
  200. stream.WriteUint32(val)
  201. stream.Flush()
  202. should.Nil(stream.Error)
  203. should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
  204. })
  205. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  206. should := require.New(t)
  207. buf := &bytes.Buffer{}
  208. stream := NewStream(buf, 4096)
  209. stream.WriteVal(val)
  210. stream.Flush()
  211. should.Nil(stream.Error)
  212. should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
  213. })
  214. }
  215. should := require.New(t)
  216. buf := &bytes.Buffer{}
  217. stream := NewStream(buf, 10)
  218. stream.WriteRaw("a")
  219. stream.WriteUint32(0xffffffff) // should clear buffer
  220. stream.Flush()
  221. should.Nil(stream.Error)
  222. should.Equal("a4294967295", buf.String())
  223. }
  224. func Test_write_int32(t *testing.T) {
  225. vals := []int32{0, 1, 11, 111, 255, 999999, 0xfff, 0xffff, 0xfffff, 0xffffff, 0xfffffff, 0x7fffffff, -0x7fffffff}
  226. for _, val := range vals {
  227. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  228. should := require.New(t)
  229. buf := &bytes.Buffer{}
  230. stream := NewStream(buf, 4096)
  231. stream.WriteInt32(val)
  232. stream.Flush()
  233. should.Nil(stream.Error)
  234. should.Equal(strconv.FormatInt(int64(val), 10), buf.String())
  235. })
  236. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  237. should := require.New(t)
  238. buf := &bytes.Buffer{}
  239. stream := NewStream(buf, 4096)
  240. stream.WriteVal(val)
  241. stream.Flush()
  242. should.Nil(stream.Error)
  243. should.Equal(strconv.FormatInt(int64(val), 10), buf.String())
  244. })
  245. }
  246. should := require.New(t)
  247. buf := &bytes.Buffer{}
  248. stream := NewStream(buf, 11)
  249. stream.WriteRaw("a")
  250. stream.WriteInt32(-0x7fffffff) // should clear buffer
  251. stream.Flush()
  252. should.Nil(stream.Error)
  253. should.Equal("a-2147483647", buf.String())
  254. }
  255. func Test_write_uint64(t *testing.T) {
  256. vals := []uint64{0, 1, 11, 111, 255, 999999, 0xfff, 0xffff, 0xfffff, 0xffffff, 0xfffffff, 0xffffffff,
  257. 0xfffffffff,0xffffffffff,0xfffffffffff,0xffffffffffff,0xfffffffffffff,0xffffffffffffff,
  258. 0xfffffffffffffff,0xffffffffffffffff}
  259. for _, val := range vals {
  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.WriteUint64(val)
  265. stream.Flush()
  266. should.Nil(stream.Error)
  267. should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
  268. })
  269. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  270. should := require.New(t)
  271. buf := &bytes.Buffer{}
  272. stream := NewStream(buf, 4096)
  273. stream.WriteVal(val)
  274. stream.Flush()
  275. should.Nil(stream.Error)
  276. should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
  277. })
  278. }
  279. should := require.New(t)
  280. buf := &bytes.Buffer{}
  281. stream := NewStream(buf, 10)
  282. stream.WriteRaw("a")
  283. stream.WriteUint64(0xffffffff) // should clear buffer
  284. stream.Flush()
  285. should.Nil(stream.Error)
  286. should.Equal("a4294967295", buf.String())
  287. }
  288. func Test_write_int64(t *testing.T) {
  289. vals := []int64{0, 1, 11, 111, 255, 999999, 0xfff, 0xffff, 0xfffff, 0xffffff, 0xfffffff, 0xffffffff,
  290. 0xfffffffff,0xffffffffff,0xfffffffffff,0xffffffffffff,0xfffffffffffff,0xffffffffffffff,
  291. 0xfffffffffffffff,0x7fffffffffffffff,-0x7fffffffffffffff}
  292. for _, val := range vals {
  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.WriteInt64(val)
  298. stream.Flush()
  299. should.Nil(stream.Error)
  300. should.Equal(strconv.FormatInt(val, 10), buf.String())
  301. })
  302. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  303. should := require.New(t)
  304. buf := &bytes.Buffer{}
  305. stream := NewStream(buf, 4096)
  306. stream.WriteVal(val)
  307. stream.Flush()
  308. should.Nil(stream.Error)
  309. should.Equal(strconv.FormatInt(val, 10), buf.String())
  310. })
  311. }
  312. should := require.New(t)
  313. buf := &bytes.Buffer{}
  314. stream := NewStream(buf, 10)
  315. stream.WriteRaw("a")
  316. stream.WriteInt64(0xffffffff) // should clear buffer
  317. stream.Flush()
  318. should.Nil(stream.Error)
  319. should.Equal("a4294967295", buf.String())
  320. }
  321. func Test_write_val_int(t *testing.T) {
  322. should := require.New(t)
  323. buf := &bytes.Buffer{}
  324. stream := NewStream(buf, 4096)
  325. stream.WriteVal(1001)
  326. stream.Flush()
  327. should.Nil(stream.Error)
  328. should.Equal("1001", buf.String())
  329. }
  330. func Test_write_val_int_ptr(t *testing.T) {
  331. should := require.New(t)
  332. buf := &bytes.Buffer{}
  333. stream := NewStream(buf, 4096)
  334. val := 1001
  335. stream.WriteVal(&val)
  336. stream.Flush()
  337. should.Nil(stream.Error)
  338. should.Equal("1001", buf.String())
  339. }
  340. func Benchmark_jsoniter_encode_int(b *testing.B) {
  341. stream := NewStream(ioutil.Discard, 64)
  342. for n := 0; n < b.N; n++ {
  343. stream.n = 0
  344. stream.WriteUint64(0xffffffff)
  345. }
  346. }
  347. func Benchmark_itoa(b *testing.B) {
  348. for n := 0; n < b.N; n++ {
  349. strconv.FormatInt(0xffffffff, 10)
  350. }
  351. }
  352. func Benchmark_jsoniter_int(b *testing.B) {
  353. iter := NewIterator()
  354. input := []byte(`100`)
  355. for n := 0; n < b.N; n++ {
  356. iter.ResetBytes(input)
  357. iter.ReadInt64()
  358. }
  359. }
  360. func Benchmark_json_int(b *testing.B) {
  361. for n := 0; n < b.N; n++ {
  362. result := int64(0)
  363. json.Unmarshal([]byte(`-100`), &result)
  364. }
  365. }