github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/core/asm/asm_test.go (about)

     1  //  Copyright 2018 The go-ethereum Authors
     2  //  Copyright 2019 The go-aigar Authors
     3  //  This file is part of the go-aigar library.
     4  //
     5  //  The go-aigar library is free software: you can redistribute it and/or modify
     6  //  it under the terms of the GNU Lesser General Public License as published by
     7  //  the Free Software Foundation, either version 3 of the License, or
     8  //  (at your option) any later version.
     9  //
    10  //  The go-aigar library is distributed in the hope that it will be useful,
    11  //  but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  //  GNU Lesser General Public License for more details.
    14  //
    15  //  You should have received a copy of the GNU Lesser General Public License
    16  //  along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package asm
    19  
    20  import (
    21  	"testing"
    22  
    23  	"encoding/hex"
    24  )
    25  
    26  // Tests disassembling the instructions for valid evm code
    27  func TestInstructionIteratorValid(t *testing.T) {
    28  	cnt := 0
    29  	script, _ := hex.DecodeString("61000000")
    30  
    31  	it := NewInstructionIterator(script)
    32  	for it.Next() {
    33  		cnt++
    34  	}
    35  
    36  	if err := it.Error(); err != nil {
    37  		t.Errorf("Expected 2, but encountered error %v instead.", err)
    38  	}
    39  	if cnt != 2 {
    40  		t.Errorf("Expected 2, but got %v instead.", cnt)
    41  	}
    42  }
    43  
    44  // Tests disassembling the instructions for invalid evm code
    45  func TestInstructionIteratorInvalid(t *testing.T) {
    46  	cnt := 0
    47  	script, _ := hex.DecodeString("6100")
    48  
    49  	it := NewInstructionIterator(script)
    50  	for it.Next() {
    51  		cnt++
    52  	}
    53  
    54  	if it.Error() == nil {
    55  		t.Errorf("Expected an error, but got %v instead.", cnt)
    56  	}
    57  }
    58  
    59  // Tests disassembling the instructions for empty evm code
    60  func TestInstructionIteratorEmpty(t *testing.T) {
    61  	cnt := 0
    62  	script, _ := hex.DecodeString("")
    63  
    64  	it := NewInstructionIterator(script)
    65  	for it.Next() {
    66  		cnt++
    67  	}
    68  
    69  	if err := it.Error(); err != nil {
    70  		t.Errorf("Expected 0, but encountered error %v instead.", err)
    71  	}
    72  	if cnt != 0 {
    73  		t.Errorf("Expected 0, but got %v instead.", cnt)
    74  	}
    75  }