...

Source file src/github.com/json-iterator/go/any_tests/jsoniter_any_float_test.go

Documentation: github.com/json-iterator/go/any_tests

     1  package any_tests
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/json-iterator/go"
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  var floatConvertMap = map[string]float64{
    11  	"null":  0,
    12  	"true":  1,
    13  	"false": 0,
    14  
    15  	`"true"`:  0,
    16  	`"false"`: 0,
    17  
    18  	"1e1":  10,
    19  	"1e+1": 10,
    20  	"1e-1": .1,
    21  	"1E1":  10,
    22  	"1E+1": 10,
    23  	"1E-1": .1,
    24  
    25  	"-1e1":  -10,
    26  	"-1e+1": -10,
    27  	"-1e-1": -.1,
    28  	"-1E1":  -10,
    29  	"-1E+1": -10,
    30  	"-1E-1": -.1,
    31  
    32  	`"1e1"`:  10,
    33  	`"1e+1"`: 10,
    34  	`"1e-1"`: .1,
    35  	`"1E1"`:  10,
    36  	`"1E+1"`: 10,
    37  	`"1E-1"`: .1,
    38  
    39  	`"-1e1"`:  -10,
    40  	`"-1e+1"`: -10,
    41  	`"-1e-1"`: -.1,
    42  	`"-1E1"`:  -10,
    43  	`"-1E+1"`: -10,
    44  	`"-1E-1"`: -.1,
    45  
    46  	"123":       123,
    47  	`"123true"`: 123,
    48  	`"+"`:       0,
    49  	`"-"`:       0,
    50  
    51  	`"-123true"`:  -123,
    52  	`"-99.9true"`: -99.9,
    53  	"0":           0,
    54  	`"0"`:         0,
    55  	"-1":          -1,
    56  
    57  	"1.1":       1.1,
    58  	"0.0":       0,
    59  	"-1.1":      -1.1,
    60  	`"+1.1"`:    1.1,
    61  	`""`:        0,
    62  	"[1,2]":     1,
    63  	"[]":        0,
    64  	"{}":        0,
    65  	`{"abc":1}`: 0,
    66  }
    67  
    68  func Test_read_any_to_float(t *testing.T) {
    69  	should := require.New(t)
    70  	for k, v := range floatConvertMap {
    71  		any := jsoniter.Get([]byte(k))
    72  		should.Equal(float64(v), any.ToFloat64(), "the original val is "+k)
    73  	}
    74  
    75  	for k, v := range floatConvertMap {
    76  		any := jsoniter.Get([]byte(k))
    77  		should.Equal(float32(v), any.ToFloat32(), "the original val is "+k)
    78  	}
    79  }
    80  
    81  func Test_read_float_to_any(t *testing.T) {
    82  	should := require.New(t)
    83  	any := jsoniter.WrapFloat64(12.3)
    84  	anyFloat64 := float64(12.3)
    85  	any2 := jsoniter.WrapFloat64(-1.1)
    86  	should.Equal(float64(12.3), any.ToFloat64())
    87  	should.True(any.ToBool())
    88  	should.Equal(float32(anyFloat64), any.ToFloat32())
    89  	should.Equal(int(anyFloat64), any.ToInt())
    90  	should.Equal(int32(anyFloat64), any.ToInt32())
    91  	should.Equal(int64(anyFloat64), any.ToInt64())
    92  	should.Equal(uint(anyFloat64), any.ToUint())
    93  	should.Equal(uint32(anyFloat64), any.ToUint32())
    94  	should.Equal(uint64(anyFloat64), any.ToUint64())
    95  	should.Equal(uint(0), any2.ToUint())
    96  	should.Equal(uint32(0), any2.ToUint32())
    97  	should.Equal(uint64(0), any2.ToUint64())
    98  	should.Equal(any.ValueType(), jsoniter.NumberValue)
    99  
   100  	should.Equal("1.23E+01", any.ToString())
   101  }
   102  

View as plain text