Skip to content

Commit

Permalink
Fix invalid access to stats key 'costs'
Browse files Browse the repository at this point in the history
  • Loading branch information
BenKaufmann committed Apr 24, 2017
1 parent a47aed7 commit e0942a4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions libclasp/src/clasp_facade.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ struct ClaspFacade::SolveData {
void init(SolveAlgorithm* algo, Enumerator* en) {
this->en = en;
this->algo = algo;
costs.data = 0;
this->algo->setEnumerator(*en);
if (interruptible) {
this->algo->enableInterrupts();
Expand Down
25 changes: 25 additions & 0 deletions libclasp/tests/facade_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class FacadeTest : public CppUnit::TestFixture {
CPPUNIT_TEST(testClingoStats);
CPPUNIT_TEST(testClingoStatsKeyIntegrity);
CPPUNIT_TEST(testClingoStatsWithoutStats);
CPPUNIT_TEST(testClingoStatsBug);
#if WITH_THREADS
CPPUNIT_TEST(testClingoSolverStatsRemainValid);
CPPUNIT_TEST(testShareModeRegression);
Expand Down Expand Up @@ -888,6 +889,30 @@ class FacadeTest : public CppUnit::TestFixture {
CPPUNIT_ASSERT(stats->get(root, "summary") != root);
CPPUNIT_ASSERT_THROW_MESSAGE("accu requires stats", stats->get(root, "solving.accu"), std::out_of_range);
}
void testClingoStatsBug() {
Clasp::ClaspFacade libclasp;
Clasp::ClaspConfig config;
Clasp::Asp::LogicProgram& asp = libclasp.startAsp(config, true);
lpAdd(asp, "{x2,x3}. #minimize{not x1,x2}.");
libclasp.solve();
Potassco::AbstractStatistics* stats = libclasp.getStats();
typedef Potassco::AbstractStatistics::Key_t Key_t;
Key_t root = stats->root();
Key_t costs, minVal;
CPPUNIT_ASSERT(stats->size(root) == 3);
CPPUNIT_ASSERT((costs = stats->get(root, "summary.costs")) != root);
CPPUNIT_ASSERT(stats->type(costs) == Potassco::Statistics_t::Array);
CPPUNIT_ASSERT(stats->size(costs) == 1);
CPPUNIT_ASSERT((minVal = stats->get(root, "summary.costs.0")) != root);
CPPUNIT_ASSERT(stats->type(minVal) == Potassco::Statistics_t::Value);
config.solve.numModels = -1;
libclasp.update(true);
lpAdd(asp, ":- not x1.");
libclasp.solve();
CPPUNIT_ASSERT(stats->type(costs) == Potassco::Statistics_t::Array);
CPPUNIT_ASSERT(stats->size(costs) == 0);
CPPUNIT_ASSERT_THROW(stats->value(minVal), std::logic_error);
}
void getKeys(const Potassco::AbstractStatistics& stats, Potassco::AbstractStatistics::Key_t k, std::vector<std::string>& out, const std::string& p) {
if (stats.type(k) == Potassco::Statistics_t::Map) {
for (uint32 i = 0, end = stats.size(k); i != end; ++i) {
Expand Down

0 comments on commit e0942a4

Please sign in to comment.