github.com/hyperledger/burrow@v0.34.5-0.20220512172541-77f09336001d/execution/evm/stack_test.go (about) 1 package evm 2 3 import ( 4 "math" 5 "math/big" 6 "testing" 7 8 "github.com/hyperledger/burrow/binary" 9 "github.com/hyperledger/burrow/execution/errors" 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 ) 13 14 var maxUint64 = big.NewInt(math.MaxInt64) 15 16 func TestStack_MaxDepthInt32(t *testing.T) { 17 st := NewStack(new(errors.Maybe), 0, 0, maxUint64) 18 19 err := st.ensureCapacity(math.MaxInt32 + 1) 20 assert.Error(t, err) 21 } 22 23 // Test static memory allocation with unlimited depth - memory should grow 24 func TestStack_UnlimitedAllocation(t *testing.T) { 25 err := new(errors.Maybe) 26 st := NewStack(err, 0, 0, maxUint64) 27 28 st.Push64(math.MaxInt64) 29 require.NoError(t, err.Error()) 30 assert.Equal(t, 1, len(st.slice)) 31 assert.Equal(t, 1, cap(st.slice)) 32 } 33 34 // Test static memory allocation with maximum == initial capacity - memory should not grow 35 func TestStack_StaticAllocation(t *testing.T) { 36 err := new(errors.Maybe) 37 38 st := NewStack(err, 4, 4, maxUint64) 39 40 for i := 0; i < 4; i++ { 41 st.Push64(math.MaxInt64) 42 assert.NoError(t, err.Error()) 43 } 44 45 assert.Equal(t, 4, cap(st.slice), "Slice capacity should not grow") 46 } 47 48 // Test writing beyond the current capacity - memory should grow 49 func TestDynamicMemory_PushAhead(t *testing.T) { 50 err := new(errors.Maybe) 51 52 st := NewStack(err, 2, 4, maxUint64) 53 54 for i := 0; i < 4; i++ { 55 st.Push64(math.MaxInt64) 56 assert.NoError(t, err.Error()) 57 } 58 59 st.Push64(math.MaxInt64) 60 assert.Equal(t, errors.Codes.DataStackOverflow, errors.GetCode(err.Error())) 61 } 62 63 func TestStack_ZeroInitialCapacity(t *testing.T) { 64 err := new(errors.Maybe) 65 66 st := NewStack(err, 0, 16, maxUint64) 67 require.NoError(t, err.Error()) 68 st.Push64(math.MaxInt64) 69 assert.Equal(t, []binary.Word256{binary.Int64ToWord256(math.MaxInt64)}, st.slice) 70 } 71 72 func TestStack_ensureCapacity(t *testing.T) { 73 74 st := NewStack(new(errors.Maybe), 4, 16, maxUint64) 75 // Check we can grow within bounds 76 err := st.ensureCapacity(8) 77 assert.NoError(t, err) 78 expected := make([]binary.Word256, 8) 79 assert.Equal(t, expected, st.slice) 80 81 // Check we can grow to bounds 82 err = st.ensureCapacity(16) 83 assert.NoError(t, err) 84 expected = make([]binary.Word256, 16) 85 assert.Equal(t, expected, st.slice) 86 87 err = st.ensureCapacity(1) 88 assert.NoError(t, err) 89 assert.Equal(t, 16, len(st.slice)) 90 91 err = st.ensureCapacity(17) 92 assert.Error(t, err, "Should not be possible to grow over capacity") 93 }