jsoniter_string_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package jsoniter
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "testing"
  6. "github.com/json-iterator/go/require"
  7. "fmt"
  8. )
  9. func Test_read_normal_string(t *testing.T) {
  10. cases := map[string]string{
  11. `"0123456789012345678901234567890123456789"`: `0123456789012345678901234567890123456789`,
  12. `""`: ``,
  13. `"hello"`: `hello`,
  14. }
  15. for input, output := range cases {
  16. t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
  17. should := require.New(t)
  18. iter := ParseString(input)
  19. should.Equal(output, iter.ReadString())
  20. })
  21. t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
  22. should := require.New(t)
  23. iter := Parse(bytes.NewBufferString(input), 2)
  24. should.Equal(output, iter.ReadString())
  25. })
  26. t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
  27. should := require.New(t)
  28. iter := ParseString(input)
  29. should.Equal(output, string(iter.ReadStringAsSlice()))
  30. })
  31. t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
  32. should := require.New(t)
  33. iter := Parse(bytes.NewBufferString(input), 2)
  34. should.Equal(output, string(iter.ReadStringAsSlice()))
  35. })
  36. }
  37. }
  38. func Test_read_exotic_string(t *testing.T) {
  39. cases := map[string]string{
  40. `"hel\"lo"`: `hel"lo`,
  41. `"hel\nlo"`: "hel\nlo",
  42. `"\u4e2d\u6587"`: "中文",
  43. `"\ud83d\udc4a"`: "\xf0\x9f\x91\x8a", // surrogate
  44. }
  45. for input, output := range cases {
  46. t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
  47. should := require.New(t)
  48. iter := ParseString(input)
  49. should.Equal(output, iter.ReadString())
  50. })
  51. t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
  52. should := require.New(t)
  53. iter := Parse(bytes.NewBufferString(input), 2)
  54. should.Equal(output, iter.ReadString())
  55. })
  56. }
  57. }
  58. func Test_write_string(t *testing.T) {
  59. should := require.New(t)
  60. buf := &bytes.Buffer{}
  61. stream := NewStream(buf, 4096)
  62. stream.WriteString("hello")
  63. stream.Flush()
  64. should.Nil(stream.Error)
  65. should.Equal(`"hello"`, buf.String())
  66. }
  67. func Test_write_val_string(t *testing.T) {
  68. should := require.New(t)
  69. buf := &bytes.Buffer{}
  70. stream := NewStream(buf, 4096)
  71. stream.WriteVal("hello")
  72. stream.Flush()
  73. should.Nil(stream.Error)
  74. should.Equal(`"hello"`, buf.String())
  75. }
  76. func Benchmark_jsoniter_unicode(b *testing.B) {
  77. for n := 0; n < b.N; n++ {
  78. iter := ParseString(`"\ud83d\udc4a"`)
  79. iter.ReadString()
  80. }
  81. }
  82. func Benchmark_jsoniter_ascii(b *testing.B) {
  83. iter := NewIterator()
  84. input := []byte(`"hello, world! hello, world!"`)
  85. b.ResetTimer()
  86. for n := 0; n < b.N; n++ {
  87. iter.ResetBytes(input)
  88. iter.ReadString()
  89. }
  90. }
  91. func Benchmark_jsoniter_string_as_bytes(b *testing.B) {
  92. iter := ParseString(`"hello, world!"`)
  93. b.ResetTimer()
  94. for n := 0; n < b.N; n++ {
  95. iter.ResetBytes(iter.buf)
  96. iter.ReadStringAsSlice()
  97. }
  98. }
  99. func Benchmark_json_unicode(b *testing.B) {
  100. for n := 0; n < b.N; n++ {
  101. result := ""
  102. json.Unmarshal([]byte(`"\ud83d\udc4a"`), &result)
  103. }
  104. }
  105. func Benchmark_json_ascii(b *testing.B) {
  106. for n := 0; n < b.N; n++ {
  107. result := ""
  108. json.Unmarshal([]byte(`"hello"`), &result)
  109. }
  110. }