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

     1  package jzon
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"math"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func TestIterator_Int_ReadUint16(t *testing.T) {
    13  	t.Run("leading space", func(t *testing.T) {
    14  		withIterator(" 1", func(it *Iterator) {
    15  			u, err := it.ReadUint16()
    16  			require.NoError(t, err)
    17  			require.Equal(t, uint16(1), u)
    18  		})
    19  	})
    20  	t.Run("invalid first byte", func(t *testing.T) {
    21  		withIterator("", func(it *Iterator) {
    22  			_, err := it.ReadUint16()
    23  			require.Equal(t, io.EOF, err)
    24  		})
    25  	})
    26  	t.Run("zero", func(t *testing.T) {
    27  		withIterator("0", func(it *Iterator) {
    28  			i, err := it.ReadUint16()
    29  			require.NoError(t, err)
    30  			require.Equal(t, uint16(0), i)
    31  		})
    32  	})
    33  	t.Run("invalid first digit", func(t *testing.T) {
    34  		withIterator("-", func(it *Iterator) {
    35  			_, err := it.ReadUint16()
    36  			require.IsType(t, InvalidDigitError{}, err)
    37  		})
    38  	})
    39  	t.Run("early return", func(t *testing.T) {
    40  		withIterator("1a", func(it *Iterator) {
    41  			u, err := it.ReadUint16()
    42  			require.NoError(t, err)
    43  			require.Equal(t, uint16(1), u)
    44  		})
    45  	})
    46  	t.Run("overflow 1", func(t *testing.T) {
    47  		s := fmt.Sprintf("%d0", math.MaxUint16/10+1)
    48  		withIterator(s, func(it *Iterator) {
    49  			_, err := it.ReadUint16()
    50  			require.IsType(t, IntOverflowError{}, err)
    51  		})
    52  	})
    53  	t.Run("overflow 2", func(t *testing.T) {
    54  		d := math.MaxUint16 / 10
    55  		m := math.MaxUint16 - d*10
    56  		s := fmt.Sprintf("%d%d", d, m+1)
    57  		withIterator(s, func(it *Iterator) {
    58  			_, err := it.ReadUint16()
    59  			require.IsType(t, IntOverflowError{}, err)
    60  		})
    61  	})
    62  	t.Run("max uint16", func(t *testing.T) {
    63  		var m uint16 = math.MaxUint16
    64  		s := fmt.Sprint(m)
    65  		withIterator(s, func(it *Iterator) {
    66  			u, err := it.ReadUint16()
    67  			require.NoError(t, err)
    68  			require.Equal(t, m, u)
    69  		})
    70  	})
    71  	t.Run("reader", func(t *testing.T) {
    72  		withIterator("", func(it *Iterator) {
    73  			var m uint16 = math.MaxUint16
    74  			it.Reset(&stepByteReader{
    75  				b: fmt.Sprint(m),
    76  			})
    77  			u, err := it.ReadUint16()
    78  			require.NoError(t, err)
    79  			require.Equal(t, m, u)
    80  		})
    81  	})
    82  }
    83  
    84  func TestIterator_Int_ReadInt16(t *testing.T) {
    85  	t.Run("leading space", func(t *testing.T) {
    86  		withIterator(" -1", func(it *Iterator) {
    87  			i, err := it.ReadInt16()
    88  			require.NoError(t, err)
    89  			require.Equal(t, int16(-1), i)
    90  		})
    91  	})
    92  	t.Run("first byte error", func(t *testing.T) {
    93  		withIterator("", func(it *Iterator) {
    94  			_, err := it.ReadInt16()
    95  			require.Equal(t, io.EOF, err)
    96  		})
    97  	})
    98  	t.Run("negative error", func(t *testing.T) {
    99  		withIterator("-", func(it *Iterator) {
   100  			_, err := it.ReadInt16()
   101  			require.Equal(t, io.EOF, err)
   102  		})
   103  	})
   104  	t.Run("negative readUint16 error", func(t *testing.T) {
   105  		withIterator("-a", func(it *Iterator) {
   106  			_, err := it.ReadInt16()
   107  			require.IsType(t, InvalidDigitError{}, err)
   108  		})
   109  	})
   110  	t.Run("negative overflow", func(t *testing.T) {
   111  		s := fmt.Sprint(-math.MaxInt16 - 2)
   112  		withIterator(s, func(it *Iterator) {
   113  			_, err := it.ReadInt16()
   114  			e, ok := err.(IntOverflowError)
   115  			require.True(t, ok)
   116  			require.Equal(t, "int16", e.typ)
   117  			require.Equal(t, s, e.value)
   118  		})
   119  	})
   120  	t.Run("negative max", func(t *testing.T) {
   121  		s := fmt.Sprint(-math.MaxInt16 - 1)
   122  		withIterator(s, func(it *Iterator) {
   123  			i, err := it.ReadInt16()
   124  			require.NoError(t, err)
   125  			require.Equal(t, int16(-math.MaxInt16-1), i)
   126  		})
   127  	})
   128  	t.Run("positive readUint16 error", func(t *testing.T) {
   129  		withIterator("a", func(it *Iterator) {
   130  			_, err := it.ReadInt16()
   131  			require.IsType(t, InvalidDigitError{}, err)
   132  		})
   133  	})
   134  	t.Run("positive overflow", func(t *testing.T) {
   135  		s := fmt.Sprint(math.MaxInt16 + 1)
   136  		withIterator(s, func(it *Iterator) {
   137  			_, err := it.ReadInt16()
   138  			e, ok := err.(IntOverflowError)
   139  			require.True(t, ok)
   140  			require.Equal(t, "int16", e.typ)
   141  			require.Equal(t, s, e.value)
   142  		})
   143  	})
   144  	t.Run("positive max", func(t *testing.T) {
   145  		s := fmt.Sprint(math.MaxInt16)
   146  		withIterator(s, func(it *Iterator) {
   147  			i, err := it.ReadInt16()
   148  			require.NoError(t, err)
   149  			require.Equal(t, int16(math.MaxInt16), i)
   150  		})
   151  	})
   152  }