1
0
mirror of https://github.com/json-iterator/go.git synced 2025-05-13 21:36:29 +02:00

Merge pull request #118 from cch123/feature-increase-coverage

add int/int8/int32/int64 overflow test
This commit is contained in:
Tao Wen 2017-07-07 18:49:35 +08:00 committed by GitHub
commit eb68fff85c
2 changed files with 63 additions and 6 deletions

View File

@ -6,10 +6,11 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/stretchr/testify/require"
"io/ioutil" "io/ioutil"
"strconv" "strconv"
"testing" "testing"
"github.com/stretchr/testify/require"
) )
func Test_read_uint64_invalid(t *testing.T) { func Test_read_uint64_invalid(t *testing.T) {
@ -81,12 +82,52 @@ func Test_read_int64_array(t *testing.T) {
should.Equal(3, len(val)) should.Equal(3, len(val))
} }
func Test_read_int32_overflow(t *testing.T) { func Test_read_int_overflow(t *testing.T) {
should := require.New(t) should := require.New(t)
input := "123456789123456789," inputArr := []string{"123451", "-123451"}
iter := ParseString(ConfigDefault, input) for _, s := range inputArr {
iter.ReadInt32() iter := ParseString(ConfigDefault, s)
should.NotNil(iter.Error) iter.ReadInt8()
should.NotNil(iter.Error)
iterU := ParseString(ConfigDefault, s)
iterU.ReadUint8()
should.NotNil(iterU.Error)
}
inputArr = []string{"12345678912", "-12345678912"}
for _, s := range inputArr {
iter := ParseString(ConfigDefault, s)
iter.ReadInt16()
should.NotNil(iter.Error)
iterUint := ParseString(ConfigDefault, s)
iterUint.ReadUint16()
should.NotNil(iterUint.Error)
}
inputArr = []string{"3111111111", "-3111111111", "1234232323232323235678912", "-1234567892323232323212"}
for _, s := range inputArr {
iter := ParseString(ConfigDefault, s)
iter.ReadInt32()
should.NotNil(iter.Error)
iterUint := ParseString(ConfigDefault, s)
iterUint.ReadUint32()
should.NotNil(iterUint.Error)
}
inputArr = []string{"9223372036854775811", "-9523372036854775807", "1234232323232323235678912", "-1234567892323232323212"}
for _, s := range inputArr {
iter := ParseString(ConfigDefault, s)
iter.ReadInt64()
should.NotNil(iter.Error)
iterUint := ParseString(ConfigDefault, s)
iterUint.ReadUint64()
should.NotNil(iterUint.Error)
}
} }
func Test_read_int64(t *testing.T) { func Test_read_int64(t *testing.T) {

View File

@ -129,3 +129,19 @@ func Test_encode_map_with_sorted_keys(t *testing.T) {
should.Nil(err) should.Nil(err)
should.Equal(string(bytes), output) should.Equal(string(bytes), output)
} }
func Test_encode_map_uint_keys(t *testing.T) {
should := require.New(t)
m := map[uint64]interface{}{
uint64(1): "a",
uint64(2): "a",
uint64(4): "a",
}
bytes, err := json.Marshal(m)
should.Nil(err)
output, err := ConfigCompatibleWithStandardLibrary.MarshalToString(m)
should.Equal(string(bytes), output)
}