Skip to content

Commit

Permalink
add tests for theories
Browse files Browse the repository at this point in the history
  • Loading branch information
gciatto committed Jun 20, 2020
1 parent b2de378 commit 02bab83
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ private constructor(
)

override fun get(clause: Clause): Sequence<Clause> =
clauses.filter { it matches clause }.asSequence()
clauses.filter {
it matches clause
}.asSequence()

override fun assertA(clause: Clause): Theory =
ListedTheory(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,25 +57,27 @@ 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(),
getClauses(collection, member(Atom.of("a"), LogicList.of(Atom.of("a"))))
)
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(),
Expand All @@ -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)
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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]
Expand Down Expand Up @@ -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))

Expand Down
Original file line number Diff line number Diff line change
@@ -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.*

/**
Expand Down Expand Up @@ -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
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

0 comments on commit 02bab83

Please sign in to comment.