github.com/zerosnake0/jzon@v0.0.9-0.20230801092939-1b135cb83f7f/iterator_int8_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_ReadUint8(t *testing.T) { 13 t.Run("leading space", func(t *testing.T) { 14 withIterator(" 1", func(it *Iterator) { 15 u, err := it.ReadUint8() 16 require.NoError(t, err) 17 require.Equal(t, uint8(1), u) 18 }) 19 }) 20 t.Run("invalid first byte", func(t *testing.T) { 21 withIterator("", func(it *Iterator) { 22 _, err := it.ReadUint8() 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.ReadUint8() 29 require.NoError(t, err) 30 require.Equal(t, uint8(0), i) 31 }) 32 }) 33 t.Run("invalid first digit", func(t *testing.T) { 34 withIterator("-", func(it *Iterator) { 35 _, err := it.ReadUint8() 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.ReadUint8() 42 require.NoError(t, err) 43 require.Equal(t, uint8(1), u) 44 }) 45 }) 46 t.Run("early return2", func(t *testing.T) { 47 withIterator("12a", func(it *Iterator) { 48 u, err := it.ReadUint8() 49 require.NoError(t, err) 50 require.Equal(t, uint8(12), u) 51 }) 52 }) 53 t.Run("overflow 1", func(t *testing.T) { 54 s := fmt.Sprintf("%d0", math.MaxUint8/10+1) 55 withIterator(s, func(it *Iterator) { 56 _, err := it.ReadUint8() 57 require.IsType(t, IntOverflowError{}, err) 58 }) 59 }) 60 t.Run("overflow 2", func(t *testing.T) { 61 d := math.MaxUint8 / 10 62 m := math.MaxUint8 - d*10 63 s := fmt.Sprintf("%d%d", d, m+1) 64 withIterator(s, func(it *Iterator) { 65 _, err := it.ReadUint8() 66 require.IsType(t, IntOverflowError{}, err) 67 }) 68 }) 69 t.Run("max uint8", func(t *testing.T) { 70 var m uint8 = math.MaxUint8 71 s := fmt.Sprint(m) 72 withIterator(s, func(it *Iterator) { 73 u, err := it.ReadUint8() 74 require.NoError(t, err) 75 require.Equal(t, m, u) 76 }) 77 }) 78 t.Run("reader", func(t *testing.T) { 79 withIterator("", func(it *Iterator) { 80 var m uint8 = math.MaxUint8 81 it.Reset(&stepByteReader{ 82 b: fmt.Sprint(m), 83 }) 84 u, err := it.ReadUint8() 85 require.NoError(t, err) 86 require.Equal(t, m, u) 87 }) 88 }) 89 t.Run("all values", func(t *testing.T) { 90 for i := 0; i <= math.MaxUint8; i++ { 91 s := fmt.Sprint(i) 92 withIterator(s, func(it *Iterator) { 93 j, err := it.ReadUint8() 94 require.NoError(t, err, "value %d", i) 95 require.Equal(t, uint8(i), j) 96 }) 97 } 98 }) 99 } 100 101 func TestIterator_Int_ReadInt8(t *testing.T) { 102 t.Run("leading space", func(t *testing.T) { 103 withIterator(" -1", func(it *Iterator) { 104 i, err := it.ReadInt8() 105 require.NoError(t, err) 106 require.Equal(t, int8(-1), i) 107 }) 108 }) 109 t.Run("first byte error", func(t *testing.T) { 110 withIterator("", func(it *Iterator) { 111 _, err := it.ReadInt8() 112 require.Equal(t, io.EOF, err) 113 }) 114 }) 115 t.Run("negative error", func(t *testing.T) { 116 withIterator("-", func(it *Iterator) { 117 _, err := it.ReadInt8() 118 require.Equal(t, io.EOF, err) 119 }) 120 }) 121 t.Run("negative readUint8 error", func(t *testing.T) { 122 withIterator("-a", func(it *Iterator) { 123 _, err := it.ReadInt8() 124 require.IsType(t, InvalidDigitError{}, err) 125 }) 126 }) 127 t.Run("negative overflow", func(t *testing.T) { 128 s := fmt.Sprint(-math.MaxInt8 - 2) 129 withIterator(s, func(it *Iterator) { 130 _, err := it.ReadInt8() 131 e, ok := err.(IntOverflowError) 132 require.True(t, ok) 133 require.Equal(t, "int8", e.typ) 134 require.Equal(t, s, e.value) 135 }) 136 }) 137 t.Run("negative max", func(t *testing.T) { 138 s := fmt.Sprint(-math.MaxInt8 - 1) 139 withIterator(s, func(it *Iterator) { 140 i, err := it.ReadInt8() 141 require.NoError(t, err) 142 require.Equal(t, int8(-math.MaxInt8-1), i) 143 }) 144 }) 145 t.Run("positive readUint8 error", func(t *testing.T) { 146 withIterator("a", func(it *Iterator) { 147 _, err := it.ReadInt8() 148 require.IsType(t, InvalidDigitError{}, err) 149 }) 150 }) 151 t.Run("positive overflow", func(t *testing.T) { 152 s := fmt.Sprint(math.MaxInt8 + 1) 153 withIterator(s, func(it *Iterator) { 154 _, err := it.ReadInt8() 155 e, ok := err.(IntOverflowError) 156 require.True(t, ok) 157 require.Equal(t, "int8", e.typ) 158 require.Equal(t, s, e.value) 159 }) 160 }) 161 t.Run("positive max", func(t *testing.T) { 162 s := fmt.Sprint(math.MaxInt8) 163 withIterator(s, func(it *Iterator) { 164 i, err := it.ReadInt8() 165 require.NoError(t, err) 166 require.Equal(t, int8(math.MaxInt8), i) 167 }) 168 }) 169 }