jsoniter_string_test.go 3.2 KB

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