Skip to content

Commit 281a155

Browse files
author
thk123
committed
Import tests for checking type cast removal
1 parent bd167d0 commit 281a155

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

unit/util/simplify_expr.cpp

+79
Original file line numberDiff line numberDiff line change
@@ -420,3 +420,82 @@ 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 int_type = signedbv_typet(32);
429+
const auto long_type = signedbv_typet(32);
430+
array_typet array_type(int_type, from_integer(5, int_type));
431+
432+
symbolt a_symbol;
433+
a_symbol.base_name = "a";
434+
a_symbol.name = "a";
435+
a_symbol.type = array_type;
436+
a_symbol.is_lvalue = true;
437+
symbol_table.add(a_symbol);
438+
439+
symbolt i_symbol;
440+
i_symbol.base_name = "i";
441+
i_symbol.name = "i";
442+
i_symbol.type = int_type;
443+
i_symbol.is_lvalue = true;
444+
symbol_table.add(i_symbol);
445+
446+
config.set_arch("none");
447+
448+
SECTION("Simplifying a[(signed long int)0]")
449+
{
450+
// [(signed long int)0]
451+
index_exprt expr{symbol_exprt{"a", array_type},
452+
typecast_exprt{from_integer(0, int_type), long_type}};
453+
// cast can be removed as constant
454+
REQUIRE(
455+
simplify_expr(expr, ns) ==
456+
index_exprt{symbol_exprt{"a", array_type}, from_integer(0, int_type)});
457+
}
458+
SECTION("Simplifying a[(signed long int)i]")
459+
{
460+
// [(signed long int)0]
461+
index_exprt expr{symbol_exprt{"a", array_type},
462+
typecast_exprt{i_symbol.symbol_expr(), long_type}};
463+
// Here the cast is removed, is this correct?
464+
const auto simplified_expr = simplify_expr(expr, ns);
465+
REQUIRE(
466+
simplified_expr ==
467+
index_exprt{symbol_exprt{"a", array_type}, i_symbol.symbol_expr()});
468+
}
469+
SECTION("Simplifying a[(signed int)i]")
470+
{
471+
index_exprt expr{symbol_exprt{"a", array_type},
472+
typecast_exprt{i_symbol.symbol_expr(), int_type}};
473+
474+
const auto simplified_expr = simplify_expr(expr, ns);
475+
REQUIRE(
476+
simplified_expr ==
477+
index_exprt{symbol_exprt{"a", array_type}, i_symbol.symbol_expr()});
478+
}
479+
SECTION("Simplifying a[(signed short)0]")
480+
{
481+
const auto short_type = signedbv_typet(16);
482+
index_exprt expr{symbol_exprt{"a", array_type},
483+
typecast_exprt{from_integer(0, int_type), short_type}};
484+
485+
// Can be simplified as the number is a constant
486+
const auto simplified_expr = simplify_expr(expr, ns);
487+
REQUIRE(
488+
simplified_expr ==
489+
index_exprt{symbol_exprt{"a", array_type}, from_integer(0, int_type)});
490+
}
491+
SECTION("Simplifying a[(signed short)i]")
492+
{
493+
const auto short_type = signedbv_typet(16);
494+
index_exprt expr{symbol_exprt{"a", array_type},
495+
typecast_exprt{i_symbol.symbol_expr(), short_type}};
496+
497+
// No simplification as the cast may have an effect
498+
const auto simplified_expr = simplify_expr(expr, ns);
499+
REQUIRE(simplified_expr == expr);
500+
}
501+
}

0 commit comments

Comments
 (0)