1
0
mirror of https://github.com/json-iterator/go.git synced 2024-11-27 08:30:57 +02:00
json-iterator/any_tests/jsoniter_any_int_test.go

199 lines
5.3 KiB
Go
Raw Normal View History

2018-02-13 14:58:29 +02:00
package any_tests
2017-06-17 15:32:48 +02:00
import (
2017-07-03 18:19:41 +02:00
"fmt"
2017-06-19 17:43:53 +02:00
"testing"
2017-07-03 18:19:41 +02:00
2018-02-13 14:58:29 +02:00
"github.com/json-iterator/go"
2018-02-24 16:04:41 +02:00
"github.com/stretchr/testify/require"
2017-06-17 15:32:48 +02:00
)
2017-07-03 18:19:41 +02:00
var intConvertMap = map[string]int{
2017-07-04 11:02:46 +02:00
"null": 0,
2017-07-03 18:19:41 +02:00
"321.1": 321,
"-321.1": -321,
`"1.1"`: 1,
2017-07-04 08:00:24 +02:00
`"-321.1"`: -321,
2017-07-03 18:19:41 +02:00
"0.0": 0,
"0": 0,
`"0"`: 0,
`"0.0"`: 0,
"-1.1": -1,
"true": 1,
"false": 0,
`"true"`: 0,
`"false"`: 0,
`"true123"`: 0,
`"123true"`: 123,
2017-07-04 13:59:34 +02:00
`"-123true"`: -123,
2017-07-03 18:19:41 +02:00
`"1.2332e6"`: 1,
`""`: 0,
2017-07-03 18:29:11 +02:00
"+": 0,
"-": 0,
"[]": 0,
"[1,2]": 1,
2017-07-04 13:59:34 +02:00
`["1","2"]`: 1,
// object in php cannot convert to int
"{}": 0,
2017-07-03 18:19:41 +02:00
}
func Test_read_any_to_int(t *testing.T) {
should := require.New(t)
// int
for k, v := range intConvertMap {
2018-02-13 14:58:29 +02:00
any := jsoniter.Get([]byte(k))
2017-07-03 18:19:41 +02:00
should.Equal(v, any.ToInt(), fmt.Sprintf("origin val %v", k))
}
// int32
for k, v := range intConvertMap {
2018-02-13 14:58:29 +02:00
any := jsoniter.Get([]byte(k))
2017-07-03 18:19:41 +02:00
should.Equal(int32(v), any.ToInt32(), fmt.Sprintf("original val is %v", k))
}
// int64
for k, v := range intConvertMap {
2018-02-13 14:58:29 +02:00
any := jsoniter.Get([]byte(k))
2017-07-03 18:19:41 +02:00
should.Equal(int64(v), any.ToInt64(), fmt.Sprintf("original val is %v", k))
}
}
var uintConvertMap = map[string]int{
2017-07-04 11:02:46 +02:00
"null": 0,
2017-07-03 18:19:41 +02:00
"321.1": 321,
`"1.1"`: 1,
2017-07-04 11:02:46 +02:00
`"-123.1"`: 0,
2017-07-03 18:19:41 +02:00
"0.0": 0,
"0": 0,
`"0"`: 0,
`"0.0"`: 0,
2017-07-04 08:00:24 +02:00
`"00.0"`: 0,
2017-07-03 18:19:41 +02:00
"true": 1,
"false": 0,
`"true"`: 0,
`"false"`: 0,
`"true123"`: 0,
2017-07-04 13:59:34 +02:00
`"+1"`: 1,
2017-07-03 18:19:41 +02:00
`"123true"`: 123,
2017-07-04 11:02:46 +02:00
`"-123true"`: 0,
2017-07-03 18:19:41 +02:00
`"1.2332e6"`: 1,
`""`: 0,
2017-07-03 18:29:11 +02:00
"+": 0,
"-": 0,
2017-07-04 08:00:24 +02:00
".": 0,
2017-07-03 18:29:11 +02:00
"[]": 0,
"[1,2]": 1,
"{}": 0,
2017-07-04 08:00:24 +02:00
"{1,2}": 0,
2017-07-04 11:02:46 +02:00
"-1.1": 0,
"-321.1": 0,
2017-07-03 18:19:41 +02:00
}
func Test_read_any_to_uint(t *testing.T) {
should := require.New(t)
for k, v := range uintConvertMap {
2018-02-13 14:58:29 +02:00
any := jsoniter.Get([]byte(k))
2017-07-03 18:19:41 +02:00
should.Equal(uint64(v), any.ToUint64(), fmt.Sprintf("origin val %v", k))
}
for k, v := range uintConvertMap {
2018-02-13 14:58:29 +02:00
any := jsoniter.Get([]byte(k))
2017-07-03 18:19:41 +02:00
should.Equal(uint32(v), any.ToUint32(), fmt.Sprintf("origin val %v", k))
}
for k, v := range uintConvertMap {
2018-02-13 14:58:29 +02:00
any := jsoniter.Get([]byte(k))
2017-07-04 08:00:24 +02:00
should.Equal(uint(v), any.ToUint(), fmt.Sprintf("origin val %v", k))
2017-07-03 18:19:41 +02:00
}
}
2017-07-04 19:21:33 +02:00
func Test_read_int64_to_any(t *testing.T) {
2017-06-17 15:32:48 +02:00
should := require.New(t)
2018-02-13 14:58:29 +02:00
any := jsoniter.WrapInt64(12345)
2017-07-04 19:21:33 +02:00
should.Equal(12345, any.ToInt())
should.Equal(int32(12345), any.ToInt32())
should.Equal(int64(12345), any.ToInt64())
should.Equal(uint(12345), any.ToUint())
should.Equal(uint32(12345), any.ToUint32())
should.Equal(uint64(12345), any.ToUint64())
should.Equal(float32(12345), any.ToFloat32())
should.Equal(float64(12345), any.ToFloat64())
should.Equal("12345", any.ToString())
should.Equal(true, any.ToBool())
2018-02-13 14:58:29 +02:00
should.Equal(any.ValueType(), jsoniter.NumberValue)
stream := jsoniter.NewStream(jsoniter.ConfigDefault, nil, 32)
2017-07-05 05:40:20 +02:00
any.WriteTo(stream)
should.Equal("12345", string(stream.Buffer()))
2017-07-04 19:21:33 +02:00
}
func Test_read_int32_to_any(t *testing.T) {
should := require.New(t)
2018-02-13 14:58:29 +02:00
any := jsoniter.WrapInt32(12345)
2017-07-04 19:21:33 +02:00
should.Equal(12345, any.ToInt())
should.Equal(int32(12345), any.ToInt32())
should.Equal(int64(12345), any.ToInt64())
should.Equal(uint(12345), any.ToUint())
should.Equal(uint32(12345), any.ToUint32())
should.Equal(uint64(12345), any.ToUint64())
should.Equal(float32(12345), any.ToFloat32())
should.Equal(float64(12345), any.ToFloat64())
should.Equal("12345", any.ToString())
should.Equal(true, any.ToBool())
2018-02-13 14:58:29 +02:00
should.Equal(any.ValueType(), jsoniter.NumberValue)
stream := jsoniter.NewStream(jsoniter.ConfigDefault, nil, 32)
2017-07-05 05:40:20 +02:00
any.WriteTo(stream)
should.Equal("12345", string(stream.Buffer()))
2017-07-04 19:21:33 +02:00
}
func Test_read_uint32_to_any(t *testing.T) {
should := require.New(t)
2018-02-13 14:58:29 +02:00
any := jsoniter.WrapUint32(12345)
2017-07-04 19:21:33 +02:00
should.Equal(12345, any.ToInt())
should.Equal(int32(12345), any.ToInt32())
should.Equal(int64(12345), any.ToInt64())
should.Equal(uint(12345), any.ToUint())
should.Equal(uint32(12345), any.ToUint32())
should.Equal(uint64(12345), any.ToUint64())
should.Equal(float32(12345), any.ToFloat32())
should.Equal(float64(12345), any.ToFloat64())
should.Equal("12345", any.ToString())
should.Equal(true, any.ToBool())
2018-02-13 14:58:29 +02:00
should.Equal(any.ValueType(), jsoniter.NumberValue)
stream := jsoniter.NewStream(jsoniter.ConfigDefault, nil, 32)
2017-07-05 05:40:20 +02:00
any.WriteTo(stream)
should.Equal("12345", string(stream.Buffer()))
2017-07-04 19:21:33 +02:00
}
func Test_read_uint64_to_any(t *testing.T) {
should := require.New(t)
2018-02-13 14:58:29 +02:00
any := jsoniter.WrapUint64(12345)
2017-07-04 19:21:33 +02:00
should.Equal(12345, any.ToInt())
should.Equal(int32(12345), any.ToInt32())
should.Equal(int64(12345), any.ToInt64())
should.Equal(uint(12345), any.ToUint())
should.Equal(uint32(12345), any.ToUint32())
should.Equal(uint64(12345), any.ToUint64())
should.Equal(float32(12345), any.ToFloat32())
should.Equal(float64(12345), any.ToFloat64())
should.Equal("12345", any.ToString())
should.Equal(true, any.ToBool())
2018-02-13 14:58:29 +02:00
should.Equal(any.ValueType(), jsoniter.NumberValue)
stream := jsoniter.NewStream(jsoniter.ConfigDefault, nil, 32)
2017-07-05 05:40:20 +02:00
any.WriteTo(stream)
should.Equal("12345", string(stream.Buffer()))
2018-02-13 14:58:29 +02:00
stream = jsoniter.NewStream(jsoniter.ConfigDefault, nil, 32)
2017-07-05 07:55:10 +02:00
stream.WriteUint(uint(123))
should.Equal("123", string(stream.Buffer()))
2017-06-17 15:32:48 +02:00
}
func Test_int_lazy_any_get(t *testing.T) {
should := require.New(t)
2018-02-13 14:58:29 +02:00
any := jsoniter.Get([]byte("1234"))
2017-07-06 05:44:39 +02:00
// panic!!
//should.Equal(any.LastError(), io.EOF)
2018-02-13 14:58:29 +02:00
should.Equal(jsoniter.InvalidValue, any.Get(1, "2").ValueType())
2017-06-17 15:32:48 +02:00
}