jsoniter_string_test.go 2.7 KB

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