2016-10-08 16:03:14 +02:00
|
|
|
#include <catch.hpp>
|
2016-05-20 18:46:37 +02:00
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
#include <plasp/sas/Description.h>
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-10-08 16:03:14 +02:00
|
|
|
class SASParserTestsFixture
|
2016-05-20 18:46:37 +02:00
|
|
|
{
|
|
|
|
protected:
|
2016-10-08 16:03:14 +02:00
|
|
|
SASParserTestsFixture()
|
2016-05-23 00:47:30 +02:00
|
|
|
: m_blocksworldTestFile(readFile("data/blocksworld.sas")),
|
2016-05-24 02:03:13 +02:00
|
|
|
m_cavedivingTestFile(readFile("data/cavediving.sas")),
|
2016-05-23 01:27:28 +02:00
|
|
|
m_freecellTestFile(readFile("data/freecell.sas")),
|
2016-05-23 00:47:30 +02:00
|
|
|
m_philosophersTestFile(readFile("data/philosophers.sas"))
|
2016-05-20 18:46:37 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::stringstream readFile(const std::string &path)
|
|
|
|
{
|
|
|
|
std::ifstream fileStream(path, std::ios::in);
|
|
|
|
|
|
|
|
std::stringstream outputStream;
|
|
|
|
|
|
|
|
if (!fileStream.is_open())
|
2016-06-14 12:47:39 +02:00
|
|
|
throw std::runtime_error("Could not open file “" + path + "”");
|
2016-05-20 18:46:37 +02:00
|
|
|
|
|
|
|
outputStream << fileStream.rdbuf();
|
|
|
|
|
|
|
|
return outputStream;
|
|
|
|
}
|
|
|
|
|
2016-05-23 00:47:30 +02:00
|
|
|
std::stringstream m_blocksworldTestFile;
|
2016-05-24 02:03:13 +02:00
|
|
|
std::stringstream m_cavedivingTestFile;
|
2016-05-23 01:27:28 +02:00
|
|
|
std::stringstream m_freecellTestFile;
|
2016-05-20 18:46:37 +02:00
|
|
|
std::stringstream m_philosophersTestFile;
|
|
|
|
};
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-10-08 16:03:14 +02:00
|
|
|
TEST_CASE_METHOD(SASParserTestsFixture, "[SAS parser] A valid SAS file is parsed correctly", "[SAS parser]")
|
2016-05-20 18:46:37 +02:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const auto description = plasp::sas::Description::fromStream(m_philosophersTestFile);
|
|
|
|
|
2016-11-29 06:03:05 +01:00
|
|
|
CHECK_FALSE(description.usesActionCosts());
|
2016-10-08 16:03:14 +02:00
|
|
|
|
|
|
|
REQUIRE(description.variables().size() == 37u);
|
2016-11-29 06:03:05 +01:00
|
|
|
CHECK(description.variables()[0].axiomLayer() == -1);
|
|
|
|
CHECK(description.variables()[0].values()[0].sign() == plasp::sas::Value::Sign::Positive);
|
|
|
|
CHECK(description.variables()[0].values()[0].name() == "activate(philosopher-0, forks--pid-rfork)");
|
|
|
|
CHECK(description.variables()[36].axiomLayer() == -1);
|
|
|
|
CHECK(description.variables()[36].values()[1].sign() == plasp::sas::Value::Sign::Negative);
|
|
|
|
CHECK(description.variables()[36].values()[1].name() == "queue-tail-msg(forks-1-, fork)");
|
2016-10-08 16:03:14 +02:00
|
|
|
|
|
|
|
REQUIRE(description.mutexGroups().size() == 8u);
|
|
|
|
REQUIRE(description.mutexGroups()[0].facts().size() == 9u);
|
2016-11-29 06:03:05 +01:00
|
|
|
CHECK(&description.mutexGroups()[0].facts()[0].value() == &description.variables()[0].values()[0]);
|
2016-10-08 16:03:14 +02:00
|
|
|
REQUIRE(description.mutexGroups()[7].facts().size() == 2u);
|
2016-11-29 06:03:05 +01:00
|
|
|
CHECK(&description.mutexGroups()[7].facts()[1].value() == &description.variables()[34].values()[1]);
|
2016-10-08 16:03:14 +02:00
|
|
|
|
|
|
|
REQUIRE(description.initialState().facts().size() == 37u);
|
2016-11-29 06:03:05 +01:00
|
|
|
CHECK(&description.initialState().facts()[0].value() == &description.variables()[0].values()[8]);
|
|
|
|
CHECK(&description.initialState().facts()[36].value() == &description.variables()[36].values()[1]);
|
2016-10-08 16:03:14 +02:00
|
|
|
|
|
|
|
REQUIRE(description.goal().facts().size() == 2u);
|
2016-11-29 06:03:05 +01:00
|
|
|
CHECK(&description.goal().facts()[0].value() == &description.variables()[6].values()[0]);
|
|
|
|
CHECK(&description.goal().facts()[1].value() == &description.variables()[7].values()[0]);
|
2016-10-08 16:03:14 +02:00
|
|
|
|
|
|
|
REQUIRE(description.operators().size() == 34u);
|
2016-11-29 06:03:05 +01:00
|
|
|
CHECK(description.operators()[0].predicate().name() == "activate-trans");
|
2016-10-08 16:03:14 +02:00
|
|
|
REQUIRE(description.operators()[0].predicate().arguments().size() == 5u);
|
2016-11-29 06:03:05 +01:00
|
|
|
CHECK(description.operators()[0].predicate().arguments()[0] == "philosopher-0");
|
|
|
|
CHECK(description.operators()[0].predicate().arguments()[4] == "state-3");
|
2016-10-08 16:03:14 +02:00
|
|
|
REQUIRE(description.operators()[0].preconditions().size() == 3u);
|
2016-11-29 06:03:05 +01:00
|
|
|
CHECK(&description.operators()[0].preconditions()[0].value() == &description.variables()[4].values()[4]);
|
|
|
|
CHECK(&description.operators()[0].preconditions()[1].value() == &description.variables()[16].values()[1]);
|
|
|
|
CHECK(&description.operators()[0].preconditions()[2].value() == &description.variables()[0].values()[8]);
|
2016-10-08 16:03:14 +02:00
|
|
|
REQUIRE(description.operators()[0].effects().size() == 1u);
|
|
|
|
REQUIRE(description.operators()[0].effects()[0].conditions().size() == 0u);
|
2016-11-29 06:03:05 +01:00
|
|
|
CHECK(&description.operators()[0].effects()[0].postcondition().value() == &description.variables()[0].values()[0]);
|
|
|
|
CHECK(description.operators()[33].predicate().name() == "queue-write");
|
2016-10-08 16:03:14 +02:00
|
|
|
REQUIRE(description.operators()[33].predicate().arguments().size() == 4u);
|
2016-11-29 06:03:05 +01:00
|
|
|
CHECK(description.operators()[33].predicate().arguments()[0] == "philosopher-1");
|
|
|
|
CHECK(description.operators()[33].predicate().arguments()[3] == "fork");
|
2016-10-08 16:03:14 +02:00
|
|
|
REQUIRE(description.operators()[33].preconditions().size() == 2u);
|
2016-11-29 06:03:05 +01:00
|
|
|
CHECK(&description.operators()[33].preconditions()[0].value() == &description.variables()[1].values()[3]);
|
|
|
|
CHECK(&description.operators()[33].preconditions()[1].value() == &description.variables()[2].values()[2]);
|
2016-10-08 16:03:14 +02:00
|
|
|
REQUIRE(description.operators()[33].effects().size() == 3u);
|
|
|
|
REQUIRE(description.operators()[33].effects()[0].conditions().size() == 0u);
|
2016-11-29 06:03:05 +01:00
|
|
|
CHECK(&description.operators()[33].effects()[0].postcondition().value() == &description.variables()[1].values()[7]);
|
|
|
|
CHECK(&description.operators()[33].effects()[2].postcondition().value() == &description.variables()[35].values()[0]);
|
2016-10-08 16:03:14 +02:00
|
|
|
|
|
|
|
REQUIRE(description.axiomRules().size() == 33u);
|
|
|
|
REQUIRE(description.axiomRules()[0].conditions().size() == 4u);
|
2016-11-29 06:03:05 +01:00
|
|
|
CHECK(&description.axiomRules()[0].conditions()[0].value() == &description.variables()[0].values()[0]);
|
|
|
|
CHECK(&description.axiomRules()[0].conditions()[2].value() == &description.variables()[27].values()[0]);
|
|
|
|
CHECK(&description.axiomRules()[0].conditions()[3].value() == &description.variables()[8].values()[1]);
|
|
|
|
CHECK(&description.axiomRules()[0].postcondition().value() == &description.variables()[8].values()[0]);
|
2016-10-08 16:03:14 +02:00
|
|
|
REQUIRE(description.axiomRules()[32].conditions().size() == 2u);
|
2016-11-29 06:03:05 +01:00
|
|
|
CHECK(&description.axiomRules()[32].conditions()[0].value() == &description.variables()[15].values()[0]);
|
|
|
|
CHECK(&description.axiomRules()[32].conditions()[1].value() == &description.variables()[25].values()[0]);
|
|
|
|
CHECK(&description.axiomRules()[32].postcondition().value() == &description.variables()[25].values()[1]);
|
2016-05-20 18:46:37 +02:00
|
|
|
}
|
|
|
|
catch (const std::exception &e)
|
|
|
|
{
|
2016-10-08 16:03:14 +02:00
|
|
|
FAIL(e.what());
|
2016-05-20 18:46:37 +02:00
|
|
|
}
|
2016-08-30 13:01:22 +02:00
|
|
|
|
|
|
|
// TODO: add whitespace test
|
2016-05-20 18:46:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-10-08 16:03:14 +02:00
|
|
|
TEST_CASE_METHOD(SASParserTestsFixture, "[SAS parser] Trailing empty parentheses are removed", "[SAS parser]")
|
2016-05-23 00:47:30 +02:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const auto description = plasp::sas::Description::fromStream(m_blocksworldTestFile);
|
|
|
|
|
2016-11-29 06:03:05 +01:00
|
|
|
CHECK(description.variables()[4].values()[0].name() == "handempty");
|
|
|
|
CHECK(description.variables()[5].values()[0].name() == "holding(a)");
|
2016-05-23 00:47:30 +02:00
|
|
|
}
|
|
|
|
catch (const std::exception &e)
|
|
|
|
{
|
2016-10-08 16:03:14 +02:00
|
|
|
FAIL(e.what());
|
2016-05-23 00:47:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-10-08 16:03:14 +02:00
|
|
|
TEST_CASE_METHOD(SASParserTestsFixture, "[SAS parser] “none” values are correctly parsed", "[SAS parser]")
|
2016-05-23 01:27:28 +02:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const auto description = plasp::sas::Description::fromStream(m_freecellTestFile);
|
|
|
|
|
|
|
|
// TODO: compare by identity, not value
|
2016-11-29 06:03:05 +01:00
|
|
|
CHECK(description.variables()[0].values()[3] == plasp::sas::Value::None);
|
|
|
|
CHECK(description.variables()[5].values()[6] == plasp::sas::Value::None);
|
2016-05-23 01:27:28 +02:00
|
|
|
}
|
|
|
|
catch (const std::exception &e)
|
|
|
|
{
|
2016-10-08 16:03:14 +02:00
|
|
|
FAIL(e.what());
|
2016-05-24 02:03:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-10-08 16:03:14 +02:00
|
|
|
TEST_CASE_METHOD(SASParserTestsFixture, "[SAS parser] SAS requirements are parsed correctly", "[SAS parser]")
|
2016-05-24 02:03:13 +02:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const auto description = plasp::sas::Description::fromStream(m_cavedivingTestFile);
|
|
|
|
|
2016-11-29 06:03:05 +01:00
|
|
|
CHECK(description.usesActionCosts());
|
|
|
|
CHECK(description.usesConditionalEffects());
|
|
|
|
CHECK_FALSE(description.usesAxiomRules());
|
2016-05-24 02:13:21 +02:00
|
|
|
|
2016-10-08 16:03:14 +02:00
|
|
|
REQUIRE(description.operators().size() == 496u);
|
2016-05-24 02:13:21 +02:00
|
|
|
|
2016-11-29 06:03:05 +01:00
|
|
|
CHECK(description.operators()[0].costs() == 1u);
|
|
|
|
CHECK(description.operators()[172].costs() == 10u);
|
|
|
|
CHECK(description.operators()[173].costs() == 63u);
|
2016-05-24 02:13:21 +02:00
|
|
|
|
2016-10-08 16:03:14 +02:00
|
|
|
REQUIRE(description.operators()[172].effects().size() == 3u);
|
|
|
|
REQUIRE(description.operators()[172].effects()[1].conditions().size() == 1u);
|
2016-11-29 06:03:05 +01:00
|
|
|
CHECK(&description.operators()[172].effects()[1].conditions()[0].value() == &description.variables()[1].values()[4]);
|
2016-05-24 02:03:13 +02:00
|
|
|
}
|
|
|
|
catch (const std::exception &e)
|
|
|
|
{
|
2016-10-08 16:03:14 +02:00
|
|
|
FAIL(e.what());
|
2016-05-23 01:27:28 +02:00
|
|
|
}
|
|
|
|
}
|
2016-08-30 18:47:12 +02:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-10-08 16:03:14 +02:00
|
|
|
TEST_CASE("[SAS parser] Former issues are fixed", "[SAS parser]")
|
2016-08-30 18:47:12 +02:00
|
|
|
{
|
|
|
|
// Check issue where unexpected whitespaces in SAS files led to a parsing error
|
2016-11-29 06:03:05 +01:00
|
|
|
CHECK_NOTHROW(plasp::sas::Description::fromFile("data/issues/issue-6.sas"));
|
2016-08-30 18:47:12 +02:00
|
|
|
}
|