|
|
@@ -5,89 +5,57 @@ import (
|
|
|
"encoding/json"
|
|
|
"testing"
|
|
|
"github.com/json-iterator/go/require"
|
|
|
+ "fmt"
|
|
|
)
|
|
|
|
|
|
-func Test_read_large_string(t *testing.T) {
|
|
|
- should := require.New(t)
|
|
|
- iter := ParseString(`"0123456789012345678901234567890123456789"`)
|
|
|
- should.Equal("0123456789012345678901234567890123456789", iter.ReadString())
|
|
|
-}
|
|
|
-
|
|
|
-func Test_decode_string_empty(t *testing.T) {
|
|
|
- iter := Parse(bytes.NewBufferString(`""`), 4096)
|
|
|
- val := iter.ReadString()
|
|
|
- if iter.Error != nil {
|
|
|
- t.Fatal(iter.Error)
|
|
|
- }
|
|
|
- if val != "" {
|
|
|
- t.Fatal(val)
|
|
|
+func Test_read_normal_string(t *testing.T) {
|
|
|
+ cases := map[string]string{
|
|
|
+ `"0123456789012345678901234567890123456789"`: `0123456789012345678901234567890123456789`,
|
|
|
+ `""`: ``,
|
|
|
+ `"hello"`: `hello`,
|
|
|
+ }
|
|
|
+ for input, output := range cases {
|
|
|
+ t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
|
|
|
+ should := require.New(t)
|
|
|
+ iter := ParseString(input)
|
|
|
+ should.Equal(output, iter.ReadString())
|
|
|
+ })
|
|
|
+ t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
|
|
|
+ should := require.New(t)
|
|
|
+ iter := Parse(bytes.NewBufferString(input), 2)
|
|
|
+ should.Equal(output, iter.ReadString())
|
|
|
+ })
|
|
|
+ t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
|
|
|
+ should := require.New(t)
|
|
|
+ iter := ParseString(input)
|
|
|
+ should.Equal(output, string(iter.ReadStringAsSlice()))
|
|
|
+ })
|
|
|
+ t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
|
|
|
+ should := require.New(t)
|
|
|
+ iter := Parse(bytes.NewBufferString(input), 2)
|
|
|
+ should.Equal(output, string(iter.ReadStringAsSlice()))
|
|
|
+ })
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func Test_decode_string_hello(t *testing.T) {
|
|
|
- iter := Parse(bytes.NewBufferString(`"hello"`), 4096)
|
|
|
- val := iter.ReadString()
|
|
|
- if iter.Error != nil {
|
|
|
- t.Fatal(iter.Error)
|
|
|
- }
|
|
|
- if val != "hello" {
|
|
|
- t.Fatal(val)
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-func Test_decode_string_escape_quote(t *testing.T) {
|
|
|
- iter := Parse(bytes.NewBufferString(`"hel\"lo"`), 4096)
|
|
|
- val := iter.ReadString()
|
|
|
- if iter.Error != nil {
|
|
|
- t.Fatal(iter.Error)
|
|
|
- }
|
|
|
- if val != `hel"lo` {
|
|
|
- t.Fatal(val)
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-func Test_decode_string_escape_newline(t *testing.T) {
|
|
|
- iter := Parse(bytes.NewBufferString(`"hel\nlo"`), 4096)
|
|
|
- val := iter.ReadString()
|
|
|
- if iter.Error != nil {
|
|
|
- t.Fatal(iter.Error)
|
|
|
- }
|
|
|
- if val != "hel\nlo" {
|
|
|
- t.Fatal(val)
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-func Test_decode_string_escape_unicode(t *testing.T) {
|
|
|
- iter := Parse(bytes.NewBufferString(`"\u4e2d\u6587"`), 4096)
|
|
|
- val := iter.ReadString()
|
|
|
- if iter.Error != nil {
|
|
|
- t.Fatal(iter.Error)
|
|
|
- }
|
|
|
- if val != "中文" {
|
|
|
- t.Fatal(val)
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-func Test_decode_string_escape_unicode_with_surrogate(t *testing.T) {
|
|
|
- iter := Parse(bytes.NewBufferString(`"\ud83d\udc4a"`), 4096)
|
|
|
- val := iter.ReadString()
|
|
|
- if iter.Error != nil {
|
|
|
- t.Fatal(iter.Error)
|
|
|
- }
|
|
|
- if val != "\xf0\x9f\x91\x8a" {
|
|
|
- t.Fatal(val)
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-func Test_decode_string_as_bytes(t *testing.T) {
|
|
|
- iter := Parse(bytes.NewBufferString(`"hello""world"`), 4096)
|
|
|
- val := string(iter.readStringAsBytes())
|
|
|
- if val != "hello" {
|
|
|
- t.Fatal(val)
|
|
|
- }
|
|
|
- val = string(iter.readStringAsBytes())
|
|
|
- if val != "world" {
|
|
|
- t.Fatal(val)
|
|
|
+func Test_read_exotic_string(t *testing.T) {
|
|
|
+ cases := map[string]string{
|
|
|
+ `"hel\"lo"`: `hel"lo`,
|
|
|
+ `"hel\nlo"`: "hel\nlo",
|
|
|
+ `"\u4e2d\u6587"`: "中文",
|
|
|
+ `"\ud83d\udc4a"`: "\xf0\x9f\x91\x8a", // surrogate
|
|
|
+ }
|
|
|
+ for input, output := range cases {
|
|
|
+ t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
|
|
|
+ should := require.New(t)
|
|
|
+ iter := ParseString(input)
|
|
|
+ should.Equal(output, iter.ReadString())
|
|
|
+ })
|
|
|
+ t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
|
|
|
+ should := require.New(t)
|
|
|
+ iter := Parse(bytes.NewBufferString(input), 2)
|
|
|
+ should.Equal(output, iter.ReadString())
|
|
|
+ })
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -133,7 +101,7 @@ func Benchmark_jsoniter_string_as_bytes(b *testing.B) {
|
|
|
b.ResetTimer()
|
|
|
for n := 0; n < b.N; n++ {
|
|
|
iter.ResetBytes(iter.buf)
|
|
|
- iter.readStringAsBytes()
|
|
|
+ iter.ReadStringAsSlice()
|
|
|
}
|
|
|
}
|
|
|
|