Skip to content

implement boolbvt::get for enumeration types #8589

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/solvers/flattening/boolbv_get.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,9 @@ exprt boolbvt::bv_get_rec(const exprt &expr, const bvt &bv, std::size_t offset)
switch(bvtype)
{
case bvtypet::IS_UNKNOWN:
PRECONDITION(type.id() == ID_string || type.id() == ID_empty);
PRECONDITION(
type.id() == ID_string || type.id() == ID_empty ||
type.id() == ID_enumeration);
if(type.id()==ID_string)
{
mp_integer int_value=binary2integer(value, false);
Expand All @@ -215,6 +217,16 @@ exprt boolbvt::bv_get_rec(const exprt &expr, const bvt &bv, std::size_t offset)
{
return constant_exprt{irep_idt(), type};
}
else if(type.id() == ID_enumeration)
{
auto &elements = to_enumeration_type(type).elements();
mp_integer int_value = binary2integer(value, false);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❓ Can this be negative?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, binary2integer(..., false) won't return a negative value.

if(int_value >= elements.size())
return nil_exprt{};
else
return constant_exprt{
elements[numeric_cast_v<std::size_t>(int_value)].id(), type};
}
break;

case bvtypet::IS_RANGE:
Expand Down
1 change: 1 addition & 0 deletions unit/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ SRC += analyses/ai/ai.cpp \
pointer-analysis/value_set.cpp \
solvers/bdd/miniBDD/miniBDD.cpp \
solvers/flattening/boolbv.cpp \
solvers/flattening/boolbv_enumeration.cpp \
solvers/flattening/boolbv_onehot.cpp \
solvers/flattening/boolbv_update_bit.cpp \
solvers/floatbv/float_utils.cpp \
Expand Down
57 changes: 57 additions & 0 deletions unit/solvers/flattening/boolbv_enumeration.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*******************************************************************\
Module: Unit tests for solvers/flattening
Author: Daniel Kroening, [email protected]
\*******************************************************************/

/// \file

#include <util/cout_message.h>
#include <util/namespace.h>
#include <util/symbol_table.h>

#include <solvers/flattening/boolbv.h>
#include <solvers/sat/satcheck.h>
#include <testing-utils/use_catch.h>

TEST_CASE(
"enumeration flattening",
"[core][solvers][flattening][boolbvt][enumeration]")
{
console_message_handlert message_handler;
message_handler.set_verbosity(0);
satcheckt satcheck{message_handler};
symbol_tablet symbol_table;
namespacet ns{symbol_table};
boolbvt boolbv{ns, satcheck, message_handler};
enumeration_typet enumeration;
enumeration.elements().push_back(irept{"A"});
enumeration.elements().push_back(irept{"B"});

constant_exprt A("A", enumeration);
constant_exprt B("B", enumeration);

GIVEN("an inconsistent equality over an enumeration type")
{
boolbv << equal_exprt{A, B};

THEN("the formula is unsat")
{
REQUIRE(boolbv() == decision_proceduret::resultt::D_UNSATISFIABLE);
}
}

GIVEN("an equality over an enumeration type")
{
symbol_exprt symbol("s", enumeration);
boolbv << equal_exprt{symbol, A};

THEN("the value of the variable in the model is correct")
{
REQUIRE(boolbv() == decision_proceduret::resultt::D_SATISFIABLE);
REQUIRE(boolbv.get(symbol) == A);
}
}
}
Loading