github.com/xpaymentsorg/go-xpayments@v1.9.7/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  	for _, test := range tests {
    59  		ch := Lex([]byte(test.input), false)
    60  		c := NewCompiler(false)
    61  		c.Feed(ch)
    62  		output, err := c.Compile()
    63  		if len(err) != 0 {
    64  			t.Errorf("compile error: %v\ninput: %s", err, test.input)
    65  			continue
    66  		}
    67  		if output != test.output {
    68  			t.Errorf("incorrect output\ninput: %sgot:  %s\nwant: %s\n", test.input, output, test.output)
    69  		}
    70  	}
    71  }