Skip to content

Commit ed7344e

Browse files
committed
Add pointer offset expression unit tests
1 parent 95b7340 commit ed7344e

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

unit/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ SRC += analyses/ai/ai.cpp \
161161
util/optional_utils.cpp \
162162
util/parse_options.cpp \
163163
util/piped_process.cpp \
164+
util/pointer_expr.cpp \
164165
util/pointer_offset_size.cpp \
165166
util/prefix_filter.cpp \
166167
util/range.cpp \

unit/util/pointer_expr.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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

Comments
 (0)