mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
core/asm: make test tabledriven
This commit is contained in:
parent
50c8e8c7bc
commit
e75773d6c8
1 changed files with 31 additions and 65 deletions
|
|
@ -22,71 +22,37 @@ import (
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Tests disassembling the instructions for valid evm code
|
// Tests disassembling instructions
|
||||||
func TestInstructionIteratorValid(t *testing.T) {
|
func TestInstructionIterator(t *testing.T) {
|
||||||
cnt := 0
|
for i, tc := range []struct {
|
||||||
script, _ := hex.DecodeString("61000000")
|
want int
|
||||||
|
code string
|
||||||
|
wantErr string
|
||||||
|
}{
|
||||||
|
{2, "61000000", ""}, // valid code
|
||||||
|
{0, "6100", "incomplete push instruction at 0"}, // invalid code
|
||||||
|
{2, "5900", ""}, // push0
|
||||||
|
{0, "", ""}, // empty
|
||||||
|
|
||||||
it := NewInstructionIterator(script)
|
} {
|
||||||
|
var (
|
||||||
|
have int
|
||||||
|
code, _ = hex.DecodeString(tc.code)
|
||||||
|
it = NewInstructionIterator(code)
|
||||||
|
)
|
||||||
for it.Next() {
|
for it.Next() {
|
||||||
cnt++
|
have++
|
||||||
}
|
}
|
||||||
|
var haveErr = ""
|
||||||
if err := it.Error(); err != nil {
|
if it.Error() != nil {
|
||||||
t.Errorf("Expected 2, but encountered error %v instead.", err)
|
haveErr = it.Error().Error()
|
||||||
}
|
}
|
||||||
if cnt != 2 {
|
if haveErr != tc.wantErr {
|
||||||
t.Errorf("Expected 2, but got %v instead.", cnt)
|
t.Errorf("test %d: encountered error: %q want %q", i, haveErr, tc.wantErr)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if have != tc.want {
|
||||||
|
t.Errorf("wrong instruction count, have %d want %d", have, tc.want)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tests disassembling the instructions for invalid evm code
|
|
||||||
func TestInstructionIteratorInvalid(t *testing.T) {
|
|
||||||
cnt := 0
|
|
||||||
script, _ := hex.DecodeString("6100")
|
|
||||||
|
|
||||||
it := NewInstructionIterator(script)
|
|
||||||
for it.Next() {
|
|
||||||
cnt++
|
|
||||||
}
|
|
||||||
|
|
||||||
if it.Error() == nil {
|
|
||||||
t.Errorf("Expected an error, but got %v instead.", cnt)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Tests disassembling the instructions for PUSH0
|
|
||||||
func TestInstructionIteratorPUSH0(t *testing.T) {
|
|
||||||
cnt := 0
|
|
||||||
script, _ := hex.DecodeString("5900")
|
|
||||||
|
|
||||||
it := NewInstructionIterator(script)
|
|
||||||
for it.Next() {
|
|
||||||
cnt++
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := it.Error(); err != nil {
|
|
||||||
t.Errorf("Expected 2, but encountered error %v instead.", err)
|
|
||||||
}
|
|
||||||
if cnt != 2 {
|
|
||||||
t.Errorf("Expected 2, but got %v instead.", cnt)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Tests disassembling the instructions for empty evm code
|
|
||||||
func TestInstructionIteratorEmpty(t *testing.T) {
|
|
||||||
cnt := 0
|
|
||||||
script, _ := hex.DecodeString("")
|
|
||||||
|
|
||||||
it := NewInstructionIterator(script)
|
|
||||||
for it.Next() {
|
|
||||||
cnt++
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := it.Error(); err != nil {
|
|
||||||
t.Errorf("Expected 0, but encountered error %v instead.", err)
|
|
||||||
}
|
|
||||||
if cnt != 0 {
|
|
||||||
t.Errorf("Expected 0, but got %v instead.", cnt)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue