github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/protocol/vm/numeric_test.go (about) 1 package vm 2 3 import ( 4 "fmt" 5 "math" 6 "math/big" 7 "testing" 8 9 "github.com/bytom/bytom/common" 10 "github.com/bytom/bytom/protocol/vm/mocks" 11 "github.com/bytom/bytom/testutil" 12 ) 13 14 func TestNumericOps(t *testing.T) { 15 type testStruct struct { 16 name string 17 op Op 18 startVM *virtualMachine 19 wantErr error 20 wantVM *virtualMachine 21 } 22 tests := []struct { 23 name string 24 op Op 25 startVM *virtualMachine 26 wantErr error 27 wantVM *virtualMachine 28 }{ 29 { 30 name: "test OP_1ADD", 31 op: OP_1ADD, 32 startVM: &virtualMachine{ 33 runLimit: 50000, 34 dataStack: [][]byte{{0x02}}, 35 }, 36 wantVM: &virtualMachine{ 37 runLimit: 49998, 38 dataStack: [][]byte{{0x03}}, 39 }, 40 }, 41 { 42 name: "test OP_1SUB 2-1", 43 op: OP_1SUB, 44 startVM: &virtualMachine{ 45 runLimit: 50000, 46 dataStack: [][]byte{{2}}, 47 }, 48 wantVM: &virtualMachine{ 49 runLimit: 49998, 50 dataStack: [][]byte{{1}}, 51 }, 52 }, 53 { 54 name: "test OP_1SUB use uint256's second array elem", 55 op: OP_1SUB, 56 startVM: &virtualMachine{ 57 runLimit: 50000, 58 dataStack: [][]byte{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}}, 59 }, 60 wantVM: &virtualMachine{ 61 runLimit: 49998, 62 deferredCost: -1, 63 dataStack: [][]byte{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}}, 64 }, 65 }, 66 { 67 name: "test OP_2MUL 2*2", 68 op: OP_2MUL, 69 startVM: &virtualMachine{ 70 runLimit: 50000, 71 dataStack: [][]byte{{2}}, 72 }, 73 wantVM: &virtualMachine{ 74 runLimit: 49998, 75 dataStack: [][]byte{{4}}, 76 }, 77 }, 78 { 79 name: "test OP_2MUL use uint256's full array elem", 80 op: OP_2MUL, 81 startVM: &virtualMachine{ 82 runLimit: 50000, 83 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}}, 84 }, 85 wantVM: &virtualMachine{ 86 runLimit: 49998, 87 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}}, 88 }, 89 }, 90 { 91 name: "test OP_2MUL use uint256's second array elem", 92 op: OP_2MUL, 93 startVM: &virtualMachine{ 94 runLimit: 50000, 95 dataStack: [][]byte{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}}, 96 }, 97 wantVM: &virtualMachine{ 98 runLimit: 49998, 99 deferredCost: 1, 100 dataStack: [][]byte{{0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01}}, 101 }, 102 }, 103 { 104 name: "test OP_2DIV 2/2", 105 op: OP_2DIV, 106 startVM: &virtualMachine{ 107 runLimit: 50000, 108 dataStack: [][]byte{{2}}, 109 }, 110 wantVM: &virtualMachine{ 111 runLimit: 49998, 112 dataStack: [][]byte{{1}}, 113 }, 114 }, 115 { 116 name: "test OP_NOT", 117 op: OP_NOT, 118 startVM: &virtualMachine{ 119 runLimit: 50000, 120 dataStack: [][]byte{{2}}, 121 }, 122 wantVM: &virtualMachine{ 123 runLimit: 49998, 124 deferredCost: -1, 125 dataStack: [][]byte{{}}, 126 }, 127 }, 128 { 129 name: "test OP_0NOTEQUAL", 130 op: OP_0NOTEQUAL, 131 startVM: &virtualMachine{ 132 runLimit: 50000, 133 dataStack: [][]byte{{2}}, 134 }, 135 wantVM: &virtualMachine{ 136 runLimit: 49998, 137 dataStack: [][]byte{{1}}, 138 }, 139 }, 140 { 141 name: "test OP_ADD 2+1", 142 op: OP_ADD, 143 startVM: &virtualMachine{ 144 runLimit: 50000, 145 dataStack: [][]byte{{2}, {1}}, 146 }, 147 wantVM: &virtualMachine{ 148 runLimit: 49998, 149 deferredCost: -9, 150 dataStack: [][]byte{{3}}, 151 }, 152 }, 153 { 154 name: "test OP_SUB 2-1", 155 op: OP_SUB, 156 startVM: &virtualMachine{ 157 runLimit: 50000, 158 dataStack: [][]byte{{2}, {1}}, 159 }, 160 wantVM: &virtualMachine{ 161 runLimit: 49998, 162 deferredCost: -9, 163 dataStack: [][]byte{{1}}, 164 }, 165 }, 166 { 167 name: "test OP_MUL 2*1", 168 op: OP_MUL, 169 startVM: &virtualMachine{ 170 runLimit: 50000, 171 dataStack: [][]byte{{2}, {1}}, 172 }, 173 wantVM: &virtualMachine{ 174 runLimit: 49992, 175 deferredCost: -9, 176 dataStack: [][]byte{{2}}, 177 }, 178 }, 179 { 180 name: "test OP_DIV 2/1", 181 op: OP_DIV, 182 startVM: &virtualMachine{ 183 runLimit: 50000, 184 dataStack: [][]byte{{2}, {1}}, 185 }, 186 wantVM: &virtualMachine{ 187 runLimit: 49992, 188 deferredCost: -9, 189 dataStack: [][]byte{{2}}, 190 }, 191 }, 192 { 193 name: "test OP_DIV 2/0", 194 op: OP_DIV, 195 startVM: &virtualMachine{ 196 runLimit: 50000, 197 dataStack: [][]byte{{2}, {}}, 198 }, 199 wantErr: ErrDivZero, 200 }, 201 { 202 name: "test OP_MOD 2%1", 203 op: OP_MOD, 204 startVM: &virtualMachine{ 205 runLimit: 50000, 206 dataStack: [][]byte{{2}, {1}}, 207 }, 208 wantVM: &virtualMachine{ 209 runLimit: 49992, 210 deferredCost: -10, 211 dataStack: [][]byte{{}}, 212 }, 213 }, 214 { 215 name: "test OP_MOD 2%0", 216 op: OP_MOD, 217 startVM: &virtualMachine{ 218 runLimit: 50000, 219 dataStack: [][]byte{{2}, {0}}, 220 }, 221 wantErr: ErrDivZero, 222 }, 223 { 224 name: "test OP_LSHIFT", 225 op: OP_LSHIFT, 226 startVM: &virtualMachine{ 227 runLimit: 50000, 228 dataStack: [][]byte{{2}, {1}}, 229 }, 230 wantVM: &virtualMachine{ 231 runLimit: 49992, 232 deferredCost: -9, 233 dataStack: [][]byte{{4}}, 234 }, 235 }, 236 { 237 name: "test OP_RSHIFT", 238 op: OP_RSHIFT, 239 startVM: &virtualMachine{ 240 runLimit: 50000, 241 dataStack: [][]byte{{2}, {1}}, 242 }, 243 wantVM: &virtualMachine{ 244 runLimit: 49992, 245 deferredCost: -9, 246 dataStack: [][]byte{{1}}, 247 }, 248 }, 249 { 250 name: "test OP_BOOLAND", 251 op: OP_BOOLAND, 252 startVM: &virtualMachine{ 253 runLimit: 50000, 254 dataStack: [][]byte{{2}, {1}}, 255 }, 256 wantVM: &virtualMachine{ 257 runLimit: 49998, 258 deferredCost: -9, 259 dataStack: [][]byte{{1}}, 260 }, 261 }, 262 { 263 name: "test OP_BOOLOR", 264 op: OP_BOOLOR, 265 startVM: &virtualMachine{ 266 runLimit: 50000, 267 dataStack: [][]byte{{2}, {1}}, 268 }, 269 wantVM: &virtualMachine{ 270 runLimit: 49998, 271 deferredCost: -9, 272 dataStack: [][]byte{{1}}, 273 }, 274 }, 275 { 276 name: "test OP_NUMEQUAL", 277 op: OP_NUMEQUAL, 278 startVM: &virtualMachine{ 279 runLimit: 50000, 280 dataStack: [][]byte{{2}, {1}}, 281 }, 282 wantVM: &virtualMachine{ 283 runLimit: 49998, 284 deferredCost: -10, 285 dataStack: [][]byte{{}}, 286 }, 287 }, 288 { 289 name: "test OP_NUMEQUALVERIFY", 290 op: OP_NUMEQUALVERIFY, 291 startVM: &virtualMachine{ 292 runLimit: 50000, 293 dataStack: [][]byte{{2}, {2}}, 294 }, 295 wantVM: &virtualMachine{ 296 runLimit: 49998, 297 deferredCost: -18, 298 dataStack: [][]byte{}, 299 }, 300 }, 301 { 302 name: "test OP_NUMEQUALVERIFY", 303 op: OP_NUMEQUALVERIFY, 304 startVM: &virtualMachine{ 305 runLimit: 50000, 306 dataStack: [][]byte{{1}, {2}}, 307 }, 308 wantErr: ErrVerifyFailed, 309 }, 310 { 311 name: "test OP_NUMNOTEQUAL", 312 op: OP_NUMNOTEQUAL, 313 startVM: &virtualMachine{ 314 runLimit: 50000, 315 dataStack: [][]byte{{2}, {1}}, 316 }, 317 wantVM: &virtualMachine{ 318 runLimit: 49998, 319 deferredCost: -9, 320 dataStack: [][]byte{{1}}, 321 }, 322 }, 323 { 324 name: "test OP_LESSTHAN", 325 op: OP_LESSTHAN, 326 startVM: &virtualMachine{ 327 runLimit: 50000, 328 dataStack: [][]byte{{2}, {1}}, 329 }, 330 wantVM: &virtualMachine{ 331 runLimit: 49998, 332 deferredCost: -10, 333 dataStack: [][]byte{{}}, 334 }, 335 }, 336 { 337 name: "test OP_LESSTHANOREQUAL", 338 op: OP_LESSTHANOREQUAL, 339 startVM: &virtualMachine{ 340 runLimit: 50000, 341 dataStack: [][]byte{{2}, {1}}, 342 }, 343 wantVM: &virtualMachine{ 344 runLimit: 49998, 345 deferredCost: -10, 346 dataStack: [][]byte{{}}, 347 }, 348 }, 349 { 350 name: "test OP_GREATERTHAN", 351 op: OP_GREATERTHAN, 352 startVM: &virtualMachine{ 353 runLimit: 50000, 354 dataStack: [][]byte{{2}, {1}}, 355 }, 356 wantVM: &virtualMachine{ 357 runLimit: 49998, 358 deferredCost: -9, 359 dataStack: [][]byte{{1}}, 360 }, 361 }, 362 { 363 name: "test OP_GREATERTHANOREQUAL", 364 op: OP_GREATERTHANOREQUAL, 365 startVM: &virtualMachine{ 366 runLimit: 50000, 367 dataStack: [][]byte{{2}, {1}}, 368 }, 369 wantVM: &virtualMachine{ 370 runLimit: 49998, 371 deferredCost: -9, 372 dataStack: [][]byte{{1}}, 373 }, 374 }, 375 { 376 name: "test OP_MIN min(2,1)", 377 op: OP_MIN, 378 startVM: &virtualMachine{ 379 runLimit: 50000, 380 dataStack: [][]byte{{2}, {1}}, 381 }, 382 wantVM: &virtualMachine{ 383 runLimit: 49998, 384 deferredCost: -9, 385 dataStack: [][]byte{{1}}, 386 }, 387 }, 388 { 389 name: "test OP_MIN min(1,2)", 390 op: OP_MIN, 391 startVM: &virtualMachine{ 392 runLimit: 50000, 393 dataStack: [][]byte{{1}, {2}}, 394 }, 395 wantVM: &virtualMachine{ 396 runLimit: 49998, 397 deferredCost: -9, 398 dataStack: [][]byte{{1}}, 399 }, 400 }, 401 { 402 name: "test OP_MAX max(1,2)", 403 op: OP_MAX, 404 startVM: &virtualMachine{ 405 runLimit: 50000, 406 dataStack: [][]byte{{2}, {1}}, 407 }, 408 wantVM: &virtualMachine{ 409 runLimit: 49998, 410 deferredCost: -9, 411 dataStack: [][]byte{{2}}, 412 }, 413 }, 414 { 415 name: "test OP_MAX max(1,2)", 416 op: OP_MAX, 417 startVM: &virtualMachine{ 418 runLimit: 50000, 419 dataStack: [][]byte{{1}, {2}}, 420 }, 421 wantVM: &virtualMachine{ 422 runLimit: 49998, 423 deferredCost: -9, 424 dataStack: [][]byte{{2}}, 425 }, 426 }, 427 { 428 name: "test OP_WITHIN", 429 op: OP_WITHIN, 430 startVM: &virtualMachine{ 431 runLimit: 50000, 432 dataStack: [][]byte{{1}, {1}, {2}}, 433 }, 434 wantVM: &virtualMachine{ 435 runLimit: 49996, 436 deferredCost: -18, 437 dataStack: [][]byte{{1}}, 438 }, 439 }} 440 441 numops := []Op{ 442 OP_1ADD, OP_1SUB, OP_2MUL, OP_2DIV, OP_NOT, OP_0NOTEQUAL, 443 OP_ADD, OP_SUB, OP_MUL, OP_DIV, OP_MOD, OP_LSHIFT, OP_RSHIFT, OP_BOOLAND, 444 OP_BOOLOR, OP_NUMEQUAL, OP_NUMEQUALVERIFY, OP_NUMNOTEQUAL, OP_LESSTHAN, 445 OP_LESSTHANOREQUAL, OP_GREATERTHAN, OP_GREATERTHANOREQUAL, OP_MIN, OP_MAX, OP_WITHIN, 446 } 447 448 for _, op := range numops { 449 tests = append(tests, testStruct{ 450 op: op, 451 startVM: &virtualMachine{ 452 runLimit: 0, 453 dataStack: [][]byte{{2}, {2}, {2}}, 454 }, 455 wantErr: ErrRunLimitExceeded, 456 }) 457 } 458 459 for i, c := range tests { 460 t.Run(c.name, func(t *testing.T) { 461 err := ops[c.op].fn(c.startVM) 462 if err != c.wantErr { 463 t.Errorf("case %d, op %s: got err = %v want %v", i, ops[c.op].name, err, c.wantErr) 464 return 465 } 466 if c.wantErr != nil { 467 return 468 } 469 470 if !testutil.DeepEqual(c.startVM, c.wantVM) { 471 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) 472 } 473 }) 474 } 475 } 476 477 func TestRangeErrs(t *testing.T) { 478 cases := []struct { 479 prog string 480 expectRangeErr bool 481 }{ 482 {"0 1ADD", false}, 483 {fmt.Sprintf("%d 1ADD", int64(math.MinInt64)), true}, 484 {fmt.Sprintf("%d 1ADD", int64(math.MaxInt64)-1), false}, 485 {fmt.Sprintf("%s 1ADD", big.NewInt(0).SetBytes(common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")).String()), true}, 486 {fmt.Sprintf("%s 1ADD", big.NewInt(0).SetBytes(common.Hex2Bytes("7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")).String()), true}, 487 } 488 489 for i, c := range cases { 490 prog, _ := Assemble(c.prog) 491 vm := &virtualMachine{ 492 program: prog, 493 runLimit: 50000, 494 } 495 err := vm.run() 496 switch err { 497 case nil: 498 if c.expectRangeErr { 499 t.Errorf("case %d (%s): expected range error, got none", i, c.prog) 500 } 501 case ErrRange: 502 if !c.expectRangeErr { 503 t.Errorf("case %d (%s): got unexpected range error", i, c.prog) 504 } 505 default: 506 if c.expectRangeErr { 507 t.Errorf("case %d (%s): expected range error, got %s", i, c.prog, err) 508 } else { 509 t.Errorf("case %d (%s): got unexpected error %s", i, c.prog, err) 510 } 511 } 512 } 513 } 514 515 func TestNumCompare(t *testing.T) { 516 type args struct { 517 vm *virtualMachine 518 op int 519 } 520 tests := []struct { 521 name string 522 args args 523 want [][]byte 524 wantErr bool 525 }{ 526 { 527 name: "test 2 > 1 for cmpLess", 528 args: args{ 529 vm: &virtualMachine{ 530 dataStack: [][]byte{{0x02}, {0x01}}, 531 runLimit: 50000, 532 }, 533 op: cmpLess, 534 }, 535 want: [][]byte{{}}, 536 wantErr: false, 537 }, 538 { 539 name: "test 2 > 1 for cmpLessEqual", 540 args: args{ 541 vm: &virtualMachine{ 542 dataStack: [][]byte{{0x02}, {0x01}}, 543 runLimit: 50000, 544 }, 545 op: cmpLessEqual, 546 }, 547 want: [][]byte{{}}, 548 wantErr: false, 549 }, 550 { 551 name: "test 2 > 1 for cmpGreater", 552 args: args{ 553 vm: &virtualMachine{ 554 dataStack: [][]byte{{0x02}, {0x01}}, 555 runLimit: 50000, 556 }, 557 op: cmpGreater, 558 }, 559 want: [][]byte{{1}}, 560 wantErr: false, 561 }, 562 { 563 name: "test 2 > 1 for cmpGreaterEqual", 564 args: args{ 565 vm: &virtualMachine{ 566 dataStack: [][]byte{{0x02}, {0x01}}, 567 runLimit: 50000, 568 }, 569 op: cmpGreaterEqual, 570 }, 571 want: [][]byte{{1}}, 572 wantErr: false, 573 }, 574 { 575 name: "test 2 > 1 for cmpEqual", 576 args: args{ 577 vm: &virtualMachine{ 578 dataStack: [][]byte{{0x02}, {0x01}}, 579 runLimit: 50000, 580 }, 581 op: cmpEqual, 582 }, 583 want: [][]byte{{}}, 584 wantErr: false, 585 }, 586 { 587 name: "test 2 > 1 for cmpNotEqual", 588 args: args{ 589 vm: &virtualMachine{ 590 dataStack: [][]byte{{0x02}, {0x01}}, 591 runLimit: 50000, 592 }, 593 op: cmpNotEqual, 594 }, 595 want: [][]byte{{1}}, 596 wantErr: false, 597 }, 598 { 599 name: "test 2 == 2 for cmpLess", 600 args: args{ 601 vm: &virtualMachine{ 602 dataStack: [][]byte{{0x02}, {0x02}}, 603 runLimit: 50000, 604 }, 605 op: cmpLess, 606 }, 607 want: [][]byte{{1}}, 608 wantErr: false, 609 }, 610 { 611 name: "test 2 == 2 for cmpLessEqual", 612 args: args{ 613 vm: &virtualMachine{ 614 dataStack: [][]byte{{0x02}, {0x02}}, 615 runLimit: 50000, 616 }, 617 op: cmpLessEqual, 618 }, 619 want: [][]byte{{1}}, 620 wantErr: false, 621 }, 622 { 623 name: "test 2 == 2 for cmpGreater", 624 args: args{ 625 vm: &virtualMachine{ 626 dataStack: [][]byte{{0x02}, {0x02}}, 627 runLimit: 50000, 628 }, 629 op: cmpGreater, 630 }, 631 want: [][]byte{{1}}, 632 wantErr: false, 633 }, 634 { 635 name: "test 2 == 2 for cmpGreaterEqual", 636 args: args{ 637 vm: &virtualMachine{ 638 dataStack: [][]byte{{0x02}, {0x02}}, 639 runLimit: 50000, 640 }, 641 op: cmpGreaterEqual, 642 }, 643 want: [][]byte{{1}}, 644 wantErr: false, 645 }, 646 { 647 name: "test 2 == 2 for cmpEqual", 648 args: args{ 649 vm: &virtualMachine{ 650 dataStack: [][]byte{{0x02}, {0x02}}, 651 runLimit: 50000, 652 }, 653 op: cmpEqual, 654 }, 655 want: [][]byte{{1}}, 656 wantErr: false, 657 }, 658 { 659 name: "test 2 == 2 for cmpNotEqual", 660 args: args{ 661 vm: &virtualMachine{ 662 dataStack: [][]byte{{0x02}, {0x02}}, 663 runLimit: 50000, 664 }, 665 op: cmpNotEqual, 666 }, 667 want: [][]byte{{1}}, 668 wantErr: false, 669 }, 670 { 671 name: "test 1 < 2 for cmpLess", 672 args: args{ 673 vm: &virtualMachine{ 674 dataStack: [][]byte{{0x01}, {0x02}}, 675 runLimit: 50000, 676 }, 677 op: cmpLess, 678 }, 679 want: [][]byte{{1}}, 680 wantErr: false, 681 }, 682 { 683 name: "test 1 < 2 for cmpLessEqual", 684 args: args{ 685 vm: &virtualMachine{ 686 dataStack: [][]byte{{0x01}, {0x02}}, 687 runLimit: 50000, 688 }, 689 op: cmpLessEqual, 690 }, 691 want: [][]byte{{1}}, 692 wantErr: false, 693 }, 694 { 695 name: "test 1 < 2 for cmpGreater", 696 args: args{ 697 vm: &virtualMachine{ 698 dataStack: [][]byte{{0x01}, {0x02}}, 699 runLimit: 50000, 700 }, 701 op: cmpGreater, 702 }, 703 want: [][]byte{{}}, 704 wantErr: false, 705 }, 706 { 707 name: "test 1 < 2 for cmpGreaterEqual", 708 args: args{ 709 vm: &virtualMachine{ 710 dataStack: [][]byte{{0x01}, {0x02}}, 711 runLimit: 50000, 712 }, 713 op: cmpGreaterEqual, 714 }, 715 want: [][]byte{{}}, 716 wantErr: false, 717 }, 718 { 719 name: "test 1 < 2 for cmpEqual", 720 args: args{ 721 vm: &virtualMachine{ 722 dataStack: [][]byte{{0x01}, {0x02}}, 723 runLimit: 50000, 724 }, 725 op: cmpEqual, 726 }, 727 want: [][]byte{{}}, 728 wantErr: false, 729 }, 730 { 731 name: "test 1 < 2 for cmpNotEqual", 732 args: args{ 733 vm: &virtualMachine{ 734 dataStack: [][]byte{{0x01}, {0x02}}, 735 runLimit: 50000, 736 }, 737 op: cmpNotEqual, 738 }, 739 want: [][]byte{{1}}, 740 wantErr: false, 741 }, 742 } 743 for _, tt := range tests { 744 t.Run(tt.name, func(t *testing.T) { 745 if err := doNumCompare(tt.args.vm, tt.args.op); (err != nil) != tt.wantErr { 746 t.Errorf("doNumCompare() error = %v, wantErr %v", err, tt.wantErr) 747 } 748 }) 749 } 750 } 751 752 func TestOpMinMax(t *testing.T) { 753 type args struct { 754 vm *virtualMachine 755 f func(vm *virtualMachine) error 756 } 757 758 tests := []struct { 759 name string 760 args args 761 want [][]byte 762 wantErr bool 763 }{ 764 { 765 name: "min of (2, 3)", 766 args: args{ 767 vm: &virtualMachine{ 768 runLimit: 50000, 769 dataStack: [][]byte{{0x02}, {0x03}}, 770 }, 771 f: opMin, 772 }, 773 want: [][]byte{{0x02}}, 774 wantErr: false, 775 }, 776 777 { 778 name: "max of (2, 3)", 779 args: args{ 780 vm: &virtualMachine{ 781 runLimit: 50000, 782 dataStack: [][]byte{{0x02}, {0x03}}, 783 }, 784 f: opMax, 785 }, 786 want: [][]byte{{0x03}}, 787 wantErr: false, 788 }, 789 { 790 name: "max of (two number, one number)", 791 args: args{ 792 vm: &virtualMachine{ 793 runLimit: 50000, 794 dataStack: [][]byte{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, {0xff}}, 795 }, 796 f: opMax, 797 }, 798 want: [][]byte{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}}, 799 wantErr: false, 800 }, 801 { 802 name: "min of (0, -1) got error", 803 args: args{ 804 vm: &virtualMachine{ 805 runLimit: 50000, 806 dataStack: [][]byte{{}, mocks.U256NumNegative1}, 807 }, 808 f: opMin, 809 }, 810 want: nil, 811 wantErr: true, 812 }, 813 { 814 name: "max of (-1, -1) got error", 815 args: args{ 816 vm: &virtualMachine{ 817 runLimit: 50000, 818 dataStack: [][]byte{mocks.U256NumNegative1, mocks.U256NumNegative1}, 819 }, 820 f: opMax, 821 }, 822 want: nil, 823 wantErr: true, 824 }, 825 } 826 for _, tt := range tests { 827 t.Run(tt.name, func(t *testing.T) { 828 if err := tt.args.f(tt.args.vm); err != nil { 829 if !tt.wantErr { 830 t.Errorf("opAdd() error = %v, wantErr %v", err, tt.wantErr) 831 } 832 return 833 } 834 if !testutil.DeepEqual(tt.args.vm.dataStack, tt.want) { 835 t.Errorf("opAdd() error, got %v and wantErr %v", tt.args.vm.dataStack, tt.want) 836 } 837 }) 838 } 839 } 840 841 func Test_op2Mul(t *testing.T) { 842 type args struct { 843 vm *virtualMachine 844 } 845 tests := []struct { 846 name string 847 args args 848 wantErr bool 849 }{ 850 { 851 name: "test normal mul op", 852 args: args{ 853 vm: &virtualMachine{ 854 runLimit: 50000, 855 dataStack: [][]byte{{2}}, 856 }, 857 }, 858 wantErr: false, 859 }, 860 { 861 name: "test normal mul op of big number", 862 args: args{ 863 vm: &virtualMachine{ 864 runLimit: 50000, 865 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}}, 866 }, 867 }, 868 wantErr: false, 869 }, 870 { 871 name: "test error of mul op negative", 872 args: args{ 873 vm: &virtualMachine{ 874 runLimit: 50000, 875 dataStack: [][]byte{mocks.U256NumNegative1}, 876 }, 877 }, 878 wantErr: true, 879 }, 880 { 881 name: "test error of mul op out range", 882 args: args{ 883 vm: &virtualMachine{ 884 runLimit: 50000, 885 dataStack: [][]byte{mocks.MaxU256}, 886 }, 887 }, 888 wantErr: true, 889 }, 890 { 891 name: "test error of mul op out range which result is min number", 892 args: args{ 893 vm: &virtualMachine{ 894 runLimit: 50000, 895 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}}, 896 }, 897 }, 898 wantErr: true, 899 }, 900 } 901 for _, tt := range tests { 902 t.Run(tt.name, func(t *testing.T) { 903 if err := op2Mul(tt.args.vm); (err != nil) != tt.wantErr { 904 t.Errorf("op2Mul() error = %v, wantErr %v", err, tt.wantErr) 905 } 906 }) 907 } 908 } 909 910 func Test_opMul(t *testing.T) { 911 type args struct { 912 vm *virtualMachine 913 } 914 tests := []struct { 915 name string 916 args args 917 want [][]byte 918 wantErr bool 919 }{ 920 { 921 name: "test normal mul op", 922 args: args{ 923 vm: &virtualMachine{ 924 runLimit: 50000, 925 dataStack: [][]byte{{2}, {2}}, 926 }, 927 }, 928 want: [][]byte{{4}}, 929 wantErr: false, 930 }, 931 { 932 name: "test normal mul op of big number", 933 args: args{ 934 vm: &virtualMachine{ 935 runLimit: 50000, 936 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}}, 937 }, 938 }, 939 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}}, 940 wantErr: false, 941 }, 942 { 943 name: "test error of mul op negative", 944 args: args{ 945 vm: &virtualMachine{ 946 runLimit: 50000, 947 dataStack: [][]byte{mocks.U256NumNegative1, {0x02}}, 948 }, 949 }, 950 wantErr: true, 951 }, 952 { 953 name: "test error of mul op out range", 954 args: args{ 955 vm: &virtualMachine{ 956 runLimit: 50000, 957 dataStack: [][]byte{mocks.MaxU256}, 958 }, 959 }, 960 wantErr: true, 961 }, 962 { 963 name: "test error of mul op out range which result is min number", 964 args: args{ 965 vm: &virtualMachine{ 966 runLimit: 50000, 967 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}}, 968 }, 969 }, 970 wantErr: true, 971 }, 972 } 973 for _, tt := range tests { 974 t.Run(tt.name, func(t *testing.T) { 975 if err := opMul(tt.args.vm); err != nil { 976 if !tt.wantErr { 977 t.Errorf("opMul() error = %v, wantErr %v", err, tt.wantErr) 978 } 979 return 980 } 981 if !testutil.DeepEqual(tt.args.vm.dataStack, tt.want) { 982 t.Errorf("opMul() error, got %v and wantErr %v", tt.args.vm.dataStack, tt.want) 983 } 984 }) 985 } 986 } 987 988 func Test_op1Sub(t *testing.T) { 989 type args struct { 990 vm *virtualMachine 991 } 992 tests := []struct { 993 name string 994 args args 995 want [][]byte 996 wantErr bool 997 }{ 998 { 999 name: "Test 2 - 1 = 1", 1000 args: args{ 1001 vm: &virtualMachine{ 1002 runLimit: 50000, 1003 dataStack: [][]byte{{0x02}}, 1004 }, 1005 }, 1006 want: [][]byte{{0x01}}, 1007 wantErr: false, 1008 }, 1009 { 1010 name: "Test that two number become one number after op sub", 1011 args: args{ 1012 vm: &virtualMachine{ 1013 runLimit: 50000, 1014 dataStack: [][]byte{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}}, 1015 }, 1016 }, 1017 want: [][]byte{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}}, 1018 wantErr: false, 1019 }, 1020 { 1021 name: "Test for 0 - 1 got error", 1022 args: args{ 1023 vm: &virtualMachine{ 1024 runLimit: 50000, 1025 dataStack: [][]byte{{}}, 1026 }, 1027 }, 1028 want: nil, 1029 wantErr: true, 1030 }, 1031 { 1032 name: "Test for -1 - 1 got error", 1033 args: args{ 1034 vm: &virtualMachine{ 1035 runLimit: 50000, 1036 dataStack: [][]byte{mocks.U256NumNegative1}, 1037 }, 1038 }, 1039 want: nil, 1040 wantErr: true, 1041 }, 1042 } 1043 for _, tt := range tests { 1044 t.Run(tt.name, func(t *testing.T) { 1045 if err := op1Sub(tt.args.vm); (err != nil) != tt.wantErr { 1046 t.Errorf("op1Sub() error = %v, wantErr %v", err, tt.wantErr) 1047 } 1048 if !testutil.DeepEqual(tt.args.vm.dataStack, tt.want) { 1049 t.Errorf("op1Sub() error, got %v and wantErr %v", tt.args.vm.dataStack, tt.want) 1050 } 1051 }) 1052 } 1053 } 1054 1055 func Test_opSub(t *testing.T) { 1056 type args struct { 1057 vm *virtualMachine 1058 } 1059 tests := []struct { 1060 name string 1061 args args 1062 want [][]byte 1063 wantErr bool 1064 }{ 1065 { 1066 name: "Test 2 - 1 = 1", 1067 args: args{ 1068 vm: &virtualMachine{ 1069 runLimit: 50000, 1070 dataStack: [][]byte{{0x02}, {0x01}}, 1071 }, 1072 }, 1073 want: [][]byte{{0x01}}, 1074 wantErr: false, 1075 }, 1076 { 1077 name: "Test that two number become one number", 1078 args: args{ 1079 vm: &virtualMachine{ 1080 runLimit: 50000, 1081 dataStack: [][]byte{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}, {0x01}}, 1082 }, 1083 }, 1084 want: [][]byte{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}}, 1085 wantErr: false, 1086 }, 1087 { 1088 name: "Test for 0 - 1 got error", 1089 args: args{ 1090 vm: &virtualMachine{ 1091 runLimit: 50000, 1092 dataStack: [][]byte{{}, {0x01}}, 1093 }, 1094 }, 1095 want: nil, 1096 wantErr: true, 1097 }, 1098 { 1099 name: "Test for -1 - 1 got error", 1100 args: args{ 1101 vm: &virtualMachine{ 1102 runLimit: 50000, 1103 dataStack: [][]byte{mocks.U256NumNegative1, {0x01}}, 1104 }, 1105 }, 1106 want: nil, 1107 wantErr: true, 1108 }, 1109 } 1110 for _, tt := range tests { 1111 t.Run(tt.name, func(t *testing.T) { 1112 if err := opSub(tt.args.vm); (err != nil) != tt.wantErr { 1113 t.Errorf("opSub() error = %v, wantErr %v", err, tt.wantErr) 1114 } 1115 if !testutil.DeepEqual(tt.args.vm.dataStack, tt.want) { 1116 t.Errorf("opSub() error, got %v and wantErr %v", tt.args.vm.dataStack, tt.want) 1117 } 1118 }) 1119 } 1120 } 1121 1122 func Test_op2Div(t *testing.T) { 1123 type args struct { 1124 vm *virtualMachine 1125 } 1126 tests := []struct { 1127 name string 1128 args args 1129 want [][]byte 1130 wantErr bool 1131 }{ 1132 { 1133 name: "Test 2 div 2 = 1", 1134 args: args{ 1135 vm: &virtualMachine{ 1136 runLimit: 50000, 1137 dataStack: [][]byte{{0x02}}, 1138 }, 1139 }, 1140 want: [][]byte{{0x01}}, 1141 wantErr: false, 1142 }, 1143 { 1144 name: "Test that two number become one number after op div", 1145 args: args{ 1146 vm: &virtualMachine{ 1147 runLimit: 50000, 1148 dataStack: [][]byte{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}}, 1149 }, 1150 }, 1151 want: [][]byte{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80}}, 1152 wantErr: false, 1153 }, 1154 { 1155 name: "Test for 0 div 2 got 0", 1156 args: args{ 1157 vm: &virtualMachine{ 1158 runLimit: 50000, 1159 dataStack: [][]byte{{}}, 1160 }, 1161 }, 1162 want: [][]byte{{}}, 1163 wantErr: false, 1164 }, 1165 { 1166 name: "Test for -1 div 2 got error", 1167 args: args{ 1168 vm: &virtualMachine{ 1169 runLimit: 50000, 1170 dataStack: [][]byte{mocks.U256NumNegative1}, 1171 }, 1172 }, 1173 want: nil, 1174 wantErr: true, 1175 }, 1176 } 1177 for _, tt := range tests { 1178 t.Run(tt.name, func(t *testing.T) { 1179 if err := op2Div(tt.args.vm); err != nil { 1180 if !tt.wantErr { 1181 t.Errorf("op2Div() error = %v, wantErr %v", err, tt.wantErr) 1182 } 1183 return 1184 } 1185 if !testutil.DeepEqual(tt.args.vm.dataStack, tt.want) { 1186 t.Errorf("op2Div() error, got %v and wantErr %v", tt.args.vm.dataStack, tt.want) 1187 } 1188 }) 1189 } 1190 } 1191 1192 func Test_opDiv(t *testing.T) { 1193 type args struct { 1194 vm *virtualMachine 1195 } 1196 tests := []struct { 1197 name string 1198 args args 1199 want [][]byte 1200 wantErr bool 1201 }{ 1202 { 1203 name: "Test 2 div 2 = 1", 1204 args: args{ 1205 vm: &virtualMachine{ 1206 runLimit: 50000, 1207 dataStack: [][]byte{{0x02}, {0x02}}, 1208 }, 1209 }, 1210 want: [][]byte{{0x01}}, 1211 wantErr: false, 1212 }, 1213 { 1214 name: "Test 2 div 1 = 2", 1215 args: args{ 1216 vm: &virtualMachine{ 1217 runLimit: 50000, 1218 dataStack: [][]byte{{0x02}, {0x01}}, 1219 }, 1220 }, 1221 want: [][]byte{{0x02}}, 1222 wantErr: false, 1223 }, 1224 { 1225 name: "Test that two number become one number after op div", 1226 args: args{ 1227 vm: &virtualMachine{ 1228 runLimit: 50000, 1229 dataStack: [][]byte{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}, {0x02}}, 1230 }, 1231 }, 1232 want: [][]byte{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80}}, 1233 wantErr: false, 1234 }, 1235 { 1236 name: "Test for 0 div 2 got 0", 1237 args: args{ 1238 vm: &virtualMachine{ 1239 runLimit: 50000, 1240 dataStack: [][]byte{{}, {0x02}}, 1241 }, 1242 }, 1243 want: [][]byte{{}}, 1244 wantErr: false, 1245 }, 1246 { 1247 name: "Test for -1 div 2 got error", 1248 args: args{ 1249 vm: &virtualMachine{ 1250 runLimit: 50000, 1251 dataStack: [][]byte{mocks.U256NumNegative1, {0x02}}, 1252 }, 1253 }, 1254 want: nil, 1255 wantErr: true, 1256 }, 1257 { 1258 name: "Test for 1 div 0 got error", 1259 args: args{ 1260 vm: &virtualMachine{ 1261 runLimit: 50000, 1262 dataStack: [][]byte{{0x01}, {}}, 1263 }, 1264 }, 1265 want: nil, 1266 wantErr: true, 1267 }, 1268 } 1269 for _, tt := range tests { 1270 t.Run(tt.name, func(t *testing.T) { 1271 if err := opDiv(tt.args.vm); err != nil { 1272 if !tt.wantErr { 1273 t.Errorf("opDiv() error = %v, wantErr %v", err, tt.wantErr) 1274 } 1275 return 1276 } 1277 if !testutil.DeepEqual(tt.args.vm.dataStack, tt.want) { 1278 t.Errorf("opDiv() error, got %v and wantErr %v", tt.args.vm.dataStack, tt.want) 1279 } 1280 }) 1281 } 1282 } 1283 1284 func Test_opAdd(t *testing.T) { 1285 type args struct { 1286 vm *virtualMachine 1287 } 1288 1289 tests := []struct { 1290 name string 1291 args args 1292 want [][]byte 1293 wantErr bool 1294 }{ 1295 { 1296 name: "Test 2 + 2 = 4", 1297 args: args{ 1298 vm: &virtualMachine{ 1299 runLimit: 50000, 1300 dataStack: [][]byte{{0x02}, {0x02}}, 1301 }, 1302 }, 1303 want: [][]byte{{0x04}}, 1304 wantErr: false, 1305 }, 1306 { 1307 name: "Test that one number become two number", 1308 args: args{ 1309 vm: &virtualMachine{ 1310 runLimit: 50000, 1311 dataStack: [][]byte{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, {0x01}}, 1312 }, 1313 }, 1314 want: [][]byte{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}}, 1315 wantErr: false, 1316 }, 1317 { 1318 name: "Test for 0 + -1 got error", 1319 args: args{ 1320 vm: &virtualMachine{ 1321 runLimit: 50000, 1322 dataStack: [][]byte{{}, mocks.U256NumNegative1}, 1323 }, 1324 }, 1325 want: nil, 1326 wantErr: true, 1327 }, 1328 { 1329 name: "Test for -1 + -1 got error", 1330 args: args{ 1331 vm: &virtualMachine{ 1332 runLimit: 50000, 1333 dataStack: [][]byte{mocks.U256NumNegative1, mocks.U256NumNegative1}, 1334 }, 1335 }, 1336 want: nil, 1337 wantErr: true, 1338 }, 1339 } 1340 for _, tt := range tests { 1341 t.Run(tt.name, func(t *testing.T) { 1342 if err := opAdd(tt.args.vm); err != nil { 1343 if !tt.wantErr { 1344 t.Errorf("opAdd() error = %v, wantErr %v", err, tt.wantErr) 1345 } 1346 return 1347 } 1348 if !testutil.DeepEqual(tt.args.vm.dataStack, tt.want) { 1349 t.Errorf("opAdd() error, got %v and wantErr %v", tt.args.vm.dataStack, tt.want) 1350 } 1351 }) 1352 } 1353 } 1354 1355 func Test_opMod(t *testing.T) { 1356 type args struct { 1357 vm *virtualMachine 1358 } 1359 tests := []struct { 1360 name string 1361 args args 1362 want [][]byte 1363 wantErr bool 1364 }{ 1365 { 1366 name: "Test 2 mod 2 = 0", 1367 args: args{ 1368 vm: &virtualMachine{ 1369 runLimit: 50000, 1370 dataStack: [][]byte{{0x02}, {0x02}}, 1371 }, 1372 }, 1373 want: [][]byte{{}}, 1374 wantErr: false, 1375 }, 1376 { 1377 name: "Test 2 mod 1 = 0", 1378 args: args{ 1379 vm: &virtualMachine{ 1380 runLimit: 50000, 1381 dataStack: [][]byte{{0x02}, {0x01}}, 1382 }, 1383 }, 1384 want: [][]byte{{}}, 1385 wantErr: false, 1386 }, 1387 { 1388 name: "Test 255 mod 4 = 3", 1389 args: args{ 1390 vm: &virtualMachine{ 1391 runLimit: 50000, 1392 dataStack: [][]byte{{0xff}, {0x04}}, 1393 }, 1394 }, 1395 want: [][]byte{{0x03}}, 1396 wantErr: false, 1397 }, 1398 { 1399 name: "Test that two number become one number", 1400 args: args{ 1401 vm: &virtualMachine{ 1402 runLimit: 50000, 1403 dataStack: [][]byte{{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, {0x03}}, 1404 }, 1405 }, 1406 want: [][]byte{{0x01}}, 1407 wantErr: false, 1408 }, 1409 { 1410 name: "Test for 0 mod 2 got 0", 1411 args: args{ 1412 vm: &virtualMachine{ 1413 runLimit: 50000, 1414 dataStack: [][]byte{{}, {0x02}}, 1415 }, 1416 }, 1417 want: [][]byte{{}}, 1418 wantErr: false, 1419 }, 1420 { 1421 name: "Test for -1 div 2 got error", 1422 args: args{ 1423 vm: &virtualMachine{ 1424 runLimit: 50000, 1425 dataStack: [][]byte{mocks.U256NumNegative1, {0x02}}, 1426 }, 1427 }, 1428 want: nil, 1429 wantErr: true, 1430 }, 1431 { 1432 name: "Test for 1 div 0 got error", 1433 args: args{ 1434 vm: &virtualMachine{ 1435 runLimit: 50000, 1436 dataStack: [][]byte{{0x01}, {}}, 1437 }, 1438 }, 1439 want: nil, 1440 wantErr: true, 1441 }, 1442 } 1443 for _, tt := range tests { 1444 t.Run(tt.name, func(t *testing.T) { 1445 if err := opMod(tt.args.vm); err != nil { 1446 if !tt.wantErr { 1447 t.Errorf("opMod() error = %v, wantErr %v", err, tt.wantErr) 1448 } 1449 return 1450 } 1451 if !testutil.DeepEqual(tt.args.vm.dataStack, tt.want) { 1452 t.Errorf("opMod() error, got %v and wantErr %v", tt.args.vm.dataStack, tt.want) 1453 } 1454 }) 1455 } 1456 } 1457 1458 func TestOpShift(t *testing.T) { 1459 type args struct { 1460 vm *virtualMachine 1461 f func(vm *virtualMachine) error 1462 } 1463 1464 tests := []struct { 1465 name string 1466 args args 1467 want [][]byte 1468 wantErr bool 1469 }{ 1470 { 1471 name: "2 left shift 0", 1472 args: args{ 1473 vm: &virtualMachine{ 1474 runLimit: 50000, 1475 dataStack: [][]byte{{0x02}, {}}, 1476 }, 1477 f: opLshift, 1478 }, 1479 want: [][]byte{{0x02}}, 1480 wantErr: false, 1481 }, 1482 { 1483 name: "2 right shift 0", 1484 args: args{ 1485 vm: &virtualMachine{ 1486 runLimit: 50000, 1487 dataStack: [][]byte{{0x02}, {}}, 1488 }, 1489 f: opRshift, 1490 }, 1491 want: [][]byte{{0x02}}, 1492 wantErr: false, 1493 }, 1494 { 1495 name: "2 left shift 3", 1496 args: args{ 1497 vm: &virtualMachine{ 1498 runLimit: 50000, 1499 dataStack: [][]byte{{0x02}, {0x03}}, 1500 }, 1501 f: opLshift, 1502 }, 1503 want: [][]byte{{0x10}}, 1504 wantErr: false, 1505 }, 1506 { 1507 name: "2 right shift 3", 1508 args: args{ 1509 vm: &virtualMachine{ 1510 runLimit: 50000, 1511 dataStack: [][]byte{{0x02}, {0x03}}, 1512 }, 1513 f: opRshift, 1514 }, 1515 want: [][]byte{{}}, 1516 wantErr: false, 1517 }, 1518 { 1519 name: "two number right shift become one number", 1520 args: args{ 1521 vm: &virtualMachine{ 1522 runLimit: 50000, 1523 dataStack: [][]byte{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, {0x0f}}, 1524 }, 1525 f: opRshift, 1526 }, 1527 want: [][]byte{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01}}, 1528 wantErr: false, 1529 }, 1530 { 1531 name: "two number left shift become overflow", 1532 args: args{ 1533 vm: &virtualMachine{ 1534 runLimit: 50000, 1535 dataStack: [][]byte{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, {0xff}}, 1536 }, 1537 f: opLshift, 1538 }, 1539 wantErr: true, 1540 }, 1541 { 1542 name: "left shift not uint64", 1543 args: args{ 1544 vm: &virtualMachine{ 1545 runLimit: 50000, 1546 dataStack: [][]byte{{0xff}, {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}}, 1547 }, 1548 f: opLshift, 1549 }, 1550 want: [][]byte{{}}, 1551 wantErr: false, 1552 }, 1553 { 1554 name: "right shift not uint64", 1555 args: args{ 1556 vm: &virtualMachine{ 1557 runLimit: 50000, 1558 dataStack: [][]byte{{0xff}, {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}}, 1559 }, 1560 f: opRshift, 1561 }, 1562 want: [][]byte{{}}, 1563 wantErr: false, 1564 }, 1565 { 1566 name: "0 left shift -1 got error", 1567 args: args{ 1568 vm: &virtualMachine{ 1569 runLimit: 50000, 1570 dataStack: [][]byte{{}, mocks.U256NumNegative1}, 1571 }, 1572 f: opLshift, 1573 }, 1574 want: nil, 1575 wantErr: true, 1576 }, 1577 { 1578 name: "-1 right shift -1 got error", 1579 args: args{ 1580 vm: &virtualMachine{ 1581 runLimit: 50000, 1582 dataStack: [][]byte{mocks.U256NumNegative1, mocks.U256NumNegative1}, 1583 }, 1584 f: opRshift, 1585 }, 1586 want: nil, 1587 wantErr: true, 1588 }, 1589 } 1590 for _, tt := range tests { 1591 t.Run(tt.name, func(t *testing.T) { 1592 if err := tt.args.f(tt.args.vm); err != nil { 1593 if !tt.wantErr { 1594 t.Errorf("opShift() error = %v, wantErr %v", err, tt.wantErr) 1595 } 1596 return 1597 } 1598 if !testutil.DeepEqual(tt.args.vm.dataStack, tt.want) { 1599 t.Errorf("opShift() error, got %v and wantErr %v", tt.args.vm.dataStack, tt.want) 1600 } 1601 }) 1602 } 1603 }