|
| 1 | +/*******************************************************************\ |
| 2 | +
|
| 3 | + Module: MODULE NAME |
| 4 | +
|
| 5 | + Author: DiffBlue Limited. All rights reserved. |
| 6 | +
|
| 7 | +\*******************************************************************/ |
| 8 | + |
| 9 | +/// \file |
| 10 | +/// Unit tests for util/simplify_expr.cpp |
| 11 | + |
| 12 | +#include <catch.hpp> |
| 13 | +#include <util/config.h> |
| 14 | +#include <util/namespace.h> |
| 15 | +#include <util/symbol_table.h> |
| 16 | +#include <util/simplify_expr.h> |
| 17 | +#include <util/std_expr.h> |
| 18 | +#include <util/std_types.h> |
| 19 | +#include <util/ui_message.h> |
| 20 | + |
| 21 | +#include <ansi-c/ansi_c_language.h> |
| 22 | + |
| 23 | + |
| 24 | +#include <unit-src/unit_util.h> |
| 25 | + |
| 26 | +SCENARIO("Simplifying an expression", "[core][util][simplify_expr]" |
| 27 | + "[simplify_typecast]") |
| 28 | +{ |
| 29 | + ui_message_handlert message_handler; |
| 30 | + ansi_c_languaget language; |
| 31 | + language.set_message_handler(message_handler); |
| 32 | + |
| 33 | + symbol_tablet symbol_table; |
| 34 | + namespacet ns(symbol_table); |
| 35 | + GIVEN("A namespace with some symbols in on a default architecture") |
| 36 | + { |
| 37 | + array_typet array_type( |
| 38 | + signedbv_typet(32), constant_exprt::integer_constant(5)); |
| 39 | + |
| 40 | + symbolt a_symbol; |
| 41 | + a_symbol.base_name="a"; |
| 42 | + a_symbol.name="a"; |
| 43 | + a_symbol.type=array_type; |
| 44 | + a_symbol.is_lvalue=true; |
| 45 | + symbol_table.add(a_symbol); |
| 46 | + |
| 47 | + symbolt i_symbol; |
| 48 | + i_symbol.base_name="i"; |
| 49 | + i_symbol.name="i"; |
| 50 | + i_symbol.type=signedbv_typet(32); |
| 51 | + i_symbol.is_lvalue=true; |
| 52 | + symbol_table.add(i_symbol); |
| 53 | + |
| 54 | + config.set_arch("none"); |
| 55 | + |
| 56 | + WHEN("Simplifying a[(signed long int)0]") |
| 57 | + { |
| 58 | + exprt out_expr; |
| 59 | + bool success=language.to_expr("a[(signed long int)0]", "", out_expr, ns); |
| 60 | + |
| 61 | + // Validate the conversion to expr |
| 62 | + REQUIRE_FALSE(success); |
| 63 | + THEN("Should get a[0]") |
| 64 | + { |
| 65 | + exprt simplified_expr=simplify_expr(out_expr, ns); |
| 66 | + REQUIRE(simplified_expr.id()==ID_index); |
| 67 | + |
| 68 | + index_exprt index=to_index_expr(simplified_expr); |
| 69 | + |
| 70 | + const exprt &index_part=index.index(); |
| 71 | + REQUIRE(index_part.id()==ID_constant); |
| 72 | + } |
| 73 | + } |
| 74 | + WHEN("Simplifying a[(signed long int)i]") |
| 75 | + { |
| 76 | + exprt out_expr; |
| 77 | + bool success=language.to_expr("a[(signed long int)i]", "", out_expr, ns); |
| 78 | + |
| 79 | + // Validate the conversion to expr |
| 80 | + REQUIRE_FALSE(success); |
| 81 | + THEN("Should get a[i]") |
| 82 | + { |
| 83 | + exprt simplified_expr=simplify_expr(out_expr, ns); |
| 84 | + REQUIRE(simplified_expr.id()==ID_index); |
| 85 | + |
| 86 | + index_exprt index=to_index_expr(simplified_expr); |
| 87 | + |
| 88 | + const exprt &index_part=index.index(); |
| 89 | + REQUIRE(index_part.id()==ID_symbol); |
| 90 | + const symbol_exprt symbol_expr=to_symbol_expr(index_part); |
| 91 | + REQUIRE(symbol_expr.get_identifier()=="i"); |
| 92 | + } |
| 93 | + } |
| 94 | + WHEN("Simplifying a[(signed int)i]") |
| 95 | + { |
| 96 | + exprt out_expr; |
| 97 | + bool success=language.to_expr("a[(signed int)i]", "", out_expr, ns); |
| 98 | + |
| 99 | + // Validate the conversion to expr |
| 100 | + REQUIRE_FALSE(success); |
| 101 | + THEN("Should get a[i]") |
| 102 | + { |
| 103 | + exprt simplified_expr=simplify_expr(out_expr, ns); |
| 104 | + REQUIRE(simplified_expr.id()==ID_index); |
| 105 | + |
| 106 | + index_exprt index=to_index_expr(simplified_expr); |
| 107 | + |
| 108 | + const exprt &index_part=index.index(); |
| 109 | + REQUIRE(index_part.id()==ID_symbol); |
| 110 | + const symbol_exprt symbol_expr=to_symbol_expr(index_part); |
| 111 | + REQUIRE(symbol_expr.get_identifier()=="i"); |
| 112 | + } |
| 113 | + } |
| 114 | + WHEN("Simplifying a[(signed short)i]") |
| 115 | + { |
| 116 | + exprt out_expr; |
| 117 | + bool success=language.to_expr("a[(signed short)i]", "", out_expr, ns); |
| 118 | + |
| 119 | + // Validate the conversion to expr |
| 120 | + REQUIRE_FALSE(success); |
| 121 | + |
| 122 | + // Since this is cast could go wrong (if i has a value greater than |
| 123 | + // SHRT_MAX for example) it should not be removed by the simplify |
| 124 | + THEN("Should get a[(signed short)i]") |
| 125 | + { |
| 126 | + exprt simplified_expr=simplify_expr(out_expr, ns); |
| 127 | + REQUIRE(simplified_expr.id()==ID_index); |
| 128 | + |
| 129 | + index_exprt index=to_index_expr(simplified_expr); |
| 130 | + |
| 131 | + const exprt &index_part=index.index(); |
| 132 | + |
| 133 | + // The expression will have changed as we are still able to remove |
| 134 | + // one type cast (the cast from short to long) |
| 135 | + REQUIRE(index_part.id()==ID_typecast); |
| 136 | + REQUIRE(index_part.type().id()==ID_signedbv); |
| 137 | + signedbv_typet signed_bv_type=to_signedbv_type(index_part.type()); |
| 138 | + REQUIRE(signed_bv_type.get_width()==config.ansi_c.short_int_width); |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments