2019-03-15 22:42:35 +00:00
|
|
|
package vm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/big"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/vm/stack"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2019-03-26 23:15:13 +00:00
|
|
|
func TestIncOp(t *testing.T) {
|
|
|
|
|
|
|
|
v := VM{}
|
|
|
|
|
|
|
|
a, err := stack.NewInt(big.NewInt(20))
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
2019-03-26 23:15:13 +00:00
|
|
|
|
|
|
|
ctx := stack.NewContext([]byte{})
|
|
|
|
ctx.Estack.Push(a)
|
|
|
|
|
2019-08-12 13:25:16 +00:00
|
|
|
_, err = v.executeOp(stack.INC, ctx)
|
|
|
|
assert.Nil(t, err)
|
2019-03-26 23:15:13 +00:00
|
|
|
|
|
|
|
// Stack should have one item
|
|
|
|
assert.Equal(t, 1, ctx.Estack.Len())
|
|
|
|
|
|
|
|
item, err := ctx.Estack.PopInt()
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
2019-03-26 23:15:13 +00:00
|
|
|
|
|
|
|
assert.Equal(t, int64(21), item.Value().Int64())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecOp(t *testing.T) {
|
|
|
|
|
|
|
|
v := VM{}
|
|
|
|
|
|
|
|
a, err := stack.NewInt(big.NewInt(20))
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
2019-03-26 23:15:13 +00:00
|
|
|
|
|
|
|
ctx := stack.NewContext([]byte{})
|
|
|
|
ctx.Estack.Push(a)
|
|
|
|
|
2019-08-12 13:25:16 +00:00
|
|
|
_, err = v.executeOp(stack.DEC, ctx)
|
|
|
|
assert.Nil(t, err)
|
2019-03-26 23:15:13 +00:00
|
|
|
|
|
|
|
// Stack should have one item
|
|
|
|
assert.Equal(t, 1, ctx.Estack.Len())
|
|
|
|
|
|
|
|
item, err := ctx.Estack.PopInt()
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
2019-03-26 23:15:13 +00:00
|
|
|
|
|
|
|
assert.Equal(t, int64(19), item.Value().Int64())
|
|
|
|
}
|
|
|
|
|
2019-03-15 22:42:35 +00:00
|
|
|
func TestAddOp(t *testing.T) {
|
|
|
|
|
|
|
|
v := VM{}
|
|
|
|
|
|
|
|
a, err := stack.NewInt(big.NewInt(20))
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2019-03-15 22:42:35 +00:00
|
|
|
b, err := stack.NewInt(big.NewInt(23))
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
2019-03-15 22:42:35 +00:00
|
|
|
|
|
|
|
ctx := stack.NewContext([]byte{})
|
|
|
|
ctx.Estack.Push(a).Push(b)
|
|
|
|
|
2019-08-12 13:25:16 +00:00
|
|
|
_, err = v.executeOp(stack.ADD, ctx)
|
|
|
|
assert.Nil(t, err)
|
2019-03-15 22:42:35 +00:00
|
|
|
|
|
|
|
// Stack should have one item
|
|
|
|
assert.Equal(t, 1, ctx.Estack.Len())
|
|
|
|
|
|
|
|
item, err := ctx.Estack.PopInt()
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
2019-03-15 22:42:35 +00:00
|
|
|
|
|
|
|
assert.Equal(t, int64(43), item.Value().Int64())
|
|
|
|
|
|
|
|
}
|
2019-03-15 22:55:08 +00:00
|
|
|
|
|
|
|
func TestSubOp(t *testing.T) {
|
|
|
|
|
|
|
|
v := VM{}
|
|
|
|
|
|
|
|
a, err := stack.NewInt(big.NewInt(30))
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2019-03-15 22:55:08 +00:00
|
|
|
b, err := stack.NewInt(big.NewInt(40))
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
2019-03-15 22:55:08 +00:00
|
|
|
|
|
|
|
ctx := stack.NewContext([]byte{})
|
|
|
|
ctx.Estack.Push(a).Push(b)
|
|
|
|
|
2019-08-12 13:25:16 +00:00
|
|
|
_, err = v.executeOp(stack.SUB, ctx)
|
|
|
|
assert.Nil(t, err)
|
2019-03-15 22:55:08 +00:00
|
|
|
|
|
|
|
// Stack should have one item
|
|
|
|
assert.Equal(t, 1, ctx.Estack.Len())
|
|
|
|
|
|
|
|
item, err := ctx.Estack.PopInt()
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
2019-03-15 22:55:08 +00:00
|
|
|
|
|
|
|
assert.Equal(t, int64(-10), item.Value().Int64())
|
|
|
|
|
|
|
|
}
|
2019-03-28 18:30:36 +00:00
|
|
|
|
2019-03-28 20:32:32 +00:00
|
|
|
func TestDivOp(t *testing.T) {
|
|
|
|
|
|
|
|
v := VM{}
|
|
|
|
|
|
|
|
a, err := stack.NewInt(big.NewInt(10))
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2019-03-28 20:32:32 +00:00
|
|
|
b, err := stack.NewInt(big.NewInt(4))
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
2019-03-28 20:32:32 +00:00
|
|
|
|
|
|
|
ctx := stack.NewContext([]byte{})
|
|
|
|
ctx.Estack.Push(a).Push(b)
|
|
|
|
|
2019-08-12 13:25:16 +00:00
|
|
|
_, err = v.executeOp(stack.DIV, ctx)
|
|
|
|
assert.Nil(t, err)
|
2019-03-28 20:32:32 +00:00
|
|
|
|
|
|
|
// Stack should have one item
|
|
|
|
assert.Equal(t, 1, ctx.Estack.Len())
|
|
|
|
|
|
|
|
item, err := ctx.Estack.PopInt()
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
2019-03-28 22:28:20 +00:00
|
|
|
|
2019-03-28 20:32:32 +00:00
|
|
|
assert.Equal(t, int64(2), item.Value().Int64())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestModOp(t *testing.T) {
|
|
|
|
|
|
|
|
v := VM{}
|
|
|
|
|
|
|
|
a, err := stack.NewInt(big.NewInt(15))
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2019-03-28 20:32:32 +00:00
|
|
|
b, err := stack.NewInt(big.NewInt(4))
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
2019-03-28 20:32:32 +00:00
|
|
|
|
|
|
|
ctx := stack.NewContext([]byte{})
|
|
|
|
ctx.Estack.Push(a).Push(b)
|
|
|
|
|
2019-08-12 13:25:16 +00:00
|
|
|
_, err = v.executeOp(stack.MOD, ctx)
|
|
|
|
assert.Nil(t, err)
|
2019-03-28 20:32:32 +00:00
|
|
|
|
|
|
|
// Stack should have one item
|
|
|
|
assert.Equal(t, 1, ctx.Estack.Len())
|
|
|
|
|
|
|
|
item, err := ctx.Estack.PopInt()
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
2019-03-28 20:32:32 +00:00
|
|
|
|
|
|
|
assert.Equal(t, int64(3), item.Value().Int64())
|
|
|
|
}
|
|
|
|
|
2019-03-28 19:47:47 +00:00
|
|
|
func TestNzOp(t *testing.T) {
|
|
|
|
|
|
|
|
v := VM{}
|
|
|
|
|
|
|
|
a, err := stack.NewInt(big.NewInt(20))
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
2019-03-28 19:47:47 +00:00
|
|
|
|
|
|
|
ctx := stack.NewContext([]byte{})
|
|
|
|
ctx.Estack.Push(a)
|
|
|
|
|
2019-08-12 13:25:16 +00:00
|
|
|
_, err = v.executeOp(stack.NZ, ctx)
|
|
|
|
assert.Nil(t, err)
|
2019-03-28 19:47:47 +00:00
|
|
|
|
|
|
|
// Stack should have one item
|
|
|
|
assert.Equal(t, 1, ctx.Estack.Len())
|
|
|
|
|
|
|
|
item, err := ctx.Estack.PopBoolean()
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
2019-03-28 19:47:47 +00:00
|
|
|
|
|
|
|
assert.Equal(t, true, item.Value())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMulOp(t *testing.T) {
|
|
|
|
|
|
|
|
v := VM{}
|
|
|
|
|
|
|
|
a, err := stack.NewInt(big.NewInt(20))
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2019-03-28 19:47:47 +00:00
|
|
|
b, err := stack.NewInt(big.NewInt(20))
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
2019-03-28 19:47:47 +00:00
|
|
|
|
|
|
|
ctx := stack.NewContext([]byte{})
|
|
|
|
ctx.Estack.Push(a).Push(b)
|
|
|
|
|
2019-08-12 13:25:16 +00:00
|
|
|
_, err = v.executeOp(stack.MUL, ctx)
|
|
|
|
assert.Nil(t, err)
|
2019-03-28 19:47:47 +00:00
|
|
|
|
|
|
|
// Stack should have one item
|
|
|
|
assert.Equal(t, 1, ctx.Estack.Len())
|
|
|
|
|
|
|
|
item, err := ctx.Estack.PopInt()
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
2019-03-28 19:47:47 +00:00
|
|
|
|
|
|
|
assert.Equal(t, int64(400), item.Value().Int64())
|
|
|
|
}
|
|
|
|
|
2019-03-28 19:26:55 +00:00
|
|
|
func TestAbsOp(t *testing.T) {
|
|
|
|
|
|
|
|
v := VM{}
|
|
|
|
|
|
|
|
a, err := stack.NewInt(big.NewInt(-20))
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
2019-03-28 19:26:55 +00:00
|
|
|
|
|
|
|
ctx := stack.NewContext([]byte{})
|
|
|
|
ctx.Estack.Push(a)
|
|
|
|
|
2019-08-12 13:25:16 +00:00
|
|
|
_, err = v.executeOp(stack.ABS, ctx)
|
|
|
|
assert.Nil(t, err)
|
2019-03-28 19:26:55 +00:00
|
|
|
|
|
|
|
// Stack should have one item
|
|
|
|
assert.Equal(t, 1, ctx.Estack.Len())
|
|
|
|
|
|
|
|
item, err := ctx.Estack.PopInt()
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
2019-03-28 19:26:55 +00:00
|
|
|
|
|
|
|
assert.Equal(t, int64(20), item.Value().Int64())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNotOp(t *testing.T) {
|
|
|
|
|
|
|
|
v := VM{}
|
|
|
|
|
|
|
|
b := stack.NewBoolean(false)
|
|
|
|
|
|
|
|
ctx := stack.NewContext([]byte{})
|
|
|
|
ctx.Estack.Push(b)
|
|
|
|
|
2019-08-12 13:25:16 +00:00
|
|
|
_, err := v.executeOp(stack.NOT, ctx)
|
|
|
|
assert.Nil(t, err)
|
2019-03-28 19:26:55 +00:00
|
|
|
|
|
|
|
// Stack should have one item
|
|
|
|
assert.Equal(t, 1, ctx.Estack.Len())
|
|
|
|
|
|
|
|
item, err := ctx.Estack.PopBoolean()
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
2019-03-28 19:26:55 +00:00
|
|
|
|
|
|
|
assert.Equal(t, true, item.Value())
|
|
|
|
}
|
|
|
|
|
2019-03-30 15:01:06 +00:00
|
|
|
func TestNumEqual(t *testing.T) {
|
|
|
|
|
|
|
|
v := VM{}
|
|
|
|
|
|
|
|
a, err := stack.NewInt(big.NewInt(6))
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2019-03-30 15:01:06 +00:00
|
|
|
b, err := stack.NewInt(big.NewInt(6))
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
2019-03-30 15:01:06 +00:00
|
|
|
|
|
|
|
ctx := stack.NewContext([]byte{})
|
|
|
|
ctx.Estack.Push(a).Push(b)
|
|
|
|
|
2019-08-12 13:25:16 +00:00
|
|
|
_, err = v.executeOp(stack.NUMEQUAL, ctx)
|
|
|
|
assert.Nil(t, err)
|
2019-03-30 15:01:06 +00:00
|
|
|
|
|
|
|
// Stack should have one item
|
|
|
|
assert.Equal(t, 1, ctx.Estack.Len())
|
|
|
|
|
|
|
|
item, err := ctx.Estack.PopBoolean()
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
2019-03-30 15:01:06 +00:00
|
|
|
|
|
|
|
assert.Equal(t, true, item.Value())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNumNotEqual(t *testing.T) {
|
|
|
|
|
|
|
|
v := VM{}
|
|
|
|
|
|
|
|
a, err := stack.NewInt(big.NewInt(5))
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2019-03-30 15:01:06 +00:00
|
|
|
b, err := stack.NewInt(big.NewInt(6))
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
2019-03-30 15:01:06 +00:00
|
|
|
|
|
|
|
ctx := stack.NewContext([]byte{})
|
|
|
|
ctx.Estack.Push(a).Push(b)
|
|
|
|
|
2019-08-12 13:25:16 +00:00
|
|
|
_, err = v.executeOp(stack.NUMNOTEQUAL, ctx)
|
|
|
|
assert.Nil(t, err)
|
2019-03-30 15:01:06 +00:00
|
|
|
|
|
|
|
// Stack should have one item
|
|
|
|
assert.Equal(t, 1, ctx.Estack.Len())
|
2019-03-28 19:26:55 +00:00
|
|
|
|
|
|
|
item, err := ctx.Estack.PopBoolean()
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
2019-03-28 19:26:55 +00:00
|
|
|
|
|
|
|
assert.Equal(t, true, item.Value())
|
|
|
|
}
|
|
|
|
|
2019-03-28 18:30:36 +00:00
|
|
|
func TestSignOp(t *testing.T) {
|
|
|
|
|
|
|
|
v := VM{}
|
|
|
|
|
|
|
|
a, err := stack.NewInt(big.NewInt(-20))
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
2019-03-28 18:30:36 +00:00
|
|
|
|
|
|
|
ctx := stack.NewContext([]byte{})
|
|
|
|
ctx.Estack.Push(a)
|
|
|
|
|
2019-08-12 13:25:16 +00:00
|
|
|
_, err = v.executeOp(stack.SIGN, ctx)
|
|
|
|
assert.Nil(t, err)
|
2019-03-28 18:30:36 +00:00
|
|
|
|
|
|
|
// Stack should have one item
|
|
|
|
assert.Equal(t, 1, ctx.Estack.Len())
|
|
|
|
|
|
|
|
item, err := ctx.Estack.PopInt()
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
2019-03-28 18:30:36 +00:00
|
|
|
|
|
|
|
assert.Equal(t, int64(-1), item.Value().Int64())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNegateOp(t *testing.T) {
|
|
|
|
|
|
|
|
v := VM{}
|
|
|
|
|
|
|
|
a, err := stack.NewInt(big.NewInt(-20))
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
2019-03-28 18:30:36 +00:00
|
|
|
|
|
|
|
ctx := stack.NewContext([]byte{})
|
|
|
|
ctx.Estack.Push(a)
|
|
|
|
|
2019-08-12 13:25:16 +00:00
|
|
|
_, err = v.executeOp(stack.NEGATE, ctx)
|
|
|
|
assert.Nil(t, err)
|
2019-03-28 18:30:36 +00:00
|
|
|
|
|
|
|
// Stack should have one item
|
|
|
|
assert.Equal(t, 1, ctx.Estack.Len())
|
|
|
|
|
|
|
|
item, err := ctx.Estack.PopInt()
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
2019-03-28 18:30:36 +00:00
|
|
|
|
|
|
|
assert.Equal(t, int64(20), item.Value().Int64())
|
|
|
|
}
|
2019-03-28 22:28:20 +00:00
|
|
|
|
2019-04-02 22:43:52 +00:00
|
|
|
func TestLteOp(t *testing.T) {
|
|
|
|
|
|
|
|
v := VM{}
|
|
|
|
|
|
|
|
a, err := stack.NewInt(big.NewInt(10))
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
b, err := stack.NewInt(big.NewInt(10))
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
ctx := stack.NewContext([]byte{})
|
|
|
|
ctx.Estack.Push(a).Push(b)
|
|
|
|
|
|
|
|
// b is the first item pop.
|
|
|
|
// a is the second item pop.
|
|
|
|
// we perform a <= b and place
|
|
|
|
// the result on top of the evaluation
|
|
|
|
// stack
|
2019-08-12 13:25:16 +00:00
|
|
|
_, err = v.executeOp(stack.LTE, ctx)
|
|
|
|
assert.Nil(t, err)
|
2019-04-02 22:43:52 +00:00
|
|
|
|
|
|
|
// Stack should have one item
|
|
|
|
assert.Equal(t, 1, ctx.Estack.Len())
|
|
|
|
|
|
|
|
item, err := ctx.Estack.PopBoolean()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, true, item.Value())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGteOp(t *testing.T) {
|
|
|
|
|
|
|
|
v := VM{}
|
|
|
|
|
|
|
|
a, err := stack.NewInt(big.NewInt(10))
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
b, err := stack.NewInt(big.NewInt(2))
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
ctx := stack.NewContext([]byte{})
|
|
|
|
ctx.Estack.Push(a).Push(b)
|
|
|
|
|
|
|
|
// b is the first item pop.
|
|
|
|
// a is the second item pop.
|
|
|
|
// we perform a >= b and place
|
|
|
|
// the result on top of the evaluation
|
|
|
|
// stack
|
2019-08-12 13:25:16 +00:00
|
|
|
_, err = v.executeOp(stack.GTE, ctx)
|
|
|
|
assert.Nil(t, err)
|
2019-04-02 22:43:52 +00:00
|
|
|
|
|
|
|
// Stack should have one item
|
|
|
|
assert.Equal(t, 1, ctx.Estack.Len())
|
|
|
|
|
|
|
|
item, err := ctx.Estack.PopBoolean()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, true, item.Value())
|
|
|
|
}
|
|
|
|
|
2019-03-28 22:28:20 +00:00
|
|
|
func TestShlOp(t *testing.T) {
|
|
|
|
|
|
|
|
v := VM{}
|
|
|
|
|
|
|
|
a, err := stack.NewInt(big.NewInt(2))
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2019-03-28 22:28:20 +00:00
|
|
|
b, err := stack.NewInt(big.NewInt(3))
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
2019-03-28 22:28:20 +00:00
|
|
|
|
|
|
|
ctx := stack.NewContext([]byte{})
|
|
|
|
ctx.Estack.Push(a).Push(b)
|
|
|
|
|
|
|
|
// b is the first item pop.
|
|
|
|
// a is the second item pop.
|
|
|
|
// we perform a.Lsh(b) and place
|
|
|
|
// the result on top of the evaluation
|
|
|
|
// stack
|
2019-08-12 13:25:16 +00:00
|
|
|
_, err = v.executeOp(stack.SHL, ctx)
|
|
|
|
assert.Nil(t, err)
|
2019-03-28 22:28:20 +00:00
|
|
|
|
|
|
|
// Stack should have one item
|
|
|
|
assert.Equal(t, 1, ctx.Estack.Len())
|
|
|
|
|
|
|
|
item, err := ctx.Estack.PopInt()
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
2019-03-28 22:28:20 +00:00
|
|
|
|
|
|
|
assert.Equal(t, int64(16), item.Value().Int64())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShrOp(t *testing.T) {
|
|
|
|
|
|
|
|
v := VM{}
|
|
|
|
|
|
|
|
a, err := stack.NewInt(big.NewInt(10))
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2019-03-28 22:28:20 +00:00
|
|
|
b, err := stack.NewInt(big.NewInt(2))
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
2019-03-28 22:28:20 +00:00
|
|
|
|
|
|
|
ctx := stack.NewContext([]byte{})
|
|
|
|
ctx.Estack.Push(a).Push(b)
|
|
|
|
|
|
|
|
// b is the first item pop.
|
|
|
|
// a is the second item pop.
|
|
|
|
// we perform a.Rsh(b) and place
|
|
|
|
// the result on top of the evaluation
|
|
|
|
// stack
|
2019-08-12 13:25:16 +00:00
|
|
|
_, err = v.executeOp(stack.SHR, ctx)
|
|
|
|
assert.Nil(t, err)
|
2019-03-28 22:28:20 +00:00
|
|
|
|
|
|
|
// Stack should have one item
|
|
|
|
assert.Equal(t, 1, ctx.Estack.Len())
|
|
|
|
|
|
|
|
item, err := ctx.Estack.PopInt()
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
2019-03-28 22:28:20 +00:00
|
|
|
|
|
|
|
assert.Equal(t, int64(2), item.Value().Int64())
|
|
|
|
}
|
2019-03-29 16:43:16 +00:00
|
|
|
|
2019-03-29 21:22:45 +00:00
|
|
|
func TestBoolAndOp(t *testing.T) {
|
|
|
|
|
2019-04-02 22:43:52 +00:00
|
|
|
v := VM{}
|
2019-03-29 21:22:45 +00:00
|
|
|
|
2019-04-02 22:43:52 +00:00
|
|
|
a := stack.NewBoolean(true)
|
2019-03-29 21:22:45 +00:00
|
|
|
b := stack.NewBoolean(true)
|
|
|
|
|
2019-04-02 22:43:52 +00:00
|
|
|
ctx := stack.NewContext([]byte{})
|
2019-03-29 21:22:45 +00:00
|
|
|
ctx.Estack.Push(a).Push(b)
|
|
|
|
|
2019-08-12 13:25:16 +00:00
|
|
|
_, err := v.executeOp(stack.BOOLAND, ctx)
|
|
|
|
assert.Nil(t, err)
|
2019-03-29 21:22:45 +00:00
|
|
|
|
2019-04-02 22:43:52 +00:00
|
|
|
// Stack should have one item
|
2019-03-29 21:22:45 +00:00
|
|
|
assert.Equal(t, 1, ctx.Estack.Len())
|
|
|
|
|
2019-04-02 22:43:52 +00:00
|
|
|
item, err := ctx.Estack.PopBoolean()
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
2019-03-29 21:22:45 +00:00
|
|
|
|
2019-04-02 22:43:52 +00:00
|
|
|
assert.Equal(t, true, item.Value())
|
2019-03-29 21:22:45 +00:00
|
|
|
}
|
|
|
|
|
2019-04-02 22:43:52 +00:00
|
|
|
func TestBoolOrOp(t *testing.T) {
|
2019-03-29 21:22:45 +00:00
|
|
|
|
2019-04-02 22:43:52 +00:00
|
|
|
v := VM{}
|
2019-03-29 21:22:45 +00:00
|
|
|
|
2019-04-02 22:43:52 +00:00
|
|
|
a := stack.NewBoolean(false)
|
2019-03-29 21:22:45 +00:00
|
|
|
b := stack.NewBoolean(true)
|
|
|
|
|
2019-04-02 22:43:52 +00:00
|
|
|
ctx := stack.NewContext([]byte{})
|
2019-03-29 21:22:45 +00:00
|
|
|
ctx.Estack.Push(a).Push(b)
|
|
|
|
|
2019-08-12 13:25:16 +00:00
|
|
|
_, err := v.executeOp(stack.BOOLOR, ctx)
|
|
|
|
assert.Nil(t, err)
|
2019-03-29 21:22:45 +00:00
|
|
|
|
2019-04-02 22:43:52 +00:00
|
|
|
// Stack should have one item
|
2019-03-29 21:22:45 +00:00
|
|
|
assert.Equal(t, 1, ctx.Estack.Len())
|
|
|
|
|
2019-04-02 22:43:52 +00:00
|
|
|
item, err := ctx.Estack.PopBoolean()
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
2019-03-29 21:22:45 +00:00
|
|
|
|
2019-04-02 22:43:52 +00:00
|
|
|
assert.Equal(t, true, item.Value())
|
2019-03-29 21:22:45 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 19:35:16 +00:00
|
|
|
func TestLtOp(t *testing.T) {
|
|
|
|
|
|
|
|
v := VM{}
|
|
|
|
|
|
|
|
a, err := stack.NewInt(big.NewInt(10))
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
2019-03-29 19:35:16 +00:00
|
|
|
|
|
|
|
b, err := stack.NewInt(big.NewInt(2))
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
2019-03-29 19:35:16 +00:00
|
|
|
|
|
|
|
ctx := stack.NewContext([]byte{})
|
|
|
|
ctx.Estack.Push(a).Push(b)
|
|
|
|
|
|
|
|
// b is the first item pop.
|
|
|
|
// a is the second item pop.
|
|
|
|
// we perform a < b and place
|
|
|
|
// the result on top of the evaluation
|
|
|
|
// stack
|
2019-08-12 13:25:16 +00:00
|
|
|
_, err = v.executeOp(stack.LT, ctx)
|
|
|
|
assert.Nil(t, err)
|
2019-03-29 19:35:16 +00:00
|
|
|
|
|
|
|
// Stack should have one item
|
|
|
|
assert.Equal(t, 1, ctx.Estack.Len())
|
|
|
|
|
|
|
|
item, err := ctx.Estack.PopBoolean()
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
2019-03-29 19:35:16 +00:00
|
|
|
|
|
|
|
assert.Equal(t, false, item.Value())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGtOp(t *testing.T) {
|
|
|
|
|
|
|
|
v := VM{}
|
|
|
|
|
|
|
|
a, err := stack.NewInt(big.NewInt(10))
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
2019-03-29 19:35:16 +00:00
|
|
|
|
|
|
|
b, err := stack.NewInt(big.NewInt(2))
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
2019-03-29 19:35:16 +00:00
|
|
|
|
|
|
|
ctx := stack.NewContext([]byte{})
|
|
|
|
ctx.Estack.Push(a).Push(b)
|
|
|
|
|
|
|
|
// b is the first item pop.
|
|
|
|
// a is the second item pop.
|
|
|
|
// we perform a > b and place
|
|
|
|
// the result on top of the evaluation
|
|
|
|
// stack
|
2019-08-12 13:25:16 +00:00
|
|
|
_, err = v.executeOp(stack.GT, ctx)
|
|
|
|
assert.Nil(t, err)
|
2019-03-29 19:35:16 +00:00
|
|
|
|
|
|
|
// Stack should have one item
|
|
|
|
assert.Equal(t, 1, ctx.Estack.Len())
|
|
|
|
|
|
|
|
item, err := ctx.Estack.PopBoolean()
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
2019-03-29 19:35:16 +00:00
|
|
|
|
|
|
|
assert.Equal(t, true, item.Value())
|
|
|
|
}
|
2019-04-03 18:36:54 +00:00
|
|
|
|
2019-03-29 16:43:16 +00:00
|
|
|
func TestMinOp(t *testing.T) {
|
|
|
|
|
|
|
|
v := VM{}
|
|
|
|
|
|
|
|
a, err := stack.NewInt(big.NewInt(10))
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
2019-03-29 16:43:16 +00:00
|
|
|
|
|
|
|
b, err := stack.NewInt(big.NewInt(2))
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
2019-03-29 16:43:16 +00:00
|
|
|
|
|
|
|
ctx := stack.NewContext([]byte{})
|
|
|
|
ctx.Estack.Push(a).Push(b)
|
|
|
|
|
2019-08-12 13:25:16 +00:00
|
|
|
_, err = v.executeOp(stack.MIN, ctx)
|
|
|
|
assert.Nil(t, err)
|
2019-03-29 16:43:16 +00:00
|
|
|
|
|
|
|
// Stack should have one item
|
|
|
|
assert.Equal(t, 1, ctx.Estack.Len())
|
|
|
|
|
|
|
|
item, err := ctx.Estack.PopInt()
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
2019-03-29 16:43:16 +00:00
|
|
|
|
|
|
|
assert.Equal(t, int64(2), item.Value().Int64())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMaxOp(t *testing.T) {
|
|
|
|
|
|
|
|
v := VM{}
|
|
|
|
|
|
|
|
a, err := stack.NewInt(big.NewInt(10))
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
2019-03-29 16:43:16 +00:00
|
|
|
|
|
|
|
b, err := stack.NewInt(big.NewInt(2))
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
2019-03-29 16:43:16 +00:00
|
|
|
|
|
|
|
ctx := stack.NewContext([]byte{})
|
|
|
|
ctx.Estack.Push(a).Push(b)
|
|
|
|
|
2019-08-12 13:25:16 +00:00
|
|
|
_, err = v.executeOp(stack.MAX, ctx)
|
|
|
|
assert.Nil(t, err)
|
2019-03-29 16:43:16 +00:00
|
|
|
|
|
|
|
// Stack should have one item
|
|
|
|
assert.Equal(t, 1, ctx.Estack.Len())
|
|
|
|
|
|
|
|
item, err := ctx.Estack.PopInt()
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
2019-03-29 16:43:16 +00:00
|
|
|
|
|
|
|
assert.Equal(t, int64(10), item.Value().Int64())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWithinOp(t *testing.T) {
|
|
|
|
|
|
|
|
v := VM{}
|
|
|
|
|
|
|
|
a, err := stack.NewInt(big.NewInt(5))
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
2019-03-29 16:43:16 +00:00
|
|
|
|
|
|
|
b, err := stack.NewInt(big.NewInt(2))
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
2019-03-29 16:43:16 +00:00
|
|
|
|
|
|
|
c, err := stack.NewInt(big.NewInt(10))
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
2019-03-29 16:43:16 +00:00
|
|
|
|
|
|
|
ctx := stack.NewContext([]byte{})
|
|
|
|
ctx.Estack.Push(a).Push(b).Push(c)
|
|
|
|
|
|
|
|
// c is the first item popped.
|
|
|
|
// b is the second item popped.
|
|
|
|
// a is the third item popped.
|
|
|
|
// if a is within [b, c) we place a boolean,
|
|
|
|
// whose value is true, on top of the evaluation
|
|
|
|
// stack. Otherwise we place a boolean with
|
|
|
|
// false value.
|
2019-08-12 13:25:16 +00:00
|
|
|
_, err = v.executeOp(stack.WITHIN, ctx)
|
|
|
|
assert.Nil(t, err)
|
2019-03-29 16:43:16 +00:00
|
|
|
|
|
|
|
// Stack should have one item
|
|
|
|
assert.Equal(t, 1, ctx.Estack.Len())
|
|
|
|
|
|
|
|
item, err := ctx.Estack.PopBoolean()
|
2019-04-05 19:13:23 +00:00
|
|
|
assert.Nil(t, err)
|
2019-03-29 16:43:16 +00:00
|
|
|
|
|
|
|
assert.Equal(t, true, item.Value())
|
|
|
|
}
|