| 8 | ) |
| 9 | |
| 10 | func TestStack(t *testing.T) { |
| 11 | tests := []struct { |
| 12 | name string |
| 13 | operations func(st *Stack) interface{} |
| 14 | expected interface{} |
| 15 | }{ |
| 16 | { |
| 17 | name: "Push and Pop", |
| 18 | operations: func(st *Stack) interface{} { |
| 19 | value := uint256.NewInt(1) |
| 20 | st.Push(value) |
| 21 | item := st.Pop() |
| 22 | return item.Uint64() |
| 23 | }, |
| 24 | expected: uint64(1), |
| 25 | }, |
| 26 | { |
| 27 | name: "Peek", |
| 28 | operations: func(st *Stack) interface{} { |
| 29 | value := uint256.NewInt(2) |
| 30 | st.Push(value) |
| 31 | item := st.Peek() |
| 32 | return item.Uint64() |
| 33 | }, |
| 34 | expected: uint64(2), |
| 35 | }, |
| 36 | { |
| 37 | name: "ToString", |
| 38 | operations: func(st *Stack) interface{} { |
| 39 | values := []uint64{1, 2, 3} |
| 40 | for _, v := range values { |
| 41 | st.Push(uint256.NewInt(v)) |
| 42 | } |
| 43 | return st.ToString() |
| 44 | }, |
| 45 | expected: "[0x3, 0x2, 0x1]", |
| 46 | }, |
| 47 | { |
| 48 | name: "Stack Underflow on Pop", |
| 49 | operations: func(st *Stack) interface{} { |
| 50 | defer func() { // We can either use this 'recover' approach or assert.Panics(...) |
| 51 | if r := recover(); r != nil { |
| 52 | assert.Equal(t, ErrStackUnderflow.Error(), r) |
| 53 | } |
| 54 | }() |
| 55 | return st.Pop() |
| 56 | }, |
| 57 | expected: nil, // we expect panic |
| 58 | }, |
| 59 | { |
| 60 | name: "Stack Underflow on Peek", |
| 61 | operations: func(st *Stack) interface{} { |
| 62 | defer func() { // We can either use this 'recover' approach or assert.Panics(...) |
| 63 | if r := recover(); r != nil { |
| 64 | assert.Equal(t, ErrStackUnderflow.Error(), r) |
| 65 | } |
| 66 | }() |
| 67 | return st.Peek() |