jit_util_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright 2015 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. package vm
  17. import "testing"
  18. type matchTest struct {
  19. input []OpCode
  20. match []OpCode
  21. matches int
  22. }
  23. func TestMatchFn(t *testing.T) {
  24. tests := []matchTest{
  25. matchTest{
  26. []OpCode{PUSH1, PUSH1, MSTORE, JUMP},
  27. []OpCode{PUSH1, MSTORE},
  28. 1,
  29. },
  30. matchTest{
  31. []OpCode{PUSH1, PUSH1, MSTORE, JUMP},
  32. []OpCode{PUSH1, MSTORE, PUSH1},
  33. 0,
  34. },
  35. matchTest{
  36. []OpCode{},
  37. []OpCode{PUSH1},
  38. 0,
  39. },
  40. }
  41. for i, test := range tests {
  42. var matchCount int
  43. MatchFn(test.input, test.match, func(i int) bool {
  44. matchCount++
  45. return true
  46. })
  47. if matchCount != test.matches {
  48. t.Errorf("match count failed on test[%d]: expected %d matches, got %d", i, test.matches, matchCount)
  49. }
  50. }
  51. }
  52. type parseTest struct {
  53. base OpCode
  54. size int
  55. output OpCode
  56. }
  57. func TestParser(t *testing.T) {
  58. tests := []parseTest{
  59. parseTest{PUSH1, 32, PUSH},
  60. parseTest{DUP1, 16, DUP},
  61. parseTest{SWAP1, 16, SWAP},
  62. parseTest{MSTORE, 1, MSTORE},
  63. }
  64. for _, test := range tests {
  65. for i := 0; i < test.size; i++ {
  66. code := append([]byte{byte(byte(test.base) + byte(i))}, make([]byte, i+1)...)
  67. output := Parse(code)
  68. if len(output) == 0 {
  69. t.Fatal("empty output")
  70. }
  71. if output[0] != test.output {
  72. t.Error("%v failed: expected %v but got %v", test.base+OpCode(i), output[0])
  73. }
  74. }
  75. }
  76. }