int_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. package test
  2. import (
  3. "bytes"
  4. "fmt"
  5. "github.com/json-iterator/go"
  6. "github.com/stretchr/testify/require"
  7. "strconv"
  8. "testing"
  9. )
  10. func init() {
  11. unmarshalCases = append(unmarshalCases, unmarshalCase{
  12. ptr: (*struct {
  13. F1 int8
  14. F2 int16
  15. F3 int32
  16. F4 int64
  17. F5 int
  18. F6 uint8
  19. F7 uint16
  20. F8 uint32
  21. F9 uint64
  22. F10 uint
  23. F11 float32
  24. F12 float64
  25. F13 uintptr
  26. })(nil),
  27. input: `{
  28. "f1":null,
  29. "f2":null,
  30. "f3":null,
  31. "f4":null,
  32. "f5":null,
  33. "f6":null,
  34. "f7":null,
  35. "f8":null,
  36. "f9":null,
  37. "f10":null,
  38. "f11":null,
  39. "f12":null,
  40. "f13":null
  41. }`,
  42. })
  43. }
  44. func Test_int8(t *testing.T) {
  45. inputs := []string{`127`, `-128`}
  46. for _, input := range inputs {
  47. t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
  48. should := require.New(t)
  49. iter := jsoniter.ParseString(jsoniter.ConfigDefault, input)
  50. expected, err := strconv.ParseInt(input, 10, 8)
  51. should.Nil(err)
  52. should.Equal(int8(expected), iter.ReadInt8())
  53. })
  54. }
  55. }
  56. func Test_read_int16(t *testing.T) {
  57. inputs := []string{`32767`, `-32768`}
  58. for _, input := range inputs {
  59. t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
  60. should := require.New(t)
  61. iter := jsoniter.ParseString(jsoniter.ConfigDefault, input)
  62. expected, err := strconv.ParseInt(input, 10, 16)
  63. should.Nil(err)
  64. should.Equal(int16(expected), iter.ReadInt16())
  65. })
  66. }
  67. }
  68. func Test_read_int32(t *testing.T) {
  69. inputs := []string{`1`, `12`, `123`, `1234`, `12345`, `123456`, `2147483647`, `-2147483648`}
  70. for _, input := range inputs {
  71. t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
  72. should := require.New(t)
  73. iter := jsoniter.ParseString(jsoniter.ConfigDefault, input)
  74. expected, err := strconv.ParseInt(input, 10, 32)
  75. should.Nil(err)
  76. should.Equal(int32(expected), iter.ReadInt32())
  77. })
  78. t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
  79. should := require.New(t)
  80. iter := jsoniter.Parse(jsoniter.ConfigDefault, bytes.NewBufferString(input), 2)
  81. expected, err := strconv.ParseInt(input, 10, 32)
  82. should.Nil(err)
  83. should.Equal(int32(expected), iter.ReadInt32())
  84. })
  85. }
  86. }
  87. func Test_read_int_overflow(t *testing.T) {
  88. should := require.New(t)
  89. inputArr := []string{"123451", "-123451"}
  90. for _, s := range inputArr {
  91. iter := jsoniter.ParseString(jsoniter.ConfigDefault, s)
  92. iter.ReadInt8()
  93. should.NotNil(iter.Error)
  94. iterU := jsoniter.ParseString(jsoniter.ConfigDefault, s)
  95. iterU.ReadUint8()
  96. should.NotNil(iterU.Error)
  97. }
  98. inputArr = []string{"12345678912", "-12345678912"}
  99. for _, s := range inputArr {
  100. iter := jsoniter.ParseString(jsoniter.ConfigDefault, s)
  101. iter.ReadInt16()
  102. should.NotNil(iter.Error)
  103. iterUint := jsoniter.ParseString(jsoniter.ConfigDefault, s)
  104. iterUint.ReadUint16()
  105. should.NotNil(iterUint.Error)
  106. }
  107. inputArr = []string{"3111111111", "-3111111111", "1234232323232323235678912", "-1234567892323232323212"}
  108. for _, s := range inputArr {
  109. iter := jsoniter.ParseString(jsoniter.ConfigDefault, s)
  110. iter.ReadInt32()
  111. should.NotNil(iter.Error)
  112. iterUint := jsoniter.ParseString(jsoniter.ConfigDefault, s)
  113. iterUint.ReadUint32()
  114. should.NotNil(iterUint.Error)
  115. }
  116. inputArr = []string{"9223372036854775811", "-9523372036854775807", "1234232323232323235678912", "-1234567892323232323212"}
  117. for _, s := range inputArr {
  118. iter := jsoniter.ParseString(jsoniter.ConfigDefault, s)
  119. iter.ReadInt64()
  120. should.NotNil(iter.Error)
  121. iterUint := jsoniter.ParseString(jsoniter.ConfigDefault, s)
  122. iterUint.ReadUint64()
  123. should.NotNil(iterUint.Error)
  124. }
  125. }
  126. func Test_read_int64(t *testing.T) {
  127. inputs := []string{`1`, `12`, `123`, `1234`, `12345`, `123456`, `9223372036854775807`, `-9223372036854775808`}
  128. for _, input := range inputs {
  129. t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
  130. should := require.New(t)
  131. iter := jsoniter.ParseString(jsoniter.ConfigDefault, input)
  132. expected, err := strconv.ParseInt(input, 10, 64)
  133. should.Nil(err)
  134. should.Equal(expected, iter.ReadInt64())
  135. })
  136. t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
  137. should := require.New(t)
  138. iter := jsoniter.Parse(jsoniter.ConfigDefault, bytes.NewBufferString(input), 2)
  139. expected, err := strconv.ParseInt(input, 10, 64)
  140. should.Nil(err)
  141. should.Equal(expected, iter.ReadInt64())
  142. })
  143. }
  144. }
  145. func Test_write_uint8(t *testing.T) {
  146. vals := []uint8{0, 1, 11, 111, 255}
  147. for _, val := range vals {
  148. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  149. should := require.New(t)
  150. buf := &bytes.Buffer{}
  151. stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4096)
  152. stream.WriteUint8(val)
  153. stream.Flush()
  154. should.Nil(stream.Error)
  155. should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
  156. })
  157. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  158. should := require.New(t)
  159. buf := &bytes.Buffer{}
  160. stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4096)
  161. stream.WriteVal(val)
  162. stream.Flush()
  163. should.Nil(stream.Error)
  164. should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
  165. })
  166. }
  167. should := require.New(t)
  168. buf := &bytes.Buffer{}
  169. stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 3)
  170. stream.WriteRaw("a")
  171. stream.WriteUint8(100) // should clear buffer
  172. stream.Flush()
  173. should.Nil(stream.Error)
  174. should.Equal("a100", buf.String())
  175. }
  176. func Test_write_int8(t *testing.T) {
  177. vals := []int8{0, 1, -1, 99, 0x7f, -0x80}
  178. for _, val := range vals {
  179. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  180. should := require.New(t)
  181. buf := &bytes.Buffer{}
  182. stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4096)
  183. stream.WriteInt8(val)
  184. stream.Flush()
  185. should.Nil(stream.Error)
  186. should.Equal(strconv.FormatInt(int64(val), 10), buf.String())
  187. })
  188. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  189. should := require.New(t)
  190. buf := &bytes.Buffer{}
  191. stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4096)
  192. stream.WriteVal(val)
  193. stream.Flush()
  194. should.Nil(stream.Error)
  195. should.Equal(strconv.FormatInt(int64(val), 10), buf.String())
  196. })
  197. }
  198. should := require.New(t)
  199. buf := &bytes.Buffer{}
  200. stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4)
  201. stream.WriteRaw("a")
  202. stream.WriteInt8(-100) // should clear buffer
  203. stream.Flush()
  204. should.Nil(stream.Error)
  205. should.Equal("a-100", buf.String())
  206. }
  207. func Test_write_uint16(t *testing.T) {
  208. vals := []uint16{0, 1, 11, 111, 255, 0xfff, 0xffff}
  209. for _, val := range vals {
  210. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  211. should := require.New(t)
  212. buf := &bytes.Buffer{}
  213. stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4096)
  214. stream.WriteUint16(val)
  215. stream.Flush()
  216. should.Nil(stream.Error)
  217. should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
  218. })
  219. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  220. should := require.New(t)
  221. buf := &bytes.Buffer{}
  222. stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4096)
  223. stream.WriteVal(val)
  224. stream.Flush()
  225. should.Nil(stream.Error)
  226. should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
  227. })
  228. }
  229. should := require.New(t)
  230. buf := &bytes.Buffer{}
  231. stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 5)
  232. stream.WriteRaw("a")
  233. stream.WriteUint16(10000) // should clear buffer
  234. stream.Flush()
  235. should.Nil(stream.Error)
  236. should.Equal("a10000", buf.String())
  237. }
  238. func Test_write_int16(t *testing.T) {
  239. vals := []int16{0, 1, 11, 111, 255, 0xfff, 0x7fff, -0x8000}
  240. for _, val := range vals {
  241. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  242. should := require.New(t)
  243. buf := &bytes.Buffer{}
  244. stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4096)
  245. stream.WriteInt16(val)
  246. stream.Flush()
  247. should.Nil(stream.Error)
  248. should.Equal(strconv.FormatInt(int64(val), 10), buf.String())
  249. })
  250. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  251. should := require.New(t)
  252. buf := &bytes.Buffer{}
  253. stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4096)
  254. stream.WriteVal(val)
  255. stream.Flush()
  256. should.Nil(stream.Error)
  257. should.Equal(strconv.FormatInt(int64(val), 10), buf.String())
  258. })
  259. }
  260. should := require.New(t)
  261. buf := &bytes.Buffer{}
  262. stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 6)
  263. stream.WriteRaw("a")
  264. stream.WriteInt16(-10000) // should clear buffer
  265. stream.Flush()
  266. should.Nil(stream.Error)
  267. should.Equal("a-10000", buf.String())
  268. }
  269. func Test_write_uint32(t *testing.T) {
  270. vals := []uint32{0, 1, 11, 111, 255, 999999, 0xfff, 0xffff, 0xfffff, 0xffffff, 0xfffffff, 0xffffffff}
  271. for _, val := range vals {
  272. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  273. should := require.New(t)
  274. buf := &bytes.Buffer{}
  275. stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4096)
  276. stream.WriteUint32(val)
  277. stream.Flush()
  278. should.Nil(stream.Error)
  279. should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
  280. })
  281. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  282. should := require.New(t)
  283. buf := &bytes.Buffer{}
  284. stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4096)
  285. stream.WriteVal(val)
  286. stream.Flush()
  287. should.Nil(stream.Error)
  288. should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
  289. })
  290. }
  291. should := require.New(t)
  292. buf := &bytes.Buffer{}
  293. stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 10)
  294. stream.WriteRaw("a")
  295. stream.WriteUint32(0xffffffff) // should clear buffer
  296. stream.Flush()
  297. should.Nil(stream.Error)
  298. should.Equal("a4294967295", buf.String())
  299. }
  300. func Test_write_int32(t *testing.T) {
  301. vals := []int32{0, 1, 11, 111, 255, 999999, 0xfff, 0xffff, 0xfffff, 0xffffff, 0xfffffff, 0x7fffffff, -0x80000000}
  302. for _, val := range vals {
  303. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  304. should := require.New(t)
  305. buf := &bytes.Buffer{}
  306. stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4096)
  307. stream.WriteInt32(val)
  308. stream.Flush()
  309. should.Nil(stream.Error)
  310. should.Equal(strconv.FormatInt(int64(val), 10), buf.String())
  311. })
  312. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  313. should := require.New(t)
  314. buf := &bytes.Buffer{}
  315. stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4096)
  316. stream.WriteVal(val)
  317. stream.Flush()
  318. should.Nil(stream.Error)
  319. should.Equal(strconv.FormatInt(int64(val), 10), buf.String())
  320. })
  321. }
  322. should := require.New(t)
  323. buf := &bytes.Buffer{}
  324. stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 11)
  325. stream.WriteRaw("a")
  326. stream.WriteInt32(-0x7fffffff) // should clear buffer
  327. stream.Flush()
  328. should.Nil(stream.Error)
  329. should.Equal("a-2147483647", buf.String())
  330. }
  331. func Test_write_uint64(t *testing.T) {
  332. vals := []uint64{0, 1, 11, 111, 255, 999999, 0xfff, 0xffff, 0xfffff, 0xffffff, 0xfffffff, 0xffffffff,
  333. 0xfffffffff, 0xffffffffff, 0xfffffffffff, 0xffffffffffff, 0xfffffffffffff, 0xffffffffffffff,
  334. 0xfffffffffffffff, 0xffffffffffffffff}
  335. for _, val := range vals {
  336. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  337. should := require.New(t)
  338. buf := &bytes.Buffer{}
  339. stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4096)
  340. stream.WriteUint64(val)
  341. stream.Flush()
  342. should.Nil(stream.Error)
  343. should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
  344. })
  345. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  346. should := require.New(t)
  347. buf := &bytes.Buffer{}
  348. stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4096)
  349. stream.WriteVal(val)
  350. stream.Flush()
  351. should.Nil(stream.Error)
  352. should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
  353. })
  354. }
  355. should := require.New(t)
  356. buf := &bytes.Buffer{}
  357. stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 10)
  358. stream.WriteRaw("a")
  359. stream.WriteUint64(0xffffffff) // should clear buffer
  360. stream.Flush()
  361. should.Nil(stream.Error)
  362. should.Equal("a4294967295", buf.String())
  363. }
  364. func Test_write_int64(t *testing.T) {
  365. vals := []int64{0, 1, 11, 111, 255, 999999, 0xfff, 0xffff, 0xfffff, 0xffffff, 0xfffffff, 0xffffffff,
  366. 0xfffffffff, 0xffffffffff, 0xfffffffffff, 0xffffffffffff, 0xfffffffffffff, 0xffffffffffffff,
  367. 0xfffffffffffffff, 0x7fffffffffffffff, -0x8000000000000000}
  368. for _, val := range vals {
  369. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  370. should := require.New(t)
  371. buf := &bytes.Buffer{}
  372. stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4096)
  373. stream.WriteInt64(val)
  374. stream.Flush()
  375. should.Nil(stream.Error)
  376. should.Equal(strconv.FormatInt(val, 10), buf.String())
  377. })
  378. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  379. should := require.New(t)
  380. buf := &bytes.Buffer{}
  381. stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4096)
  382. stream.WriteVal(val)
  383. stream.Flush()
  384. should.Nil(stream.Error)
  385. should.Equal(strconv.FormatInt(val, 10), buf.String())
  386. })
  387. }
  388. should := require.New(t)
  389. buf := &bytes.Buffer{}
  390. stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 10)
  391. stream.WriteRaw("a")
  392. stream.WriteInt64(0xffffffff) // should clear buffer
  393. stream.Flush()
  394. should.Nil(stream.Error)
  395. should.Equal("a4294967295", buf.String())
  396. }