Skip to content

Commit 6957a04

Browse files
authored
Merge pull request #8589 from diffblue/enumeration_bv_get
implement boolbvt::get for enumeration types
2 parents 66004dc + b4f9748 commit 6957a04

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

src/solvers/flattening/boolbv_get.cpp

+13-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,9 @@ exprt boolbvt::bv_get_rec(const exprt &expr, const bvt &bv, std::size_t offset)
199199
switch(bvtype)
200200
{
201201
case bvtypet::IS_UNKNOWN:
202-
PRECONDITION(type.id() == ID_string || type.id() == ID_empty);
202+
PRECONDITION(
203+
type.id() == ID_string || type.id() == ID_empty ||
204+
type.id() == ID_enumeration);
203205
if(type.id()==ID_string)
204206
{
205207
mp_integer int_value=binary2integer(value, false);
@@ -215,6 +217,16 @@ exprt boolbvt::bv_get_rec(const exprt &expr, const bvt &bv, std::size_t offset)
215217
{
216218
return constant_exprt{irep_idt(), type};
217219
}
220+
else if(type.id() == ID_enumeration)
221+
{
222+
auto &elements = to_enumeration_type(type).elements();
223+
mp_integer int_value = binary2integer(value, false);
224+
if(int_value >= elements.size())
225+
return nil_exprt{};
226+
else
227+
return constant_exprt{
228+
elements[numeric_cast_v<std::size_t>(int_value)].id(), type};
229+
}
218230
break;
219231

220232
case bvtypet::IS_RANGE:

unit/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ SRC += analyses/ai/ai.cpp \
9696
pointer-analysis/value_set.cpp \
9797
solvers/bdd/miniBDD/miniBDD.cpp \
9898
solvers/flattening/boolbv.cpp \
99+
solvers/flattening/boolbv_enumeration.cpp \
99100
solvers/flattening/boolbv_onehot.cpp \
100101
solvers/flattening/boolbv_update_bit.cpp \
101102
solvers/floatbv/float_utils.cpp \
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*******************************************************************\
2+
3+
Module: Unit tests for solvers/flattening
4+
5+
Author: Daniel Kroening, dkr@amazon.com
6+
7+
\*******************************************************************/
8+
9+
/// \file
10+
11+
#include <util/cout_message.h>
12+
#include <util/namespace.h>
13+
#include <util/symbol_table.h>
14+
15+
#include <solvers/flattening/boolbv.h>
16+
#include <solvers/sat/satcheck.h>
17+
#include <testing-utils/use_catch.h>
18+
19+
TEST_CASE(
20+
"enumeration flattening",
21+
"[core][solvers][flattening][boolbvt][enumeration]")
22+
{
23+
console_message_handlert message_handler;
24+
message_handler.set_verbosity(0);
25+
satcheckt satcheck{message_handler};
26+
symbol_tablet symbol_table;
27+
namespacet ns{symbol_table};
28+
boolbvt boolbv{ns, satcheck, message_handler};
29+
enumeration_typet enumeration;
30+
enumeration.elements().push_back(irept{"A"});
31+
enumeration.elements().push_back(irept{"B"});
32+
33+
constant_exprt A("A", enumeration);
34+
constant_exprt B("B", enumeration);
35+
36+
GIVEN("an inconsistent equality over an enumeration type")
37+
{
38+
boolbv << equal_exprt{A, B};
39+
40+
THEN("the formula is unsat")
41+
{
42+
REQUIRE(boolbv() == decision_proceduret::resultt::D_UNSATISFIABLE);
43+
}
44+
}
45+
46+
GIVEN("an equality over an enumeration type")
47+
{
48+
symbol_exprt symbol("s", enumeration);
49+
boolbv << equal_exprt{symbol, A};
50+
51+
THEN("the value of the variable in the model is correct")
52+
{
53+
REQUIRE(boolbv() == decision_proceduret::resultt::D_SATISFIABLE);
54+
REQUIRE(boolbv.get(symbol) == A);
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)