neoneo-go/pkg/vm/stack/stack_test.go
decentralisedkev c1b6738bdb
VM: Add basic vm (#166)
* VM:Add abstract stack item

* VM: Add stackItems; Array, Boolean, Int and ByteArray

* VM: Add tests for stack item

* VM: first pass at Random Access Stack object

* VM: Add Sub, Mul, Mod LSH, RSH

* VM: moved test helper functions into separate file

* VM: removed helper functions from stack_test.go

* Add conversions for bytearray and Int stack items

* Add instructions file for vm

* - Add guide to stack readme
- Add testReadInt64

* Add Builder

* Refactor Int, Boolean, ByteArray conversion

* Add Context stack Item

* Add Invocation stack - convenience RAS

* rename testhelper to test_helper

* Move opcode file

* - Add `Add` OpCode
- Add Opcode Function map

* - Add test for math `Add` opcode
- basic opcode execution

* Add popTwoIntegers convenience func

* Add `SUB` Opcode

* Export Context Read methods
- Return errors where failable

* - Add `Op` to handleOP func signature
- Add PushNBytes OPcode

* remove error on NewBoolean
- Expose underlying with Getter on Boolean StackItem
- Add Equals method for ByteArray

* Make Next() method on Context failable, refactor peekContext and Peek

* Add ExecuteOp, Step and Run methods on the VM

* Add Equal Opcode

* Add THROWIFNOT Opcode

* Add RET Opcode

* Refactor PushNBytes Opcode

* refactor Add, Sub to return VMSTATE
add popTwoByteArrays helper function

* Add basic tests for vm

* clarify vm states

* Add astack

* [VM]

Pass ResultStack to the opcode handlers

* [VM]

refactor handlers to have rstack as argument

* [Stack]

- Change RemoveCurrentContext for PopCurrentContext
- Add CopTo method to stack

* [VM]

Add Result stack Len check in simple run test

* [VM] fix typo

* [Peer/Stall]

Change seconds to milliseconds in test
2019-03-18 21:40:21 +00:00

161 lines
3.6 KiB
Go

package stack
import (
"fmt"
"math/big"
"testing"
"github.com/stretchr/testify/assert"
)
func TestStackPushPop(t *testing.T) {
// Create two stack Integers
a, err := NewInt(big.NewInt(10))
if err != nil {
t.Fail()
}
b, err := NewInt(big.NewInt(20))
if err != nil {
t.Fail()
}
// Create a new stack
testStack := New()
// Push to stack
testStack.Push(a).Push(b)
// There should only be two values on the stack
assert.Equal(t, 2, testStack.Len())
// Pop first element and it should be equal to b
stackElement, err := testStack.Pop()
if err != nil {
t.Fail()
}
item, err := stackElement.Integer()
if err != nil {
t.Fail()
}
assert.Equal(t, true, item.Equal(b))
// Pop second element and it should be equal to a
stackElement, err = testStack.Pop()
if err != nil {
t.Fail()
}
item, err = stackElement.Integer()
if err != nil {
t.Fail()
}
assert.Equal(t, true, item.Equal(a))
// We should get an error as there are nomore items left to pop
stackElement, err = testStack.Pop()
assert.NotNil(t, err)
}
// For this test to pass, we should get an error when popping from a nil stack
// and we should initialise and push an element if pushing to an empty stack
func TestPushPopNil(t *testing.T) {
// stack is nil when initialised without New constructor
testStack := RandomAccess{}
// Popping from nil stack
// - should give an error
// - element returned should be nil
stackElement, err := testStack.Pop()
assert.NotNil(t, err)
assert.Nil(t, stackElement)
// stack should still be nil after failing to pop
assert.Nil(t, testStack.vals)
// create a random test stack item
a, err := NewInt(big.NewInt(2))
assert.Nil(t, err)
// push random item to stack
testStack.Push(a)
// push should initialise the stack and put one element on the stack
assert.Equal(t, 1, testStack.Len())
}
// Test passes if we can peek and modify an item
//without modifying the value on the stack
func TestStackPeekMutability(t *testing.T) {
testStack := New()
a, err := NewInt(big.NewInt(2))
assert.Nil(t, err)
b, err := NewInt(big.NewInt(3))
assert.Nil(t, err)
testStack.Push(a).Push(b)
peekedItem := testPeakInteger(t, testStack, 0)
assert.Equal(t, true, peekedItem.Equal(b))
// Check that by modifying the peeked value,
// we did not modify the item on the stack
peekedItem = a
peekedItem.val = big.NewInt(0)
// Pop item from stack and check it is still the same
poppedItem := testPopInteger(t, testStack)
assert.Equal(t, true, poppedItem.Equal(b))
}
func TestStackPeek(t *testing.T) {
testStack := New()
values := []int64{23, 45, 67, 89, 12, 344}
for _, val := range values {
a := testMakeStackInt(t, val)
testStack.Push(a)
}
// i starts at 0, j starts at len(values)-1
for i, j := 0, len(values)-1; j >= 0; i, j = i+1, j-1 {
peekedItem := testPeakInteger(t, testStack, uint16(i))
a := testMakeStackInt(t, values[j])
fmt.Printf("%#v\n", peekedItem.val.Int64())
assert.Equal(t, true, a.Equal(peekedItem))
}
}
func TestStackInsert(t *testing.T) {
testStack := New()
a := testMakeStackInt(t, 2)
b := testMakeStackInt(t, 4)
c := testMakeStackInt(t, 6)
// insert on an empty stack should put element on top
_, err := testStack.Insert(0, a)
assert.Equal(t, err, nil)
_, err = testStack.Insert(0, b)
assert.Equal(t, err, nil)
_, err = testStack.Insert(1, c)
assert.Equal(t, err, nil)
// Order should be [a,c,b]
pop1 := testPopInteger(t, testStack)
pop2 := testPopInteger(t, testStack)
pop3 := testPopInteger(t, testStack)
assert.Equal(t, true, pop1.Equal(b))
assert.Equal(t, true, pop2.Equal(c))
assert.Equal(t, true, pop3.Equal(a))
}