jsoniter_int_test.go 12 KB

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