Skip to content

Commit 6d0a7aa

Browse files
committed
implement boolbvt::get for enumeration types
This adds the missing implementation for getting values from models for enumeration types.
1 parent af4e2e4 commit 6d0a7aa

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

src/solvers/flattening/boolbv_get.cpp

Lines changed: 14 additions & 1 deletion
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,17 @@ 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+
irep_idt element;
225+
if(int_value >= elements.size())
226+
element = irep_idt{};
227+
else
228+
element = elements[numeric_cast_v<std::size_t>(int_value)].id();
229+
return constant_exprt{element, type};
230+
}
218231
break;
219232

220233
case bvtypet::IS_RANGE:

unit/Makefile

Lines changed: 1 addition & 0 deletions
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 \
Lines changed: 57 additions & 0 deletions
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, [email protected]
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)