Skip to content

Commit 351cecb

Browse files
committed
Add pointer object expression unit tests
1 parent ed7344e commit 351cecb

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

unit/util/pointer_expr.cpp

+58
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,61 @@ TEST_CASE("pointer_offset_exprt", "[core][util]")
6464
}
6565
}
6666
}
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

Comments
 (0)