github.com/zerosnake0/jzon@v0.0.9-0.20230801092939-1b135cb83f7f/val_decoder_native_float_test.go (about)

     1  package jzon
     2  
     3  import (
     4  	"io"
     5  	"testing"
     6  )
     7  
     8  func TestValDecoder_Native_Float32(t *testing.T) {
     9  	f := func(t *testing.T, data string, ex error, initValue float32) {
    10  		var p1 *float32
    11  		var p2 *float32
    12  		if initValue != 0 {
    13  			b1 := initValue
    14  			p1 = &b1
    15  			b2 := initValue
    16  			p2 = &b2
    17  		}
    18  		checkDecodeWithStandard(t, DefaultDecoderConfig, data, ex, p1, p2)
    19  	}
    20  	f2 := func(t *testing.T, data string, ex error) {
    21  		f(t, data, ex, 1.234)
    22  	}
    23  	t.Run("nil pointer", func(t *testing.T) {
    24  		f(t, "null", ErrNilPointerReceiver, 0)
    25  	})
    26  	t.Run("eof", func(t *testing.T) {
    27  		f2(t, "", io.EOF)
    28  	})
    29  	t.Run("invalid first byte", func(t *testing.T) {
    30  		f2(t, `true`, InvalidFloatError{})
    31  	})
    32  	t.Run("invalid null", func(t *testing.T) {
    33  		f2(t, "nul", io.EOF)
    34  	})
    35  	t.Run("null", func(t *testing.T) {
    36  		f2(t, "null", nil)
    37  	})
    38  	t.Run("valid", func(t *testing.T) {
    39  		f2(t, "0.123e-4", nil)
    40  	})
    41  }
    42  
    43  func TestValDecoder_Native_Float64(t *testing.T) {
    44  	f := func(t *testing.T, data string, ex error, initValue float64) {
    45  		var p1 *float64
    46  		var p2 *float64
    47  		if initValue != 0 {
    48  			b1 := initValue
    49  			p1 = &b1
    50  			b2 := initValue
    51  			p2 = &b2
    52  		}
    53  		checkDecodeWithStandard(t, DefaultDecoderConfig, data, ex, p1, p2)
    54  	}
    55  	f2 := func(t *testing.T, data string, ex error) {
    56  		f(t, data, ex, 1.234)
    57  	}
    58  	t.Run("nil pointer", func(t *testing.T) {
    59  		f(t, "null", ErrNilPointerReceiver, 0)
    60  	})
    61  	t.Run("eof", func(t *testing.T) {
    62  		f2(t, "", io.EOF)
    63  	})
    64  	t.Run("invalid first byte", func(t *testing.T) {
    65  		f2(t, `true`, InvalidFloatError{})
    66  	})
    67  	t.Run("invalid null", func(t *testing.T) {
    68  		f2(t, "nul", io.EOF)
    69  	})
    70  	t.Run("null", func(t *testing.T) {
    71  		f2(t, "null", nil)
    72  	})
    73  	t.Run("valid", func(t *testing.T) {
    74  		f2(t, "0.123e-45", nil)
    75  	})
    76  }