Skip to content

Commit 257bfc4

Browse files
author
Enrico Steffinlongo
committed
Add SMT2 array theory store function
1 parent f51c4cc commit 257bfc4

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

src/solvers/smt2_incremental/smt_array_theory.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,32 @@ void smt_array_theoryt::selectt::validate(
2727

2828
const smt_function_application_termt::factoryt<smt_array_theoryt::selectt>
2929
smt_array_theoryt::select{};
30+
31+
const char *smt_array_theoryt::storet::identifier()
32+
{
33+
return "store";
34+
}
35+
smt_sortt smt_array_theoryt::storet::return_sort(
36+
const smt_termt &array,
37+
const smt_termt &index,
38+
const smt_termt &value)
39+
{
40+
return array.get_sort();
41+
}
42+
void smt_array_theoryt::storet::validate(
43+
const smt_termt &array,
44+
const smt_termt &index,
45+
const smt_termt &value)
46+
{
47+
const auto array_sort = array.get_sort().cast<smt_array_sortt>();
48+
INVARIANT(array_sort, "\"store\" may only update an array.");
49+
INVARIANT(
50+
array_sort->index_sort() == index.get_sort(),
51+
"Sort of arrays index must match the sort of the index supplied.");
52+
INVARIANT(
53+
array_sort->element_sort() == value.get_sort(),
54+
"Sort of arrays value must match the sort of the value supplied.");
55+
}
56+
57+
const smt_function_application_termt::factoryt<smt_array_theoryt::storet>
58+
smt_array_theoryt::store{};

src/solvers/smt2_incremental/smt_array_theory.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,20 @@ class smt_array_theoryt
1616
static void validate(const smt_termt &array, const smt_termt &index);
1717
};
1818
static const smt_function_application_termt::factoryt<selectt> select;
19+
20+
struct storet final
21+
{
22+
static const char *identifier();
23+
static smt_sortt return_sort(
24+
const smt_termt &array,
25+
const smt_termt &index,
26+
const smt_termt &value);
27+
static void validate(
28+
const smt_termt &array,
29+
const smt_termt &index,
30+
const smt_termt &value);
31+
};
32+
static const smt_function_application_termt::factoryt<storet> store;
1933
};
2034

2135
#endif // CPROVER_SOLVERS_SMT2_INCREMENTAL_SMT_ARRAY_THEORY_H

unit/solvers/smt2_incremental/smt_array_theory.cpp

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,36 @@ TEST_CASE("SMT array theory \"select\".", "[core][smt2_incremental]")
3232
invariant_failedt,
3333
invariant_failure_containing(
3434
"Sort of arrays index must match the sort of the index supplied."));
35-
}
35+
}
36+
37+
TEST_CASE("SMT array theory \"store\".", "[core][smt2_incremental]")
38+
{
39+
const smt_identifier_termt index_term("index", smt_bit_vector_sortt(64));
40+
const smt_identifier_termt value_term("value", smt_bit_vector_sortt(32));
41+
const smt_identifier_termt array_term(
42+
"array", smt_array_sortt(index_term.get_sort(), value_term.get_sort()));
43+
const smt_function_application_termt store =
44+
smt_array_theoryt::store(array_term, index_term, value_term);
45+
CHECK(store.get_sort() == array_term.get_sort());
46+
CHECK(store.function_identifier().identifier() == "store");
47+
REQUIRE(store.arguments().size() == 3);
48+
CHECK(store.arguments()[0].get() == array_term);
49+
CHECK(store.arguments()[1].get() == index_term);
50+
CHECK(store.arguments()[2].get() == value_term);
51+
cbmc_invariants_should_throwt invariants_throw;
52+
const smt_bit_vector_constant_termt two{2, 8};
53+
REQUIRE_THROWS_MATCHES(
54+
smt_array_theoryt::store(two, index_term, value_term),
55+
invariant_failedt,
56+
invariant_failure_containing("\"store\" may only update an array."));
57+
REQUIRE_THROWS_MATCHES(
58+
smt_array_theoryt::store(array_term, two, value_term),
59+
invariant_failedt,
60+
invariant_failure_containing(
61+
"Sort of arrays index must match the sort of the index supplied."));
62+
REQUIRE_THROWS_MATCHES(
63+
smt_array_theoryt::store(array_term, index_term, two),
64+
invariant_failedt,
65+
invariant_failure_containing(
66+
"Sort of arrays value must match the sort of the value supplied."));
67+
}

0 commit comments

Comments
 (0)