|
| 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 | +} |
| 67 | + |
| 68 | +TEST_CASE("pointer_object_exprt", "[core][util]") |
| 69 | +{ |
| 70 | + const exprt pointer = symbol_exprt{"foo", pointer_type(void_type())}; |
| 71 | + const pointer_object_exprt pointer_object{pointer, size_type()}; |
| 72 | + SECTION("Is equivalent to free function.") |
| 73 | + { |
| 74 | + CHECK(::pointer_object(pointer) == pointer_object); |
| 75 | + } |
| 76 | + SECTION("Result type") |
| 77 | + { |
| 78 | + CHECK(pointer_object.type() == size_type()); |
| 79 | + } |
| 80 | + SECTION("Pointer operand accessor") |
| 81 | + { |
| 82 | + CHECK(pointer_object.pointer() == pointer); |
| 83 | + } |
| 84 | + SECTION("Downcasting") |
| 85 | + { |
| 86 | + const exprt &upcast = pointer_object; |
| 87 | + CHECK(expr_try_dynamic_cast<pointer_object_exprt>(pointer_object)); |
| 88 | + CHECK_FALSE(expr_try_dynamic_cast<pointer_offset_exprt>(upcast)); |
| 89 | + SECTION("Validation") |
| 90 | + { |
| 91 | + SECTION("Invalid operand") |
| 92 | + { |
| 93 | + unary_exprt invalid_type = pointer_object; |
| 94 | + invalid_type.op().type() = bool_typet{}; |
| 95 | + const cbmc_invariants_should_throwt invariants_throw; |
| 96 | + REQUIRE_THROWS_MATCHES( |
| 97 | + expr_try_dynamic_cast<pointer_object_exprt>(invalid_type), |
| 98 | + invariant_failedt, |
| 99 | + invariant_failure_containing( |
| 100 | + "pointer_object must have pointer-typed operand")); |
| 101 | + } |
| 102 | + SECTION("Missing operand") |
| 103 | + { |
| 104 | + exprt missing_operand = pointer_object; |
| 105 | + missing_operand.operands().clear(); |
| 106 | + const cbmc_invariants_should_throwt invariants_throw; |
| 107 | + REQUIRE_THROWS_MATCHES( |
| 108 | + expr_try_dynamic_cast<pointer_object_exprt>(missing_operand), |
| 109 | + invariant_failedt, |
| 110 | + invariant_failure_containing("pointer_object must have one operand")); |
| 111 | + } |
| 112 | + SECTION("Too many operands") |
| 113 | + { |
| 114 | + exprt two_operands = pointer_object; |
| 115 | + two_operands.operands().push_back(pointer); |
| 116 | + const cbmc_invariants_should_throwt invariants_throw; |
| 117 | + REQUIRE_THROWS_MATCHES( |
| 118 | + expr_try_dynamic_cast<pointer_object_exprt>(two_operands), |
| 119 | + invariant_failedt, |
| 120 | + invariant_failure_containing("pointer_object must have one operand")); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments