1
0
mirror of https://github.com/json-iterator/go.git synced 2025-06-15 22:50:24 +02:00

fix read int ranges

This commit is contained in:
Tao Wen
2017-01-15 23:17:17 +08:00
parent 399ee3faaa
commit 54a69f1da2
4 changed files with 120 additions and 169 deletions

View File

@ -10,70 +10,16 @@ import (
"io/ioutil"
)
func Test_decode_decode_uint64_0(t *testing.T) {
iter := Parse(bytes.NewBufferString("0"), 4096)
val := iter.ReadUint64()
if iter.Error != nil {
t.Fatal(iter.Error)
}
if val != 0 {
t.Fatal(val)
}
}
func Test_decode_uint64_1(t *testing.T) {
iter := Parse(bytes.NewBufferString("1"), 4096)
val := iter.ReadUint64()
if val != 1 {
t.Fatal(val)
}
}
func Test_decode_uint64_100(t *testing.T) {
iter := Parse(bytes.NewBufferString("100"), 4096)
val := iter.ReadUint64()
if val != 100 {
t.Fatal(val)
}
}
func Test_decode_uint64_100_comma(t *testing.T) {
iter := Parse(bytes.NewBufferString("100,"), 4096)
val := iter.ReadUint64()
if iter.Error != nil {
t.Fatal(iter.Error)
}
if val != 100 {
t.Fatal(val)
}
}
func Test_decode_uint64_invalid(t *testing.T) {
iter := Parse(bytes.NewBufferString(","), 4096)
func Test_read_uint64_invalid(t *testing.T) {
should := require.New(t)
iter := ParseString(",")
iter.ReadUint64()
if iter.Error == nil {
t.FailNow()
}
}
func Test_decode_int64_100(t *testing.T) {
iter := Parse(bytes.NewBufferString("100"), 4096)
val := iter.ReadInt64()
if val != 100 {
t.Fatal(val)
}
}
func Test_decode_int64_minus_100(t *testing.T) {
iter := Parse(bytes.NewBufferString("-100"), 4096)
val := iter.ReadInt64()
if val != -100 {
t.Fatal(val)
}
should.NotNil(iter.Error)
}
func Test_read_int32(t *testing.T) {
inputs := []string{`1`, `12`, `123`}
inputs := []string{`1`, `12`, `123`, `1234`, `12345`, `123456`, `2147483647`}
for _, input := range inputs {
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
should := require.New(t)
@ -82,6 +28,13 @@ func Test_read_int32(t *testing.T) {
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(bytes.NewBufferString(input), 2)
expected, err := strconv.ParseInt(input, 10, 32)
should.Nil(err)
should.Equal(int32(expected), iter.ReadInt32())
})
}
}
@ -93,6 +46,34 @@ func Test_read_int32_overflow(t *testing.T) {
should.NotNil(iter.Error)
}
func Test_read_int64(t *testing.T) {
inputs := []string{`1`, `12`, `123`, `1234`, `12345`, `123456`, `9223372036854775807`}
for _, input := range inputs {
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
should := require.New(t)
iter := ParseString(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(bytes.NewBufferString(input), 2)
expected, err := strconv.ParseInt(input, 10, 64)
should.Nil(err)
should.Equal(expected, iter.ReadInt64())
})
}
}
func Test_read_int64_overflow(t *testing.T) {
should := require.New(t)
input := "123456789123456789"
iter := ParseString(input)
iter.ReadInt64()
should.NotNil(iter.Error)
}
func Test_write_uint8(t *testing.T) {
vals := []uint8{0, 1, 11, 111, 255}
for _, val := range vals {