github.1485827954.workers.dev/ethereum/go-ethereum@v1.14.3/core/asm/compiler_test.go (about)

     1  // Copyright 2019 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package asm
    18  
    19  import (
    20  	"testing"
    21  )
    22  
    23  func TestCompiler(t *testing.T) {
    24  	tests := []struct {
    25  		input, output string
    26  	}{
    27  		{
    28  			input: `
    29  	GAS
    30  	label:
    31  	PUSH @label
    32  `,
    33  			output: "5a5b6300000001",
    34  		},
    35  		{
    36  			input: `
    37  	PUSH @label
    38  	label:
    39  `,
    40  			output: "63000000055b",
    41  		},
    42  		{
    43  			input: `
    44  	PUSH @label
    45  	JUMP
    46  	label:
    47  `,
    48  			output: "6300000006565b",
    49  		},
    50  		{
    51  			input: `
    52  	JUMP @label
    53  	label:
    54  `,
    55  			output: "6300000006565b",
    56  		},
    57  		{
    58  			input: `
    59  	JUMP @label
    60  label: ;; comment
    61  	ADD ;; comment
    62  `,
    63  			output: "6300000006565b01",
    64  		},
    65  	}
    66  	for _, test := range tests {
    67  		ch := Lex([]byte(test.input), false)
    68  		c := NewCompiler(false)
    69  		c.Feed(ch)
    70  		output, err := c.Compile()
    71  		if len(err) != 0 {
    72  			t.Errorf("compile error: %v\ninput: %s", err, test.input)
    73  			continue
    74  		}
    75  		if output != test.output {
    76  			t.Errorf("incorrect output\ninput: %sgot:  %s\nwant: %s\n", test.input, output, test.output)
    77  		}
    78  	}
    79  }