|
| 1 | +// Author: Diffblue Ltd. |
| 2 | + |
| 3 | +#include <util/c_types.h> |
| 4 | +#include <util/pointer_expr.h> |
| 5 | +#include <util/pointer_predicates.h> |
| 6 | + |
| 7 | +#include <testing-utils/invariant.h> |
| 8 | +#include <testing-utils/use_catch.h> |
| 9 | + |
| 10 | +TEST_CASE("pointer_offset_exprt", "[core][util]") |
| 11 | +{ |
| 12 | + const exprt pointer = symbol_exprt{"foo", pointer_type(void_type())}; |
| 13 | + const pointer_offset_exprt pointer_offset{pointer, signed_size_type()}; |
| 14 | + SECTION("Is equivalent to free function.") |
| 15 | + { |
| 16 | + CHECK(::pointer_offset(pointer) == pointer_offset); |
| 17 | + } |
| 18 | + SECTION("Result type") |
| 19 | + { |
| 20 | + CHECK(pointer_offset.type() == signed_size_type()); |
| 21 | + } |
| 22 | + SECTION("Pointer operand accessor") |
| 23 | + { |
| 24 | + CHECK(pointer_offset.pointer() == pointer); |
| 25 | + } |
| 26 | + SECTION("Downcasting") |
| 27 | + { |
| 28 | + const exprt &upcast = pointer_offset; |
| 29 | + CHECK(expr_try_dynamic_cast<pointer_offset_exprt>(pointer_offset)); |
| 30 | + CHECK_FALSE(expr_try_dynamic_cast<pointer_object_exprt>(upcast)); |
| 31 | + SECTION("Validation") |
| 32 | + { |
| 33 | + SECTION("Invalid operand") |
| 34 | + { |
| 35 | + unary_exprt invalid_type = pointer_offset; |
| 36 | + invalid_type.op().type() = bool_typet{}; |
| 37 | + const cbmc_invariants_should_throwt invariants_throw; |
| 38 | + REQUIRE_THROWS_MATCHES( |
| 39 | + expr_try_dynamic_cast<pointer_offset_exprt>(invalid_type), |
| 40 | + invariant_failedt, |
| 41 | + invariant_failure_containing( |
| 42 | + "pointer_offset must have pointer-typed operand")); |
| 43 | + } |
| 44 | + SECTION("Missing operand") |
| 45 | + { |
| 46 | + exprt missing_operand = pointer_offset; |
| 47 | + missing_operand.operands().clear(); |
| 48 | + const cbmc_invariants_should_throwt invariants_throw; |
| 49 | + REQUIRE_THROWS_MATCHES( |
| 50 | + expr_try_dynamic_cast<pointer_offset_exprt>(missing_operand), |
| 51 | + invariant_failedt, |
| 52 | + invariant_failure_containing("pointer_offset must have one operand")); |
| 53 | + } |
| 54 | + SECTION("Too many operands") |
| 55 | + { |
| 56 | + exprt two_operands = pointer_offset; |
| 57 | + two_operands.operands().push_back(pointer); |
| 58 | + const cbmc_invariants_should_throwt invariants_throw; |
| 59 | + REQUIRE_THROWS_MATCHES( |
| 60 | + expr_try_dynamic_cast<pointer_offset_exprt>(two_operands), |
| 61 | + invariant_failedt, |
| 62 | + invariant_failure_containing("pointer_offset must have one operand")); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | +} |
0 commit comments