|
|
@@ -1,69 +1,55 @@
|
|
|
-// +build go1.8
|
|
|
-
|
|
|
-package jsoniter
|
|
|
+package test
|
|
|
|
|
|
import (
|
|
|
- "bytes"
|
|
|
- "encoding/json"
|
|
|
- "fmt"
|
|
|
- "io/ioutil"
|
|
|
+ "github.com/stretchr/testify/require"
|
|
|
"strconv"
|
|
|
+ "fmt"
|
|
|
"testing"
|
|
|
-
|
|
|
- "github.com/stretchr/testify/require"
|
|
|
+ "bytes"
|
|
|
+ "github.com/json-iterator/go"
|
|
|
)
|
|
|
|
|
|
-func Test_read_uint64_invalid(t *testing.T) {
|
|
|
- should := require.New(t)
|
|
|
- iter := ParseString(ConfigDefault, ",")
|
|
|
- iter.ReadUint64()
|
|
|
- should.NotNil(iter.Error)
|
|
|
+func init() {
|
|
|
+ unmarshalCases = append(unmarshalCases, unmarshalCase{
|
|
|
+ ptr: (*struct {
|
|
|
+ F1 int8
|
|
|
+ F2 int16
|
|
|
+ F3 int32
|
|
|
+ F4 int64
|
|
|
+ F5 int
|
|
|
+ F6 uint8
|
|
|
+ F7 uint16
|
|
|
+ F8 uint32
|
|
|
+ F9 uint64
|
|
|
+ F10 uint
|
|
|
+ F11 float32
|
|
|
+ F12 float64
|
|
|
+ F13 uintptr
|
|
|
+ })(nil),
|
|
|
+ input: `{
|
|
|
+ "f1":null,
|
|
|
+ "f2":null,
|
|
|
+ "f3":null,
|
|
|
+ "f4":null,
|
|
|
+ "f5":null,
|
|
|
+ "f6":null,
|
|
|
+ "f7":null,
|
|
|
+ "f8":null,
|
|
|
+ "f9":null,
|
|
|
+ "f10":null,
|
|
|
+ "f11":null,
|
|
|
+ "f12":null,
|
|
|
+ "f13":null
|
|
|
+ }`,
|
|
|
+ })
|
|
|
}
|
|
|
|
|
|
-func Test_read_int_from_null(t *testing.T) {
|
|
|
-
|
|
|
- type TestObject struct {
|
|
|
- F1 int8
|
|
|
- F2 int16
|
|
|
- F3 int32
|
|
|
- F4 int64
|
|
|
- F5 int
|
|
|
- F6 uint8
|
|
|
- F7 uint16
|
|
|
- F8 uint32
|
|
|
- F9 uint64
|
|
|
- F10 uint
|
|
|
- F11 float32
|
|
|
- F12 float64
|
|
|
- F13 uintptr
|
|
|
- }
|
|
|
-
|
|
|
- should := require.New(t)
|
|
|
- obj := TestObject{}
|
|
|
- err := Unmarshal([]byte(`{
|
|
|
- "f1":null,
|
|
|
- "f2":null,
|
|
|
- "f3":null,
|
|
|
- "f4":null,
|
|
|
- "f5":null,
|
|
|
- "f6":null,
|
|
|
- "f7":null,
|
|
|
- "f8":null,
|
|
|
- "f9":null,
|
|
|
- "f10":null,
|
|
|
- "f11":null,
|
|
|
- "f12":null,
|
|
|
- "f13":null
|
|
|
- }`), &obj)
|
|
|
- should.Nil(err)
|
|
|
-}
|
|
|
-
|
|
|
-func _int8(t *testing.T) {
|
|
|
+func Test_int8(t *testing.T) {
|
|
|
inputs := []string{`127`, `-128`}
|
|
|
for _, input := range inputs {
|
|
|
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
|
|
should := require.New(t)
|
|
|
- iter := ParseString(ConfigDefault, input)
|
|
|
+ iter := jsoniter.ParseString(jsoniter.ConfigDefault, input)
|
|
|
expected, err := strconv.ParseInt(input, 10, 8)
|
|
|
should.Nil(err)
|
|
|
should.Equal(int8(expected), iter.ReadInt8())
|
|
|
@@ -76,7 +62,7 @@ func Test_read_int16(t *testing.T) {
|
|
|
for _, input := range inputs {
|
|
|
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
|
|
should := require.New(t)
|
|
|
- iter := ParseString(ConfigDefault, input)
|
|
|
+ iter := jsoniter.ParseString(jsoniter.ConfigDefault, input)
|
|
|
expected, err := strconv.ParseInt(input, 10, 16)
|
|
|
should.Nil(err)
|
|
|
should.Equal(int16(expected), iter.ReadInt16())
|
|
|
@@ -89,14 +75,14 @@ func Test_read_int32(t *testing.T) {
|
|
|
for _, input := range inputs {
|
|
|
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
|
|
should := require.New(t)
|
|
|
- iter := ParseString(ConfigDefault, input)
|
|
|
+ iter := jsoniter.ParseString(jsoniter.ConfigDefault, input)
|
|
|
expected, err := strconv.ParseInt(input, 10, 32)
|
|
|
should.Nil(err)
|
|
|
should.Equal(int32(expected), iter.ReadInt32())
|
|
|
})
|
|
|
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
|
|
should := require.New(t)
|
|
|
- iter := Parse(ConfigDefault, bytes.NewBufferString(input), 2)
|
|
|
+ iter := jsoniter.Parse(jsoniter.ConfigDefault, bytes.NewBufferString(input), 2)
|
|
|
expected, err := strconv.ParseInt(input, 10, 32)
|
|
|
should.Nil(err)
|
|
|
should.Equal(int32(expected), iter.ReadInt32())
|
|
|
@@ -104,31 +90,15 @@ func Test_read_int32(t *testing.T) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func Test_read_int32_array(t *testing.T) {
|
|
|
- should := require.New(t)
|
|
|
- input := `[123,456,789]`
|
|
|
- val := make([]int32, 0)
|
|
|
- UnmarshalFromString(input, &val)
|
|
|
- should.Equal(3, len(val))
|
|
|
-}
|
|
|
-
|
|
|
-func Test_read_int64_array(t *testing.T) {
|
|
|
- should := require.New(t)
|
|
|
- input := `[123,456,789]`
|
|
|
- val := make([]int64, 0)
|
|
|
- UnmarshalFromString(input, &val)
|
|
|
- should.Equal(3, len(val))
|
|
|
-}
|
|
|
-
|
|
|
func Test_read_int_overflow(t *testing.T) {
|
|
|
should := require.New(t)
|
|
|
inputArr := []string{"123451", "-123451"}
|
|
|
for _, s := range inputArr {
|
|
|
- iter := ParseString(ConfigDefault, s)
|
|
|
+ iter := jsoniter.ParseString(jsoniter.ConfigDefault, s)
|
|
|
iter.ReadInt8()
|
|
|
should.NotNil(iter.Error)
|
|
|
|
|
|
- iterU := ParseString(ConfigDefault, s)
|
|
|
+ iterU := jsoniter.ParseString(jsoniter.ConfigDefault, s)
|
|
|
iterU.ReadUint8()
|
|
|
should.NotNil(iterU.Error)
|
|
|
|
|
|
@@ -136,33 +106,33 @@ func Test_read_int_overflow(t *testing.T) {
|
|
|
|
|
|
inputArr = []string{"12345678912", "-12345678912"}
|
|
|
for _, s := range inputArr {
|
|
|
- iter := ParseString(ConfigDefault, s)
|
|
|
+ iter := jsoniter.ParseString(jsoniter.ConfigDefault, s)
|
|
|
iter.ReadInt16()
|
|
|
should.NotNil(iter.Error)
|
|
|
|
|
|
- iterUint := ParseString(ConfigDefault, s)
|
|
|
+ iterUint := jsoniter.ParseString(jsoniter.ConfigDefault, s)
|
|
|
iterUint.ReadUint16()
|
|
|
should.NotNil(iterUint.Error)
|
|
|
}
|
|
|
|
|
|
inputArr = []string{"3111111111", "-3111111111", "1234232323232323235678912", "-1234567892323232323212"}
|
|
|
for _, s := range inputArr {
|
|
|
- iter := ParseString(ConfigDefault, s)
|
|
|
+ iter := jsoniter.ParseString(jsoniter.ConfigDefault, s)
|
|
|
iter.ReadInt32()
|
|
|
should.NotNil(iter.Error)
|
|
|
|
|
|
- iterUint := ParseString(ConfigDefault, s)
|
|
|
+ iterUint := jsoniter.ParseString(jsoniter.ConfigDefault, s)
|
|
|
iterUint.ReadUint32()
|
|
|
should.NotNil(iterUint.Error)
|
|
|
}
|
|
|
|
|
|
inputArr = []string{"9223372036854775811", "-9523372036854775807", "1234232323232323235678912", "-1234567892323232323212"}
|
|
|
for _, s := range inputArr {
|
|
|
- iter := ParseString(ConfigDefault, s)
|
|
|
+ iter := jsoniter.ParseString(jsoniter.ConfigDefault, s)
|
|
|
iter.ReadInt64()
|
|
|
should.NotNil(iter.Error)
|
|
|
|
|
|
- iterUint := ParseString(ConfigDefault, s)
|
|
|
+ iterUint := jsoniter.ParseString(jsoniter.ConfigDefault, s)
|
|
|
iterUint.ReadUint64()
|
|
|
should.NotNil(iterUint.Error)
|
|
|
}
|
|
|
@@ -173,14 +143,14 @@ func Test_read_int64(t *testing.T) {
|
|
|
for _, input := range inputs {
|
|
|
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
|
|
should := require.New(t)
|
|
|
- iter := ParseString(ConfigDefault, input)
|
|
|
+ iter := jsoniter.ParseString(jsoniter.ConfigDefault, input)
|
|
|
expected, err := strconv.ParseInt(input, 10, 64)
|
|
|
should.Nil(err)
|
|
|
should.Equal(expected, iter.ReadInt64())
|
|
|
})
|
|
|
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
|
|
should := require.New(t)
|
|
|
- iter := Parse(ConfigDefault, bytes.NewBufferString(input), 2)
|
|
|
+ iter := jsoniter.Parse(jsoniter.ConfigDefault, bytes.NewBufferString(input), 2)
|
|
|
expected, err := strconv.ParseInt(input, 10, 64)
|
|
|
should.Nil(err)
|
|
|
should.Equal(expected, iter.ReadInt64())
|
|
|
@@ -188,20 +158,6 @@ func Test_read_int64(t *testing.T) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func Test_read_int64_overflow(t *testing.T) {
|
|
|
- should := require.New(t)
|
|
|
- input := "123456789123456789123456789123456789,"
|
|
|
- iter := ParseString(ConfigDefault, input)
|
|
|
- iter.ReadInt64()
|
|
|
- should.NotNil(iter.Error)
|
|
|
-}
|
|
|
-
|
|
|
-func Test_wrap_int(t *testing.T) {
|
|
|
- should := require.New(t)
|
|
|
- str, err := MarshalToString(WrapInt64(100))
|
|
|
- should.Nil(err)
|
|
|
- should.Equal("100", str)
|
|
|
-}
|
|
|
|
|
|
func Test_write_uint8(t *testing.T) {
|
|
|
vals := []uint8{0, 1, 11, 111, 255}
|
|
|
@@ -209,7 +165,7 @@ func Test_write_uint8(t *testing.T) {
|
|
|
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
|
|
should := require.New(t)
|
|
|
buf := &bytes.Buffer{}
|
|
|
- stream := NewStream(ConfigDefault, buf, 4096)
|
|
|
+ stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4096)
|
|
|
stream.WriteUint8(val)
|
|
|
stream.Flush()
|
|
|
should.Nil(stream.Error)
|
|
|
@@ -218,7 +174,7 @@ func Test_write_uint8(t *testing.T) {
|
|
|
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
|
|
should := require.New(t)
|
|
|
buf := &bytes.Buffer{}
|
|
|
- stream := NewStream(ConfigDefault, buf, 4096)
|
|
|
+ stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4096)
|
|
|
stream.WriteVal(val)
|
|
|
stream.Flush()
|
|
|
should.Nil(stream.Error)
|
|
|
@@ -227,7 +183,7 @@ func Test_write_uint8(t *testing.T) {
|
|
|
}
|
|
|
should := require.New(t)
|
|
|
buf := &bytes.Buffer{}
|
|
|
- stream := NewStream(ConfigDefault, buf, 3)
|
|
|
+ stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 3)
|
|
|
stream.WriteRaw("a")
|
|
|
stream.WriteUint8(100) // should clear buffer
|
|
|
stream.Flush()
|
|
|
@@ -241,7 +197,7 @@ func Test_write_int8(t *testing.T) {
|
|
|
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
|
|
should := require.New(t)
|
|
|
buf := &bytes.Buffer{}
|
|
|
- stream := NewStream(ConfigDefault, buf, 4096)
|
|
|
+ stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4096)
|
|
|
stream.WriteInt8(val)
|
|
|
stream.Flush()
|
|
|
should.Nil(stream.Error)
|
|
|
@@ -250,7 +206,7 @@ func Test_write_int8(t *testing.T) {
|
|
|
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
|
|
should := require.New(t)
|
|
|
buf := &bytes.Buffer{}
|
|
|
- stream := NewStream(ConfigDefault, buf, 4096)
|
|
|
+ stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4096)
|
|
|
stream.WriteVal(val)
|
|
|
stream.Flush()
|
|
|
should.Nil(stream.Error)
|
|
|
@@ -259,7 +215,7 @@ func Test_write_int8(t *testing.T) {
|
|
|
}
|
|
|
should := require.New(t)
|
|
|
buf := &bytes.Buffer{}
|
|
|
- stream := NewStream(ConfigDefault, buf, 4)
|
|
|
+ stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4)
|
|
|
stream.WriteRaw("a")
|
|
|
stream.WriteInt8(-100) // should clear buffer
|
|
|
stream.Flush()
|
|
|
@@ -273,7 +229,7 @@ func Test_write_uint16(t *testing.T) {
|
|
|
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
|
|
should := require.New(t)
|
|
|
buf := &bytes.Buffer{}
|
|
|
- stream := NewStream(ConfigDefault, buf, 4096)
|
|
|
+ stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4096)
|
|
|
stream.WriteUint16(val)
|
|
|
stream.Flush()
|
|
|
should.Nil(stream.Error)
|
|
|
@@ -282,7 +238,7 @@ func Test_write_uint16(t *testing.T) {
|
|
|
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
|
|
should := require.New(t)
|
|
|
buf := &bytes.Buffer{}
|
|
|
- stream := NewStream(ConfigDefault, buf, 4096)
|
|
|
+ stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4096)
|
|
|
stream.WriteVal(val)
|
|
|
stream.Flush()
|
|
|
should.Nil(stream.Error)
|
|
|
@@ -291,7 +247,7 @@ func Test_write_uint16(t *testing.T) {
|
|
|
}
|
|
|
should := require.New(t)
|
|
|
buf := &bytes.Buffer{}
|
|
|
- stream := NewStream(ConfigDefault, buf, 5)
|
|
|
+ stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 5)
|
|
|
stream.WriteRaw("a")
|
|
|
stream.WriteUint16(10000) // should clear buffer
|
|
|
stream.Flush()
|
|
|
@@ -305,7 +261,7 @@ func Test_write_int16(t *testing.T) {
|
|
|
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
|
|
should := require.New(t)
|
|
|
buf := &bytes.Buffer{}
|
|
|
- stream := NewStream(ConfigDefault, buf, 4096)
|
|
|
+ stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4096)
|
|
|
stream.WriteInt16(val)
|
|
|
stream.Flush()
|
|
|
should.Nil(stream.Error)
|
|
|
@@ -314,7 +270,7 @@ func Test_write_int16(t *testing.T) {
|
|
|
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
|
|
should := require.New(t)
|
|
|
buf := &bytes.Buffer{}
|
|
|
- stream := NewStream(ConfigDefault, buf, 4096)
|
|
|
+ stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4096)
|
|
|
stream.WriteVal(val)
|
|
|
stream.Flush()
|
|
|
should.Nil(stream.Error)
|
|
|
@@ -323,7 +279,7 @@ func Test_write_int16(t *testing.T) {
|
|
|
}
|
|
|
should := require.New(t)
|
|
|
buf := &bytes.Buffer{}
|
|
|
- stream := NewStream(ConfigDefault, buf, 6)
|
|
|
+ stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 6)
|
|
|
stream.WriteRaw("a")
|
|
|
stream.WriteInt16(-10000) // should clear buffer
|
|
|
stream.Flush()
|
|
|
@@ -337,7 +293,7 @@ func Test_write_uint32(t *testing.T) {
|
|
|
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
|
|
should := require.New(t)
|
|
|
buf := &bytes.Buffer{}
|
|
|
- stream := NewStream(ConfigDefault, buf, 4096)
|
|
|
+ stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4096)
|
|
|
stream.WriteUint32(val)
|
|
|
stream.Flush()
|
|
|
should.Nil(stream.Error)
|
|
|
@@ -346,7 +302,7 @@ func Test_write_uint32(t *testing.T) {
|
|
|
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
|
|
should := require.New(t)
|
|
|
buf := &bytes.Buffer{}
|
|
|
- stream := NewStream(ConfigDefault, buf, 4096)
|
|
|
+ stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4096)
|
|
|
stream.WriteVal(val)
|
|
|
stream.Flush()
|
|
|
should.Nil(stream.Error)
|
|
|
@@ -355,7 +311,7 @@ func Test_write_uint32(t *testing.T) {
|
|
|
}
|
|
|
should := require.New(t)
|
|
|
buf := &bytes.Buffer{}
|
|
|
- stream := NewStream(ConfigDefault, buf, 10)
|
|
|
+ stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 10)
|
|
|
stream.WriteRaw("a")
|
|
|
stream.WriteUint32(0xffffffff) // should clear buffer
|
|
|
stream.Flush()
|
|
|
@@ -369,7 +325,7 @@ func Test_write_int32(t *testing.T) {
|
|
|
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
|
|
should := require.New(t)
|
|
|
buf := &bytes.Buffer{}
|
|
|
- stream := NewStream(ConfigDefault, buf, 4096)
|
|
|
+ stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4096)
|
|
|
stream.WriteInt32(val)
|
|
|
stream.Flush()
|
|
|
should.Nil(stream.Error)
|
|
|
@@ -378,7 +334,7 @@ func Test_write_int32(t *testing.T) {
|
|
|
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
|
|
should := require.New(t)
|
|
|
buf := &bytes.Buffer{}
|
|
|
- stream := NewStream(ConfigDefault, buf, 4096)
|
|
|
+ stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4096)
|
|
|
stream.WriteVal(val)
|
|
|
stream.Flush()
|
|
|
should.Nil(stream.Error)
|
|
|
@@ -387,7 +343,7 @@ func Test_write_int32(t *testing.T) {
|
|
|
}
|
|
|
should := require.New(t)
|
|
|
buf := &bytes.Buffer{}
|
|
|
- stream := NewStream(ConfigDefault, buf, 11)
|
|
|
+ stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 11)
|
|
|
stream.WriteRaw("a")
|
|
|
stream.WriteInt32(-0x7fffffff) // should clear buffer
|
|
|
stream.Flush()
|
|
|
@@ -403,7 +359,7 @@ func Test_write_uint64(t *testing.T) {
|
|
|
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
|
|
should := require.New(t)
|
|
|
buf := &bytes.Buffer{}
|
|
|
- stream := NewStream(ConfigDefault, buf, 4096)
|
|
|
+ stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4096)
|
|
|
stream.WriteUint64(val)
|
|
|
stream.Flush()
|
|
|
should.Nil(stream.Error)
|
|
|
@@ -412,7 +368,7 @@ func Test_write_uint64(t *testing.T) {
|
|
|
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
|
|
should := require.New(t)
|
|
|
buf := &bytes.Buffer{}
|
|
|
- stream := NewStream(ConfigDefault, buf, 4096)
|
|
|
+ stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4096)
|
|
|
stream.WriteVal(val)
|
|
|
stream.Flush()
|
|
|
should.Nil(stream.Error)
|
|
|
@@ -421,7 +377,7 @@ func Test_write_uint64(t *testing.T) {
|
|
|
}
|
|
|
should := require.New(t)
|
|
|
buf := &bytes.Buffer{}
|
|
|
- stream := NewStream(ConfigDefault, buf, 10)
|
|
|
+ stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 10)
|
|
|
stream.WriteRaw("a")
|
|
|
stream.WriteUint64(0xffffffff) // should clear buffer
|
|
|
stream.Flush()
|
|
|
@@ -437,7 +393,7 @@ func Test_write_int64(t *testing.T) {
|
|
|
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
|
|
should := require.New(t)
|
|
|
buf := &bytes.Buffer{}
|
|
|
- stream := NewStream(ConfigDefault, buf, 4096)
|
|
|
+ stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4096)
|
|
|
stream.WriteInt64(val)
|
|
|
stream.Flush()
|
|
|
should.Nil(stream.Error)
|
|
|
@@ -446,7 +402,7 @@ func Test_write_int64(t *testing.T) {
|
|
|
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
|
|
should := require.New(t)
|
|
|
buf := &bytes.Buffer{}
|
|
|
- stream := NewStream(ConfigDefault, buf, 4096)
|
|
|
+ stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4096)
|
|
|
stream.WriteVal(val)
|
|
|
stream.Flush()
|
|
|
should.Nil(stream.Error)
|
|
|
@@ -455,119 +411,10 @@ func Test_write_int64(t *testing.T) {
|
|
|
}
|
|
|
should := require.New(t)
|
|
|
buf := &bytes.Buffer{}
|
|
|
- stream := NewStream(ConfigDefault, buf, 10)
|
|
|
+ stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 10)
|
|
|
stream.WriteRaw("a")
|
|
|
stream.WriteInt64(0xffffffff) // should clear buffer
|
|
|
stream.Flush()
|
|
|
should.Nil(stream.Error)
|
|
|
should.Equal("a4294967295", buf.String())
|
|
|
-}
|
|
|
-
|
|
|
-func Test_write_val_int(t *testing.T) {
|
|
|
- should := require.New(t)
|
|
|
- buf := &bytes.Buffer{}
|
|
|
- stream := NewStream(ConfigDefault, buf, 4096)
|
|
|
- stream.WriteVal(1001)
|
|
|
- stream.Flush()
|
|
|
- should.Nil(stream.Error)
|
|
|
- should.Equal("1001", buf.String())
|
|
|
-}
|
|
|
-
|
|
|
-func Test_write_val_int_ptr(t *testing.T) {
|
|
|
- should := require.New(t)
|
|
|
- buf := &bytes.Buffer{}
|
|
|
- stream := NewStream(ConfigDefault, buf, 4096)
|
|
|
- val := 1001
|
|
|
- stream.WriteVal(&val)
|
|
|
- stream.Flush()
|
|
|
- should.Nil(stream.Error)
|
|
|
- should.Equal("1001", buf.String())
|
|
|
-}
|
|
|
-
|
|
|
-func Test_json_number(t *testing.T) {
|
|
|
- should := require.New(t)
|
|
|
- var arr []json.Number
|
|
|
- err := Unmarshal([]byte(`[1]`), &arr)
|
|
|
- should.Nil(err)
|
|
|
- should.Equal(json.Number("1"), arr[0])
|
|
|
- str, err := MarshalToString(arr)
|
|
|
- should.Nil(err)
|
|
|
- should.Equal(`[1]`, str)
|
|
|
-}
|
|
|
-
|
|
|
-func Test_jsoniter_number(t *testing.T) {
|
|
|
- should := require.New(t)
|
|
|
- var arr []Number
|
|
|
- err := Unmarshal([]byte(`[1]`), &arr)
|
|
|
- should.Nil(err)
|
|
|
- should.Equal(Number("1"), arr[0])
|
|
|
- str, isNumber := CastJsonNumber(arr[0])
|
|
|
- should.True(isNumber)
|
|
|
- should.Equal("1", str)
|
|
|
-}
|
|
|
-
|
|
|
-func Test_non_numeric_as_number(t *testing.T) {
|
|
|
- should := require.New(t)
|
|
|
- var v1 json.Number
|
|
|
- err := Unmarshal([]byte(`"500"`), &v1)
|
|
|
- should.Nil(err)
|
|
|
- should.Equal("500", string(v1))
|
|
|
- var v2 Number
|
|
|
- err = Unmarshal([]byte(`"500"`), &v2)
|
|
|
- should.Nil(err)
|
|
|
- should.Equal("500", string(v2))
|
|
|
-}
|
|
|
-
|
|
|
-func Test_null_as_number(t *testing.T) {
|
|
|
- should := require.New(t)
|
|
|
- var v1 json.Number
|
|
|
- err := json.Unmarshal([]byte(`null`), &v1)
|
|
|
- should.Nil(err)
|
|
|
- should.Equal("", string(v1))
|
|
|
- output, err := json.Marshal(v1)
|
|
|
- should.NoError(err)
|
|
|
- should.Equal("0", string(output))
|
|
|
- var v2 Number
|
|
|
- err = Unmarshal([]byte(`null`), &v2)
|
|
|
- should.Nil(err)
|
|
|
- should.Equal("", string(v2))
|
|
|
- output, err = Marshal(v2)
|
|
|
- should.NoError(err)
|
|
|
- should.Equal("0", string(output))
|
|
|
-}
|
|
|
-
|
|
|
-func Test_float_as_int(t *testing.T) {
|
|
|
- should := require.New(t)
|
|
|
- var i int
|
|
|
- should.NotNil(Unmarshal([]byte(`1.1`), &i))
|
|
|
-}
|
|
|
-
|
|
|
-func Benchmark_jsoniter_encode_int(b *testing.B) {
|
|
|
- stream := NewStream(ConfigDefault, ioutil.Discard, 64)
|
|
|
- for n := 0; n < b.N; n++ {
|
|
|
- stream.n = 0
|
|
|
- stream.WriteUint64(0xffffffff)
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-func Benchmark_itoa(b *testing.B) {
|
|
|
- for n := 0; n < b.N; n++ {
|
|
|
- strconv.FormatInt(0xffffffff, 10)
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-func Benchmark_jsoniter_int(b *testing.B) {
|
|
|
- iter := NewIterator(ConfigDefault)
|
|
|
- input := []byte(`100`)
|
|
|
- for n := 0; n < b.N; n++ {
|
|
|
- iter.ResetBytes(input)
|
|
|
- iter.ReadInt64()
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-func Benchmark_json_int(b *testing.B) {
|
|
|
- for n := 0; n < b.N; n++ {
|
|
|
- result := int64(0)
|
|
|
- json.Unmarshal([]byte(`-100`), &result)
|
|
|
- }
|
|
|
-}
|
|
|
+}
|