github.com/zerosnake0/jzon@v0.0.9-0.20230801092939-1b135cb83f7f/val_encoder_native_float_test.go (about) 1 package jzon 2 3 import ( 4 "math" 5 "testing" 6 ) 7 8 func TestValEncoder_Float32(t *testing.T) { 9 f := func(t *testing.T, f float32) { 10 checkEncodeValueWithStandard(t, f, nil) 11 } 12 t.Run("test", func(t *testing.T) { 13 f(t, math.MaxFloat32) 14 }) 15 t.Run("nil pointer", func(t *testing.T) { 16 checkEncodeValueWithStandard(t, (*float32)(nil), nil) 17 }) 18 t.Run("pointer", func(t *testing.T) { 19 f := float32(math.MaxFloat32) 20 checkEncodeValueWithStandard(t, &f, nil) 21 }) 22 } 23 24 func TestValEncoder_Float64(t *testing.T) { 25 f := func(t *testing.T, f float64) { 26 checkEncodeValueWithStandard(t, f, nil) 27 } 28 t.Run("test", func(t *testing.T) { 29 f(t, math.MaxFloat64) 30 }) 31 t.Run("nil pointer", func(t *testing.T) { 32 checkEncodeValueWithStandard(t, (*float64)(nil), nil) 33 }) 34 t.Run("pointer", func(t *testing.T) { 35 f := float64(math.MaxFloat64) 36 checkEncodeValueWithStandard(t, &f, nil) 37 }) 38 } 39 40 func TestValEncoder_Float32_OmitEmpty(t *testing.T) { 41 type st struct { 42 A float32 `json:",omitempty"` 43 } 44 t.Run("zero", func(t *testing.T) { 45 checkEncodeValueWithStandard(t, st{}, nil) 46 }) 47 t.Run("explicit zero", func(t *testing.T) { 48 checkEncodeValueWithStandard(t, st{ 49 A: 0.0, 50 }, nil) 51 }) 52 } 53 54 func TestValEncoder_Float64_OmitEmpty(t *testing.T) { 55 type st struct { 56 A float64 `json:",omitempty"` 57 } 58 t.Run("zero", func(t *testing.T) { 59 checkEncodeValueWithStandard(t, st{}, nil) 60 }) 61 t.Run("explicit zero", func(t *testing.T) { 62 checkEncodeValueWithStandard(t, st{ 63 A: 0.0, 64 }, nil) 65 }) 66 }