jsoniter_int_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package jsoniter
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "testing"
  6. "github.com/json-iterator/go/require"
  7. "fmt"
  8. "strconv"
  9. )
  10. func Test_decode_decode_uint64_0(t *testing.T) {
  11. iter := Parse(bytes.NewBufferString("0"), 4096)
  12. val := iter.ReadUint64()
  13. if iter.Error != nil {
  14. t.Fatal(iter.Error)
  15. }
  16. if val != 0 {
  17. t.Fatal(val)
  18. }
  19. }
  20. func Test_decode_uint64_1(t *testing.T) {
  21. iter := Parse(bytes.NewBufferString("1"), 4096)
  22. val := iter.ReadUint64()
  23. if val != 1 {
  24. t.Fatal(val)
  25. }
  26. }
  27. func Test_decode_uint64_100(t *testing.T) {
  28. iter := Parse(bytes.NewBufferString("100"), 4096)
  29. val := iter.ReadUint64()
  30. if val != 100 {
  31. t.Fatal(val)
  32. }
  33. }
  34. func Test_decode_uint64_100_comma(t *testing.T) {
  35. iter := Parse(bytes.NewBufferString("100,"), 4096)
  36. val := iter.ReadUint64()
  37. if iter.Error != nil {
  38. t.Fatal(iter.Error)
  39. }
  40. if val != 100 {
  41. t.Fatal(val)
  42. }
  43. }
  44. func Test_decode_uint64_invalid(t *testing.T) {
  45. iter := Parse(bytes.NewBufferString(","), 4096)
  46. iter.ReadUint64()
  47. if iter.Error == nil {
  48. t.FailNow()
  49. }
  50. }
  51. func Test_decode_int64_100(t *testing.T) {
  52. iter := Parse(bytes.NewBufferString("100"), 4096)
  53. val := iter.ReadInt64()
  54. if val != 100 {
  55. t.Fatal(val)
  56. }
  57. }
  58. func Test_decode_int64_minus_100(t *testing.T) {
  59. iter := Parse(bytes.NewBufferString("-100"), 4096)
  60. val := iter.ReadInt64()
  61. if val != -100 {
  62. t.Fatal(val)
  63. }
  64. }
  65. func Test_write_uint8(t *testing.T) {
  66. vals := []uint8{0, 1, 11, 111, 255}
  67. for _, val := range vals {
  68. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  69. should := require.New(t)
  70. buf := &bytes.Buffer{}
  71. stream := NewStream(buf, 4096)
  72. stream.WriteUint8(val)
  73. stream.Flush()
  74. should.Nil(stream.Error)
  75. should.Equal(strconv.Itoa(int(val)), buf.String())
  76. })
  77. }
  78. should := require.New(t)
  79. buf := &bytes.Buffer{}
  80. stream := NewStream(buf, 3)
  81. stream.WriteString("a")
  82. stream.WriteUint8(100) // should clear buffer
  83. stream.Flush()
  84. should.Nil(stream.Error)
  85. should.Equal("a100", buf.String())
  86. }
  87. func Test_write_int8(t *testing.T) {
  88. vals := []int8{0, 1, -1, 99, 0x7f, -0x7f}
  89. for _, val := range vals {
  90. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  91. should := require.New(t)
  92. buf := &bytes.Buffer{}
  93. stream := NewStream(buf, 4096)
  94. stream.WriteInt8(val)
  95. stream.Flush()
  96. should.Nil(stream.Error)
  97. should.Equal(strconv.Itoa(int(val)), buf.String())
  98. })
  99. }
  100. should := require.New(t)
  101. buf := &bytes.Buffer{}
  102. stream := NewStream(buf, 4)
  103. stream.WriteString("a")
  104. stream.WriteInt8(-100) // should clear buffer
  105. stream.Flush()
  106. should.Nil(stream.Error)
  107. should.Equal("a-100", buf.String())
  108. }
  109. func Test_write_uint16(t *testing.T) {
  110. vals := []uint16{0, 1, 11, 111, 255, 0xfff, 0xffff}
  111. for _, val := range vals {
  112. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  113. should := require.New(t)
  114. buf := &bytes.Buffer{}
  115. stream := NewStream(buf, 4096)
  116. stream.WriteUint16(val)
  117. stream.Flush()
  118. should.Nil(stream.Error)
  119. should.Equal(strconv.Itoa(int(val)), buf.String())
  120. })
  121. }
  122. should := require.New(t)
  123. buf := &bytes.Buffer{}
  124. stream := NewStream(buf, 5)
  125. stream.WriteString("a")
  126. stream.WriteUint16(10000) // should clear buffer
  127. stream.Flush()
  128. should.Nil(stream.Error)
  129. should.Equal("a10000", buf.String())
  130. }
  131. func Benchmark_jsoniter_int(b *testing.B) {
  132. for n := 0; n < b.N; n++ {
  133. iter := ParseString(`-100`)
  134. iter.ReadInt64()
  135. }
  136. }
  137. func Benchmark_json_int(b *testing.B) {
  138. for n := 0; n < b.N; n++ {
  139. result := int64(0)
  140. json.Unmarshal([]byte(`-100`), &result)
  141. }
  142. }