@@ -420,3 +420,83 @@ TEST_CASE("simplify_expr boolean expressions", "[core][util]")
420420 }
421421 }
422422}
423+
424+ TEST_CASE (" Simplifying cast expressions" , " [core][util]" )
425+ {
426+ symbol_tablet symbol_table;
427+ namespacet ns (symbol_table);
428+ const auto short_type = signedbv_typet (16 );
429+ const auto int_type = signedbv_typet (32 );
430+ const auto long_type = signedbv_typet (64 );
431+ array_typet array_type (int_type, from_integer (5 , int_type));
432+
433+ symbolt a_symbol;
434+ a_symbol.base_name = " a" ;
435+ a_symbol.name = " a" ;
436+ a_symbol.type = array_type;
437+ a_symbol.is_lvalue = true ;
438+ symbol_table.add (a_symbol);
439+
440+ symbolt i_symbol;
441+ i_symbol.base_name = " i" ;
442+ i_symbol.name = " i" ;
443+ i_symbol.type = int_type;
444+ i_symbol.is_lvalue = true ;
445+ symbol_table.add (i_symbol);
446+
447+ config.set_arch (" none" );
448+
449+ SECTION (" Simplifying a[(signed long int)0]" )
450+ {
451+ // a[(signed long int)0]
452+ index_exprt expr{symbol_exprt{" a" , array_type},
453+ typecast_exprt{from_integer (0 , int_type), long_type}};
454+ // cast is applied on the constant
455+ const auto simplified_expr = simplify_expr (expr, ns);
456+ REQUIRE (
457+ simplified_expr ==
458+ index_exprt{symbol_exprt{" a" , array_type}, from_integer (0 , long_type)});
459+ }
460+ SECTION (" Simplifying a[(signed long int)i]" )
461+ {
462+ // a[(signed long int)0]
463+ index_exprt expr{symbol_exprt{" a" , array_type},
464+ typecast_exprt{i_symbol.symbol_expr (), long_type}};
465+ // Cast is not removed as up casting a symbol
466+ const auto simplified_expr = simplify_expr (expr, ns);
467+ REQUIRE (simplified_expr == expr);
468+ }
469+ SECTION (" Simplifying a[(signed int)i]" )
470+ {
471+ // a[(signed int)i]
472+ index_exprt expr{symbol_exprt{" a" , array_type},
473+ typecast_exprt{i_symbol.symbol_expr (), int_type}};
474+
475+ const auto simplified_expr = simplify_expr (expr, ns);
476+ REQUIRE (
477+ simplified_expr ==
478+ index_exprt{symbol_exprt{" a" , array_type}, i_symbol.symbol_expr ()});
479+ }
480+ SECTION (" Simplifying a[(signed short)0]" )
481+ {
482+ // a[(signed short)0]
483+ index_exprt expr{symbol_exprt{" a" , array_type},
484+ typecast_exprt{from_integer (0 , int_type), short_type}};
485+
486+ // Can be simplified as the number is a constant
487+ const auto simplified_expr = simplify_expr (expr, ns);
488+ REQUIRE (
489+ simplified_expr ==
490+ index_exprt{symbol_exprt{" a" , array_type}, from_integer (0 , short_type)});
491+ }
492+ SECTION (" Simplifying a[(signed short)i]" )
493+ {
494+ // a[(signed short)i]
495+ index_exprt expr{symbol_exprt{" a" , array_type},
496+ typecast_exprt{i_symbol.symbol_expr (), short_type}};
497+
498+ // No simplification as the cast may have an effect
499+ const auto simplified_expr = simplify_expr (expr, ns);
500+ REQUIRE (simplified_expr == expr);
501+ }
502+ }
0 commit comments