|
| 1 | +// Author: Diffblue Ltd. |
| 2 | + |
| 3 | +#include <util/c_types.h> |
| 4 | +#include <util/namespace.h> |
| 5 | +#include <util/optional.h> |
| 6 | +#include <util/std_expr.h> |
| 7 | +#include <util/symbol_table.h> |
| 8 | + |
| 9 | +#include <solvers/smt2_incremental/object_tracking.h> |
| 10 | +#include <testing-utils/use_catch.h> |
| 11 | + |
| 12 | +#include <string> |
| 13 | +#include <utility> |
| 14 | + |
| 15 | +TEST_CASE("find_object_base_expression", "[core][smt2_incremental]") |
| 16 | +{ |
| 17 | + const typet base_type = pointer_typet{unsignedbv_typet{8}, 18}; |
| 18 | + const symbol_exprt object_base{"base", base_type}; |
| 19 | + const symbol_exprt index{"index", base_type}; |
| 20 | + const pointer_typet pointer_type{base_type, 12}; |
| 21 | + std::string description; |
| 22 | + optionalt<address_of_exprt> address_of; |
| 23 | + using rowt = std::pair<std::string, address_of_exprt>; |
| 24 | + std::tie(description, address_of) = GENERATE_REF( |
| 25 | + rowt{"Address of symbol", {object_base, pointer_type}}, |
| 26 | + rowt{"Address of index", {index_exprt{object_base, index}, pointer_type}}, |
| 27 | + rowt{ |
| 28 | + "Address of struct member", |
| 29 | + {member_exprt{object_base, "baz", unsignedbv_typet{8}}, pointer_type}}, |
| 30 | + rowt{ |
| 31 | + "Address of index of struct member", |
| 32 | + {index_exprt{ |
| 33 | + member_exprt{object_base, "baz", unsignedbv_typet{8}}, index}, |
| 34 | + pointer_type}}, |
| 35 | + rowt{ |
| 36 | + "Address of struct member at index", |
| 37 | + {member_exprt{ |
| 38 | + index_exprt{object_base, index}, "baz", unsignedbv_typet{8}}, |
| 39 | + pointer_type}}); |
| 40 | + SECTION(description) |
| 41 | + { |
| 42 | + CHECK(find_object_base_expression(*address_of) == object_base); |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +TEST_CASE("Tracking object base expressions", "[core][smt2_incremental]") |
| 47 | +{ |
| 48 | + const typet base_type = pointer_typet{signedbv_typet{16}, 18}; |
| 49 | + const symbol_exprt foo{"foo", base_type}; |
| 50 | + const symbol_exprt bar{"bar", base_type}; |
| 51 | + const symbol_exprt index{"index", base_type}; |
| 52 | + const pointer_typet pointer_type{base_type, 32}; |
| 53 | + const exprt bar_address = address_of_exprt{bar, pointer_type}; |
| 54 | + const exprt compound_expression = and_exprt{ |
| 55 | + equal_exprt{ |
| 56 | + address_of_exprt{index_exprt{foo, index}, pointer_type}, bar_address}, |
| 57 | + notequal_exprt{ |
| 58 | + address_of_exprt{ |
| 59 | + member_exprt{foo, "baz", unsignedbv_typet{8}}, pointer_type}, |
| 60 | + bar_address}}; |
| 61 | + SECTION("Find base expressions") |
| 62 | + { |
| 63 | + std::vector<exprt> expressions; |
| 64 | + find_object_base_expressions(compound_expression, [&](const exprt &expr) { |
| 65 | + expressions.push_back(expr); |
| 66 | + }); |
| 67 | + CHECK(expressions == std::vector<exprt>{bar, foo, bar, foo}); |
| 68 | + } |
| 69 | + smt_object_mapt object_map = initial_smt_object_map(); |
| 70 | + SECTION("Check initial object map has null pointer") |
| 71 | + { |
| 72 | + REQUIRE(object_map.size() == 1); |
| 73 | + const exprt null_pointer = null_pointer_exprt{::pointer_type(void_type())}; |
| 74 | + CHECK(object_map.begin()->first == null_pointer); |
| 75 | + CHECK(object_map.begin()->second.unique_id == 0); |
| 76 | + CHECK(object_map.begin()->second.base_expression == null_pointer); |
| 77 | + } |
| 78 | + symbol_tablet symbol_table; |
| 79 | + namespacet ns{symbol_table}; |
| 80 | + SECTION("Check objects of compound expression not yet tracked") |
| 81 | + { |
| 82 | + CHECK_FALSE(objects_are_already_tracked(compound_expression, object_map)); |
| 83 | + } |
| 84 | + track_expression_objects(compound_expression, ns, object_map); |
| 85 | + SECTION("Tracking expression objects") |
| 86 | + { |
| 87 | + CHECK(object_map.size() == 3); |
| 88 | + const auto foo_object = object_map.find(foo); |
| 89 | + REQUIRE(foo_object != object_map.end()); |
| 90 | + CHECK(foo_object->second.base_expression == foo); |
| 91 | + CHECK(foo_object->second.unique_id == 2); |
| 92 | + const auto bar_object = object_map.find(bar); |
| 93 | + REQUIRE(bar_object != object_map.end()); |
| 94 | + CHECK(bar_object->second.base_expression == bar); |
| 95 | + CHECK(bar_object->second.unique_id == 1); |
| 96 | + } |
| 97 | + SECTION("Confirming objects are tracked.") |
| 98 | + { |
| 99 | + CHECK(objects_are_already_tracked(compound_expression, object_map)); |
| 100 | + } |
| 101 | +} |
0 commit comments