func TestNumericOps(t *testing.T) {
type testStruct struct {
+ name string
op Op
startVM *virtualMachine
wantErr error
wantVM *virtualMachine
}
- cases := []testStruct{{
- op: OP_1ADD,
- startVM: &virtualMachine{
- runLimit: 50000,
- dataStack: [][]byte{{0x02}},
- },
- wantVM: &virtualMachine{
- runLimit: 49998,
- dataStack: [][]byte{{0x03}},
- },
- }, {
- op: OP_1SUB,
- startVM: &virtualMachine{
- runLimit: 50000,
- dataStack: [][]byte{{2}},
- },
- wantVM: &virtualMachine{
- runLimit: 49998,
- dataStack: [][]byte{{1}},
- },
- }, {
- op: OP_1SUB,
- startVM: &virtualMachine{
- runLimit: 50000,
- dataStack: [][]byte{{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}},
- },
- wantVM: &virtualMachine{
- runLimit: 49998,
- deferredCost: -1,
- dataStack: [][]byte{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}},
- },
- }, {
- op: OP_2MUL,
- startVM: &virtualMachine{
- runLimit: 50000,
- dataStack: [][]byte{{2}},
- },
- wantVM: &virtualMachine{
- runLimit: 49998,
- dataStack: [][]byte{{4}},
- },
- }, {
- op: OP_2MUL,
- startVM: &virtualMachine{
- runLimit: 50000,
- dataStack: [][]byte{{0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}},
- },
- wantVM: &virtualMachine{
- runLimit: 49998,
- dataStack: [][]byte{{0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe}},
- },
- }, {
- op: OP_2MUL,
- startVM: &virtualMachine{
- runLimit: 50000,
- dataStack: [][]byte{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}},
- },
- wantVM: &virtualMachine{
- runLimit: 49998,
- deferredCost: 1,
- dataStack: [][]byte{{0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe}},
- },
- }, {
- op: OP_2DIV,
- startVM: &virtualMachine{
- runLimit: 50000,
- dataStack: [][]byte{{2}},
- },
- wantVM: &virtualMachine{
- runLimit: 49998,
- dataStack: [][]byte{{1}},
- },
- }, {
- op: OP_NOT,
- startVM: &virtualMachine{
- runLimit: 50000,
- dataStack: [][]byte{{2}},
- },
- wantVM: &virtualMachine{
- runLimit: 49998,
- deferredCost: -1,
- dataStack: [][]byte{{}},
- },
- }, {
- op: OP_0NOTEQUAL,
- startVM: &virtualMachine{
- runLimit: 50000,
- dataStack: [][]byte{{2}},
- },
- wantVM: &virtualMachine{
- runLimit: 49998,
- dataStack: [][]byte{{1}},
- },
- }, {
- op: OP_ADD,
- startVM: &virtualMachine{
- runLimit: 50000,
- dataStack: [][]byte{{2}, {1}},
- },
- wantVM: &virtualMachine{
- runLimit: 49998,
- deferredCost: -9,
- dataStack: [][]byte{{3}},
- },
- }, {
- op: OP_SUB,
- startVM: &virtualMachine{
- runLimit: 50000,
- dataStack: [][]byte{{2}, {1}},
- },
- wantVM: &virtualMachine{
- runLimit: 49998,
- deferredCost: -9,
- dataStack: [][]byte{{1}},
- },
- }, {
- op: OP_MUL,
- startVM: &virtualMachine{
- runLimit: 50000,
- dataStack: [][]byte{{2}, {1}},
- },
- wantVM: &virtualMachine{
- runLimit: 49992,
- deferredCost: -9,
- dataStack: [][]byte{{2}},
- },
- }, {
- op: OP_DIV,
- startVM: &virtualMachine{
- runLimit: 50000,
- dataStack: [][]byte{{2}, {1}},
- },
- wantVM: &virtualMachine{
- runLimit: 49992,
- deferredCost: -9,
- dataStack: [][]byte{{2}},
- },
- }, {
- op: OP_DIV,
- startVM: &virtualMachine{
- runLimit: 50000,
- dataStack: [][]byte{{2}, {}},
- },
- wantErr: ErrDivZero,
- }, {
- op: OP_MOD,
- startVM: &virtualMachine{
- runLimit: 50000,
- dataStack: [][]byte{{2}, {1}},
- },
- wantVM: &virtualMachine{
- runLimit: 49992,
- deferredCost: -10,
- dataStack: [][]byte{{}},
- },
- }, {
- op: OP_MOD,
- startVM: &virtualMachine{
- runLimit: 50000,
- dataStack: [][]byte{{2}, {0}},
- },
- wantErr: ErrDivZero,
- }, {
- op: OP_LSHIFT,
- startVM: &virtualMachine{
- runLimit: 50000,
- dataStack: [][]byte{{2}, {1}},
- },
- wantVM: &virtualMachine{
- runLimit: 49992,
- deferredCost: -9,
- dataStack: [][]byte{{4}},
- },
- }, {
- op: OP_RSHIFT,
- startVM: &virtualMachine{
- runLimit: 50000,
- dataStack: [][]byte{{2}, {1}},
- },
- wantVM: &virtualMachine{
- runLimit: 49992,
- deferredCost: -9,
- dataStack: [][]byte{{1}},
- },
- }, {
- op: OP_BOOLAND,
- startVM: &virtualMachine{
- runLimit: 50000,
- dataStack: [][]byte{{2}, {1}},
- },
- wantVM: &virtualMachine{
- runLimit: 49998,
- deferredCost: -9,
- dataStack: [][]byte{{1}},
- },
- }, {
- op: OP_BOOLOR,
- startVM: &virtualMachine{
- runLimit: 50000,
- dataStack: [][]byte{{2}, {1}},
- },
- wantVM: &virtualMachine{
- runLimit: 49998,
- deferredCost: -9,
- dataStack: [][]byte{{1}},
- },
- }, {
- op: OP_NUMEQUAL,
- startVM: &virtualMachine{
- runLimit: 50000,
- dataStack: [][]byte{{2}, {1}},
- },
- wantVM: &virtualMachine{
- runLimit: 49998,
- deferredCost: -10,
- dataStack: [][]byte{{}},
- },
- }, {
- op: OP_NUMEQUALVERIFY,
- startVM: &virtualMachine{
- runLimit: 50000,
- dataStack: [][]byte{{2}, {2}},
- },
- wantVM: &virtualMachine{
- runLimit: 49998,
- deferredCost: -18,
- dataStack: [][]byte{},
- },
- }, {
- op: OP_NUMEQUALVERIFY,
- startVM: &virtualMachine{
- runLimit: 50000,
- dataStack: [][]byte{{1}, {2}},
- },
- wantErr: ErrVerifyFailed,
- }, {
- op: OP_NUMNOTEQUAL,
- startVM: &virtualMachine{
- runLimit: 50000,
- dataStack: [][]byte{{2}, {1}},
- },
- wantVM: &virtualMachine{
- runLimit: 49998,
- deferredCost: -9,
- dataStack: [][]byte{{1}},
- },
- }, {
- op: OP_LESSTHAN,
- startVM: &virtualMachine{
- runLimit: 50000,
- dataStack: [][]byte{{2}, {1}},
- },
- wantVM: &virtualMachine{
- runLimit: 49998,
- deferredCost: -10,
- dataStack: [][]byte{{}},
- },
- }, {
- op: OP_LESSTHANOREQUAL,
- startVM: &virtualMachine{
- runLimit: 50000,
- dataStack: [][]byte{{2}, {1}},
- },
- wantVM: &virtualMachine{
- runLimit: 49998,
- deferredCost: -10,
- dataStack: [][]byte{{}},
- },
- }, {
- op: OP_GREATERTHAN,
- startVM: &virtualMachine{
- runLimit: 50000,
- dataStack: [][]byte{{2}, {1}},
- },
- wantVM: &virtualMachine{
- runLimit: 49998,
- deferredCost: -9,
- dataStack: [][]byte{{1}},
- },
- }, {
- op: OP_GREATERTHANOREQUAL,
- startVM: &virtualMachine{
- runLimit: 50000,
- dataStack: [][]byte{{2}, {1}},
- },
- wantVM: &virtualMachine{
- runLimit: 49998,
- deferredCost: -9,
- dataStack: [][]byte{{1}},
- },
- }, {
- op: OP_MIN,
- startVM: &virtualMachine{
- runLimit: 50000,
- dataStack: [][]byte{{2}, {1}},
- },
- wantVM: &virtualMachine{
- runLimit: 49998,
- deferredCost: -9,
- dataStack: [][]byte{{1}},
- },
- }, {
- op: OP_MIN,
- startVM: &virtualMachine{
- runLimit: 50000,
- dataStack: [][]byte{{1}, {2}},
- },
- wantVM: &virtualMachine{
- runLimit: 49998,
- deferredCost: -9,
- dataStack: [][]byte{{1}},
- },
- }, {
- op: OP_MAX,
- startVM: &virtualMachine{
- runLimit: 50000,
- dataStack: [][]byte{{2}, {1}},
- },
- wantVM: &virtualMachine{
- runLimit: 49998,
- deferredCost: -9,
- dataStack: [][]byte{{2}},
- },
- }, {
- op: OP_MAX,
- startVM: &virtualMachine{
- runLimit: 50000,
- dataStack: [][]byte{{1}, {2}},
- },
- wantVM: &virtualMachine{
- runLimit: 49998,
- deferredCost: -9,
- dataStack: [][]byte{{2}},
- },
- }, {
- op: OP_WITHIN,
- startVM: &virtualMachine{
- runLimit: 50000,
- dataStack: [][]byte{{1}, {1}, {2}},
- },
- wantVM: &virtualMachine{
- runLimit: 49996,
- deferredCost: -18,
- dataStack: [][]byte{{1}},
- },
- }}
+ tests := []struct {
+ name string
+ op Op
+ startVM *virtualMachine
+ wantErr error
+ wantVM *virtualMachine
+ }{
+ {
+ name: "test OP_1ADD",
+ op: OP_1ADD,
+ startVM: &virtualMachine{
+ runLimit: 50000,
+ dataStack: [][]byte{{0x02}},
+ },
+ wantVM: &virtualMachine{
+ runLimit: 49998,
+ dataStack: [][]byte{{0x03}},
+ },
+ },
+ {
+ name: "test OP_1SUB 2-1",
+ op: OP_1SUB,
+ startVM: &virtualMachine{
+ runLimit: 50000,
+ dataStack: [][]byte{{2}},
+ },
+ wantVM: &virtualMachine{
+ runLimit: 49998,
+ dataStack: [][]byte{{1}},
+ },
+ },
+ {
+ name: "test OP_1SUB use uint256's second array elem",
+ op: OP_1SUB,
+ startVM: &virtualMachine{
+ runLimit: 50000,
+ dataStack: [][]byte{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}},
+ },
+ wantVM: &virtualMachine{
+ runLimit: 49998,
+ deferredCost: -1,
+ dataStack: [][]byte{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}},
+ },
+ },
+ {
+ name: "test OP_2MUL 2*2",
+ op: OP_2MUL,
+ startVM: &virtualMachine{
+ runLimit: 50000,
+ dataStack: [][]byte{{2}},
+ },
+ wantVM: &virtualMachine{
+ runLimit: 49998,
+ dataStack: [][]byte{{4}},
+ },
+ },
+ {
+ name: "test OP_2MUL use uint256's full array elem",
+ op: OP_2MUL,
+ startVM: &virtualMachine{
+ runLimit: 50000,
+ dataStack: [][]byte{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f}},
+ },
+ wantVM: &virtualMachine{
+ runLimit: 49998,
+ dataStack: [][]byte{{0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}},
+ },
+ },
+ {
+ name: "test OP_2MUL use uint256's second array elem",
+ op: OP_2MUL,
+ startVM: &virtualMachine{
+ runLimit: 50000,
+ dataStack: [][]byte{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}},
+ },
+ wantVM: &virtualMachine{
+ runLimit: 49998,
+ deferredCost: 1,
+ dataStack: [][]byte{{0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01}},
+ },
+ },
+ {
+ name: "test OP_2DIV 2/2",
+ op: OP_2DIV,
+ startVM: &virtualMachine{
+ runLimit: 50000,
+ dataStack: [][]byte{{2}},
+ },
+ wantVM: &virtualMachine{
+ runLimit: 49998,
+ dataStack: [][]byte{{1}},
+ },
+ },
+ {
+ name: "test OP_NOT",
+ op: OP_NOT,
+ startVM: &virtualMachine{
+ runLimit: 50000,
+ dataStack: [][]byte{{2}},
+ },
+ wantVM: &virtualMachine{
+ runLimit: 49998,
+ deferredCost: -1,
+ dataStack: [][]byte{{}},
+ },
+ },
+ {
+ name: "test OP_0NOTEQUAL",
+ op: OP_0NOTEQUAL,
+ startVM: &virtualMachine{
+ runLimit: 50000,
+ dataStack: [][]byte{{2}},
+ },
+ wantVM: &virtualMachine{
+ runLimit: 49998,
+ dataStack: [][]byte{{1}},
+ },
+ },
+ {
+ name: "test OP_ADD 2+1",
+ op: OP_ADD,
+ startVM: &virtualMachine{
+ runLimit: 50000,
+ dataStack: [][]byte{{2}, {1}},
+ },
+ wantVM: &virtualMachine{
+ runLimit: 49998,
+ deferredCost: -9,
+ dataStack: [][]byte{{3}},
+ },
+ },
+ {
+ name: "test OP_SUB 2-1",
+ op: OP_SUB,
+ startVM: &virtualMachine{
+ runLimit: 50000,
+ dataStack: [][]byte{{2}, {1}},
+ },
+ wantVM: &virtualMachine{
+ runLimit: 49998,
+ deferredCost: -9,
+ dataStack: [][]byte{{1}},
+ },
+ },
+ {
+ name: "test OP_MUL 2*1",
+ op: OP_MUL,
+ startVM: &virtualMachine{
+ runLimit: 50000,
+ dataStack: [][]byte{{2}, {1}},
+ },
+ wantVM: &virtualMachine{
+ runLimit: 49992,
+ deferredCost: -9,
+ dataStack: [][]byte{{2}},
+ },
+ },
+ {
+ name: "test OP_DIV 2/1",
+ op: OP_DIV,
+ startVM: &virtualMachine{
+ runLimit: 50000,
+ dataStack: [][]byte{{2}, {1}},
+ },
+ wantVM: &virtualMachine{
+ runLimit: 49992,
+ deferredCost: -9,
+ dataStack: [][]byte{{2}},
+ },
+ },
+ {
+ name: "test OP_DIV 2/0",
+ op: OP_DIV,
+ startVM: &virtualMachine{
+ runLimit: 50000,
+ dataStack: [][]byte{{2}, {}},
+ },
+ wantErr: ErrDivZero,
+ },
+ {
+ name: "test OP_MOD 2%1",
+ op: OP_MOD,
+ startVM: &virtualMachine{
+ runLimit: 50000,
+ dataStack: [][]byte{{2}, {1}},
+ },
+ wantVM: &virtualMachine{
+ runLimit: 49992,
+ deferredCost: -10,
+ dataStack: [][]byte{{}},
+ },
+ },
+ {
+ name: "test OP_MOD 2%0",
+ op: OP_MOD,
+ startVM: &virtualMachine{
+ runLimit: 50000,
+ dataStack: [][]byte{{2}, {0}},
+ },
+ wantErr: ErrDivZero,
+ },
+ {
+ name: "test OP_LSHIFT",
+ op: OP_LSHIFT,
+ startVM: &virtualMachine{
+ runLimit: 50000,
+ dataStack: [][]byte{{2}, {1}},
+ },
+ wantVM: &virtualMachine{
+ runLimit: 49992,
+ deferredCost: -9,
+ dataStack: [][]byte{{4}},
+ },
+ },
+ {
+ name: "test OP_RSHIFT",
+ op: OP_RSHIFT,
+ startVM: &virtualMachine{
+ runLimit: 50000,
+ dataStack: [][]byte{{2}, {1}},
+ },
+ wantVM: &virtualMachine{
+ runLimit: 49992,
+ deferredCost: -9,
+ dataStack: [][]byte{{1}},
+ },
+ },
+ {
+ name: "test OP_BOOLAND",
+ op: OP_BOOLAND,
+ startVM: &virtualMachine{
+ runLimit: 50000,
+ dataStack: [][]byte{{2}, {1}},
+ },
+ wantVM: &virtualMachine{
+ runLimit: 49998,
+ deferredCost: -9,
+ dataStack: [][]byte{{1}},
+ },
+ },
+ {
+ name: "test OP_BOOLOR",
+ op: OP_BOOLOR,
+ startVM: &virtualMachine{
+ runLimit: 50000,
+ dataStack: [][]byte{{2}, {1}},
+ },
+ wantVM: &virtualMachine{
+ runLimit: 49998,
+ deferredCost: -9,
+ dataStack: [][]byte{{1}},
+ },
+ },
+ {
+ name: "test OP_NUMEQUAL",
+ op: OP_NUMEQUAL,
+ startVM: &virtualMachine{
+ runLimit: 50000,
+ dataStack: [][]byte{{2}, {1}},
+ },
+ wantVM: &virtualMachine{
+ runLimit: 49998,
+ deferredCost: -10,
+ dataStack: [][]byte{{}},
+ },
+ },
+ {
+ name: "test OP_NUMEQUALVERIFY",
+ op: OP_NUMEQUALVERIFY,
+ startVM: &virtualMachine{
+ runLimit: 50000,
+ dataStack: [][]byte{{2}, {2}},
+ },
+ wantVM: &virtualMachine{
+ runLimit: 49998,
+ deferredCost: -18,
+ dataStack: [][]byte{},
+ },
+ },
+ {
+ name: "test OP_NUMEQUALVERIFY",
+ op: OP_NUMEQUALVERIFY,
+ startVM: &virtualMachine{
+ runLimit: 50000,
+ dataStack: [][]byte{{1}, {2}},
+ },
+ wantErr: ErrVerifyFailed,
+ },
+ {
+ name: "test OP_NUMNOTEQUAL",
+ op: OP_NUMNOTEQUAL,
+ startVM: &virtualMachine{
+ runLimit: 50000,
+ dataStack: [][]byte{{2}, {1}},
+ },
+ wantVM: &virtualMachine{
+ runLimit: 49998,
+ deferredCost: -9,
+ dataStack: [][]byte{{1}},
+ },
+ },
+ {
+ name: "test OP_LESSTHAN",
+ op: OP_LESSTHAN,
+ startVM: &virtualMachine{
+ runLimit: 50000,
+ dataStack: [][]byte{{2}, {1}},
+ },
+ wantVM: &virtualMachine{
+ runLimit: 49998,
+ deferredCost: -10,
+ dataStack: [][]byte{{}},
+ },
+ },
+ {
+ name: "test OP_LESSTHANOREQUAL",
+ op: OP_LESSTHANOREQUAL,
+ startVM: &virtualMachine{
+ runLimit: 50000,
+ dataStack: [][]byte{{2}, {1}},
+ },
+ wantVM: &virtualMachine{
+ runLimit: 49998,
+ deferredCost: -10,
+ dataStack: [][]byte{{}},
+ },
+ },
+ {
+ name: "test OP_GREATERTHAN",
+ op: OP_GREATERTHAN,
+ startVM: &virtualMachine{
+ runLimit: 50000,
+ dataStack: [][]byte{{2}, {1}},
+ },
+ wantVM: &virtualMachine{
+ runLimit: 49998,
+ deferredCost: -9,
+ dataStack: [][]byte{{1}},
+ },
+ },
+ {
+ name: "test OP_GREATERTHANOREQUAL",
+ op: OP_GREATERTHANOREQUAL,
+ startVM: &virtualMachine{
+ runLimit: 50000,
+ dataStack: [][]byte{{2}, {1}},
+ },
+ wantVM: &virtualMachine{
+ runLimit: 49998,
+ deferredCost: -9,
+ dataStack: [][]byte{{1}},
+ },
+ },
+ {
+ name: "test OP_MIN min(2,1)",
+ op: OP_MIN,
+ startVM: &virtualMachine{
+ runLimit: 50000,
+ dataStack: [][]byte{{2}, {1}},
+ },
+ wantVM: &virtualMachine{
+ runLimit: 49998,
+ deferredCost: -9,
+ dataStack: [][]byte{{1}},
+ },
+ },
+ {
+ name: "test OP_MIN min(1,2)",
+ op: OP_MIN,
+ startVM: &virtualMachine{
+ runLimit: 50000,
+ dataStack: [][]byte{{1}, {2}},
+ },
+ wantVM: &virtualMachine{
+ runLimit: 49998,
+ deferredCost: -9,
+ dataStack: [][]byte{{1}},
+ },
+ },
+ {
+ name: "test OP_MAX max(1,2)",
+ op: OP_MAX,
+ startVM: &virtualMachine{
+ runLimit: 50000,
+ dataStack: [][]byte{{2}, {1}},
+ },
+ wantVM: &virtualMachine{
+ runLimit: 49998,
+ deferredCost: -9,
+ dataStack: [][]byte{{2}},
+ },
+ },
+ {
+ name: "test OP_MAX max(1,2)",
+ op: OP_MAX,
+ startVM: &virtualMachine{
+ runLimit: 50000,
+ dataStack: [][]byte{{1}, {2}},
+ },
+ wantVM: &virtualMachine{
+ runLimit: 49998,
+ deferredCost: -9,
+ dataStack: [][]byte{{2}},
+ },
+ },
+ {
+ name: "test OP_WITHIN",
+ op: OP_WITHIN,
+ startVM: &virtualMachine{
+ runLimit: 50000,
+ dataStack: [][]byte{{1}, {1}, {2}},
+ },
+ wantVM: &virtualMachine{
+ runLimit: 49996,
+ deferredCost: -18,
+ dataStack: [][]byte{{1}},
+ },
+ }}
numops := []Op{
OP_1ADD, OP_1SUB, OP_2MUL, OP_2DIV, OP_NOT, OP_0NOTEQUAL,
}
for _, op := range numops {
- cases = append(cases, testStruct{
+ tests = append(tests, testStruct{
op: op,
startVM: &virtualMachine{
runLimit: 0,
})
}
- for i, c := range cases {
- err := ops[c.op].fn(c.startVM)
-
- if err != c.wantErr {
- t.Errorf("case %d, op %s: got err = %v want %v", i, ops[c.op].name, err, c.wantErr)
- continue
- }
- if c.wantErr != nil {
- continue
- }
+ for i, c := range tests {
+ t.Run(c.name, func(t *testing.T) {
+ err := ops[c.op].fn(c.startVM)
+ if err != c.wantErr {
+ t.Errorf("case %d, op %s: got err = %v want %v", i, ops[c.op].name, err, c.wantErr)
+ return
+ }
+ if c.wantErr != nil {
+ return
+ }
- if !testutil.DeepEqual(c.startVM, c.wantVM) {
- t.Errorf("case %d, op %s: unexpected vm result\n\tgot: %+v\n\twant: %+v\n", i, ops[c.op].name, c.startVM, c.wantVM)
- }
+ if !testutil.DeepEqual(c.startVM, c.wantVM) {
+ t.Errorf("case %d, op %s: unexpected vm result\n\tgot: %+v\n\twant: %+v\n", i, ops[c.op].name, c.startVM, c.wantVM)
+ }
+ })
}
}
args: args{
vm: &virtualMachine{
runLimit: 50000,
- dataStack: [][]byte{{0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}},
+ dataStack: [][]byte{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f}},
},
},
wantErr: false,
args: args{
vm: &virtualMachine{
runLimit: 50000,
- dataStack: [][]byte{{0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}},
+ dataStack: [][]byte{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40}},
},
},
wantErr: true,
args: args{
vm: &virtualMachine{
runLimit: 50000,
- dataStack: [][]byte{{0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, {0x02}},
+ dataStack: [][]byte{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f}, {0x02}},
},
},
- want: [][]byte{{0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe}},
+ want: [][]byte{{0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}},
wantErr: false,
},
{
args: args{
vm: &virtualMachine{
runLimit: 50000,
- dataStack: [][]byte{{0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, {0x02}},
+ dataStack: [][]byte{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40}, {0x02}},
},
},
wantErr: true,
args: args{
vm: &virtualMachine{
runLimit: 50000,
- dataStack: [][]byte{{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}},
+ dataStack: [][]byte{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}},
},
},
want: [][]byte{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}},
args: args{
vm: &virtualMachine{
runLimit: 50000,
- dataStack: [][]byte{{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, {0x01}},
+ dataStack: [][]byte{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}, {0x01}},
},
},
want: [][]byte{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}},
args: args{
vm: &virtualMachine{
runLimit: 50000,
- dataStack: [][]byte{{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}},
+ dataStack: [][]byte{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}},
},
},
- want: [][]byte{{0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}},
+ want: [][]byte{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80}},
wantErr: false,
},
{
args: args{
vm: &virtualMachine{
runLimit: 50000,
- dataStack: [][]byte{{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, {0x02}},
+ dataStack: [][]byte{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}, {0x02}},
},
},
- want: [][]byte{{0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}},
+ want: [][]byte{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80}},
wantErr: false,
},
{
dataStack: [][]byte{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, {0x01}},
},
},
- want: [][]byte{{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}},
+ want: [][]byte{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}},
wantErr: false,
},
{
},
f: opRshift,
},
- want: [][]byte{{0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}},
+ want: [][]byte{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01}},
wantErr: false,
},
{
{num: new(big.Int).SetInt64(0), input: []byte{}},
{num: new(big.Int).SetInt64(1), input: []byte{0x01}},
{num: new(big.Int).SetInt64(255), input: []byte{0xff}},
- {num: new(big.Int).SetInt64(256), input: []byte{0x01, 0x00}},
- {num: new(big.Int).SetInt64(46657), input: []byte{0xb6, 0x41}},
- {num: new(big.Int).SetInt64(1 << 32), input: []byte{0x01, 0x00, 0x00, 0x00, 0x00}},
+ {num: new(big.Int).SetInt64(256), input: []byte{0x00, 0x01}},
+ {num: new(big.Int).SetInt64(46657), input: []byte{0x41, 0xb6}},
+ {num: new(big.Int).SetInt64(1 << 32), input: []byte{0x00, 0x00, 0x00, 0x00, 0x01}},
{
num: new(big.Int).Exp(new(big.Int).SetInt64(10), new(big.Int).SetInt64(32), nil),
- input: []byte{0x04, 0xee, 0x2d, 0x6d, 0x41, 0x5b, 0x85, 0xac, 0xef, 0x81, 0x00, 0x00, 0x00, 0x00},
+ input: []byte{0x00, 0x00, 0x00, 0x00, 0x81, 0xef, 0xac, 0x85, 0x5b, 0x41, 0x6d, 0x2d, 0xee, 0x04},
},
{num: new(big.Int).SetInt64(-1), input: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff}},
- {num: new(big.Int).SetInt64(-256), input: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ {num: new(big.Int).SetInt64(-256), input: []byte{0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0x00}},
+ 0xff, 0xff, 0xff, 0xff}},
}
for _, test := range tests {
fromBig, b := uint256.FromBig(test.num)
}{
{num: new(big.Int), input: []byte{}},
{num: new(big.Int), input: []byte{0x00}},
- {num: new(big.Int).SetInt64(0), input: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00}},
- {num: new(big.Int).SetInt64(1), input: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x01}},
- {num: new(big.Int).SetInt64(255), input: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0xff}},
- {num: new(big.Int).SetInt64(256), input: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x01, 0x00}},
- {num: new(big.Int).SetInt64(46657), input: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0xb6, 0x41}},
- {num: new(big.Int).SetInt64(1 << 32), input: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x01, 0x00, 0x00, 0x00, 0x00}},
+ {num: new(big.Int).SetInt64(0), input: []byte{0x00}},
+ {num: new(big.Int).SetInt64(1), input: []byte{0x01}},
+ {num: new(big.Int).SetInt64(255), input: []byte{0xff}},
+ {num: new(big.Int).SetInt64(256), input: []byte{0x00, 0x01}},
+ {num: new(big.Int).SetInt64(46657), input: []byte{0x41, 0xb6}},
+ {num: new(big.Int).SetInt64(1 << 32), input: []byte{0x00, 0x00, 0x00, 0x00, 0x01}},
{
num: new(big.Int).Exp(new(big.Int).SetInt64(10), new(big.Int).SetInt64(32), nil),
- input: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0xee, 0x2d, 0x6d, 0x41, 0x5b, 0x85, 0xac, 0xef, 0x81, 0x00, 0x00, 0x00, 0x00},
+ input: []byte{0x00, 0x00, 0x00, 0x00, 0x81, 0xef, 0xac, 0x85, 0x5b, 0x41, 0x6d, 0x2d, 0xee, 0x04},
},
{num: new(big.Int).SetInt64(-1), input: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, wantError: true},
- {num: new(big.Int).SetInt64(-256), input: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00}, wantError: true},
- {input: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00}, wantError: true},
+ {num: new(big.Int).SetInt64(-256), input: []byte{0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00}, wantError: true},
+ {input: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, wantError: true},
}
for _, test := range tests {
data, err := AsBigInt(test.input)