github.com/zerosnake0/jzon@v0.0.9-0.20230801092939-1b135cb83f7f/iterator_float32_test.go (about) 1 package jzon 2 3 import ( 4 "errors" 5 "io" 6 "testing" 7 8 "github.com/stretchr/testify/require" 9 ) 10 11 func TestIterator_Float_ReadFloat32(t *testing.T) { 12 t.Run("leading space", func(t *testing.T) { 13 withIterator(" 1", func(it *Iterator) { 14 f, err := it.ReadFloat32() 15 require.NoError(t, err) 16 require.Equal(t, float32(1), f) 17 }) 18 }) 19 t.Run("first byte error", func(t *testing.T) { 20 withIterator(" ", func(it *Iterator) { 21 _, err := it.ReadFloat32() 22 require.Equal(t, io.EOF, err) 23 }) 24 }) 25 t.Run("negative error", func(t *testing.T) { 26 withIterator("-", func(it *Iterator) { 27 _, err := it.ReadFloat32() 28 require.Equal(t, io.EOF, err) 29 }) 30 }) 31 t.Run("negative", func(t *testing.T) { 32 withIterator("-0", func(it *Iterator) { 33 f, err := it.ReadFloat32() 34 require.NoError(t, err) 35 require.Equal(t, float32(0), f) 36 }) 37 }) 38 t.Run("negative 1", func(t *testing.T) { 39 withIterator("-3.1415926535", func(it *Iterator) { 40 f, err := it.ReadFloat32() 41 require.NoError(t, err) 42 require.InDelta(t, float32(-3.1415926535), f, 1e-10) 43 }) 44 }) 45 t.Run("positive", func(t *testing.T) { 46 withIterator("0", func(it *Iterator) { 47 f, err := it.ReadFloat32() 48 require.NoError(t, err) 49 require.Equal(t, float32(0), f) 50 }) 51 }) 52 t.Run("positive 1", func(t *testing.T) { 53 withIterator("3.1415926535", func(it *Iterator) { 54 f, err := it.ReadFloat32() 55 require.NoError(t, err) 56 require.InDelta(t, float32(3.1415926535), f, 1e-10) 57 }) 58 }) 59 t.Run("invalid first char", func(t *testing.T) { 60 withIterator("a", func(it *Iterator) { 61 _, err := it.ReadFloat32() 62 require.IsType(t, InvalidFloatError{}, err) 63 }) 64 }) 65 } 66 67 func TestIterator_Float_ReadFloat32_LeadingZero(t *testing.T) { 68 t.Run("reader error", func(t *testing.T) { 69 withIterator("", func(it *Iterator) { 70 e := errors.New("test") 71 it.Reset(&stepByteReader{ 72 b: "0", 73 err: e, 74 }) 75 _, err := it.ReadFloat32() 76 require.Equal(t, e, err) 77 }) 78 }) 79 t.Run("fraction eof after dot", func(t *testing.T) { 80 withIterator("0.", func(it *Iterator) { 81 it.ResetBytes([]byte("0.")) 82 _, err := it.ReadFloat32() 83 require.Equal(t, io.EOF, err) 84 }) 85 }) 86 t.Run("fraction invalid byte after dot", func(t *testing.T) { 87 withIterator("0.a", func(it *Iterator) { 88 _, err := it.ReadFloat32() 89 require.IsType(t, InvalidFloatError{}, err) 90 }) 91 }) 92 t.Run("fraction 1", func(t *testing.T) { 93 withIterator("0.1a", func(it *Iterator) { 94 f, err := it.ReadFloat32() 95 require.NoError(t, err) 96 require.InDelta(t, float32(0.1), f, 1e-10) 97 }) 98 }) 99 t.Run("fraction 2", func(t *testing.T) { 100 withIterator("0.1", func(it *Iterator) { 101 f, err := it.ReadFloat32() 102 require.NoError(t, err) 103 require.InDelta(t, float32(0.1), f, 1e-10) 104 }) 105 }) 106 t.Run("fraction reader error", func(t *testing.T) { 107 withIterator("", func(it *Iterator) { 108 e := errors.New("test") 109 it.Reset(&stepByteReader{ 110 b: "0.", 111 err: e, 112 }) 113 _, err := it.ReadFloat32() 114 require.Equal(t, e, err) 115 }) 116 }) 117 t.Run("fraction reader error 2", func(t *testing.T) { 118 withIterator("", func(it *Iterator) { 119 e := errors.New("test") 120 it.Reset(&stepByteReader{ 121 b: "0.1", 122 err: e, 123 }) 124 _, err := it.ReadFloat32() 125 require.Equal(t, e, err) 126 }) 127 }) 128 t.Run("exponent eof", func(t *testing.T) { 129 withIterator("0.1e", func(it *Iterator) { 130 _, err := it.ReadFloat32() 131 require.Equal(t, io.EOF, err) 132 }) 133 }) 134 t.Run("exponent eof after sign", func(t *testing.T) { 135 withIterator("0.1e+", func(it *Iterator) { 136 _, err := it.ReadFloat32() 137 require.Equal(t, io.EOF, err) 138 }) 139 }) 140 t.Run("exponent invalid byte after sign", func(t *testing.T) { 141 withIterator("0.1e++", func(it *Iterator) { 142 _, err := it.ReadFloat32() 143 require.IsType(t, InvalidFloatError{}, err) 144 }) 145 }) 146 t.Run("exponent 1", func(t *testing.T) { 147 withIterator("0.1e+1", func(it *Iterator) { 148 f, err := it.ReadFloat32() 149 require.NoError(t, err) 150 require.InDelta(t, float32(1), f, 1e-10) 151 }) 152 }) 153 t.Run("exponent 2", func(t *testing.T) { 154 withIterator("0.1e+1a", func(it *Iterator) { 155 f, err := it.ReadFloat32() 156 require.NoError(t, err) 157 require.InDelta(t, float32(1), f, 1e-10) 158 }) 159 }) 160 t.Run("exponent reader error", func(t *testing.T) { 161 withIterator("", func(it *Iterator) { 162 e := errors.New("test") 163 it.Reset(&stepByteReader{ 164 b: "0.1e+1", 165 err: e, 166 }) 167 _, err := it.ReadFloat32() 168 require.Equal(t, e, err) 169 }) 170 }) 171 t.Run("no fraction part", func(t *testing.T) { 172 withIterator("0e+1", func(it *Iterator) { 173 f, err := it.ReadFloat32() 174 require.NoError(t, err) 175 require.Equal(t, float32(0), f) 176 }) 177 }) 178 t.Run("only zero", func(t *testing.T) { 179 withIterator("0a", func(it *Iterator) { 180 f, err := it.ReadFloat32() 181 require.NoError(t, err) 182 require.Equal(t, float32(0), f) 183 }) 184 }) 185 t.Run("double zero", func(t *testing.T) { 186 withIterator("00", func(it *Iterator) { 187 f, err := it.ReadFloat32() 188 require.NoError(t, err) 189 require.Equal(t, float32(0), f) 190 }) 191 }) 192 } 193 194 func TestIterator_Float_ReadFloat32_NonLeadingZero(t *testing.T) { 195 t.Run("exponent", func(t *testing.T) { 196 withIterator("1e1", func(it *Iterator) { 197 f, err := it.ReadFloat32() 198 require.NoError(t, err) 199 require.InDelta(t, float32(10), f, 1e-10) 200 }) 201 }) 202 t.Run("no fraction no exponent 1", func(t *testing.T) { 203 withIterator("10", func(it *Iterator) { 204 f, err := it.ReadFloat32() 205 require.NoError(t, err) 206 require.InDelta(t, float32(10), f, 1e-10) 207 }) 208 }) 209 t.Run("no fraction no exponent 2", func(t *testing.T) { 210 withIterator("10a", func(it *Iterator) { 211 f, err := it.ReadFloat32() 212 require.NoError(t, err) 213 require.InDelta(t, float32(10), f, 1e-10) 214 }) 215 }) 216 t.Run("reader error", func(t *testing.T) { 217 withIterator("", func(it *Iterator) { 218 e := errors.New("test") 219 it.Reset(&stepByteReader{ 220 b: "10", 221 err: e, 222 }) 223 _, err := it.ReadFloat32() 224 require.Equal(t, e, err) 225 }) 226 }) 227 }