1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
/*******************************************************************\
Module: Unit tests of the expression simplifier
Author: Michael Tautschnig
\*******************************************************************/
#include <testing-utils/use_catch.h>
#include <java_bytecode/java_types.h>
#include <util/config.h>
#include <util/namespace.h>
#include <util/simplify_expr.h>
#include <util/std_expr.h>
#include <util/symbol_table.h>
namespace
{
void test_unnecessary_cast(const typet &type)
{
config.set_arch("none");
WHEN("The casts can be removed, they are")
{
const exprt simplified=simplify_expr(
typecast_exprt(
typecast_exprt(symbol_exprt("foo", type), java_int_type()),
type),
namespacet(symbol_tablet()));
REQUIRE(simplified.id()==ID_symbol);
REQUIRE(simplified.type()==type);
const auto &symbol=to_symbol_expr(simplified);
REQUIRE(symbol.get_identifier()=="foo");
}
WHEN("Casts should remain, they are left untouched")
{
{
const exprt simplified=simplify_expr(
typecast_exprt(symbol_exprt("foo", type), java_int_type()),
namespacet(symbol_tablet()));
REQUIRE(simplified.id()==ID_typecast);
REQUIRE(simplified.type()==java_int_type());
}
// casts from boolean get rewritten to ?:
if(type == java_boolean_type())
{
const exprt simplified = simplify_expr(
typecast_exprt(symbol_exprt("foo", java_int_type()), type),
namespacet(symbol_tablet()));
REQUIRE(simplified.id() == ID_if);
REQUIRE(simplified.type() == type);
}
else
{
const exprt simplified=simplify_expr(
typecast_exprt(symbol_exprt("foo", java_int_type()), type),
namespacet(symbol_tablet()));
REQUIRE(simplified.id()==ID_typecast);
REQUIRE(simplified.type()==type);
}
}
WHEN("Deeply nested casts are present, they are collapsed appropriately")
{
{
const exprt simplified=simplify_expr(
typecast_exprt(
typecast_exprt(
typecast_exprt(
typecast_exprt(
typecast_exprt(symbol_exprt("foo", type), java_int_type()),
type),
java_int_type()),
type),
java_int_type()),
namespacet(symbol_tablet()));
REQUIRE(
simplified==typecast_exprt(symbol_exprt("foo", type), java_int_type()));
}
}
}
} // namespace
TEST_CASE("Simplify Java boolean -> int -> boolean casts")
{
test_unnecessary_cast(java_boolean_type());
}
TEST_CASE("Simplify Java byte -> int -> byte casts")
{
test_unnecessary_cast(java_byte_type());
}
TEST_CASE("Simplify Java char -> int -> char casts")
{
test_unnecessary_cast(java_char_type());
}
TEST_CASE("Simplify Java short -> int -> short casts")
{
test_unnecessary_cast(java_short_type());
}
|