jsoniter_string_test.go 2.4 KB

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