jsoniter_int_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. // +build go1.8
  2. package jsoniter
  3. import (
  4. "bytes"
  5. "encoding/json"
  6. "fmt"
  7. "io/ioutil"
  8. "strconv"
  9. "testing"
  10. "github.com/stretchr/testify/require"
  11. )
  12. func Test_read_uint64_invalid(t *testing.T) {
  13. should := require.New(t)
  14. iter := ParseString(ConfigDefault, ",")
  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(ConfigDefault, 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(ConfigDefault, 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(ConfigDefault, 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(ConfigDefault, 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_array(t *testing.T) {
  62. should := require.New(t)
  63. input := `[123,456,789]`
  64. val := make([]int32, 0)
  65. UnmarshalFromString(input, &val)
  66. should.Equal(3, len(val))
  67. }
  68. func Test_read_int64_array(t *testing.T) {
  69. should := require.New(t)
  70. input := `[123,456,789]`
  71. val := make([]int64, 0)
  72. UnmarshalFromString(input, &val)
  73. should.Equal(3, len(val))
  74. }
  75. func Test_read_int_overflow(t *testing.T) {
  76. should := require.New(t)
  77. inputArr := []string{"123451", "-123451"}
  78. for _, s := range inputArr {
  79. iter := ParseString(ConfigDefault, s)
  80. iter.ReadInt8()
  81. should.NotNil(iter.Error)
  82. iterU := ParseString(ConfigDefault, s)
  83. iterU.ReadUint8()
  84. should.NotNil(iterU.Error)
  85. }
  86. inputArr = []string{"12345678912", "-12345678912"}
  87. for _, s := range inputArr {
  88. iter := ParseString(ConfigDefault, s)
  89. iter.ReadInt16()
  90. should.NotNil(iter.Error)
  91. iterUint := ParseString(ConfigDefault, s)
  92. iterUint.ReadUint16()
  93. should.NotNil(iterUint.Error)
  94. }
  95. inputArr = []string{"3111111111", "-3111111111", "1234232323232323235678912", "-1234567892323232323212"}
  96. for _, s := range inputArr {
  97. iter := ParseString(ConfigDefault, s)
  98. iter.ReadInt32()
  99. should.NotNil(iter.Error)
  100. iterUint := ParseString(ConfigDefault, s)
  101. iterUint.ReadUint32()
  102. should.NotNil(iterUint.Error)
  103. }
  104. inputArr = []string{"9223372036854775811", "-9523372036854775807", "1234232323232323235678912", "-1234567892323232323212"}
  105. for _, s := range inputArr {
  106. iter := ParseString(ConfigDefault, s)
  107. iter.ReadInt64()
  108. should.NotNil(iter.Error)
  109. iterUint := ParseString(ConfigDefault, s)
  110. iterUint.ReadUint64()
  111. should.NotNil(iterUint.Error)
  112. }
  113. }
  114. func Test_read_int64(t *testing.T) {
  115. inputs := []string{`1`, `12`, `123`, `1234`, `12345`, `123456`, `9223372036854775807`, `-9223372036854775808`}
  116. for _, input := range inputs {
  117. t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
  118. should := require.New(t)
  119. iter := ParseString(ConfigDefault, input)
  120. expected, err := strconv.ParseInt(input, 10, 64)
  121. should.Nil(err)
  122. should.Equal(expected, iter.ReadInt64())
  123. })
  124. t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
  125. should := require.New(t)
  126. iter := Parse(ConfigDefault, bytes.NewBufferString(input), 2)
  127. expected, err := strconv.ParseInt(input, 10, 64)
  128. should.Nil(err)
  129. should.Equal(expected, iter.ReadInt64())
  130. })
  131. }
  132. }
  133. func Test_read_int64_overflow(t *testing.T) {
  134. should := require.New(t)
  135. input := "123456789123456789123456789123456789,"
  136. iter := ParseString(ConfigDefault, input)
  137. iter.ReadInt64()
  138. should.NotNil(iter.Error)
  139. }
  140. func Test_wrap_int(t *testing.T) {
  141. should := require.New(t)
  142. str, err := MarshalToString(WrapInt64(100))
  143. should.Nil(err)
  144. should.Equal("100", str)
  145. }
  146. func Test_write_uint8(t *testing.T) {
  147. vals := []uint8{0, 1, 11, 111, 255}
  148. for _, val := range vals {
  149. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  150. should := require.New(t)
  151. buf := &bytes.Buffer{}
  152. stream := NewStream(ConfigDefault, buf, 4096)
  153. stream.WriteUint8(val)
  154. stream.Flush()
  155. should.Nil(stream.Error)
  156. should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
  157. })
  158. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  159. should := require.New(t)
  160. buf := &bytes.Buffer{}
  161. stream := NewStream(ConfigDefault, buf, 4096)
  162. stream.WriteVal(val)
  163. stream.Flush()
  164. should.Nil(stream.Error)
  165. should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
  166. })
  167. }
  168. should := require.New(t)
  169. buf := &bytes.Buffer{}
  170. stream := NewStream(ConfigDefault, buf, 3)
  171. stream.WriteRaw("a")
  172. stream.WriteUint8(100) // should clear buffer
  173. stream.Flush()
  174. should.Nil(stream.Error)
  175. should.Equal("a100", buf.String())
  176. }
  177. func Test_write_int8(t *testing.T) {
  178. vals := []int8{0, 1, -1, 99, 0x7f, -0x80}
  179. for _, val := range vals {
  180. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  181. should := require.New(t)
  182. buf := &bytes.Buffer{}
  183. stream := NewStream(ConfigDefault, buf, 4096)
  184. stream.WriteInt8(val)
  185. stream.Flush()
  186. should.Nil(stream.Error)
  187. should.Equal(strconv.FormatInt(int64(val), 10), buf.String())
  188. })
  189. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  190. should := require.New(t)
  191. buf := &bytes.Buffer{}
  192. stream := NewStream(ConfigDefault, buf, 4096)
  193. stream.WriteVal(val)
  194. stream.Flush()
  195. should.Nil(stream.Error)
  196. should.Equal(strconv.FormatInt(int64(val), 10), buf.String())
  197. })
  198. }
  199. should := require.New(t)
  200. buf := &bytes.Buffer{}
  201. stream := NewStream(ConfigDefault, buf, 4)
  202. stream.WriteRaw("a")
  203. stream.WriteInt8(-100) // should clear buffer
  204. stream.Flush()
  205. should.Nil(stream.Error)
  206. should.Equal("a-100", buf.String())
  207. }
  208. func Test_write_uint16(t *testing.T) {
  209. vals := []uint16{0, 1, 11, 111, 255, 0xfff, 0xffff}
  210. for _, val := range vals {
  211. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  212. should := require.New(t)
  213. buf := &bytes.Buffer{}
  214. stream := NewStream(ConfigDefault, buf, 4096)
  215. stream.WriteUint16(val)
  216. stream.Flush()
  217. should.Nil(stream.Error)
  218. should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
  219. })
  220. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  221. should := require.New(t)
  222. buf := &bytes.Buffer{}
  223. stream := NewStream(ConfigDefault, buf, 4096)
  224. stream.WriteVal(val)
  225. stream.Flush()
  226. should.Nil(stream.Error)
  227. should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
  228. })
  229. }
  230. should := require.New(t)
  231. buf := &bytes.Buffer{}
  232. stream := NewStream(ConfigDefault, buf, 5)
  233. stream.WriteRaw("a")
  234. stream.WriteUint16(10000) // should clear buffer
  235. stream.Flush()
  236. should.Nil(stream.Error)
  237. should.Equal("a10000", buf.String())
  238. }
  239. func Test_write_int16(t *testing.T) {
  240. vals := []int16{0, 1, 11, 111, 255, 0xfff, 0x7fff, -0x8000}
  241. for _, val := range vals {
  242. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  243. should := require.New(t)
  244. buf := &bytes.Buffer{}
  245. stream := NewStream(ConfigDefault, buf, 4096)
  246. stream.WriteInt16(val)
  247. stream.Flush()
  248. should.Nil(stream.Error)
  249. should.Equal(strconv.FormatInt(int64(val), 10), buf.String())
  250. })
  251. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  252. should := require.New(t)
  253. buf := &bytes.Buffer{}
  254. stream := NewStream(ConfigDefault, buf, 4096)
  255. stream.WriteVal(val)
  256. stream.Flush()
  257. should.Nil(stream.Error)
  258. should.Equal(strconv.FormatInt(int64(val), 10), buf.String())
  259. })
  260. }
  261. should := require.New(t)
  262. buf := &bytes.Buffer{}
  263. stream := NewStream(ConfigDefault, buf, 6)
  264. stream.WriteRaw("a")
  265. stream.WriteInt16(-10000) // should clear buffer
  266. stream.Flush()
  267. should.Nil(stream.Error)
  268. should.Equal("a-10000", buf.String())
  269. }
  270. func Test_write_uint32(t *testing.T) {
  271. vals := []uint32{0, 1, 11, 111, 255, 999999, 0xfff, 0xffff, 0xfffff, 0xffffff, 0xfffffff, 0xffffffff}
  272. for _, val := range vals {
  273. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  274. should := require.New(t)
  275. buf := &bytes.Buffer{}
  276. stream := NewStream(ConfigDefault, buf, 4096)
  277. stream.WriteUint32(val)
  278. stream.Flush()
  279. should.Nil(stream.Error)
  280. should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
  281. })
  282. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  283. should := require.New(t)
  284. buf := &bytes.Buffer{}
  285. stream := NewStream(ConfigDefault, buf, 4096)
  286. stream.WriteVal(val)
  287. stream.Flush()
  288. should.Nil(stream.Error)
  289. should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
  290. })
  291. }
  292. should := require.New(t)
  293. buf := &bytes.Buffer{}
  294. stream := NewStream(ConfigDefault, buf, 10)
  295. stream.WriteRaw("a")
  296. stream.WriteUint32(0xffffffff) // should clear buffer
  297. stream.Flush()
  298. should.Nil(stream.Error)
  299. should.Equal("a4294967295", buf.String())
  300. }
  301. func Test_write_int32(t *testing.T) {
  302. vals := []int32{0, 1, 11, 111, 255, 999999, 0xfff, 0xffff, 0xfffff, 0xffffff, 0xfffffff, 0x7fffffff, -0x80000000}
  303. for _, val := range vals {
  304. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  305. should := require.New(t)
  306. buf := &bytes.Buffer{}
  307. stream := NewStream(ConfigDefault, buf, 4096)
  308. stream.WriteInt32(val)
  309. stream.Flush()
  310. should.Nil(stream.Error)
  311. should.Equal(strconv.FormatInt(int64(val), 10), buf.String())
  312. })
  313. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  314. should := require.New(t)
  315. buf := &bytes.Buffer{}
  316. stream := NewStream(ConfigDefault, buf, 4096)
  317. stream.WriteVal(val)
  318. stream.Flush()
  319. should.Nil(stream.Error)
  320. should.Equal(strconv.FormatInt(int64(val), 10), buf.String())
  321. })
  322. }
  323. should := require.New(t)
  324. buf := &bytes.Buffer{}
  325. stream := NewStream(ConfigDefault, buf, 11)
  326. stream.WriteRaw("a")
  327. stream.WriteInt32(-0x7fffffff) // should clear buffer
  328. stream.Flush()
  329. should.Nil(stream.Error)
  330. should.Equal("a-2147483647", buf.String())
  331. }
  332. func Test_write_uint64(t *testing.T) {
  333. vals := []uint64{0, 1, 11, 111, 255, 999999, 0xfff, 0xffff, 0xfffff, 0xffffff, 0xfffffff, 0xffffffff,
  334. 0xfffffffff, 0xffffffffff, 0xfffffffffff, 0xffffffffffff, 0xfffffffffffff, 0xffffffffffffff,
  335. 0xfffffffffffffff, 0xffffffffffffffff}
  336. for _, val := range vals {
  337. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  338. should := require.New(t)
  339. buf := &bytes.Buffer{}
  340. stream := NewStream(ConfigDefault, buf, 4096)
  341. stream.WriteUint64(val)
  342. stream.Flush()
  343. should.Nil(stream.Error)
  344. should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
  345. })
  346. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  347. should := require.New(t)
  348. buf := &bytes.Buffer{}
  349. stream := NewStream(ConfigDefault, buf, 4096)
  350. stream.WriteVal(val)
  351. stream.Flush()
  352. should.Nil(stream.Error)
  353. should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
  354. })
  355. }
  356. should := require.New(t)
  357. buf := &bytes.Buffer{}
  358. stream := NewStream(ConfigDefault, buf, 10)
  359. stream.WriteRaw("a")
  360. stream.WriteUint64(0xffffffff) // should clear buffer
  361. stream.Flush()
  362. should.Nil(stream.Error)
  363. should.Equal("a4294967295", buf.String())
  364. }
  365. func Test_write_int64(t *testing.T) {
  366. vals := []int64{0, 1, 11, 111, 255, 999999, 0xfff, 0xffff, 0xfffff, 0xffffff, 0xfffffff, 0xffffffff,
  367. 0xfffffffff, 0xffffffffff, 0xfffffffffff, 0xffffffffffff, 0xfffffffffffff, 0xffffffffffffff,
  368. 0xfffffffffffffff, 0x7fffffffffffffff, -0x8000000000000000}
  369. for _, val := range vals {
  370. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  371. should := require.New(t)
  372. buf := &bytes.Buffer{}
  373. stream := NewStream(ConfigDefault, buf, 4096)
  374. stream.WriteInt64(val)
  375. stream.Flush()
  376. should.Nil(stream.Error)
  377. should.Equal(strconv.FormatInt(val, 10), buf.String())
  378. })
  379. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  380. should := require.New(t)
  381. buf := &bytes.Buffer{}
  382. stream := NewStream(ConfigDefault, buf, 4096)
  383. stream.WriteVal(val)
  384. stream.Flush()
  385. should.Nil(stream.Error)
  386. should.Equal(strconv.FormatInt(val, 10), buf.String())
  387. })
  388. }
  389. should := require.New(t)
  390. buf := &bytes.Buffer{}
  391. stream := NewStream(ConfigDefault, buf, 10)
  392. stream.WriteRaw("a")
  393. stream.WriteInt64(0xffffffff) // should clear buffer
  394. stream.Flush()
  395. should.Nil(stream.Error)
  396. should.Equal("a4294967295", buf.String())
  397. }
  398. func Test_write_val_int(t *testing.T) {
  399. should := require.New(t)
  400. buf := &bytes.Buffer{}
  401. stream := NewStream(ConfigDefault, buf, 4096)
  402. stream.WriteVal(1001)
  403. stream.Flush()
  404. should.Nil(stream.Error)
  405. should.Equal("1001", buf.String())
  406. }
  407. func Test_write_val_int_ptr(t *testing.T) {
  408. should := require.New(t)
  409. buf := &bytes.Buffer{}
  410. stream := NewStream(ConfigDefault, buf, 4096)
  411. val := 1001
  412. stream.WriteVal(&val)
  413. stream.Flush()
  414. should.Nil(stream.Error)
  415. should.Equal("1001", buf.String())
  416. }
  417. func Test_json_number(t *testing.T) {
  418. should := require.New(t)
  419. var arr []json.Number
  420. err := Unmarshal([]byte(`[1]`), &arr)
  421. should.Nil(err)
  422. should.Equal(json.Number("1"), arr[0])
  423. str, err := MarshalToString(arr)
  424. should.Nil(err)
  425. should.Equal(`[1]`, str)
  426. }
  427. func Benchmark_jsoniter_encode_int(b *testing.B) {
  428. stream := NewStream(ConfigDefault, ioutil.Discard, 64)
  429. for n := 0; n < b.N; n++ {
  430. stream.n = 0
  431. stream.WriteUint64(0xffffffff)
  432. }
  433. }
  434. func Benchmark_itoa(b *testing.B) {
  435. for n := 0; n < b.N; n++ {
  436. strconv.FormatInt(0xffffffff, 10)
  437. }
  438. }
  439. func Benchmark_jsoniter_int(b *testing.B) {
  440. iter := NewIterator(ConfigDefault)
  441. input := []byte(`100`)
  442. for n := 0; n < b.N; n++ {
  443. iter.ResetBytes(input)
  444. iter.ReadInt64()
  445. }
  446. }
  447. func Benchmark_json_int(b *testing.B) {
  448. for n := 0; n < b.N; n++ {
  449. result := int64(0)
  450. json.Unmarshal([]byte(`-100`), &result)
  451. }
  452. }