diff --git a/CMakeLists.txt b/CMakeLists.txt index 0a50346..a7e7f0e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,3 +13,6 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) add_subdirectory(src) add_subdirectory(apps) +if(BUILD_TESTS) + add_subdirectory(tests) +endif(BUILD_TESTS) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..b5a66dd --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,23 @@ +set(target tests) + +file(GLOB core_sources "*.cpp") + +find_package(GTest) + +INCLUDE_DIRECTORIES( + ${GTEST_INCLUDE_DIRS} + ${Boost_INCLUDE_DIRS} + ${PROJECT_SOURCE_DIR}/include +) + +set(libraries + ${Boost_LIBRARIES} + ${GTEST_BOTH_LIBRARIES} + plasp +) + +file(COPY data DESTINATION ${CMAKE_BINARY_DIR}) + +add_executable(${target} ${core_sources}) +target_link_libraries(${target} ${libraries}) +GTEST_ADD_TESTS(tests "" ${core_sources}) diff --git a/tests/TestSASParser.cpp b/tests/TestSASParser.cpp new file mode 100644 index 0000000..e9015d2 --- /dev/null +++ b/tests/TestSASParser.cpp @@ -0,0 +1,104 @@ +#include + +#include +#include +#include +#include + +#include + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +class SASParserTests : public ::testing::Test +{ + protected: + SASParserTests() + : m_philosophersTestFile(readFile("data/philosophers.sas")) + { + } + + static std::stringstream readFile(const std::string &path) + { + std::ifstream fileStream(path, std::ios::in); + + std::stringstream outputStream; + + if (!fileStream.is_open()) + throw std::runtime_error("Could not open file \"" + path + "\""); + + outputStream << fileStream.rdbuf(); + + return outputStream; + } + + std::stringstream m_philosophersTestFile; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +TEST_F(SASParserTests, ParseValidSASFile) +{ + try + { + const auto description = plasp::sas::Description::fromStream(m_philosophersTestFile); + + ASSERT_FALSE(description.usesActionCosts()); + + ASSERT_EQ(description.variables().size(), 37); + ASSERT_EQ(description.variables()[0].axiomLayer, -1); + ASSERT_EQ(description.variables()[36].axiomLayer, -1); + + ASSERT_EQ(description.mutexGroups().size(), 8); + ASSERT_EQ(description.mutexGroups()[0].facts.size(), 9); + ASSERT_EQ(&description.mutexGroups()[0].facts[0].value, &description.variables()[0].values[0]); + ASSERT_EQ(description.mutexGroups()[7].facts.size(), 2); + ASSERT_EQ(&description.mutexGroups()[7].facts[1].value, &description.variables()[34].values[1]); + + ASSERT_EQ(description.initialStateFacts().size(), 37); + ASSERT_EQ(&description.initialStateFacts()[0].value, &description.variables()[0].values[8]); + ASSERT_EQ(&description.initialStateFacts()[36].value, &description.variables()[36].values[1]); + + ASSERT_EQ(description.goalFacts().size(), 2); + ASSERT_EQ(&description.goalFacts()[0].value, &description.variables()[6].values[0]); + ASSERT_EQ(&description.goalFacts()[1].value, &description.variables()[7].values[0]); + + ASSERT_EQ(description.operators().size(), 34); + ASSERT_EQ(description.operators()[0].preconditions.size(), 3); + ASSERT_EQ(&description.operators()[0].preconditions[0].value, &description.variables()[4].values[4]); + ASSERT_EQ(&description.operators()[0].preconditions[1].value, &description.variables()[16].values[1]); + ASSERT_EQ(&description.operators()[0].preconditions[2].value, &description.variables()[0].values[8]); + ASSERT_EQ(description.operators()[0].effects.size(), 1); + ASSERT_EQ(description.operators()[0].effects[0].conditions.size(), 0); + ASSERT_EQ(&description.operators()[0].effects[0].postcondition.value, &description.variables()[0].values[0]); + ASSERT_EQ(description.operators()[33].preconditions.size(), 2); + ASSERT_EQ(&description.operators()[33].preconditions[0].value, &description.variables()[1].values[3]); + ASSERT_EQ(&description.operators()[33].preconditions[1].value, &description.variables()[2].values[2]); + ASSERT_EQ(description.operators()[33].effects.size(), 3); + ASSERT_EQ(description.operators()[33].effects[0].conditions.size(), 0); + ASSERT_EQ(&description.operators()[33].effects[0].postcondition.value, &description.variables()[1].values[7]); + ASSERT_EQ(&description.operators()[33].effects[2].postcondition.value, &description.variables()[35].values[0]); + + ASSERT_EQ(description.axiomRules().size(), 33); + ASSERT_EQ(description.axiomRules()[0].conditions.size(), 4); + ASSERT_EQ(&description.axiomRules()[0].conditions[0].value, &description.variables()[0].values[0]); + ASSERT_EQ(&description.axiomRules()[0].conditions[2].value, &description.variables()[27].values[0]); + ASSERT_EQ(&description.axiomRules()[0].conditions[3].value, &description.variables()[8].values[1]); + ASSERT_EQ(&description.axiomRules()[0].postcondition.value, &description.variables()[8].values[0]); + ASSERT_EQ(description.axiomRules()[32].conditions.size(), 2); + ASSERT_EQ(&description.axiomRules()[32].conditions[0].value, &description.variables()[15].values[0]); + ASSERT_EQ(&description.axiomRules()[32].conditions[1].value, &description.variables()[25].values[0]); + ASSERT_EQ(&description.axiomRules()[32].postcondition.value, &description.variables()[25].values[1]); + } + catch (const std::exception &e) + { + FAIL() << e.what(); + } +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +int main(int argc, char **argv) +{ + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} diff --git a/tests/data/philosophers.sas b/tests/data/philosophers.sas new file mode 100644 index 0000000..25e71c9 --- /dev/null +++ b/tests/data/philosophers.sas @@ -0,0 +1,915 @@ +begin_version +3 +end_version +begin_metric +0 +end_metric +37 +begin_variable +var0 +-1 +9 +Atom activate(philosopher-0, forks--pid-rfork) +Atom activate(philosopher-0, forks--pid-wfork) +Atom activate(philosopher-0, forks-__-pidp1__2_-rfork) +Atom activate(philosopher-0, forks-__-pidp1__2_-wfork) +Atom enabled(philosopher-0, forks--pid-rfork) +Atom enabled(philosopher-0, forks--pid-wfork) +Atom enabled(philosopher-0, forks-__-pidp1__2_-rfork) +Atom enabled(philosopher-0, forks-__-pidp1__2_-wfork) +Atom pending(philosopher-0) +end_variable +begin_variable +var1 +-1 +9 +Atom activate(philosopher-1, forks--pid-rfork) +Atom activate(philosopher-1, forks--pid-wfork) +Atom activate(philosopher-1, forks-__-pidp1__2_-rfork) +Atom activate(philosopher-1, forks-__-pidp1__2_-wfork) +Atom enabled(philosopher-1, forks--pid-rfork) +Atom enabled(philosopher-1, forks--pid-wfork) +Atom enabled(philosopher-1, forks-__-pidp1__2_-rfork) +Atom enabled(philosopher-1, forks-__-pidp1__2_-wfork) +Atom pending(philosopher-1) +end_variable +begin_variable +var2 +-1 +3 +Atom advance-head(forks-0-) +Atom advance-tail(forks-0-) +Atom settled(forks-0-) +end_variable +begin_variable +var3 +-1 +3 +Atom advance-head(forks-1-) +Atom advance-tail(forks-1-) +Atom settled(forks-1-) +end_variable +begin_variable +var4 +-1 +5 +Atom at-process(philosopher-0, state-1) +Atom at-process(philosopher-0, state-3) +Atom at-process(philosopher-0, state-4) +Atom at-process(philosopher-0, state-5) +Atom at-process(philosopher-0, state-6) +end_variable +begin_variable +var5 +-1 +5 +Atom at-process(philosopher-1, state-1) +Atom at-process(philosopher-1, state-3) +Atom at-process(philosopher-1, state-4) +Atom at-process(philosopher-1, state-5) +Atom at-process(philosopher-1, state-6) +end_variable +begin_variable +var6 +0 +2 +Atom blocked(philosopher-0) +NegatedAtom blocked(philosopher-0) +end_variable +begin_variable +var7 +0 +2 +Atom blocked(philosopher-1) +NegatedAtom blocked(philosopher-1) +end_variable +begin_variable +var8 +0 +2 +Atom blocked-trans(philosopher-0, forks--pid-rfork) +NegatedAtom blocked-trans(philosopher-0, forks--pid-rfork) +end_variable +begin_variable +var9 +0 +2 +Atom blocked-trans(philosopher-0, forks--pid-wfork) +NegatedAtom blocked-trans(philosopher-0, forks--pid-wfork) +end_variable +begin_variable +var10 +0 +2 +Atom blocked-trans(philosopher-0, forks-__-pidp1__2_-rfork) +NegatedAtom blocked-trans(philosopher-0, forks-__-pidp1__2_-rfork) +end_variable +begin_variable +var11 +0 +2 +Atom blocked-trans(philosopher-0, forks-__-pidp1__2_-wfork) +NegatedAtom blocked-trans(philosopher-0, forks-__-pidp1__2_-wfork) +end_variable +begin_variable +var12 +0 +2 +Atom blocked-trans(philosopher-1, forks--pid-rfork) +NegatedAtom blocked-trans(philosopher-1, forks--pid-rfork) +end_variable +begin_variable +var13 +0 +2 +Atom blocked-trans(philosopher-1, forks--pid-wfork) +NegatedAtom blocked-trans(philosopher-1, forks--pid-wfork) +end_variable +begin_variable +var14 +0 +2 +Atom blocked-trans(philosopher-1, forks-__-pidp1__2_-rfork) +NegatedAtom blocked-trans(philosopher-1, forks-__-pidp1__2_-rfork) +end_variable +begin_variable +var15 +0 +2 +Atom blocked-trans(philosopher-1, forks-__-pidp1__2_-wfork) +NegatedAtom blocked-trans(philosopher-1, forks-__-pidp1__2_-wfork) +end_variable +begin_variable +var16 +0 +2 +Atom new-axiom@0() +NegatedAtom new-axiom@0() +end_variable +begin_variable +var17 +0 +2 +Atom new-axiom@1(philosopher-0, philosopher, state-1) +NegatedAtom new-axiom@1(philosopher-0, philosopher, state-1) +end_variable +begin_variable +var18 +0 +2 +Atom new-axiom@1(philosopher-0, philosopher, state-3) +NegatedAtom new-axiom@1(philosopher-0, philosopher, state-3) +end_variable +begin_variable +var19 +0 +2 +Atom new-axiom@1(philosopher-0, philosopher, state-4) +NegatedAtom new-axiom@1(philosopher-0, philosopher, state-4) +end_variable +begin_variable +var20 +0 +2 +Atom new-axiom@1(philosopher-0, philosopher, state-5) +NegatedAtom new-axiom@1(philosopher-0, philosopher, state-5) +end_variable +begin_variable +var21 +0 +2 +Atom new-axiom@1(philosopher-0, philosopher, state-6) +NegatedAtom new-axiom@1(philosopher-0, philosopher, state-6) +end_variable +begin_variable +var22 +0 +2 +Atom new-axiom@1(philosopher-1, philosopher, state-1) +NegatedAtom new-axiom@1(philosopher-1, philosopher, state-1) +end_variable +begin_variable +var23 +0 +2 +Atom new-axiom@1(philosopher-1, philosopher, state-3) +NegatedAtom new-axiom@1(philosopher-1, philosopher, state-3) +end_variable +begin_variable +var24 +0 +2 +Atom new-axiom@1(philosopher-1, philosopher, state-4) +NegatedAtom new-axiom@1(philosopher-1, philosopher, state-4) +end_variable +begin_variable +var25 +0 +2 +Atom new-axiom@1(philosopher-1, philosopher, state-5) +NegatedAtom new-axiom@1(philosopher-1, philosopher, state-5) +end_variable +begin_variable +var26 +0 +2 +Atom new-axiom@1(philosopher-1, philosopher, state-6) +NegatedAtom new-axiom@1(philosopher-1, philosopher, state-6) +end_variable +begin_variable +var27 +-1 +2 +Atom queue-head-msg(forks-0-, empty) +NegatedAtom queue-head-msg(forks-0-, empty) +end_variable +begin_variable +var28 +-1 +2 +Atom queue-head-msg(forks-0-, fork) +NegatedAtom queue-head-msg(forks-0-, fork) +end_variable +begin_variable +var29 +-1 +2 +Atom queue-head-msg(forks-1-, empty) +NegatedAtom queue-head-msg(forks-1-, empty) +end_variable +begin_variable +var30 +-1 +2 +Atom queue-head-msg(forks-1-, fork) +NegatedAtom queue-head-msg(forks-1-, fork) +end_variable +begin_variable +var31 +-1 +2 +Atom queue-msg(forks-0-, qs-0, fork) +NegatedAtom queue-msg(forks-0-, qs-0, fork) +end_variable +begin_variable +var32 +-1 +2 +Atom queue-msg(forks-1-, qs-0, fork) +NegatedAtom queue-msg(forks-1-, qs-0, fork) +end_variable +begin_variable +var33 +-1 +2 +Atom queue-size(forks-0-, one) +Atom queue-size(forks-0-, zero) +end_variable +begin_variable +var34 +-1 +2 +Atom queue-size(forks-1-, one) +Atom queue-size(forks-1-, zero) +end_variable +begin_variable +var35 +-1 +2 +Atom queue-tail-msg(forks-0-, fork) +NegatedAtom queue-tail-msg(forks-0-, fork) +end_variable +begin_variable +var36 +-1 +2 +Atom queue-tail-msg(forks-1-, fork) +NegatedAtom queue-tail-msg(forks-1-, fork) +end_variable +8 +begin_mutex_group +9 +0 0 +0 1 +0 2 +0 3 +0 4 +0 5 +0 6 +0 7 +0 8 +end_mutex_group +begin_mutex_group +9 +1 0 +1 1 +1 2 +1 3 +1 4 +1 5 +1 6 +1 7 +1 8 +end_mutex_group +begin_mutex_group +3 +2 0 +2 1 +2 2 +end_mutex_group +begin_mutex_group +3 +3 0 +3 1 +3 2 +end_mutex_group +begin_mutex_group +5 +4 0 +4 1 +4 2 +4 3 +4 4 +end_mutex_group +begin_mutex_group +5 +5 0 +5 1 +5 2 +5 3 +5 4 +end_mutex_group +begin_mutex_group +2 +33 0 +33 1 +end_mutex_group +begin_mutex_group +2 +34 0 +34 1 +end_mutex_group +begin_state +8 +8 +2 +2 +0 +0 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +1 +0 +1 +1 +1 +1 +1 +1 +1 +end_state +begin_goal +2 +6 0 +7 0 +end_goal +34 +begin_operator +activate-trans philosopher-0 philosopher forks--pid-rfork state-6 state-3 +2 +4 4 +16 1 +1 +0 0 8 0 +1 +end_operator +begin_operator +activate-trans philosopher-0 philosopher forks--pid-wfork state-1 state-6 +2 +4 0 +16 1 +1 +0 0 8 1 +1 +end_operator +begin_operator +activate-trans philosopher-0 philosopher forks--pid-wfork state-4 state-5 +2 +4 2 +16 1 +1 +0 0 8 1 +1 +end_operator +begin_operator +activate-trans philosopher-0 philosopher forks-__-pidp1__2_-rfork state-3 state-4 +2 +4 1 +16 1 +1 +0 0 8 2 +1 +end_operator +begin_operator +activate-trans philosopher-0 philosopher forks-__-pidp1__2_-wfork state-5 state-6 +2 +4 3 +16 1 +1 +0 0 8 3 +1 +end_operator +begin_operator +activate-trans philosopher-1 philosopher forks--pid-rfork state-6 state-3 +2 +5 4 +16 1 +1 +0 1 8 0 +1 +end_operator +begin_operator +activate-trans philosopher-1 philosopher forks--pid-wfork state-1 state-6 +2 +5 0 +16 1 +1 +0 1 8 1 +1 +end_operator +begin_operator +activate-trans philosopher-1 philosopher forks--pid-wfork state-4 state-5 +2 +5 2 +16 1 +1 +0 1 8 1 +1 +end_operator +begin_operator +activate-trans philosopher-1 philosopher forks-__-pidp1__2_-rfork state-3 state-4 +2 +5 1 +16 1 +1 +0 1 8 2 +1 +end_operator +begin_operator +activate-trans philosopher-1 philosopher forks-__-pidp1__2_-wfork state-5 state-6 +2 +5 3 +16 1 +1 +0 1 8 3 +1 +end_operator +begin_operator +advance-empty-queue-tail forks-0- queue-1 qs-0 qs-0 fork empty zero one +1 +35 0 +5 +0 2 1 2 +0 27 0 1 +0 28 -1 0 +0 31 -1 0 +0 33 1 0 +1 +end_operator +begin_operator +advance-empty-queue-tail forks-0- queue-1 qs-0 qs-0 fork fork zero one +2 +28 0 +35 0 +3 +0 2 1 2 +0 31 -1 0 +0 33 1 0 +1 +end_operator +begin_operator +advance-empty-queue-tail forks-1- queue-1 qs-0 qs-0 fork empty zero one +1 +36 0 +5 +0 3 1 2 +0 29 0 1 +0 30 -1 0 +0 32 -1 0 +0 34 1 0 +1 +end_operator +begin_operator +advance-empty-queue-tail forks-1- queue-1 qs-0 qs-0 fork fork zero one +2 +30 0 +36 0 +3 +0 3 1 2 +0 32 -1 0 +0 34 1 0 +1 +end_operator +begin_operator +advance-queue-head forks-0- queue-1 qs-0 qs-0 fork one zero +1 +31 0 +3 +0 2 0 2 +0 28 -1 0 +0 33 0 1 +1 +end_operator +begin_operator +advance-queue-head forks-1- queue-1 qs-0 qs-0 fork one zero +1 +32 0 +3 +0 3 0 2 +0 30 -1 0 +0 34 0 1 +1 +end_operator +begin_operator +perform-trans philosopher-0 philosopher forks--pid-rfork state-6 state-3 +1 +16 1 +2 +0 0 4 8 +0 4 4 1 +1 +end_operator +begin_operator +perform-trans philosopher-0 philosopher forks--pid-wfork state-1 state-6 +1 +16 1 +2 +0 0 5 8 +0 4 0 4 +1 +end_operator +begin_operator +perform-trans philosopher-0 philosopher forks--pid-wfork state-4 state-5 +1 +16 1 +2 +0 0 5 8 +0 4 2 3 +1 +end_operator +begin_operator +perform-trans philosopher-0 philosopher forks-__-pidp1__2_-rfork state-3 state-4 +1 +16 1 +2 +0 0 6 8 +0 4 1 2 +1 +end_operator +begin_operator +perform-trans philosopher-0 philosopher forks-__-pidp1__2_-wfork state-5 state-6 +1 +16 1 +2 +0 0 7 8 +0 4 3 4 +1 +end_operator +begin_operator +perform-trans philosopher-1 philosopher forks--pid-rfork state-6 state-3 +1 +16 1 +2 +0 1 4 8 +0 5 4 1 +1 +end_operator +begin_operator +perform-trans philosopher-1 philosopher forks--pid-wfork state-1 state-6 +1 +16 1 +2 +0 1 5 8 +0 5 0 4 +1 +end_operator +begin_operator +perform-trans philosopher-1 philosopher forks--pid-wfork state-4 state-5 +1 +16 1 +2 +0 1 5 8 +0 5 2 3 +1 +end_operator +begin_operator +perform-trans philosopher-1 philosopher forks-__-pidp1__2_-rfork state-3 state-4 +1 +16 1 +2 +0 1 6 8 +0 5 1 2 +1 +end_operator +begin_operator +perform-trans philosopher-1 philosopher forks-__-pidp1__2_-wfork state-5 state-6 +1 +16 1 +2 +0 1 7 8 +0 5 3 4 +1 +end_operator +begin_operator +queue-read philosopher-0 forks--pid-rfork forks-0- fork +1 +28 0 +2 +0 0 0 4 +0 2 2 0 +1 +end_operator +begin_operator +queue-read philosopher-0 forks-__-pidp1__2_-rfork forks-1- fork +1 +30 0 +2 +0 0 2 6 +0 3 2 0 +1 +end_operator +begin_operator +queue-read philosopher-1 forks--pid-rfork forks-1- fork +1 +30 0 +2 +0 1 0 4 +0 3 2 0 +1 +end_operator +begin_operator +queue-read philosopher-1 forks-__-pidp1__2_-rfork forks-0- fork +1 +28 0 +2 +0 1 2 6 +0 2 2 0 +1 +end_operator +begin_operator +queue-write philosopher-0 forks--pid-wfork forks-0- fork +0 +3 +0 0 1 5 +0 2 2 1 +0 35 -1 0 +1 +end_operator +begin_operator +queue-write philosopher-0 forks-__-pidp1__2_-wfork forks-1- fork +0 +3 +0 0 3 7 +0 3 2 1 +0 36 -1 0 +1 +end_operator +begin_operator +queue-write philosopher-1 forks--pid-wfork forks-1- fork +0 +3 +0 1 1 5 +0 3 2 1 +0 36 -1 0 +1 +end_operator +begin_operator +queue-write philosopher-1 forks-__-pidp1__2_-wfork forks-0- fork +0 +3 +0 1 3 7 +0 2 2 1 +0 35 -1 0 +1 +end_operator +33 +begin_rule +3 +0 0 +2 2 +27 0 +8 1 0 +end_rule +begin_rule +3 +0 0 +2 2 +33 1 +8 1 0 +end_rule +begin_rule +3 +0 1 +2 2 +33 0 +9 1 0 +end_rule +begin_rule +3 +0 2 +3 2 +29 0 +10 1 0 +end_rule +begin_rule +3 +0 2 +3 2 +34 1 +10 1 0 +end_rule +begin_rule +3 +0 3 +3 2 +34 0 +11 1 0 +end_rule +begin_rule +3 +1 0 +3 2 +29 0 +12 1 0 +end_rule +begin_rule +3 +1 0 +3 2 +34 1 +12 1 0 +end_rule +begin_rule +3 +1 1 +3 2 +34 0 +13 1 0 +end_rule +begin_rule +3 +1 2 +2 2 +27 0 +14 1 0 +end_rule +begin_rule +3 +1 2 +2 2 +33 1 +14 1 0 +end_rule +begin_rule +3 +1 3 +2 2 +33 0 +15 1 0 +end_rule +begin_rule +2 +2 2 +3 2 +16 0 1 +end_rule +begin_rule +2 +4 0 +17 1 +6 1 0 +end_rule +begin_rule +2 +4 1 +18 1 +6 1 0 +end_rule +begin_rule +2 +4 2 +19 1 +6 1 0 +end_rule +begin_rule +2 +4 3 +20 1 +6 1 0 +end_rule +begin_rule +2 +4 4 +21 1 +6 1 0 +end_rule +begin_rule +2 +5 0 +22 1 +7 1 0 +end_rule +begin_rule +2 +5 1 +23 1 +7 1 0 +end_rule +begin_rule +2 +5 2 +24 1 +7 1 0 +end_rule +begin_rule +2 +5 3 +25 1 +7 1 0 +end_rule +begin_rule +2 +5 4 +26 1 +7 1 0 +end_rule +begin_rule +1 +8 0 +21 0 1 +end_rule +begin_rule +1 +9 0 +17 0 1 +end_rule +begin_rule +1 +9 0 +19 0 1 +end_rule +begin_rule +1 +10 0 +18 0 1 +end_rule +begin_rule +1 +11 0 +20 0 1 +end_rule +begin_rule +1 +12 0 +26 0 1 +end_rule +begin_rule +1 +13 0 +22 0 1 +end_rule +begin_rule +1 +13 0 +24 0 1 +end_rule +begin_rule +1 +14 0 +23 0 1 +end_rule +begin_rule +1 +15 0 +25 0 1 +end_rule