aboutsummaryrefslogtreecommitdiffstats
path: root/core/vm/jit_util_test.go
blob: 2123efe59e4290cab6712e7ac90c053847675ba4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright 2015 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package vm

import "testing"

type matchTest struct {
    input   []OpCode
    match   []OpCode
    matches int
}

func TestMatchFn(t *testing.T) {
    tests := []matchTest{
        matchTest{
            []OpCode{PUSH1, PUSH1, MSTORE, JUMP},
            []OpCode{PUSH1, MSTORE},
            1,
        },
        matchTest{
            []OpCode{PUSH1, PUSH1, MSTORE, JUMP},
            []OpCode{PUSH1, MSTORE, PUSH1},
            0,
        },
        matchTest{
            []OpCode{},
            []OpCode{PUSH1},
            0,
        },
    }

    for i, test := range tests {
        var matchCount int
        MatchFn(test.input, test.match, func(i int) bool {
            matchCount++
            return true
        })
        if matchCount != test.matches {
            t.Errorf("match count failed on test[%d]: expected %d matches, got %d", i, test.matches, matchCount)
        }
    }
}

type parseTest struct {
    base   OpCode
    size   int
    output OpCode
}

func TestParser(t *testing.T) {
    tests := []parseTest{
        parseTest{PUSH1, 32, PUSH},
        parseTest{DUP1, 16, DUP},
        parseTest{SWAP1, 16, SWAP},
        parseTest{MSTORE, 1, MSTORE},
    }

    for _, test := range tests {
        for i := 0; i < test.size; i++ {
            code := append([]byte{byte(byte(test.base) + byte(i))}, make([]byte, i+1)...)
            output := Parse(code)
            if len(output) == 0 {
                t.Fatal("empty output")
            }
            if output[0] != test.output {
                t.Errorf("%v failed: expected %v but got %v", test.base+OpCode(i), test.output, output[0])
            }
        }
    }
}