Merge pull request #2314 from nspcc-dev/vm-pretty-print

vm: cut trailing spaces in `PrintOps`
This commit is contained in:
Roman Khimov 2021-12-29 06:20:08 +03:00 committed by GitHub
commit f26de523fd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 4 deletions

View file

@ -145,7 +145,7 @@ func (v *VM) PrintOps(out io.Writer) {
out = os.Stdout out = os.Stdout
} }
w := tabwriter.NewWriter(out, 0, 0, 4, ' ', 0) w := tabwriter.NewWriter(out, 0, 0, 4, ' ', 0)
fmt.Fprintln(w, "INDEX\tOPCODE\tPARAMETER\t") fmt.Fprintln(w, "INDEX\tOPCODE\tPARAMETER")
realctx := v.Context() realctx := v.Context()
ctx := realctx.Copy() ctx := realctx.Copy()
ctx.ip = 0 ctx.ip = 0
@ -154,10 +154,10 @@ func (v *VM) PrintOps(out io.Writer) {
cursor := "" cursor := ""
instr, parameter, err := ctx.Next() instr, parameter, err := ctx.Next()
if ctx.ip == realctx.ip { if ctx.ip == realctx.ip {
cursor = "<<" cursor = "\t<<"
} }
if err != nil { if err != nil {
fmt.Fprintf(w, "%d\t%s\tERROR: %s\t%s\n", ctx.ip, instr, err, cursor) fmt.Fprintf(w, "%d\t%s\tERROR: %s%s\n", ctx.ip, instr, err, cursor)
break break
} }
var desc = "" var desc = ""
@ -203,7 +203,7 @@ func (v *VM) PrintOps(out io.Writer) {
} }
} }
fmt.Fprintf(w, "%d\t%s\t%s\t%s\n", ctx.ip, instr, desc, cursor) fmt.Fprintf(w, "%d\t%s\t%s%s\n", ctx.ip, instr, desc, cursor)
if ctx.nextip >= len(ctx.prog) { if ctx.nextip >= len(ctx.prog) {
break break
} }

View file

@ -9,6 +9,7 @@ import (
"math" "math"
"math/big" "math/big"
"math/rand" "math/rand"
"strings"
"testing" "testing"
"github.com/nspcc-dev/neo-go/internal/random" "github.com/nspcc-dev/neo-go/internal/random"
@ -1181,6 +1182,25 @@ func TestPICKITEM(t *testing.T) {
}) })
} }
func TestVMPrintOps(t *testing.T) {
w := io.NewBufBinWriter()
emit.Bytes(w.BinWriter, make([]byte, 1000))
emit.Opcodes(w.BinWriter, opcode.PUSH0)
emit.Bytes(w.BinWriter, make([]byte, 8))
buf := bytes.NewBuffer(nil)
v := New()
v.Load(w.Bytes())
v.PrintOps(buf)
ss := strings.Split(buf.String(), "\n")
require.Equal(t, 5, len(ss)) // header + 3 opcodes + trailing newline
require.True(t, len(ss[0]) < 1000)
require.True(t, len(ss[1]) > 1000)
require.True(t, len(ss[2]) < 1000)
require.True(t, len(ss[3]) < 1000)
}
func TestPICKITEMDupArray(t *testing.T) { func TestPICKITEMDupArray(t *testing.T) {
prog := makeProgram(opcode.DUP, opcode.PUSH0, opcode.PICKITEM, opcode.ABS) prog := makeProgram(opcode.DUP, opcode.PUSH0, opcode.PICKITEM, opcode.ABS)
vm := load(prog) vm := load(prog)