2018-03-04 13:56:49 +00:00
|
|
|
package vm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestEmitInt(t *testing.T) {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
EmitInt(buf, 10)
|
2019-08-14 12:40:31 +00:00
|
|
|
assert.Equal(t, Instruction(buf.Bytes()[0]), PUSH10)
|
2018-03-04 13:56:49 +00:00
|
|
|
buf.Reset()
|
|
|
|
EmitInt(buf, 100)
|
|
|
|
assert.Equal(t, buf.Bytes()[0], uint8(1))
|
|
|
|
assert.Equal(t, buf.Bytes()[1], uint8(100))
|
|
|
|
buf.Reset()
|
|
|
|
EmitInt(buf, 1000)
|
|
|
|
assert.Equal(t, buf.Bytes()[0], uint8(2))
|
|
|
|
assert.Equal(t, buf.Bytes()[1:3], []byte{0xe8, 0x03})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEmitBool(t *testing.T) {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
EmitBool(buf, true)
|
|
|
|
EmitBool(buf, false)
|
2019-08-14 12:40:31 +00:00
|
|
|
assert.Equal(t, Instruction(buf.Bytes()[0]), PUSH1)
|
|
|
|
assert.Equal(t, Instruction(buf.Bytes()[1]), PUSH0)
|
2018-03-04 13:56:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEmitString(t *testing.T) {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
str := "City Of Zion"
|
|
|
|
EmitString(buf, str)
|
|
|
|
assert.Equal(t, buf.Len(), len(str)+1)
|
|
|
|
assert.Equal(t, buf.Bytes()[1:], []byte(str))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEmitSyscall(t *testing.T) {
|
|
|
|
syscalls := []string{
|
|
|
|
"Neo.Runtime.Log",
|
|
|
|
"Neo.Runtime.Notify",
|
|
|
|
"Neo.Runtime.Whatever",
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
for _, syscall := range syscalls {
|
|
|
|
EmitSyscall(buf, syscall)
|
2019-08-14 12:40:31 +00:00
|
|
|
assert.Equal(t, Instruction(buf.Bytes()[0]), SYSCALL)
|
2018-03-04 13:56:49 +00:00
|
|
|
assert.Equal(t, buf.Bytes()[1], uint8(len(syscall)))
|
|
|
|
assert.Equal(t, buf.Bytes()[2:], []byte(syscall))
|
|
|
|
buf.Reset()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEmitCall(t *testing.T) {
|
|
|
|
buf := new(bytes.Buffer)
|
2019-08-14 12:40:31 +00:00
|
|
|
EmitCall(buf, JMP, 100)
|
|
|
|
assert.Equal(t, Instruction(buf.Bytes()[0]), JMP)
|
2018-03-04 13:56:49 +00:00
|
|
|
label := binary.LittleEndian.Uint16(buf.Bytes()[1:3])
|
|
|
|
assert.Equal(t, label, uint16(100))
|
|
|
|
}
|