github.com/zerosnake0/jzon@v0.0.9-0.20230801092939-1b135cb83f7f/iterator_float64_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_ReadFloat64(t *testing.T) {
    12  	t.Run("leading space", func(t *testing.T) {
    13  		withIterator(" 1", func(it *Iterator) {
    14  			f, err := it.ReadFloat64()
    15  			require.NoError(t, err)
    16  			require.Equal(t, float64(1), f)
    17  		})
    18  	})
    19  	t.Run("first byte error", func(t *testing.T) {
    20  		withIterator("", func(it *Iterator) {
    21  			_, err := it.ReadFloat64()
    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.ReadFloat64()
    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.ReadFloat64()
    34  			require.NoError(t, err)
    35  			require.Equal(t, float64(0), f)
    36  		})
    37  	})
    38  	t.Run("negative 1", func(t *testing.T) {
    39  		withIterator("-3.1415926535", func(it *Iterator) {
    40  			f, err := it.ReadFloat64()
    41  			require.NoError(t, err)
    42  			require.InDelta(t, float64(-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.ReadFloat64()
    48  			require.NoError(t, err)
    49  			require.Equal(t, float64(0), f)
    50  		})
    51  	})
    52  	t.Run("positive 1", func(t *testing.T) {
    53  		withIterator("3.1415926535", func(it *Iterator) {
    54  			f, err := it.ReadFloat64()
    55  			require.NoError(t, err)
    56  			require.InDelta(t, float64(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.ReadFloat64()
    62  			require.IsType(t, InvalidFloatError{}, err)
    63  		})
    64  	})
    65  }
    66  
    67  func TestIterator_Float_ReadFloat64_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.ReadFloat64()
    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  			_, err := it.ReadFloat64()
    82  			require.Equal(t, io.EOF, err)
    83  		})
    84  	})
    85  	t.Run("fraction invalid byte after dot", func(t *testing.T) {
    86  		withIterator("0.a", func(it *Iterator) {
    87  			_, err := it.ReadFloat64()
    88  			require.IsType(t, InvalidFloatError{}, err)
    89  		})
    90  	})
    91  	t.Run("fraction 1", func(t *testing.T) {
    92  		withIterator("0.1a", func(it *Iterator) {
    93  			f, err := it.ReadFloat64()
    94  			require.NoError(t, err)
    95  			require.InDelta(t, float64(0.1), f, 1e-10)
    96  		})
    97  	})
    98  	t.Run("fraction 2", func(t *testing.T) {
    99  		withIterator("0.1", func(it *Iterator) {
   100  			f, err := it.ReadFloat64()
   101  			require.NoError(t, err)
   102  			require.InDelta(t, float64(0.1), f, 1e-10)
   103  		})
   104  	})
   105  	t.Run("fraction reader error", func(t *testing.T) {
   106  		withIterator("", func(it *Iterator) {
   107  			e := errors.New("test")
   108  			it.Reset(&stepByteReader{
   109  				b:   "0.",
   110  				err: e,
   111  			})
   112  			_, err := it.ReadFloat64()
   113  			require.Equal(t, e, err)
   114  		})
   115  	})
   116  	t.Run("fraction reader error 2", func(t *testing.T) {
   117  		withIterator("", func(it *Iterator) {
   118  			e := errors.New("test")
   119  			it.Reset(&stepByteReader{
   120  				b:   "0.1",
   121  				err: e,
   122  			})
   123  			_, err := it.ReadFloat64()
   124  			require.Equal(t, e, err)
   125  		})
   126  	})
   127  	t.Run("exponent eof", func(t *testing.T) {
   128  		withIterator("0.1e", func(it *Iterator) {
   129  			_, err := it.ReadFloat64()
   130  			require.Equal(t, io.EOF, err)
   131  		})
   132  	})
   133  	t.Run("exponent eof after sign", func(t *testing.T) {
   134  		withIterator("0.1e+", func(it *Iterator) {
   135  			_, err := it.ReadFloat64()
   136  			require.Equal(t, io.EOF, err)
   137  		})
   138  	})
   139  	t.Run("exponent invalid byte after sign", func(t *testing.T) {
   140  		withIterator("0.1e++", func(it *Iterator) {
   141  			_, err := it.ReadFloat64()
   142  			require.IsType(t, InvalidFloatError{}, err)
   143  		})
   144  	})
   145  	t.Run("exponent 1", func(t *testing.T) {
   146  		withIterator("0.1e+1", func(it *Iterator) {
   147  			f, err := it.ReadFloat64()
   148  			require.NoError(t, err)
   149  			require.InDelta(t, float64(1), f, 1e-10)
   150  		})
   151  	})
   152  	t.Run("exponent 2", func(t *testing.T) {
   153  		withIterator("0.1e+1a", func(it *Iterator) {
   154  			f, err := it.ReadFloat64()
   155  			require.NoError(t, err)
   156  			require.InDelta(t, float64(1), f, 1e-10)
   157  		})
   158  	})
   159  	t.Run("exponent reader error", func(t *testing.T) {
   160  		withIterator("", func(it *Iterator) {
   161  			e := errors.New("test")
   162  			it.Reset(&stepByteReader{
   163  				b:   "0.1e+1",
   164  				err: e,
   165  			})
   166  			_, err := it.ReadFloat64()
   167  			require.Equal(t, e, err)
   168  		})
   169  	})
   170  	t.Run("no fraction part", func(t *testing.T) {
   171  		withIterator("0e+1", func(it *Iterator) {
   172  			f, err := it.ReadFloat64()
   173  			require.NoError(t, err)
   174  			require.Equal(t, float64(0), f)
   175  		})
   176  	})
   177  	t.Run("only zero", func(t *testing.T) {
   178  		withIterator("0a", func(it *Iterator) {
   179  			f, err := it.ReadFloat64()
   180  			require.NoError(t, err)
   181  			require.Equal(t, float64(0), f)
   182  		})
   183  	})
   184  	t.Run("double zero", func(t *testing.T) {
   185  		withIterator("00", func(it *Iterator) {
   186  			f, err := it.ReadFloat64()
   187  			require.NoError(t, err)
   188  			require.Equal(t, float64(0), f)
   189  		})
   190  	})
   191  }
   192  
   193  func TestIterator_Float_ReadFloat64_NonLeadingZero(t *testing.T) {
   194  	t.Run("exponent", func(t *testing.T) {
   195  		withIterator("1e1", func(it *Iterator) {
   196  			f, err := it.ReadFloat64()
   197  			require.NoError(t, err)
   198  			require.InDelta(t, float64(10), f, 1e-10)
   199  		})
   200  	})
   201  	t.Run("no fraction no exponent 1", func(t *testing.T) {
   202  		withIterator("10", func(it *Iterator) {
   203  			f, err := it.ReadFloat64()
   204  			require.NoError(t, err)
   205  			require.InDelta(t, float64(10), f, 1e-10)
   206  		})
   207  	})
   208  	t.Run("no fraction no exponent 2", func(t *testing.T) {
   209  		withIterator("10a", func(it *Iterator) {
   210  			f, err := it.ReadFloat64()
   211  			require.NoError(t, err)
   212  			require.InDelta(t, float64(10), f, 1e-10)
   213  		})
   214  	})
   215  	t.Run("reader error", func(t *testing.T) {
   216  		withIterator("", func(it *Iterator) {
   217  			e := errors.New("test")
   218  			it.Reset(&stepByteReader{
   219  				b:   "10",
   220  				err: e,
   221  			})
   222  			_, err := it.ReadFloat64()
   223  			require.Equal(t, e, err)
   224  		})
   225  	})
   226  }