@@ -462,3 +462,99 @@ fn test_not_no_arg() {
462
462
let fun_context = get_fun_context ( & module) ;
463
463
let _result = type_safety:: verify ( & module, & fun_context, & mut DummyMeter ) ;
464
464
}
465
+
466
+
467
+ #[ test]
468
+ fn test_comparison_correct_types ( ) {
469
+ for instr in vec ! [
470
+ Bytecode :: Lt ,
471
+ Bytecode :: Gt ,
472
+ Bytecode :: Le ,
473
+ Bytecode :: Ge ,
474
+ Bytecode :: Eq ,
475
+ Bytecode :: Neq ,
476
+ ] {
477
+ for push_ty_instr in vec ! [
478
+ Bytecode :: LdU8 ( 42 ) ,
479
+ Bytecode :: LdU16 ( 257 ) ,
480
+ Bytecode :: LdU32 ( 89 ) ,
481
+ Bytecode :: LdU64 ( 94 ) ,
482
+ Bytecode :: LdU128 ( Box :: new( 9999 ) ) ,
483
+ Bytecode :: LdU256 ( Box :: new( U256 :: from( 745_u32 ) ) ) ,
484
+ ] {
485
+ let code = vec ! [ push_ty_instr. clone( ) , push_ty_instr. clone( ) , instr. clone( ) ] ;
486
+ let module = make_module ( code) ;
487
+ let fun_context = get_fun_context ( & module) ;
488
+ let result = type_safety:: verify ( & module, & fun_context, & mut DummyMeter ) ;
489
+ assert ! ( result. is_ok( ) ) ;
490
+ }
491
+ }
492
+ }
493
+
494
+ #[ test]
495
+ fn test_comparison_mismatched_types ( ) {
496
+ for instr in vec ! [
497
+ Bytecode :: Lt ,
498
+ Bytecode :: Gt ,
499
+ Bytecode :: Le ,
500
+ Bytecode :: Ge ,
501
+ ] {
502
+ let code = vec ! [ Bytecode :: LdU8 ( 42 ) , Bytecode :: LdU64 ( 94 ) , instr. clone( ) ] ;
503
+ let module = make_module ( code) ;
504
+ let fun_context = get_fun_context ( & module) ;
505
+ let result = type_safety:: verify ( & module, & fun_context, & mut DummyMeter ) ;
506
+ assert_eq ! (
507
+ result. unwrap_err( ) . major_status( ) ,
508
+ StatusCode :: INTEGER_OP_TYPE_MISMATCH_ERROR
509
+ ) ;
510
+ }
511
+ }
512
+
513
+ #[ test]
514
+ fn test_comparison_wrong_type ( ) {
515
+ for instr in vec ! [
516
+ Bytecode :: Lt ,
517
+ Bytecode :: Gt ,
518
+ Bytecode :: Le ,
519
+ Bytecode :: Ge ,
520
+ ] {
521
+ let code = vec ! [ Bytecode :: LdTrue , Bytecode :: LdU64 ( 94 ) , instr. clone( ) ] ;
522
+ let module = make_module ( code) ;
523
+ let fun_context = get_fun_context ( & module) ;
524
+ let result = type_safety:: verify ( & module, & fun_context, & mut DummyMeter ) ;
525
+ assert_eq ! (
526
+ result. unwrap_err( ) . major_status( ) ,
527
+ StatusCode :: INTEGER_OP_TYPE_MISMATCH_ERROR
528
+ ) ;
529
+
530
+ let code = vec ! [ Bytecode :: LdU32 ( 94 ) , Bytecode :: LdFalse , instr. clone( ) ] ;
531
+ let module = make_module ( code) ;
532
+ let fun_context = get_fun_context ( & module) ;
533
+ let result = type_safety:: verify ( & module, & fun_context, & mut DummyMeter ) ;
534
+ assert_eq ! (
535
+ result. unwrap_err( ) . major_status( ) ,
536
+ StatusCode :: INTEGER_OP_TYPE_MISMATCH_ERROR
537
+ ) ;
538
+ }
539
+ }
540
+
541
+ #[ test]
542
+ #[ should_panic]
543
+ fn test_comparison_too_few_args ( ) {
544
+ for instr in vec ! [
545
+ Bytecode :: Lt ,
546
+ Bytecode :: Gt ,
547
+ Bytecode :: Le ,
548
+ Bytecode :: Ge ,
549
+ ] {
550
+ let code = vec ! [ Bytecode :: LdU16 ( 42 ) , instr. clone( ) ] ;
551
+ let module = make_module ( code) ;
552
+ let fun_context = get_fun_context ( & module) ;
553
+ let _result = type_safety:: verify ( & module, & fun_context, & mut DummyMeter ) ;
554
+
555
+ let code = vec ! [ instr. clone( ) ] ;
556
+ let module = make_module ( code) ;
557
+ let fun_context = get_fun_context ( & module) ;
558
+ let _result = type_safety:: verify ( & module, & fun_context, & mut DummyMeter ) ;
559
+ }
560
+ }
0 commit comments