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