@@ -22,53 +22,37 @@ import (
22
22
"encoding/hex"
23
23
)
24
24
25
- // Tests disassembling the instructions for valid evm code
26
- func TestInstructionIteratorValid (t * testing.T ) {
27
- cnt := 0
28
- script , _ := hex .DecodeString ("61000000" )
29
-
30
- it := NewInstructionIterator (script )
31
- for it .Next () {
32
- cnt ++
33
- }
34
-
35
- if err := it .Error (); err != nil {
36
- t .Errorf ("Expected 2, but encountered error %v instead." , err )
37
- }
38
- if cnt != 2 {
39
- t .Errorf ("Expected 2, but got %v instead." , cnt )
40
- }
41
- }
42
-
43
- // Tests disassembling the instructions for invalid evm code
44
- func TestInstructionIteratorInvalid (t * testing.T ) {
45
- cnt := 0
46
- script , _ := hex .DecodeString ("6100" )
47
-
48
- it := NewInstructionIterator (script )
49
- for it .Next () {
50
- cnt ++
51
- }
52
-
53
- if it .Error () == nil {
54
- t .Errorf ("Expected an error, but got %v instead." , cnt )
55
- }
56
- }
57
-
58
- // Tests disassembling the instructions for empty evm code
59
- func TestInstructionIteratorEmpty (t * testing.T ) {
60
- cnt := 0
61
- script , _ := hex .DecodeString ("" )
62
-
63
- it := NewInstructionIterator (script )
64
- for it .Next () {
65
- cnt ++
66
- }
67
-
68
- if err := it .Error (); err != nil {
69
- t .Errorf ("Expected 0, but encountered error %v instead." , err )
70
- }
71
- if cnt != 0 {
72
- t .Errorf ("Expected 0, but got %v instead." , cnt )
25
+ // Tests disassembling instructions
26
+ func TestInstructionIterator (t * testing.T ) {
27
+ for i , tc := range []struct {
28
+ want int
29
+ code string
30
+ wantErr string
31
+ }{
32
+ {2 , "61000000" , "" }, // valid code
33
+ {0 , "6100" , "incomplete push instruction at 0" }, // invalid code
34
+ {2 , "5900" , "" }, // push0
35
+ {0 , "" , "" }, // empty
36
+
37
+ } {
38
+ var (
39
+ have int
40
+ code , _ = hex .DecodeString (tc .code )
41
+ it = NewInstructionIterator (code )
42
+ )
43
+ for it .Next () {
44
+ have ++
45
+ }
46
+ var haveErr = ""
47
+ if it .Error () != nil {
48
+ haveErr = it .Error ().Error ()
49
+ }
50
+ if haveErr != tc .wantErr {
51
+ t .Errorf ("test %d: encountered error: %q want %q" , i , haveErr , tc .wantErr )
52
+ continue
53
+ }
54
+ if have != tc .want {
55
+ t .Errorf ("wrong instruction count, have %d want %d" , have , tc .want )
56
+ }
73
57
}
74
58
}
0 commit comments