Skip to content

Commit

Permalink
Test If-Then-Else implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
DavideGreco committed Aug 29, 2020
1 parent 0481b6d commit fda1f57
Show file tree
Hide file tree
Showing 4 changed files with 306 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package it.unibo.tuprolog.solve.systemtest

import it.unibo.tuprolog.solve.ClassicSolverFactory
import it.unibo.tuprolog.solve.SolverFactory
import it.unibo.tuprolog.solve.TestIfThenElse
import kotlin.test.Test

class TestClassicIfThenElse : TestIfThenElse, SolverFactory by ClassicSolverFactory {
private val prototype = TestIfThenElse.prototype(this)

@Test
override fun testIfTrueElseFail() {
prototype.testIfTrueElseFail()
}

@Test
override fun testIfFailElseTrue() {
prototype.testIfFailElseTrue()
}

@Test
override fun testIfTrueThenElseFail() {
prototype.testIfTrueThenElseFail()
}

@Test
override fun testIfFailElseFail() {
prototype.testIfFailElseFail()
}

@Test
override fun testIfXTrueElseX() {
prototype.testIfXTrueElseX()
}

@Test
override fun testIfFailElseX() {
prototype.testIfFailElseX()
}

@Test
override fun testIfThenElseOrWithDoubleSub() {
prototype.testIfThenElseOrWithDoubleSub()
}

@Test
override fun testIfOrElseTrue() {
prototype.testIfOrElseTrue()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package it.unibo.tuprolog.solve.systemtest

import it.unibo.tuprolog.solve.StreamsSolverFactory
import it.unibo.tuprolog.solve.SolverFactory
import it.unibo.tuprolog.solve.TestIfThenElse
import kotlin.test.Test

class TestStreamsIfThenElse : TestIfThenElse, SolverFactory by StreamsSolverFactory {
private val prototype = TestIfThenElse.prototype(this)

@Test
override fun testIfTrueElseFail() {
prototype.testIfTrueElseFail()
}

@Test
override fun testIfFailElseTrue() {
prototype.testIfFailElseTrue()
}

@Test
override fun testIfTrueThenElseFail() {
prototype.testIfTrueThenElseFail()
}

@Test
override fun testIfFailElseFail() {
prototype.testIfFailElseFail()
}

@Test
override fun testIfXTrueElseX() {
prototype.testIfXTrueElseX()
}

@Test
override fun testIfFailElseX() {
prototype.testIfFailElseX()
}

@Test
override fun testIfThenElseOrWithDoubleSub() {
prototype.testIfThenElseOrWithDoubleSub()
}

@Test
override fun testIfOrElseTrue() {
prototype.testIfOrElseTrue()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package it.unibo.tuprolog.solve

interface TestIfThenElse : SolverTest {
companion object {
fun prototype(solverFactory: SolverFactory): TestIfThenElse =
TestIfThenElseImpl(solverFactory)
}

/**
* Tests the queries
* ```prolog
* ?- ';'('->'(true, true), fail).
* ```
* succeeds on a solver initialized with default built-ins and with and empty theory.
*/
fun testIfTrueElseFail()

/**
* Tests the queries
* ```prolog
* ?- ';'('->'(fail, true), true).
* ```
* succeeds on a solver initialized with default built-ins and with and empty theory.
*/
fun testIfFailElseTrue()

/**
* Tests the queries
* ```prolog
* ?- ';'('->'(true, fail), fail).
* ```
* fails on a solver initialized with default built-ins and with and empty theory.
*/
fun testIfTrueThenElseFail()

/**
* Tests the queries
* ```prolog
* ?- ';'('->'(fail, true), fail).
* ```
* fails on a solver initialized with default built-ins and with and empty theory,
*/
fun testIfFailElseFail()

/**
* Tests the queries
* ```prolog
* ?- ';'('->'(true, X=1), X=2).
* ```
* succeeds on a solver initialized with default built-ins and with and empty theory,
* producing 1 solution which binds variable `X` to `1`.
*/
fun testIfXTrueElseX()

/**
* Tests the queries
* ```prolog
* ?- ';'('->'(fail, X=1), X=2).
* ```
* succeeds on a solver initialized with default built-ins and with and empty theory,
* producing 1 solution which binds variable`X` to `2` .
*/
fun testIfFailElseX()

/**
* Tests the queries
* ```prolog
* ?- ';'('->'(true, ';'(X=1, X=2)), true).
* ```
* succeeds on a solver initialized with default built-ins and with and empty theory,
* producing 2 solutions which binds variable `X` to `1` and `X` to `2`.
*/
fun testIfThenElseOrWithDoubleSub()

/**
* Tests the queries
* ```prolog
* ?- ';'('->'(';'(X=1, X=2), true), true).
* ```
* succeeds on a solver initialized with default built-ins and with and empty theory,
* producing 1 solution which binds variable `X` to `1`.
*/
fun testIfOrElseTrue()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package it.unibo.tuprolog.solve

import it.unibo.tuprolog.dsl.theory.prolog

internal class TestIfThenElseImpl(private val solverFactory: SolverFactory) : TestIfThenElse{
override fun testIfTrueElseFail() {
prolog {
val solver = solverFactory.solverWithDefaultBuiltins()

val query = ";"("->"(true, true), fail)
val solutions = solver.solve(query, mediumDuration).toList()

assertSolutionEquals(
kotlin.collections.listOf(query.yes()),
solutions
)
}
}

override fun testIfFailElseTrue() {
prolog {
val solver = solverFactory.solverWithDefaultBuiltins()

val query = ";"("->"(fail, true), true)
val solutions = solver.solve(query, mediumDuration).toList()

assertSolutionEquals(
kotlin.collections.listOf(query.yes()),
solutions
)
}
}

override fun testIfTrueThenElseFail() {
prolog {
val solver = solverFactory.solverWithDefaultBuiltins()

val query = ";"("->"(true, fail), fail)
val solutions = solver.solve(query, mediumDuration).toList()

assertSolutionEquals(
kotlin.collections.listOf(query.no()),
solutions
)
}
}

override fun testIfFailElseFail() {
prolog {
val solver = solverFactory.solverWithDefaultBuiltins()

val query = ";"("->"(fail, true), fail)
val solutions = solver.solve(query, mediumDuration).toList()

assertSolutionEquals(
kotlin.collections.listOf(query.no()),
solutions
)
}
}

override fun testIfXTrueElseX() {
prolog {
val solver = solverFactory.solverWithDefaultBuiltins()

val query = ";"("->"(true, ("X" `=` 1)), "X" `=` 2)
val solutions = solver.solve(query, mediumDuration).toList()

assertSolutionEquals(
kotlin.collections.listOf(query.yes("X" to 1)),
solutions
)
}
}

override fun testIfFailElseX() {
prolog {
val solver = solverFactory.solverWithDefaultBuiltins()

val query = ";"("->"(fail, ("X" `=` 1)), "X" `=` 2)
val solutions = solver.solve(query, mediumDuration).toList()

assertSolutionEquals(
kotlin.collections.listOf(query.yes("X" to 2)),
solutions
)
}
}

override fun testIfThenElseOrWithDoubleSub() {
prolog {
val solver = solverFactory.solverWithDefaultBuiltins()

val query = ";"("->"(true,("X" `=` 1) or ("X" `=` 2)), true)
val solutions = solver.solve(query, mediumDuration).toList()

assertSolutionEquals(
with(query) {
kotlin.collections.listOf(
yes("X" to 1),
yes("X" to 2)
)
},
solutions
)
}
}

override fun testIfOrElseTrue() {
prolog {
val solver = solverFactory.solverWithDefaultBuiltins()

val query = ";"("->"(("X" `=` 1) or ("X" `=` 2), true), true)
val solutions = solver.solve(query, mediumDuration).toList()

assertSolutionEquals(
kotlin.collections.listOf(query.yes("X" to 1)),
solutions
)
}
}
}

0 comments on commit fda1f57

Please sign in to comment.