jsoniter_int_test.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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_write_uint8(t *testing.T) {
  67. vals := []uint8{0, 1, 11, 111, 255}
  68. for _, val := range vals {
  69. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  70. should := require.New(t)
  71. buf := &bytes.Buffer{}
  72. stream := NewStream(buf, 4096)
  73. stream.WriteUint8(val)
  74. stream.Flush()
  75. should.Nil(stream.Error)
  76. should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
  77. })
  78. }
  79. should := require.New(t)
  80. buf := &bytes.Buffer{}
  81. stream := NewStream(buf, 3)
  82. stream.WriteString("a")
  83. stream.WriteUint8(100) // should clear buffer
  84. stream.Flush()
  85. should.Nil(stream.Error)
  86. should.Equal("a100", buf.String())
  87. }
  88. func Test_write_int8(t *testing.T) {
  89. vals := []int8{0, 1, -1, 99, 0x7f, -0x7f}
  90. for _, val := range vals {
  91. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  92. should := require.New(t)
  93. buf := &bytes.Buffer{}
  94. stream := NewStream(buf, 4096)
  95. stream.WriteInt8(val)
  96. stream.Flush()
  97. should.Nil(stream.Error)
  98. should.Equal(strconv.FormatInt(int64(val), 10), buf.String())
  99. })
  100. }
  101. should := require.New(t)
  102. buf := &bytes.Buffer{}
  103. stream := NewStream(buf, 4)
  104. stream.WriteString("a")
  105. stream.WriteInt8(-100) // should clear buffer
  106. stream.Flush()
  107. should.Nil(stream.Error)
  108. should.Equal("a-100", buf.String())
  109. }
  110. func Test_write_uint16(t *testing.T) {
  111. vals := []uint16{0, 1, 11, 111, 255, 0xfff, 0xffff}
  112. for _, val := range vals {
  113. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  114. should := require.New(t)
  115. buf := &bytes.Buffer{}
  116. stream := NewStream(buf, 4096)
  117. stream.WriteUint16(val)
  118. stream.Flush()
  119. should.Nil(stream.Error)
  120. should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
  121. })
  122. }
  123. should := require.New(t)
  124. buf := &bytes.Buffer{}
  125. stream := NewStream(buf, 5)
  126. stream.WriteString("a")
  127. stream.WriteUint16(10000) // should clear buffer
  128. stream.Flush()
  129. should.Nil(stream.Error)
  130. should.Equal("a10000", buf.String())
  131. }
  132. func Test_write_int16(t *testing.T) {
  133. vals := []int16{0, 1, 11, 111, 255, 0xfff, 0x7fff, -0x7fff}
  134. for _, val := range vals {
  135. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  136. should := require.New(t)
  137. buf := &bytes.Buffer{}
  138. stream := NewStream(buf, 4096)
  139. stream.WriteInt16(val)
  140. stream.Flush()
  141. should.Nil(stream.Error)
  142. should.Equal(strconv.FormatInt(int64(val), 10), buf.String())
  143. })
  144. }
  145. should := require.New(t)
  146. buf := &bytes.Buffer{}
  147. stream := NewStream(buf, 6)
  148. stream.WriteString("a")
  149. stream.WriteInt16(-10000) // should clear buffer
  150. stream.Flush()
  151. should.Nil(stream.Error)
  152. should.Equal("a-10000", buf.String())
  153. }
  154. func Test_write_uint32(t *testing.T) {
  155. vals := []uint32{0, 1, 11, 111, 255, 999999, 0xfff, 0xffff, 0xfffff, 0xffffff, 0xfffffff, 0xffffffff}
  156. for _, val := range vals {
  157. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  158. should := require.New(t)
  159. buf := &bytes.Buffer{}
  160. stream := NewStream(buf, 4096)
  161. stream.WriteUint32(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 := NewStream(buf, 10)
  170. stream.WriteString("a")
  171. stream.WriteUint32(0xffffffff) // should clear buffer
  172. stream.Flush()
  173. should.Nil(stream.Error)
  174. should.Equal("a4294967295", buf.String())
  175. }
  176. func Test_write_int32(t *testing.T) {
  177. vals := []int32{0, 1, 11, 111, 255, 999999, 0xfff, 0xffff, 0xfffff, 0xffffff, 0xfffffff, 0x7fffffff, -0x7fffffff}
  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 := NewStream(buf, 4096)
  183. stream.WriteInt32(val)
  184. stream.Flush()
  185. should.Nil(stream.Error)
  186. should.Equal(strconv.FormatInt(int64(val), 10), buf.String())
  187. })
  188. }
  189. should := require.New(t)
  190. buf := &bytes.Buffer{}
  191. stream := NewStream(buf, 11)
  192. stream.WriteString("a")
  193. stream.WriteInt32(-0x7fffffff) // should clear buffer
  194. stream.Flush()
  195. should.Nil(stream.Error)
  196. should.Equal("a-2147483647", buf.String())
  197. }
  198. func Test_write_uint64(t *testing.T) {
  199. vals := []uint64{0, 1, 11, 111, 255, 999999, 0xfff, 0xffff, 0xfffff, 0xffffff, 0xfffffff, 0xffffffff,
  200. 0xfffffffff,0xffffffffff,0xfffffffffff,0xffffffffffff,0xfffffffffffff,0xffffffffffffff,
  201. 0xfffffffffffffff,0xffffffffffffffff}
  202. for _, val := range vals {
  203. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  204. should := require.New(t)
  205. buf := &bytes.Buffer{}
  206. stream := NewStream(buf, 4096)
  207. stream.WriteUint64(val)
  208. stream.Flush()
  209. should.Nil(stream.Error)
  210. should.Equal(strconv.FormatUint(uint64(val), 10), buf.String())
  211. })
  212. }
  213. should := require.New(t)
  214. buf := &bytes.Buffer{}
  215. stream := NewStream(buf, 10)
  216. stream.WriteString("a")
  217. stream.WriteUint64(0xffffffff) // should clear buffer
  218. stream.Flush()
  219. should.Nil(stream.Error)
  220. should.Equal("a4294967295", buf.String())
  221. }
  222. func Test_write_int64(t *testing.T) {
  223. vals := []int64{0, 1, 11, 111, 255, 999999, 0xfff, 0xffff, 0xfffff, 0xffffff, 0xfffffff, 0xffffffff,
  224. 0xfffffffff,0xffffffffff,0xfffffffffff,0xffffffffffff,0xfffffffffffff,0xffffffffffffff,
  225. 0xfffffffffffffff,0x7fffffffffffffff,-0x7fffffffffffffff}
  226. for _, val := range vals {
  227. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  228. should := require.New(t)
  229. buf := &bytes.Buffer{}
  230. stream := NewStream(buf, 4096)
  231. stream.WriteInt64(val)
  232. stream.Flush()
  233. should.Nil(stream.Error)
  234. should.Equal(strconv.FormatInt(val, 10), buf.String())
  235. })
  236. }
  237. should := require.New(t)
  238. buf := &bytes.Buffer{}
  239. stream := NewStream(buf, 10)
  240. stream.WriteString("a")
  241. stream.WriteInt64(0xffffffff) // should clear buffer
  242. stream.Flush()
  243. should.Nil(stream.Error)
  244. should.Equal("a4294967295", buf.String())
  245. }
  246. func Benchmark_jsoniter_encode_int(b *testing.B) {
  247. stream := NewStream(ioutil.Discard, 64)
  248. for n := 0; n < b.N; n++ {
  249. stream.n = 0
  250. stream.WriteUint64(0xffffffff)
  251. }
  252. }
  253. func Benchmark_itoa(b *testing.B) {
  254. for n := 0; n < b.N; n++ {
  255. strconv.FormatInt(0xffffffff, 10)
  256. }
  257. }
  258. func Benchmark_jsoniter_int(b *testing.B) {
  259. for n := 0; n < b.N; n++ {
  260. iter := ParseString(`-100`)
  261. iter.ReadInt64()
  262. }
  263. }
  264. func Benchmark_json_int(b *testing.B) {
  265. for n := 0; n < b.N; n++ {
  266. result := int64(0)
  267. json.Unmarshal([]byte(`-100`), &result)
  268. }
  269. }