jsoniter_int_test.go 11 KB

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