From 02bab830ae08d377def55ab0414ab5c1095a7f88 Mon Sep 17 00:00:00 2001 From: Giovanni Ciatto Date: Sat, 20 Jun 2020 18:03:50 +0200 Subject: [PATCH] add tests for theories --- .../tuprolog/theory/impl/ListedTheory.kt | 4 +- .../PrototypeClauseCollectionTestImpl.kt | 36 +++++---- .../unibo/tuprolog/testutils/TheoryUtils.kt | 18 ++++- .../tuprolog/theory/PrototypeTheoryTest.kt | 80 +++++++++++++++++++ .../tuprolog/theory/impl/IndexedTheoryTest.kt | 20 +++++ .../tuprolog/theory/impl/ListedTheoryTest.kt | 20 +++++ 6 files changed, 160 insertions(+), 18 deletions(-) diff --git a/theory/src/commonMain/kotlin/it/unibo/tuprolog/theory/impl/ListedTheory.kt b/theory/src/commonMain/kotlin/it/unibo/tuprolog/theory/impl/ListedTheory.kt index a950d7347..5edcebd51 100644 --- a/theory/src/commonMain/kotlin/it/unibo/tuprolog/theory/impl/ListedTheory.kt +++ b/theory/src/commonMain/kotlin/it/unibo/tuprolog/theory/impl/ListedTheory.kt @@ -26,7 +26,9 @@ private constructor( ) override fun get(clause: Clause): Sequence = - clauses.filter { it matches clause }.asSequence() + clauses.filter { + it matches clause + }.asSequence() override fun assertA(clause: Clause): Theory = ListedTheory( diff --git a/theory/src/commonTest/kotlin/it/unibo/tuprolog/collections/prototypes/PrototypeClauseCollectionTestImpl.kt b/theory/src/commonTest/kotlin/it/unibo/tuprolog/collections/prototypes/PrototypeClauseCollectionTestImpl.kt index be66f268f..9a46b9fa7 100644 --- a/theory/src/commonTest/kotlin/it/unibo/tuprolog/collections/prototypes/PrototypeClauseCollectionTestImpl.kt +++ b/theory/src/commonTest/kotlin/it/unibo/tuprolog/collections/prototypes/PrototypeClauseCollectionTestImpl.kt @@ -57,12 +57,13 @@ internal abstract class PrototypeClauseCollectionTestImpl( assertClausesHaveSameLengthAndContent( memberClause.asSequence(), - getClauses(collection, Scope.empty { + getClauses( + collection, member( - listOf(structOf("a", varOf("X"))), - listOf(listOf(structOf("a", numOf(1)))) + LogicList.of(Struct.of("a", Var.of("X"))), + LogicList.of(LogicList.of(Struct.of("a", Integer.of(1)))) ) - }) + ) ) assertClausesHaveSameLengthAndContent( memberClause.asSequence(), @@ -70,12 +71,13 @@ internal abstract class PrototypeClauseCollectionTestImpl( ) assertClausesHaveSameLengthAndContent( sequenceOf(), - getClauses(collection, Scope.empty { + getClauses( + collection, member( - listOf(structOf("a", varOf("X"))), - listOf(listOf(structOf("b", numOf(1)))) + LogicList.of(Struct.of("a", Var.of("X"))), + LogicList.of(LogicList.of(Struct.of("b", Integer.of(1)))) ) - }) + ) ) assertClausesHaveSameLengthAndContent( sequenceOf(), @@ -88,12 +90,13 @@ internal abstract class PrototypeClauseCollectionTestImpl( assertClausesHaveSameLengthAndContent( memberClause.asSequence(), - retractClauses(collection, Scope.empty { + retractClauses( + collection, member( - listOf(structOf("a", varOf("X"))), - listOf(listOf(structOf("a", numOf(1)))) + LogicList.of(Struct.of("a", Var.of("X"))), + LogicList.of(LogicList.of(Struct.of("a", Integer.of(1)))) ) - }) + ) ) collection = collectionGenerator(memberClause) @@ -107,12 +110,13 @@ internal abstract class PrototypeClauseCollectionTestImpl( assertClausesHaveSameLengthAndContent( sequenceOf(), - retractClauses(collection, Scope.empty { + retractClauses( + collection, member( - listOf(structOf("a", varOf("X"))), - listOf(listOf(structOf("b", numOf(1)))) + LogicList.of(Struct.of("a", Var.of("X"))), + LogicList.of(LogicList.of(Struct.of("b", Integer.of(1)))) ) - }) + ) ) collection = collectionGenerator(memberClause) diff --git a/theory/src/commonTest/kotlin/it/unibo/tuprolog/testutils/TheoryUtils.kt b/theory/src/commonTest/kotlin/it/unibo/tuprolog/testutils/TheoryUtils.kt index 23c1da8e3..8c723a242 100644 --- a/theory/src/commonTest/kotlin/it/unibo/tuprolog/testutils/TheoryUtils.kt +++ b/theory/src/commonTest/kotlin/it/unibo/tuprolog/testutils/TheoryUtils.kt @@ -1,8 +1,8 @@ package it.unibo.tuprolog.testutils import it.unibo.tuprolog.core.* -import it.unibo.tuprolog.core.List as LogicList import it.unibo.tuprolog.theory.Theory +import it.unibo.tuprolog.core.List as LogicList /** * Utils singleton for testing [Theory] @@ -47,6 +47,22 @@ internal object TheoryUtils { } ) + internal val positiveMemberQueries = listOf( + member( + LogicList.of(Struct.of("a", Var.of("X"))), + LogicList.of(LogicList.of(Struct.of("a", Integer.of(1)))) + ), + member(Atom.of("a"), LogicList.of(Atom.of("a"))) + ) + + internal val negativeMemberQueries = listOf( + member( + LogicList.of(Struct.of("a", Var.of("X"))), + LogicList.of(LogicList.of(Struct.of("b", Integer.of(1)))) + ), + member(Atom.of("a"), LogicList.of(Atom.of("b"))) + ) + internal fun member(first: Term, second: Term): Fact = Fact.of(Struct.of("member", first, second)) diff --git a/theory/src/commonTest/kotlin/it/unibo/tuprolog/theory/PrototypeTheoryTest.kt b/theory/src/commonTest/kotlin/it/unibo/tuprolog/theory/PrototypeTheoryTest.kt index aa7ad2b4f..c3061cd5e 100644 --- a/theory/src/commonTest/kotlin/it/unibo/tuprolog/theory/PrototypeTheoryTest.kt +++ b/theory/src/commonTest/kotlin/it/unibo/tuprolog/theory/PrototypeTheoryTest.kt @@ -1,8 +1,14 @@ package it.unibo.tuprolog.theory import it.unibo.tuprolog.core.* +import it.unibo.tuprolog.testutils.ClauseAssertionUtils.assertClausesHaveSameLengthAndContent import it.unibo.tuprolog.testutils.ReteNodeUtils.assertClauseHeadPartialOrderingRespected import it.unibo.tuprolog.testutils.TheoryUtils +import it.unibo.tuprolog.testutils.TheoryUtils.deepClause +import it.unibo.tuprolog.testutils.TheoryUtils.deepQueries +import it.unibo.tuprolog.testutils.TheoryUtils.memberClause +import it.unibo.tuprolog.testutils.TheoryUtils.negativeMemberQueries +import it.unibo.tuprolog.testutils.TheoryUtils.positiveMemberQueries import kotlin.test.* /** @@ -341,4 +347,78 @@ class PrototypeTheoryTest( ) } + fun getTakesUnificationIntoAccount() { + val theory = theoryGenerator(memberClause) + + for (query in positiveMemberQueries) { + assertClausesHaveSameLengthAndContent( + memberClause.asSequence(), + theory[query] + ) + assertClausesHaveSameLengthAndContent( + memberClause.asSequence(), + theory[query.head] + ) + } + + for (query in negativeMemberQueries) { + assertClausesHaveSameLengthAndContent( + emptySequence(), + theory[query] + ) + assertClausesHaveSameLengthAndContent( + emptySequence(), + theory[query.head] + ) + } + } + + fun retractTakesUnificationIntoAccount() { + + for (query in positiveMemberQueries) { + var theory = theoryGenerator(memberClause) + assertClausesHaveSameLengthAndContent( + memberClause, + (theory.retract(query) as RetractResult.Success).clauses + ) + theory = theoryGenerator(memberClause) + assertClausesHaveSameLengthAndContent( + memberClause, + (theory.retract(query.head) as RetractResult.Success).clauses + ) + } + + for (query in negativeMemberQueries) { + val theory = theoryGenerator(memberClause) + assertEquals( + RetractResult.Failure(theory), + theory.retract(query) + ) + assertEquals( + RetractResult.Failure(theory), + theory.retract(query.head) + ) + } + } + + fun nestedGetWorksAtSeveralDepthLevels() { + val theory = theoryGenerator(deepClause) + + for (query in deepQueries) { + assertClausesHaveSameLengthAndContent( + deepClause.asSequence(), + theory[query] + ) + } + } + + fun nestedRetractWorksAtSeveralDepthLevels() { + for (query in deepQueries) { + val theory = theoryGenerator(deepClause) + assertClausesHaveSameLengthAndContent( + deepClause, + (theory.retract(query) as RetractResult.Success).clauses + ) + } + } } diff --git a/theory/src/commonTest/kotlin/it/unibo/tuprolog/theory/impl/IndexedTheoryTest.kt b/theory/src/commonTest/kotlin/it/unibo/tuprolog/theory/impl/IndexedTheoryTest.kt index 022d3095a..f2d938abf 100644 --- a/theory/src/commonTest/kotlin/it/unibo/tuprolog/theory/impl/IndexedTheoryTest.kt +++ b/theory/src/commonTest/kotlin/it/unibo/tuprolog/theory/impl/IndexedTheoryTest.kt @@ -226,4 +226,24 @@ internal class IndexedTheoryTest { fun iteratorReturnsCorrectInstance() { prototype.iteratorReturnsCorrectInstance() } + + @Test + fun getTakesUnificationIntoAccount() { + prototype.getTakesUnificationIntoAccount() + } + + @Test + fun retractTakesUnificationIntoAccount() { + prototype.retractTakesUnificationIntoAccount() + } + + @Test + fun nestedGetWorksAtSeveralDepthLevels() { + prototype.nestedGetWorksAtSeveralDepthLevels() + } + + @Test + fun nestedRetractWorksAtSeveralDepthLevels() { + prototype.nestedRetractWorksAtSeveralDepthLevels() + } } \ No newline at end of file diff --git a/theory/src/commonTest/kotlin/it/unibo/tuprolog/theory/impl/ListedTheoryTest.kt b/theory/src/commonTest/kotlin/it/unibo/tuprolog/theory/impl/ListedTheoryTest.kt index f9bf89ea4..eb8a34d75 100644 --- a/theory/src/commonTest/kotlin/it/unibo/tuprolog/theory/impl/ListedTheoryTest.kt +++ b/theory/src/commonTest/kotlin/it/unibo/tuprolog/theory/impl/ListedTheoryTest.kt @@ -226,4 +226,24 @@ internal class ListedTheoryTest { fun iteratorReturnsCorrectInstance() { prototype.iteratorReturnsCorrectInstance() } + + @Test + fun getTakesUnificationIntoAccount() { + prototype.getTakesUnificationIntoAccount() + } + + @Test + fun retractTakesUnificationIntoAccount() { + prototype.retractTakesUnificationIntoAccount() + } + + @Test + fun nestedGetWorksAtSeveralDepthLevels() { + prototype.nestedGetWorksAtSeveralDepthLevels() + } + + @Test + fun nestedRetractWorksAtSeveralDepthLevels() { + prototype.nestedRetractWorksAtSeveralDepthLevels() + } } \ No newline at end of file