jsoniter_string_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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_read_string_as_interface(t *testing.T) {
  59. should := require.New(t)
  60. iter := ParseString(`"hello"`)
  61. should.Equal("hello", iter.Read())
  62. }
  63. func Test_read_string_as_any(t *testing.T) {
  64. should := require.New(t)
  65. any, err := UnmarshalAnyFromString(`"hello"`)
  66. should.Nil(err)
  67. should.Equal("hello", any.ToString())
  68. should.True(any.ToBool())
  69. any, err = UnmarshalAnyFromString(`" "`)
  70. should.False(any.ToBool())
  71. any, err = UnmarshalAnyFromString(`"false"`)
  72. should.False(any.ToBool())
  73. any, err = UnmarshalAnyFromString(`"123"`)
  74. should.Equal(123, any.ToInt())
  75. }
  76. func Test_write_string(t *testing.T) {
  77. should := require.New(t)
  78. str, err := MarshalToString("hello")
  79. should.Equal(`"hello"`, str)
  80. should.Nil(err)
  81. str, err = MarshalToString(`hel"lo`)
  82. should.Equal(`"hel\"lo"`, str)
  83. should.Nil(err)
  84. }
  85. func Test_write_val_string(t *testing.T) {
  86. should := require.New(t)
  87. buf := &bytes.Buffer{}
  88. stream := NewStream(buf, 4096)
  89. stream.WriteVal("hello")
  90. stream.Flush()
  91. should.Nil(stream.Error)
  92. should.Equal(`"hello"`, buf.String())
  93. }
  94. func Benchmark_jsoniter_unicode(b *testing.B) {
  95. for n := 0; n < b.N; n++ {
  96. iter := ParseString(`"\ud83d\udc4a"`)
  97. iter.ReadString()
  98. }
  99. }
  100. func Benchmark_jsoniter_ascii(b *testing.B) {
  101. iter := NewIterator()
  102. input := []byte(`"hello, world! hello, world!"`)
  103. b.ResetTimer()
  104. for n := 0; n < b.N; n++ {
  105. iter.ResetBytes(input)
  106. iter.ReadString()
  107. }
  108. }
  109. func Benchmark_jsoniter_string_as_bytes(b *testing.B) {
  110. iter := ParseString(`"hello, world!"`)
  111. b.ResetTimer()
  112. for n := 0; n < b.N; n++ {
  113. iter.ResetBytes(iter.buf)
  114. iter.ReadStringAsSlice()
  115. }
  116. }
  117. func Benchmark_json_unicode(b *testing.B) {
  118. for n := 0; n < b.N; n++ {
  119. result := ""
  120. json.Unmarshal([]byte(`"\ud83d\udc4a"`), &result)
  121. }
  122. }
  123. func Benchmark_json_ascii(b *testing.B) {
  124. for n := 0; n < b.N; n++ {
  125. result := ""
  126. json.Unmarshal([]byte(`"hello"`), &result)
  127. }
  128. }