From 9fe489de5388bacdc7e599187c152130eff1e9e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20L=C3=BChne?= Date: Sun, 18 Jun 2017 23:30:59 +0200 Subject: [PATCH] Added unit tests covering predicate signature matching. --- lib/pddlparse/tests/TestSignatureMatching.cpp | 149 ++++++++++++++++++ tests/data/test-cases/skeleton.pddl | 36 +++++ 2 files changed, 185 insertions(+) create mode 100644 lib/pddlparse/tests/TestSignatureMatching.cpp create mode 100644 tests/data/test-cases/skeleton.pddl diff --git a/lib/pddlparse/tests/TestSignatureMatching.cpp b/lib/pddlparse/tests/TestSignatureMatching.cpp new file mode 100644 index 0000000..8d505e8 --- /dev/null +++ b/lib/pddlparse/tests/TestSignatureMatching.cpp @@ -0,0 +1,149 @@ +#include + +#include + +#include +#include + +namespace fs = std::experimental::filesystem; + +const pddl::Context::WarningCallback ignoreWarnings = [](const auto &, const auto &){}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +TEST_CASE("[PDDL signature matching] The official PDDL instances are parsed correctly", "[PDDL signature matching]") +{ + std::stringstream input; + pddl::Tokenizer tokenizer; + pddl::Context context(std::move(tokenizer), ignoreWarnings); + const auto domainFile = fs::path("data") / "test-cases" / "skeleton.pddl"; + context.tokenizer.read(domainFile); + + SECTION("directly matching primitive types") + { + input + << "(:action test :parameters (?x - a1 ?y - a2 ?z - a3) :precondition (p1 ?x ?y ?z)))"; + context.tokenizer.read("input", input); + CHECK_NOTHROW(pddl::parseDescription(context)); + } + + SECTION("derived primitive types (direct children)") + { + input + << "(:action test :parameters (?x - b1 ?y - b2 ?z - b3) :precondition (p1 ?x ?y ?z)))"; + context.tokenizer.read("input", input); + CHECK_NOTHROW(pddl::parseDescription(context)); + } + + SECTION("derived primitive types (indirect children)") + { + input + << "(:action test :parameters (?x - c1 ?y - c2 ?z - c3) :precondition (p1 ?x ?y ?z)))"; + context.tokenizer.read("input", input); + CHECK_NOTHROW(pddl::parseDescription(context)); + } + + SECTION("incompatible primitive types") + { + input + << "(:action test :parameters (?x - a3 ?y - a2 ?z - a3) :precondition (p1 ?x ?y ?z)))"; + context.tokenizer.read("input", input); + CHECK_THROWS(pddl::parseDescription(context)); + } + + SECTION("unrelated primitive types") + { + input + << "(:action test :parameters (?x - b1 ?y - b2 ?z - b1) :precondition (p1 ?x ?y ?z)))"; + context.tokenizer.read("input", input); + CHECK_THROWS(pddl::parseDescription(context)); + } + + SECTION("incompatible parent types (1)") + { + input + << "(:action test :parameters (?x - object ?y - b2 ?z - c3) :precondition (p1 ?x ?y ?z)))"; + context.tokenizer.read("input", input); + CHECK_THROWS(pddl::parseDescription(context)); + } + + SECTION("incompatible parent types (2)") + { + input + << "(:action test :parameters (?x - a1 ?y - a2 ?z - a3) :precondition (p2 ?x ?y ?z)))"; + context.tokenizer.read("input", input); + CHECK_THROWS(pddl::parseDescription(context)); + } + + SECTION("matching types with multiple inheritance (1)") + { + input + << "(:action test :parameters (?x - b1 ?y - b2 ?z - cx) :precondition (p1 ?x ?y ?z)))"; + context.tokenizer.read("input", input); + CHECK_NOTHROW(pddl::parseDescription(context)); + } + + SECTION("matching types with multiple inheritance (2)") + { + input + << "(:action test :parameters (?x - bx ?y - b2 ?z - c3) :precondition (p1 ?x ?y ?z)))"; + context.tokenizer.read("input", input); + CHECK_NOTHROW(pddl::parseDescription(context)); + } + + SECTION("“either” type not matching primitive type") + { + input + << "(:action test :parameters (?x - either (a1 a2) ?y - b2 ?z - c3) :precondition (p1 ?x ?y ?z)))"; + context.tokenizer.read("input", input); + CHECK_THROWS(pddl::parseDescription(context)); + } + + SECTION("1-ary “either” type matching primitive type") + { + input + << "(:action test :parameters (?x - (either a1) ?y - a2 ?z - a3) :precondition (p1 ?x ?y ?z)))"; + context.tokenizer.read("input", input); + CHECK_NOTHROW(pddl::parseDescription(context)); + } + + SECTION("“either” type directly matching another “either” type") + { + input + << "(:action test :parameters (?x - (either a1 a2) ?y ?z - (either b1 b3)) :precondition (p3 ?x ?y ?z)))"; + context.tokenizer.read("input", input); + CHECK_NOTHROW(pddl::parseDescription(context)); + } + + SECTION("“either” type matching another “either” type via inheritance") + { + input + << "(:action test :parameters (?x - (either b1 a2) ?y ?z - (either c1 b3)) :precondition (p3 ?x ?y ?z)))"; + context.tokenizer.read("input", input); + CHECK_NOTHROW(pddl::parseDescription(context)); + } + + SECTION("“either” type incompatible with another “either” type") + { + input + << "(:action test :parameters (?x - (either b1 a2) ?y ?z - (either c2 b3)) :precondition (p3 ?x ?y ?z)))"; + context.tokenizer.read("input", input); + CHECK_THROWS(pddl::parseDescription(context)); + } + + SECTION("“either” type compatible with another “either” type via multiple inheritance") + { + input + << "(:action test :parameters (?x - (either cx c2) ?y ?z - (either cx c3)) :precondition (p3 ?x ?y ?z)))"; + context.tokenizer.read("input", input); + CHECK_NOTHROW(pddl::parseDescription(context)); + } + + SECTION("constants compatible with “either” type via multiple inheritance") + { + input + << "(:action test :precondition (p3 cbx cb1 cb3)))"; + context.tokenizer.read("input", input); + CHECK_NOTHROW(pddl::parseDescription(context)); + } +} diff --git a/tests/data/test-cases/skeleton.pddl b/tests/data/test-cases/skeleton.pddl new file mode 100644 index 0000000..ebd7638 --- /dev/null +++ b/tests/data/test-cases/skeleton.pddl @@ -0,0 +1,36 @@ +(define + (domain skeleton) + (:requirements :typing) + (:types + a1 a2 a3 - object + b1 - a1 + b2 - a2 + b3 - a3 + bx - a1 + bx - a2 + bx - a3 + c1 - b1 + c2 - b2 + c3 - b3 + cx - c1 + cx - c2 + cx - c3 + ) + (:predicates + (p1 ?x - a1 ?y - a2 ?z - a3) + (p2 ?x - a1 ?y - b2 ?z - c3) + (p3 ?x - (either a1 a2) ?y ?z - (either b1 b3)) + ) + (:constants + ca1 - a1 + ca2 - a2 + ca3 - a3 + cb1 - b1 + cb2 - b2 + cb3 - b3 + cbx - bx + cc1 - c1 + cc2 - c2 + cc3 - c3 + ccx - cx + )