...

Source file src/github.com/json-iterator/go/value_tests/float_test.go

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

     1  package test
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  	"github.com/json-iterator/go"
     8  	"github.com/stretchr/testify/require"
     9  	"strconv"
    10  	"testing"
    11  )
    12  
    13  func Test_read_float(t *testing.T) {
    14  	inputs := []string{
    15  		`1.1`, `1000`, `9223372036854775807`, `12.3`, `-12.3`, `720368.54775807`, `720368.547758075`,
    16  		`1e1`, `1e+1`, `1e-1`, `1E1`, `1E+1`, `1E-1`, `-1e1`, `-1e+1`, `-1e-1`,
    17  	}
    18  	for _, input := range inputs {
    19  		// non-streaming
    20  		t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
    21  			should := require.New(t)
    22  			iter := jsoniter.ParseString(jsoniter.ConfigDefault, input+",")
    23  			expected, err := strconv.ParseFloat(input, 32)
    24  			should.Nil(err)
    25  			should.Equal(float32(expected), iter.ReadFloat32())
    26  		})
    27  		t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
    28  			should := require.New(t)
    29  			iter := jsoniter.ParseString(jsoniter.ConfigDefault, input+",")
    30  			expected, err := strconv.ParseFloat(input, 64)
    31  			should.Nil(err)
    32  			should.Equal(expected, iter.ReadFloat64())
    33  		})
    34  		// streaming
    35  		t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
    36  			should := require.New(t)
    37  			iter := jsoniter.Parse(jsoniter.ConfigDefault, bytes.NewBufferString(input+","), 2)
    38  			expected, err := strconv.ParseFloat(input, 32)
    39  			should.Nil(err)
    40  			should.Equal(float32(expected), iter.ReadFloat32())
    41  		})
    42  		t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
    43  			should := require.New(t)
    44  			iter := jsoniter.Parse(jsoniter.ConfigDefault, bytes.NewBufferString(input+","), 2)
    45  			val := float64(0)
    46  			err := json.Unmarshal([]byte(input), &val)
    47  			should.Nil(err)
    48  			should.Equal(val, iter.ReadFloat64())
    49  		})
    50  	}
    51  }
    52  
    53  func Test_write_float32(t *testing.T) {
    54  	vals := []float32{0, 1, -1, 99, 0xff, 0xfff, 0xffff, 0xfffff, 0xffffff, 0x4ffffff, 0xfffffff,
    55  		-0x4ffffff, -0xfffffff, 1.2345, 1.23456, 1.234567, 1.001}
    56  	for _, val := range vals {
    57  		t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
    58  			should := require.New(t)
    59  			buf := &bytes.Buffer{}
    60  			stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4096)
    61  			stream.WriteFloat32Lossy(val)
    62  			stream.Flush()
    63  			should.Nil(stream.Error)
    64  			output, err := json.Marshal(val)
    65  			should.Nil(err)
    66  			should.Equal(string(output), buf.String())
    67  		})
    68  		t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
    69  			should := require.New(t)
    70  			buf := &bytes.Buffer{}
    71  			stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4096)
    72  			stream.WriteVal(val)
    73  			stream.Flush()
    74  			should.Nil(stream.Error)
    75  			output, err := json.Marshal(val)
    76  			should.Nil(err)
    77  			should.Equal(string(output), buf.String())
    78  		})
    79  	}
    80  	should := require.New(t)
    81  	buf := &bytes.Buffer{}
    82  	stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 10)
    83  	stream.WriteRaw("abcdefg")
    84  	stream.WriteFloat32Lossy(1.123456)
    85  	stream.Flush()
    86  	should.Nil(stream.Error)
    87  	should.Equal("abcdefg1.123456", buf.String())
    88  
    89  	stream = jsoniter.NewStream(jsoniter.ConfigDefault, nil, 0)
    90  	stream.WriteFloat32(float32(0.0000001))
    91  	should.Equal("1e-07", string(stream.Buffer()))
    92  }
    93  
    94  func Test_write_float64(t *testing.T) {
    95  	vals := []float64{0, 1, -1, 99, 0xff, 0xfff, 0xffff, 0xfffff, 0xffffff, 0x4ffffff, 0xfffffff,
    96  		-0x4ffffff, -0xfffffff, 1.2345, 1.23456, 1.234567, 1.001}
    97  	for _, val := range vals {
    98  		t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
    99  			should := require.New(t)
   100  			buf := &bytes.Buffer{}
   101  			stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4096)
   102  			stream.WriteFloat64Lossy(val)
   103  			stream.Flush()
   104  			should.Nil(stream.Error)
   105  			should.Equal(strconv.FormatFloat(val, 'f', -1, 64), buf.String())
   106  		})
   107  		t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
   108  			should := require.New(t)
   109  			buf := &bytes.Buffer{}
   110  			stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4096)
   111  			stream.WriteVal(val)
   112  			stream.Flush()
   113  			should.Nil(stream.Error)
   114  			should.Equal(strconv.FormatFloat(val, 'f', -1, 64), buf.String())
   115  		})
   116  	}
   117  	should := require.New(t)
   118  	buf := &bytes.Buffer{}
   119  	stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 10)
   120  	stream.WriteRaw("abcdefg")
   121  	stream.WriteFloat64Lossy(1.123456)
   122  	stream.Flush()
   123  	should.Nil(stream.Error)
   124  	should.Equal("abcdefg1.123456", buf.String())
   125  
   126  	stream = jsoniter.NewStream(jsoniter.ConfigDefault, nil, 0)
   127  	stream.WriteFloat64(float64(0.0000001))
   128  	should.Equal("1e-07", string(stream.Buffer()))
   129  }
   130  

View as plain text