Skip to content

Commit f02ee6a

Browse files
committed
Add unit tests for pointer object expression conversion
1 parent 15423e3 commit f02ee6a

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

unit/solvers/smt2_incremental/convert_expr_to_smt.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <util/constructor_of.h>
99
#include <util/format.h>
1010
#include <util/namespace.h>
11+
#include <util/pointer_predicates.h>
1112
#include <util/std_expr.h>
1213
#include <util/symbol_table.h>
1314

@@ -1186,3 +1187,58 @@ TEST_CASE(
11861187
smt_bit_vector_constant_termt{0, 56})));
11871188
}
11881189
}
1190+
1191+
TEST_CASE(
1192+
"expr to smt conversion for pointer object expression",
1193+
"[core][smt2_incremental]")
1194+
{
1195+
// The config lines are necessary to ensure that pointer width in configured.
1196+
config.ansi_c.mode = configt::ansi_ct::flavourt::GCC;
1197+
config.ansi_c.set_arch_spec_x86_64();
1198+
config.bv_encoding.object_bits = 8;
1199+
1200+
const auto pointer_type = pointer_typet(unsigned_int_type(), 64 /* bits */);
1201+
const pointer_object_exprt foo{
1202+
symbol_exprt{"foo", pointer_type}, pointer_type};
1203+
const pointer_object_exprt foobar{
1204+
symbol_exprt{"foobar", pointer_type}, pointer_type};
1205+
1206+
SECTION("Pointer object expression")
1207+
{
1208+
const auto converted = convert_expr_to_smt(foo);
1209+
const auto expected =
1210+
smt_bit_vector_theoryt::zero_extend(56)(smt_bit_vector_theoryt::extract(
1211+
63, 56)(smt_identifier_termt("foo", smt_bit_vector_sortt(64))));
1212+
CHECK(converted == expected);
1213+
}
1214+
1215+
SECTION("Invariant checks")
1216+
{
1217+
const cbmc_invariants_should_throwt invariants_throw;
1218+
SECTION("Pointer object's operand type should be a bitvector type")
1219+
{
1220+
auto copy_of_foo = foo;
1221+
copy_of_foo.type() = bool_typet{};
1222+
REQUIRE_THROWS_MATCHES(
1223+
convert_expr_to_smt(copy_of_foo),
1224+
invariant_failedt,
1225+
invariant_failure_containing(
1226+
"Pointer object should have a bitvector-based type."));
1227+
}
1228+
}
1229+
1230+
SECTION("Comparison of pointer objects.")
1231+
{
1232+
const exprt comparison = notequal_exprt{foobar, foo};
1233+
INFO("Expression " + comparison.pretty(1, 0));
1234+
const auto converted = convert_expr_to_smt(comparison);
1235+
const auto bv1 =
1236+
smt_bit_vector_theoryt::zero_extend(56)(smt_bit_vector_theoryt::extract(
1237+
63, 56)(smt_identifier_termt("foo", smt_bit_vector_sortt(64))));
1238+
const auto bv2 =
1239+
smt_bit_vector_theoryt::zero_extend(56)(smt_bit_vector_theoryt::extract(
1240+
63, 56)(smt_identifier_termt("foobar", smt_bit_vector_sortt(64))));
1241+
const auto expected = smt_core_theoryt::distinct(bv2, bv1);
1242+
CHECK(converted == expected);
1243+
}
1244+
}

0 commit comments

Comments
 (0)