github.com/zerosnake0/jzon@v0.0.9-0.20230801092939-1b135cb83f7f/streamer_float_test.go (about) 1 package jzon 2 3 import ( 4 "math" 5 "testing" 6 7 "github.com/stretchr/testify/require" 8 ) 9 10 func TestStreamer_Float32_Error(t *testing.T) { 11 t.Run("chain", func(t *testing.T) { 12 testStreamerChainError(t, func(s *Streamer) { 13 s.Float32(1) 14 }) 15 }) 16 t.Run("infinity", func(t *testing.T) { 17 f := float32(math.Inf(0)) 18 s := NewStreamer() 19 defer s.Release() 20 s.Float32(f) 21 require.Equal(t, ErrFloatIsInfinity, s.Error) 22 }) 23 t.Run("nan", func(t *testing.T) { 24 f := float32(math.NaN()) 25 s := NewStreamer() 26 defer s.Release() 27 s.Float32(f) 28 require.Equal(t, ErrFloatIsNan, s.Error) 29 }) 30 } 31 32 func TestStreamer_Float32(t *testing.T) { 33 f := func(t *testing.T, f float32) { 34 checkEncodeWithStandard(t, f, func(s *Streamer) { 35 s.Float32(f) 36 }, nil) 37 } 38 t.Run("1.2e-3", func(t *testing.T) { 39 f(t, 1.2e-3) 40 }) 41 t.Run("1e-7", func(t *testing.T) { 42 f(t, 1e-7) 43 }) 44 t.Run("1e21", func(t *testing.T) { 45 f(t, 1e21) 46 }) 47 t.Run("-1e-7", func(t *testing.T) { 48 f(t, -1e-7) 49 }) 50 t.Run("-1e21", func(t *testing.T) { 51 f(t, -1e21) 52 }) 53 } 54 55 func TestStreamer_Float64_Error(t *testing.T) { 56 t.Run("chain", func(t *testing.T) { 57 testStreamerChainError(t, func(s *Streamer) { 58 s.Float64(1) 59 }) 60 }) 61 t.Run("infinity", func(t *testing.T) { 62 f := math.Inf(0) 63 s := NewStreamer() 64 defer s.Release() 65 s.Float64(f) 66 require.Equal(t, ErrFloatIsInfinity, s.Error) 67 }) 68 t.Run("nan", func(t *testing.T) { 69 f := math.NaN() 70 s := NewStreamer() 71 defer s.Release() 72 s.Float64(f) 73 require.Equal(t, ErrFloatIsNan, s.Error) 74 }) 75 } 76 77 func TestStreamer_Float64(t *testing.T) { 78 f := func(t *testing.T, f float64) { 79 checkEncodeWithStandard(t, f, func(s *Streamer) { 80 s.Float64(f) 81 }, nil) 82 } 83 t.Run("1.2e-3", func(t *testing.T) { 84 f(t, 1.2e-3) 85 }) 86 t.Run("1e-7", func(t *testing.T) { 87 f(t, 1e-7) 88 }) 89 t.Run("1e21", func(t *testing.T) { 90 f(t, 1e21) 91 }) 92 t.Run("-1e-7", func(t *testing.T) { 93 f(t, -1e-7) 94 }) 95 t.Run("-1e21", func(t *testing.T) { 96 f(t, -1e21) 97 }) 98 }