1
0
mirror of https://github.com/json-iterator/go.git synced 2025-03-23 21:09:11 +02:00

Merge pull request from cch123/feature-add-int-tests

fix negative number to uint
This commit is contained in:
Tao Wen 2017-07-04 22:37:09 +08:00 committed by GitHub
commit 3829a470ae
5 changed files with 63 additions and 12 deletions

@ -1,8 +1,6 @@
package jsoniter
import (
"unsafe"
)
import "unsafe"
type numberLazyAny struct {
baseAny

@ -99,8 +99,11 @@ func (any *stringAny) ToUint64() uint64 {
startPos := 0
endPos := 0
// uint skip flag, is this correct?
if any.val[0] == '+' || any.val[0] == '-' {
if any.val[0] == '-' {
return 0
}
if any.val[0] == '+' {
startPos = 1
}

@ -8,6 +8,7 @@ import (
)
var boolConvertMap = map[string]bool{
"null": false,
"true": true,
"false": false,

@ -1,10 +1,51 @@
package jsoniter
import (
"github.com/json-iterator/go/require"
"testing"
"github.com/json-iterator/go/require"
)
var floatConvertMap = map[string]float64{
"null": 0,
"true": 1,
"false": 0,
`"true"`: 0,
`"false"`: 0,
"123": 123,
`"123true"`: 0,
`"-123true"`: 0,
"0": 0,
`"0"`: 0,
"-1": -1,
"1.1": 1.1,
"0.0": 0,
"-1.1": -1.1,
`"+1.1"`: 1.1,
`""`: 0,
"[1,2]": 1,
"[]": 0,
"{}": 0,
`{"abc":1}`: 0,
}
func Test_read_any_to_float(t *testing.T) {
should := require.New(t)
for k, v := range floatConvertMap {
any := Get([]byte(k))
should.Equal(float64(v), any.ToFloat64(), "the original val is "+k)
}
for k, v := range floatConvertMap {
any := Get([]byte(k))
should.Equal(float32(v), any.ToFloat32(), "the original val is "+k)
}
}
func Test_read_float_as_any(t *testing.T) {
should := require.New(t)
any := Get([]byte("12.3"))

@ -9,10 +9,11 @@ import (
)
var intConvertMap = map[string]int{
"null": 0,
"321.1": 321,
"-321.1": -321,
`"1.1"`: 1,
`"-1.1"`: -1,
`"-321.1"`: -321,
"0.0": 0,
"0": 0,
`"0"`: 0,
@ -24,12 +25,14 @@ var intConvertMap = map[string]int{
`"false"`: 0,
`"true123"`: 0,
`"123true"`: 123,
`"-123true"`: -123,
`"1.2332e6"`: 1,
`""`: 0,
"+": 0,
"-": 0,
"[]": 0,
"[1,2]": 1,
`["1","2"]`: 1,
// object in php cannot convert to int
"{}": 0,
}
@ -58,29 +61,34 @@ func Test_read_any_to_int(t *testing.T) {
}
var uintConvertMap = map[string]int{
"null": 0,
"321.1": 321,
`"1.1"`: 1,
`"-1.1"`: 1,
`"-123.1"`: 0,
"0.0": 0,
"0": 0,
`"0"`: 0,
`"0.0"`: 0,
`"00.0"`: 0,
"true": 1,
"false": 0,
`"true"`: 0,
`"false"`: 0,
`"true123"`: 0,
`"+1"`: 1,
`"123true"`: 123,
`"-123true"`: 0,
`"1.2332e6"`: 1,
`""`: 0,
"+": 0,
"-": 0,
".": 0,
"[]": 0,
"[1,2]": 1,
"{}": 0,
// TODO need to solve
//"-1.1": 1,
//"-321.1": 321,
"{1,2}": 0,
"-1.1": 0,
"-321.1": 0,
}
func Test_read_any_to_uint(t *testing.T) {
@ -98,7 +106,7 @@ func Test_read_any_to_uint(t *testing.T) {
for k, v := range uintConvertMap {
any := Get([]byte(k))
should.Equal(uint32(v), any.ToUint32(), fmt.Sprintf("origin val %v", k))
should.Equal(uint(v), any.ToUint(), fmt.Sprintf("origin val %v", k))
}
}