jsoniter_string_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 Test_write_val_string(t *testing.T) {
  89. should := require.New(t)
  90. buf := &bytes.Buffer{}
  91. stream := NewStream(buf, 4096)
  92. stream.WriteVal("hello")
  93. stream.Flush()
  94. should.Nil(stream.Error)
  95. should.Equal(`"hello"`, buf.String())
  96. }
  97. func Benchmark_jsoniter_unicode(b *testing.B) {
  98. for n := 0; n < b.N; n++ {
  99. iter := ParseString(`"\ud83d\udc4a"`)
  100. iter.ReadString()
  101. }
  102. }
  103. func Benchmark_jsoniter_ascii(b *testing.B) {
  104. iter := ParseString(`"hello, world!"`)
  105. b.ResetTimer()
  106. for n := 0; n < b.N; n++ {
  107. iter.ResetBytes(iter.buf)
  108. iter.ReadString()
  109. }
  110. }
  111. func Benchmark_jsoniter_string_as_bytes(b *testing.B) {
  112. iter := ParseString(`"hello, world!"`)
  113. b.ResetTimer()
  114. for n := 0; n < b.N; n++ {
  115. iter.ResetBytes(iter.buf)
  116. iter.readStringAsBytes()
  117. }
  118. }
  119. func Benchmark_json_unicode(b *testing.B) {
  120. for n := 0; n < b.N; n++ {
  121. result := ""
  122. json.Unmarshal([]byte(`"\ud83d\udc4a"`), &result)
  123. }
  124. }
  125. func Benchmark_json_ascii(b *testing.B) {
  126. for n := 0; n < b.N; n++ {
  127. result := ""
  128. json.Unmarshal([]byte(`"hello"`), &result)
  129. }
  130. }