From 0455535198f429c09d006276abf7b15d48b2d97a Mon Sep 17 00:00:00 2001 From: Davide Greco Date: Thu, 27 Aug 2020 18:13:32 +0200 Subject: [PATCH] Test Atomic implementation --- .../solve/systemtest/TestClassicAtomic.kt | 74 +++++++++++++++ .../it/unibo/tuprolog/solve/TestAtomic.kt | 77 ++++++++++++++++ .../it/unibo/tuprolog/solve/TestAtomicImpl.kt | 91 +++++++++++++++++++ 3 files changed, 242 insertions(+) create mode 100644 solve-classic/src/commonTest/kotlin/it/unibo/tuprolog/solve/systemtest/TestClassicAtomic.kt create mode 100644 test-solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/TestAtomic.kt create mode 100644 test-solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/TestAtomicImpl.kt diff --git a/solve-classic/src/commonTest/kotlin/it/unibo/tuprolog/solve/systemtest/TestClassicAtomic.kt b/solve-classic/src/commonTest/kotlin/it/unibo/tuprolog/solve/systemtest/TestClassicAtomic.kt new file mode 100644 index 000000000..c84ec23c0 --- /dev/null +++ b/solve-classic/src/commonTest/kotlin/it/unibo/tuprolog/solve/systemtest/TestClassicAtomic.kt @@ -0,0 +1,74 @@ +package it.unibo.tuprolog.solve.systemtest + +import it.unibo.tuprolog.solve.* +import it.unibo.tuprolog.solve.channel.InputChannel +import it.unibo.tuprolog.solve.channel.OutputChannel +import it.unibo.tuprolog.solve.exception.PrologWarning +import it.unibo.tuprolog.solve.library.AliasedLibrary +import it.unibo.tuprolog.solve.library.Libraries +import it.unibo.tuprolog.solve.stdlib.DefaultBuiltins +import it.unibo.tuprolog.theory.Theory +import kotlin.test.Test + +class TestClassicAtomic : TestAtomic, SolverFactory{ + private val prototype = TestAtomic.prototype(this) + + override val defaultBuiltins: AliasedLibrary + get() = DefaultBuiltins + + override fun solverOf( + libraries: Libraries, + flags: FlagStore, + staticKb: Theory, + dynamicKb: Theory, + stdIn: InputChannel, + stdOut: OutputChannel, + stdErr: OutputChannel, + warnings: OutputChannel + ) = Solver.classic( + libraries, flags, staticKb, dynamicKb, stdIn, stdOut, stdErr, warnings + ) + + override fun mutableSolverOf( + libraries: Libraries, + flags: FlagStore, + staticKb: Theory, + dynamicKb: Theory, + stdIn: InputChannel, + stdOut: OutputChannel, + stdErr: OutputChannel, + warnings: OutputChannel + ) = MutableSolver.classic( + libraries, flags, staticKb, dynamicKb, stdIn, stdOut, stdErr, warnings + ) + + @Test + override fun testAtomicAtom(){ + prototype.testAtomicAtom() + } + + @Test + override fun testAtomicAofB(){ + prototype.testAtomicAofB() + } + + @Test + override fun testAtomicVar(){ + prototype.testAtomicVar() + } + + @Test + override fun testAtomicEmptyList(){ + prototype.testAtomicEmptyList() + } + + @Test + override fun testAtomicNum(){ + prototype.testAtomicNum() + } + + @Test + override fun testAtomicNumDec(){ + prototype.testAtomicNumDec() + } +} \ No newline at end of file diff --git a/test-solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/TestAtomic.kt b/test-solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/TestAtomic.kt new file mode 100644 index 000000000..0bf19e222 --- /dev/null +++ b/test-solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/TestAtomic.kt @@ -0,0 +1,77 @@ +package it.unibo.tuprolog.solve + +/** + * Tests of atomic + */ +interface TestAtomic { + companion object{ + fun prototype(solverFactory: SolverFactory): TestAtomic = + TestAtomicImpl(solverFactory) + } + + /** A short test max duration */ + val shortDuration: TimeDuration + get() = 250L + + /** A medium test max duration */ + val mediumDuration: TimeDuration + get() = 2 * shortDuration + + /** A long test max duration */ + val longDuration: TimeDuration + get() = 4 * mediumDuration + + /** + * Tests the queries + * ```prolog + * ?- atomic(atom). + * ``` + * succeeds on a solver initialized with default built-ins and with and empty theory. + */ + fun testAtomicAtom() + + /** + * Tests the queries + * ```prolog + * ?- atomic(a(b)). + * ``` + * fails on a solver initialized with default built-ins and with and empty theory. + */ + fun testAtomicAofB() + + /** + * Tests the queries + * ```prolog + * ?- atomic(Var). + * ``` + * fails on a solver initialized with default built-ins and with and empty theory. + */ + fun testAtomicVar() + + /** + * Tests the queries + * ```prolog + * ?- atomic([]). + * ``` + * succeeds on a solver initialized with default built-ins and with and empty theory. + */ + fun testAtomicEmptyList() + + /** + * Tests the queries + * ```prolog + * ?- atomic(6). + * ``` + * succeeds on a solver initialized with default built-ins and with and empty theory. + */ + fun testAtomicNum() + + /** + * Tests the queries + * ```prolog + * ?- atomic(3.3). + * ``` + * succeeds on a solver initialized with default built-ins and with and empty theory. + */ + fun testAtomicNumDec() +} \ No newline at end of file diff --git a/test-solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/TestAtomicImpl.kt b/test-solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/TestAtomicImpl.kt new file mode 100644 index 000000000..574cffec9 --- /dev/null +++ b/test-solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/TestAtomicImpl.kt @@ -0,0 +1,91 @@ +package it.unibo.tuprolog.solve + +import it.unibo.tuprolog.dsl.theory.prolog +import kotlin.collections.listOf as ktListOf + +internal class TestAtomicImpl(private val solverFactory: SolverFactory) : TestAtomic { + + override fun testAtomicAtom() { + prolog { + val solver = solverFactory.solverWithDefaultBuiltins() + + var query = atomic("atom") + var solutions = solver.solve(query, mediumDuration).toList() + + assertSolutionEquals( + ktListOf(query.yes()), + solutions + ) + } + } + + override fun testAtomicAofB() { + prolog { + val solver = solverFactory.solverWithDefaultBuiltins() + + var query = atomic("a"("b")) + var solutions = solver.solve(query, mediumDuration).toList() + + assertSolutionEquals( + ktListOf(query.no()), + solutions + ) + } + } + + override fun testAtomicVar() { + prolog { + val solver = solverFactory.solverWithDefaultBuiltins() + + var query = atomic("Var") + var solutions = solver.solve(query, mediumDuration).toList() + + assertSolutionEquals( + ktListOf(query.no()), + solutions + ) + } + } + + override fun testAtomicEmptyList() { + prolog { + val solver = solverFactory.solverWithDefaultBuiltins() + + var query = atomic(emptyList) + var solutions = solver.solve(query, mediumDuration).toList() + + assertSolutionEquals( + ktListOf(query.yes()), + solutions + ) + } + } + + override fun testAtomicNum() { + prolog { + val solver = solverFactory.solverWithDefaultBuiltins() + + var query = atomic(6) + var solutions = solver.solve(query, mediumDuration).toList() + + assertSolutionEquals( + ktListOf(query.yes()), + solutions + ) + } + } + + override fun testAtomicNumDec() { + prolog { + val solver = solverFactory.solverWithDefaultBuiltins() + + var query = atomic(3.3) + var solutions = solver.solve(query, mediumDuration).toList() + + assertSolutionEquals( + ktListOf(query.yes()), + solutions + ) + } + } +} \ No newline at end of file