jsoniter_int_test.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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_int_from_null(t *testing.T) {
  19. type TestObject struct {
  20. F1 int8
  21. F2 int16
  22. F3 int32
  23. F4 int64
  24. F5 int
  25. F6 uint8
  26. F7 uint16
  27. F8 uint32
  28. F9 uint64
  29. F10 uint
  30. F11 float32
  31. F12 float64
  32. F13 uintptr
  33. }
  34. should := require.New(t)
  35. obj := TestObject{}
  36. err := Unmarshal([]byte(`{
  37. "f1":null,
  38. "f2":null,
  39. "f3":null,
  40. "f4":null,
  41. "f5":null,
  42. "f6":null,
  43. "f7":null,
  44. "f8":null,
  45. "f9":null,
  46. "f10":null,
  47. "f11":null,
  48. "f12":null,
  49. "f13":null
  50. }`), &obj)
  51. should.Nil(err)
  52. }
  53. func _int8(t *testing.T) {
  54. inputs := []string{`127`, `-128`}
  55. for _, input := range inputs {
  56. t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
  57. should := require.New(t)
  58. iter := ParseString(ConfigDefault, input)
  59. expected, err := strconv.ParseInt(input, 10, 8)
  60. should.Nil(err)
  61. should.Equal(int8(expected), iter.ReadInt8())
  62. })
  63. }
  64. }
  65. func Test_read_int16(t *testing.T) {
  66. inputs := []string{`32767`, `-32768`}
  67. for _, input := range inputs {
  68. t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
  69. should := require.New(t)
  70. iter := ParseString(ConfigDefault, input)
  71. expected, err := strconv.ParseInt(input, 10, 16)
  72. should.Nil(err)
  73. should.Equal(int16(expected), iter.ReadInt16())
  74. })
  75. }
  76. }
  77. func Test_read_int32(t *testing.T) {
  78. inputs := []string{`1`, `12`, `123`, `1234`, `12345`, `123456`, `2147483647`, `-2147483648`}
  79. for _, input := range inputs {
  80. t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
  81. should := require.New(t)
  82. iter := ParseString(ConfigDefault, input)
  83. expected, err := strconv.ParseInt(input, 10, 32)
  84. should.Nil(err)
  85. should.Equal(int32(expected), iter.ReadInt32())
  86. })
  87. t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
  88. should := require.New(t)
  89. iter := Parse(ConfigDefault, bytes.NewBufferString(input), 2)
  90. expected, err := strconv.ParseInt(input, 10, 32)
  91. should.Nil(err)
  92. should.Equal(int32(expected), iter.ReadInt32())
  93. })
  94. }
  95. }
  96. func Test_read_int32_array(t *testing.T) {
  97. should := require.New(t)
  98. input := `[123,456,789]`
  99. val := make([]int32, 0)
  100. UnmarshalFromString(input, &val)
  101. should.Equal(3, len(val))
  102. }
  103. func Test_read_int64_array(t *testing.T) {
  104. should := require.New(t)
  105. input := `[123,456,789]`
  106. val := make([]int64, 0)
  107. UnmarshalFromString(input, &val)
  108. should.Equal(3, len(val))
  109. }
  110. func Test_read_int_overflow(t *testing.T) {
  111. should := require.New(t)
  112. inputArr := []string{"123451", "-123451"}
  113. for _, s := range inputArr {
  114. iter := ParseString(ConfigDefault, s)
  115. iter.ReadInt8()
  116. should.NotNil(iter.Error)
  117. iterU := ParseString(ConfigDefault, s)
  118. iterU.ReadUint8()
  119. should.NotNil(iterU.Error)
  120. }
  121. inputArr = []string{"12345678912", "-12345678912"}
  122. for _, s := range inputArr {
  123. iter := ParseString(ConfigDefault, s)
  124. iter.ReadInt16()
  125. should.NotNil(iter.Error)
  126. iterUint := ParseString(ConfigDefault, s)
  127. iterUint.ReadUint16()
  128. should.NotNil(iterUint.Error)
  129. }
  130. inputArr = []string{"3111111111", "-3111111111", "1234232323232323235678912", "-1234567892323232323212"}
  131. for _, s := range inputArr {
  132. iter := ParseString(ConfigDefault, s)
  133. iter.ReadInt32()
  134. should.NotNil(iter.Error)
  135. iterUint := ParseString(ConfigDefault, s)
  136. iterUint.ReadUint32()
  137. should.NotNil(iterUint.Error)
  138. }
  139. inputArr = []string{"9223372036854775811", "-9523372036854775807", "1234232323232323235678912", "-1234567892323232323212"}
  140. for _, s := range inputArr {
  141. iter := ParseString(ConfigDefault, s)
  142. iter.ReadInt64()
  143. should.NotNil(iter.Error)
  144. iterUint := ParseString(ConfigDefault, s)
  145. iterUint.ReadUint64()
  146. should.NotNil(iterUint.Error)
  147. }
  148. }
  149. func Test_read_int64(t *testing.T) {
  150. inputs := []string{`1`, `12`, `123`, `1234`, `12345`, `123456`, `9223372036854775807`, `-9223372036854775808`}
  151. for _, input := range inputs {
  152. t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
  153. should := require.New(t)
  154. iter := ParseString(ConfigDefault, input)
  155. expected, err := strconv.ParseInt(input, 10, 64)
  156. should.Nil(err)
  157. should.Equal(expected, iter.ReadInt64())
  158. })
  159. t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
  160. should := require.New(t)
  161. iter := Parse(ConfigDefault, bytes.NewBufferString(input), 2)
  162. expected, err := strconv.ParseInt(input, 10, 64)
  163. should.Nil(err)
  164. should.Equal(expected, iter.ReadInt64())
  165. })
  166. }
  167. }
  168. func Test_read_int64_overflow(t *testing.T) {
  169. should := require.New(t)
  170. input := "123456789123456789123456789123456789,"
  171. iter := ParseString(ConfigDefault, input)
  172. iter.ReadInt64()
  173. should.NotNil(iter.Error)
  174. }
  175. func Test_wrap_int(t *testing.T) {
  176. should := require.New(t)
  177. str, err := MarshalToString(WrapInt64(100))
  178. should.Nil(err)
  179. should.Equal("100", str)
  180. }
  181. func Test_write_uint8(t *testing.T) {
  182. vals := []uint8{0, 1, 11, 111, 255}
  183. for _, val := range vals {
  184. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  185. should := require.New(t)
  186. buf := &bytes.Buffer{}
  187. stream := NewStream(ConfigDefault, buf, 4096)
  188. stream.WriteUint8(val)
  189. stream.Flush()
  190. should.Nil(stream.Error)
  191. should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
  192. })
  193. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  194. should := require.New(t)
  195. buf := &bytes.Buffer{}
  196. stream := NewStream(ConfigDefault, buf, 4096)
  197. stream.WriteVal(val)
  198. stream.Flush()
  199. should.Nil(stream.Error)
  200. should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
  201. })
  202. }
  203. should := require.New(t)
  204. buf := &bytes.Buffer{}
  205. stream := NewStream(ConfigDefault, buf, 3)
  206. stream.WriteRaw("a")
  207. stream.WriteUint8(100) // should clear buffer
  208. stream.Flush()
  209. should.Nil(stream.Error)
  210. should.Equal("a100", buf.String())
  211. }
  212. func Test_write_int8(t *testing.T) {
  213. vals := []int8{0, 1, -1, 99, 0x7f, -0x80}
  214. for _, val := range vals {
  215. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  216. should := require.New(t)
  217. buf := &bytes.Buffer{}
  218. stream := NewStream(ConfigDefault, buf, 4096)
  219. stream.WriteInt8(val)
  220. stream.Flush()
  221. should.Nil(stream.Error)
  222. should.Equal(strconv.FormatInt(int64(val), 10), buf.String())
  223. })
  224. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  225. should := require.New(t)
  226. buf := &bytes.Buffer{}
  227. stream := NewStream(ConfigDefault, buf, 4096)
  228. stream.WriteVal(val)
  229. stream.Flush()
  230. should.Nil(stream.Error)
  231. should.Equal(strconv.FormatInt(int64(val), 10), buf.String())
  232. })
  233. }
  234. should := require.New(t)
  235. buf := &bytes.Buffer{}
  236. stream := NewStream(ConfigDefault, buf, 4)
  237. stream.WriteRaw("a")
  238. stream.WriteInt8(-100) // should clear buffer
  239. stream.Flush()
  240. should.Nil(stream.Error)
  241. should.Equal("a-100", buf.String())
  242. }
  243. func Test_write_uint16(t *testing.T) {
  244. vals := []uint16{0, 1, 11, 111, 255, 0xfff, 0xffff}
  245. for _, val := range vals {
  246. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  247. should := require.New(t)
  248. buf := &bytes.Buffer{}
  249. stream := NewStream(ConfigDefault, buf, 4096)
  250. stream.WriteUint16(val)
  251. stream.Flush()
  252. should.Nil(stream.Error)
  253. should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
  254. })
  255. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  256. should := require.New(t)
  257. buf := &bytes.Buffer{}
  258. stream := NewStream(ConfigDefault, buf, 4096)
  259. stream.WriteVal(val)
  260. stream.Flush()
  261. should.Nil(stream.Error)
  262. should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
  263. })
  264. }
  265. should := require.New(t)
  266. buf := &bytes.Buffer{}
  267. stream := NewStream(ConfigDefault, buf, 5)
  268. stream.WriteRaw("a")
  269. stream.WriteUint16(10000) // should clear buffer
  270. stream.Flush()
  271. should.Nil(stream.Error)
  272. should.Equal("a10000", buf.String())
  273. }
  274. func Test_write_int16(t *testing.T) {
  275. vals := []int16{0, 1, 11, 111, 255, 0xfff, 0x7fff, -0x8000}
  276. for _, val := range vals {
  277. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  278. should := require.New(t)
  279. buf := &bytes.Buffer{}
  280. stream := NewStream(ConfigDefault, buf, 4096)
  281. stream.WriteInt16(val)
  282. stream.Flush()
  283. should.Nil(stream.Error)
  284. should.Equal(strconv.FormatInt(int64(val), 10), buf.String())
  285. })
  286. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  287. should := require.New(t)
  288. buf := &bytes.Buffer{}
  289. stream := NewStream(ConfigDefault, buf, 4096)
  290. stream.WriteVal(val)
  291. stream.Flush()
  292. should.Nil(stream.Error)
  293. should.Equal(strconv.FormatInt(int64(val), 10), buf.String())
  294. })
  295. }
  296. should := require.New(t)
  297. buf := &bytes.Buffer{}
  298. stream := NewStream(ConfigDefault, buf, 6)
  299. stream.WriteRaw("a")
  300. stream.WriteInt16(-10000) // should clear buffer
  301. stream.Flush()
  302. should.Nil(stream.Error)
  303. should.Equal("a-10000", buf.String())
  304. }
  305. func Test_write_uint32(t *testing.T) {
  306. vals := []uint32{0, 1, 11, 111, 255, 999999, 0xfff, 0xffff, 0xfffff, 0xffffff, 0xfffffff, 0xffffffff}
  307. for _, val := range vals {
  308. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  309. should := require.New(t)
  310. buf := &bytes.Buffer{}
  311. stream := NewStream(ConfigDefault, buf, 4096)
  312. stream.WriteUint32(val)
  313. stream.Flush()
  314. should.Nil(stream.Error)
  315. should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
  316. })
  317. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  318. should := require.New(t)
  319. buf := &bytes.Buffer{}
  320. stream := NewStream(ConfigDefault, buf, 4096)
  321. stream.WriteVal(val)
  322. stream.Flush()
  323. should.Nil(stream.Error)
  324. should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
  325. })
  326. }
  327. should := require.New(t)
  328. buf := &bytes.Buffer{}
  329. stream := NewStream(ConfigDefault, buf, 10)
  330. stream.WriteRaw("a")
  331. stream.WriteUint32(0xffffffff) // should clear buffer
  332. stream.Flush()
  333. should.Nil(stream.Error)
  334. should.Equal("a4294967295", buf.String())
  335. }
  336. func Test_write_int32(t *testing.T) {
  337. vals := []int32{0, 1, 11, 111, 255, 999999, 0xfff, 0xffff, 0xfffff, 0xffffff, 0xfffffff, 0x7fffffff, -0x80000000}
  338. for _, val := range vals {
  339. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  340. should := require.New(t)
  341. buf := &bytes.Buffer{}
  342. stream := NewStream(ConfigDefault, buf, 4096)
  343. stream.WriteInt32(val)
  344. stream.Flush()
  345. should.Nil(stream.Error)
  346. should.Equal(strconv.FormatInt(int64(val), 10), buf.String())
  347. })
  348. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  349. should := require.New(t)
  350. buf := &bytes.Buffer{}
  351. stream := NewStream(ConfigDefault, buf, 4096)
  352. stream.WriteVal(val)
  353. stream.Flush()
  354. should.Nil(stream.Error)
  355. should.Equal(strconv.FormatInt(int64(val), 10), buf.String())
  356. })
  357. }
  358. should := require.New(t)
  359. buf := &bytes.Buffer{}
  360. stream := NewStream(ConfigDefault, buf, 11)
  361. stream.WriteRaw("a")
  362. stream.WriteInt32(-0x7fffffff) // should clear buffer
  363. stream.Flush()
  364. should.Nil(stream.Error)
  365. should.Equal("a-2147483647", buf.String())
  366. }
  367. func Test_write_uint64(t *testing.T) {
  368. vals := []uint64{0, 1, 11, 111, 255, 999999, 0xfff, 0xffff, 0xfffff, 0xffffff, 0xfffffff, 0xffffffff,
  369. 0xfffffffff, 0xffffffffff, 0xfffffffffff, 0xffffffffffff, 0xfffffffffffff, 0xffffffffffffff,
  370. 0xfffffffffffffff, 0xffffffffffffffff}
  371. for _, val := range vals {
  372. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  373. should := require.New(t)
  374. buf := &bytes.Buffer{}
  375. stream := NewStream(ConfigDefault, buf, 4096)
  376. stream.WriteUint64(val)
  377. stream.Flush()
  378. should.Nil(stream.Error)
  379. should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
  380. })
  381. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  382. should := require.New(t)
  383. buf := &bytes.Buffer{}
  384. stream := NewStream(ConfigDefault, buf, 4096)
  385. stream.WriteVal(val)
  386. stream.Flush()
  387. should.Nil(stream.Error)
  388. should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
  389. })
  390. }
  391. should := require.New(t)
  392. buf := &bytes.Buffer{}
  393. stream := NewStream(ConfigDefault, buf, 10)
  394. stream.WriteRaw("a")
  395. stream.WriteUint64(0xffffffff) // should clear buffer
  396. stream.Flush()
  397. should.Nil(stream.Error)
  398. should.Equal("a4294967295", buf.String())
  399. }
  400. func Test_write_int64(t *testing.T) {
  401. vals := []int64{0, 1, 11, 111, 255, 999999, 0xfff, 0xffff, 0xfffff, 0xffffff, 0xfffffff, 0xffffffff,
  402. 0xfffffffff, 0xffffffffff, 0xfffffffffff, 0xffffffffffff, 0xfffffffffffff, 0xffffffffffffff,
  403. 0xfffffffffffffff, 0x7fffffffffffffff, -0x8000000000000000}
  404. for _, val := range vals {
  405. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  406. should := require.New(t)
  407. buf := &bytes.Buffer{}
  408. stream := NewStream(ConfigDefault, buf, 4096)
  409. stream.WriteInt64(val)
  410. stream.Flush()
  411. should.Nil(stream.Error)
  412. should.Equal(strconv.FormatInt(val, 10), buf.String())
  413. })
  414. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  415. should := require.New(t)
  416. buf := &bytes.Buffer{}
  417. stream := NewStream(ConfigDefault, buf, 4096)
  418. stream.WriteVal(val)
  419. stream.Flush()
  420. should.Nil(stream.Error)
  421. should.Equal(strconv.FormatInt(val, 10), buf.String())
  422. })
  423. }
  424. should := require.New(t)
  425. buf := &bytes.Buffer{}
  426. stream := NewStream(ConfigDefault, buf, 10)
  427. stream.WriteRaw("a")
  428. stream.WriteInt64(0xffffffff) // should clear buffer
  429. stream.Flush()
  430. should.Nil(stream.Error)
  431. should.Equal("a4294967295", buf.String())
  432. }
  433. func Test_write_val_int(t *testing.T) {
  434. should := require.New(t)
  435. buf := &bytes.Buffer{}
  436. stream := NewStream(ConfigDefault, buf, 4096)
  437. stream.WriteVal(1001)
  438. stream.Flush()
  439. should.Nil(stream.Error)
  440. should.Equal("1001", buf.String())
  441. }
  442. func Test_write_val_int_ptr(t *testing.T) {
  443. should := require.New(t)
  444. buf := &bytes.Buffer{}
  445. stream := NewStream(ConfigDefault, buf, 4096)
  446. val := 1001
  447. stream.WriteVal(&val)
  448. stream.Flush()
  449. should.Nil(stream.Error)
  450. should.Equal("1001", buf.String())
  451. }
  452. func Test_json_number(t *testing.T) {
  453. should := require.New(t)
  454. var arr []json.Number
  455. err := Unmarshal([]byte(`[1]`), &arr)
  456. should.Nil(err)
  457. should.Equal(json.Number("1"), arr[0])
  458. str, err := MarshalToString(arr)
  459. should.Nil(err)
  460. should.Equal(`[1]`, str)
  461. }
  462. func Test_jsoniter_number(t *testing.T) {
  463. should := require.New(t)
  464. var arr []Number
  465. err := Unmarshal([]byte(`[1]`), &arr)
  466. should.Nil(err)
  467. should.Equal(Number("1"), arr[0])
  468. str, isNumber := CastJsonNumber(arr[0])
  469. should.True(isNumber)
  470. should.Equal("1", str)
  471. }
  472. func Test_non_numeric_as_number(t *testing.T) {
  473. should := require.New(t)
  474. var v1 json.Number
  475. err := Unmarshal([]byte(`"500"`), &v1)
  476. should.Nil(err)
  477. should.Equal("500", string(v1))
  478. var v2 Number
  479. err = Unmarshal([]byte(`"500"`), &v2)
  480. should.Nil(err)
  481. should.Equal("500", string(v2))
  482. }
  483. func Test_null_as_number(t *testing.T) {
  484. should := require.New(t)
  485. var v1 json.Number
  486. err := json.Unmarshal([]byte(`null`), &v1)
  487. should.Nil(err)
  488. should.Equal("", string(v1))
  489. output, err := json.Marshal(v1)
  490. should.NoError(err)
  491. should.Equal("0", string(output))
  492. var v2 Number
  493. err = Unmarshal([]byte(`null`), &v2)
  494. should.Nil(err)
  495. should.Equal("", string(v2))
  496. output, err = Marshal(v2)
  497. should.NoError(err)
  498. should.Equal("0", string(output))
  499. }
  500. func Test_float_as_int(t *testing.T) {
  501. should := require.New(t)
  502. var i int
  503. should.NotNil(Unmarshal([]byte(`1.1`), &i))
  504. }
  505. func Benchmark_jsoniter_encode_int(b *testing.B) {
  506. stream := NewStream(ConfigDefault, ioutil.Discard, 64)
  507. for n := 0; n < b.N; n++ {
  508. stream.n = 0
  509. stream.WriteUint64(0xffffffff)
  510. }
  511. }
  512. func Benchmark_itoa(b *testing.B) {
  513. for n := 0; n < b.N; n++ {
  514. strconv.FormatInt(0xffffffff, 10)
  515. }
  516. }
  517. func Benchmark_jsoniter_int(b *testing.B) {
  518. iter := NewIterator(ConfigDefault)
  519. input := []byte(`100`)
  520. for n := 0; n < b.N; n++ {
  521. iter.ResetBytes(input)
  522. iter.ReadInt64()
  523. }
  524. }
  525. func Benchmark_json_int(b *testing.B) {
  526. for n := 0; n < b.N; n++ {
  527. result := int64(0)
  528. json.Unmarshal([]byte(`-100`), &result)
  529. }
  530. }