neo-go/pkg/vm/stack/stackitem_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

68 lines
1.7 KiB
Go

package stack
import (
"math/big"
"testing"
"github.com/stretchr/testify/assert"
)
// A simple test to ensure that by embedding the abstract interface
// we immediately become a stack item, with the default values set to nil
func TestInterfaceEmbedding(t *testing.T) {
// Create an anonymous struct that embeds the abstractItem
a := struct {
*abstractItem
}{
&abstractItem{},
}
// Since interface checking can be done at compile time.
// If he abstractItem did not implement all methods of our interface `Item`
// Then any struct which embeds it, will also not implement the Item interface.
// This test would then give errors, at compile time.
var Items []Item
Items = append(Items, a)
// Default methods should give errors
// Here we just need to test against one of the methods in the interface
for _, element := range Items {
x, err := element.Integer()
assert.Nil(t, x)
assert.NotNil(t, err, nil)
}
}
// TestIntCasting is a simple test to test that the Integer method is overwritten
// from the abstractItem
func TestIntMethodOverride(t *testing.T) {
testValues := []int64{0, 10, 200, 30, 90}
var Items []Item
// Convert a range of int64s into Stack Integers
// Adding them into an array of StackItems
for _, num := range testValues {
stackInteger, err := NewInt(big.NewInt(num))
if err != nil {
t.Fail()
}
Items = append(Items, stackInteger)
}
// For each item, call the Integer method on the interface
// Which should return an integer and no error
// as the stack integer struct overrides that method
for i, element := range Items {
k, err := element.Integer()
if err != nil {
t.Fail()
}
if k.val.Cmp(big.NewInt(testValues[i])) != 0 {
t.Fail()
}
}
}