instructions_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package vm
  2. import (
  3. "math/big"
  4. "testing"
  5. "github.com/ethereum/go-ethereum/common"
  6. "github.com/ethereum/go-ethereum/params"
  7. )
  8. func TestByteOp(t *testing.T) {
  9. var (
  10. env = NewEVM(Context{}, nil, params.TestChainConfig, Config{EnableJit: false, ForceJit: false})
  11. stack = newstack()
  12. )
  13. tests := []struct {
  14. v string
  15. th uint64
  16. expected *big.Int
  17. }{
  18. {"ABCDEF0908070605040302010000000000000000000000000000000000000000", 0, big.NewInt(0xAB)},
  19. {"ABCDEF0908070605040302010000000000000000000000000000000000000000", 1, big.NewInt(0xCD)},
  20. {"00CDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff", 0, big.NewInt(0x00)},
  21. {"00CDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff", 1, big.NewInt(0xCD)},
  22. {"0000000000000000000000000000000000000000000000000000000000102030", 31, big.NewInt(0x30)},
  23. {"0000000000000000000000000000000000000000000000000000000000102030", 30, big.NewInt(0x20)},
  24. {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 32, big.NewInt(0x0)},
  25. {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 0xFFFFFFFFFFFFFFFF, big.NewInt(0x0)},
  26. }
  27. pc := uint64(0)
  28. for _, test := range tests {
  29. val := new(big.Int).SetBytes(common.Hex2Bytes(test.v))
  30. th := new(big.Int).SetUint64(test.th)
  31. stack.push(val)
  32. stack.push(th)
  33. opByte(&pc, env, nil, nil, stack)
  34. actual := stack.pop()
  35. if actual.Cmp(test.expected) != 0 {
  36. t.Fatalf("Expected [%v] %v:th byte to be %v, was %v.", test.v, test.th, test.expected, actual)
  37. }
  38. }
  39. }