From 3cc09f70c187632b57db0a2f8cdf2af7205f9865 Mon Sep 17 00:00:00 2001 From: Davide Greco Date: Fri, 28 Aug 2020 13:23:59 +0200 Subject: [PATCH] Test Term(diff,eq,gt,gt_eq,lt,lt_eq) implementation --- .../solve/systemtest/TestClassicTerm.kt | 42 ++ .../solve/systemtest/TestStreamsTerm.kt | 42 ++ .../it/unibo/tuprolog/solve/TestTerm.kt | 255 +++++++++++ .../it/unibo/tuprolog/solve/TestTermImpl.kt | 395 ++++++++++++++++++ 4 files changed, 734 insertions(+) create mode 100644 solve-classic/src/commonTest/kotlin/it/unibo/tuprolog/solve/systemtest/TestClassicTerm.kt create mode 100644 solve-streams/src/commonTest/kotlin/it/unibo/tuprolog/solve/systemtest/TestStreamsTerm.kt create mode 100644 test-solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/TestTerm.kt create mode 100644 test-solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/TestTermImpl.kt diff --git a/solve-classic/src/commonTest/kotlin/it/unibo/tuprolog/solve/systemtest/TestClassicTerm.kt b/solve-classic/src/commonTest/kotlin/it/unibo/tuprolog/solve/systemtest/TestClassicTerm.kt new file mode 100644 index 000000000..a250fa4ab --- /dev/null +++ b/solve-classic/src/commonTest/kotlin/it/unibo/tuprolog/solve/systemtest/TestClassicTerm.kt @@ -0,0 +1,42 @@ +package it.unibo.tuprolog.solve.systemtest + +import it.unibo.tuprolog.solve.ClassicSolverFactory +import it.unibo.tuprolog.solve.SolverFactory +import it.unibo.tuprolog.solve.TestInteger +import it.unibo.tuprolog.solve.TestTerm +import kotlin.test.Test + +class TestClassicTerm : TestTerm, SolverFactory by ClassicSolverFactory { + + private val prototype = TestTerm.prototype(this) + + @Test + override fun testTermDiff() { + prototype.testTermDiff() + } + + @Test + override fun testTermEq() { + prototype.testTermEq() + } + + @Test + override fun testTermGreaterThan() { + prototype.testTermGreaterThan() + } + + @Test + override fun testTermGreaterThanEq() { + prototype.testTermGreaterThanEq() + } + + @Test + override fun testTermLessThan() { + prototype.testTermLessThan() + } + + @Test + override fun testTermLessThanEq() { + prototype.testTermLessThanEq() + } +} \ No newline at end of file diff --git a/solve-streams/src/commonTest/kotlin/it/unibo/tuprolog/solve/systemtest/TestStreamsTerm.kt b/solve-streams/src/commonTest/kotlin/it/unibo/tuprolog/solve/systemtest/TestStreamsTerm.kt new file mode 100644 index 000000000..2a2f278d8 --- /dev/null +++ b/solve-streams/src/commonTest/kotlin/it/unibo/tuprolog/solve/systemtest/TestStreamsTerm.kt @@ -0,0 +1,42 @@ +package it.unibo.tuprolog.solve.systemtest + +import it.unibo.tuprolog.solve.StreamsSolverFactory +import it.unibo.tuprolog.solve.SolverFactory +import it.unibo.tuprolog.solve.TestInteger +import it.unibo.tuprolog.solve.TestTerm +import kotlin.test.Test + +class TestStreamsTerm : TestTerm, SolverFactory by StreamsSolverFactory { + + private val prototype = TestTerm.prototype(this) + + @Test + override fun testTermDiff() { + prototype.testTermDiff() + } + + @Test + override fun testTermEq() { + prototype.testTermEq() + } + + @Test + override fun testTermGreaterThan() { + prototype.testTermGreaterThan() + } + + @Test + override fun testTermGreaterThanEq() { + prototype.testTermGreaterThanEq() + } + + @Test + override fun testTermLessThan() { + prototype.testTermLessThan() + } + + @Test + override fun testTermLessThanEq() { + prototype.testTermLessThanEq() + } +} \ No newline at end of file diff --git a/test-solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/TestTerm.kt b/test-solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/TestTerm.kt new file mode 100644 index 000000000..fecae644a --- /dev/null +++ b/test-solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/TestTerm.kt @@ -0,0 +1,255 @@ +package it.unibo.tuprolog.solve + +/** + * Tests of Term diff, eq, gt, gt_eq, lt, lt_eq + */ +interface TestTerm : SolverTest { + companion object { + fun prototype(solverFactory: SolverFactory): TestTerm = + TestTermImpl(solverFactory) + } + + /** + * Tests the queries + * ```prolog + * ?- '\\=='(1,1). + * ``` + * fails on a solver initialized with default built-ins and with and empty theory. + * + * ```prolog + * ?- '\\=='(X,X). + * ``` + * fails on a solver initialized with default built-ins and with and empty theory. + * + * ```prolog + * ?- '\\=='(1,2). + * ``` + * succeeds on a solver initialized with default built-ins and with and empty theory. + * + * ```prolog + * ?- '\\=='(X,1). + * ``` + * succeeds on a solver initialized with default built-ins and with and empty theory. + * + * ```prolog + * ?- '\\=='(X,Y). + * ``` + * succeeds on a solver initialized with default built-ins and with and empty theory. + * + * ```prolog + * ?- '\\=='(_,_). + * ``` + * succeeds on a solver initialized with default built-ins and with and empty theory. + * + * ```prolog + * ?- '\\=='(X,a(X)). + * ``` + * succeeds on a solver initialized with default built-ins and with and empty theory. + * + * ```prolog + * ?- '\\=='(f(a),f(a)). + * ``` + * fails on a solver initialized with default built-ins and with and empty theory. + */ + fun testTermDiff() + + /** + * Tests the queries + * ```prolog + * ?- '=='(1,1). + * ``` + * succeeds on a solver initialized with default built-ins and with and empty theory. + * + * ```prolog + * ?- '=='(X,X). + * ``` + * succeeds on a solver initialized with default built-ins and with and empty theory. + * + * ```prolog + * ?- '=='(1,2). + * ``` + * fails on a solver initialized with default built-ins and with and empty theory. + * + * ```prolog + * ?- '=='(X,1). + * ``` + * fails on a solver initialized with default built-ins and with and empty theory. + * + * ```prolog + * ?- '=='(X,Y). + * ``` + * fails on a solver initialized with default built-ins and with and empty theory. + * + * ```prolog + * ?- '=='(_,_). + * ``` + * fails on a solver initialized with default built-ins and with and empty theory. + * + * ```prolog + * ?- '=='(X,a(X)). + * ``` + * fails on a solver initialized with default built-ins and with and empty theory. + * + * ```prolog + * ?- '=='(f(a),f(a)). + * ``` + * succeeds on a solver initialized with default built-ins and with and empty theory. + */ + fun testTermEq() + + /** + * Tests the queries + * ```prolog + * ?- '@>'(1.0,1). + * ``` + * fails on a solver initialized with default built-ins and with and empty theory. + * + * ```prolog + * ?- '@>'(aardvark,zebra). + * ``` + * fails on a solver initialized with default built-ins and with and empty theory. + * + * ```prolog + * ?- '@>'(short,short). + * ``` + * fails on a solver initialized with default built-ins and with and empty theory. + * + * ```prolog + * ?- '@>'(short,shorter). + * ``` + * fails on a solver initialized with default built-ins and with and empty theory. + * + * ```prolog + * ?- '@>'(foo(b),foo(a)). + * ``` + * succeeds on a solver initialized with default built-ins and with and empty theory. + * + * ```prolog + * ?- '@>'(X,X). + * ``` + * fails on a solver initialized with default built-ins and with and empty theory. + * + * ```prolog + * ?- '@>'(foo(a,X),foo(b,Y)). + * ``` + * fails on a solver initialized with default built-ins and with and empty theory. + */ + fun testTermGreaterThan() + + /** + * Tests the queries + * ```prolog + * ?- '@>='(1.0,1). + * ``` + * fails on a solver initialized with default built-ins and with and empty theory. + * + * ```prolog + * ?- '@>='(aardvark,zebra). + * ``` + * fails on a solver initialized with default built-ins and with and empty theory. + * + * ```prolog + * ?- '@>='(short,short). + * ``` + * succeeds on a solver initialized with default built-ins and with and empty theory. + * + * ```prolog + * ?- '@>='(short,shorter). + * ``` + * fails on a solver initialized with default built-ins and with and empty theory. + * + * ```prolog + * ?- '@>='(foo(b),foo(a)). + * ``` + * succeeds on a solver initialized with default built-ins and with and empty theory. + * + * ```prolog + * ?- '@>='(X,X). + * ``` + * succeeds on a solver initialized with default built-ins and with and empty theory. + * + * ```prolog + * ?- '@>='(foo(a,X),foo(b,Y)). + * ``` + * fails on a solver initialized with default built-ins and with and empty theory. + */ + fun testTermGreaterThanEq() + + /** + * Tests the queries + * ```prolog + * ?- '@<'(1.0,1). + * ``` + * succeeds on a solver initialized with default built-ins and with and empty theory. + * + * ```prolog + * ?- '@<'(aardvark,zebra). + * ``` + * succeeds on a solver initialized with default built-ins and with and empty theory. + * + * ```prolog + * ?- '@<'(short,short). + * ``` + * fails on a solver initialized with default built-ins and with and empty theory. + * + * ```prolog + * ?- '@<'(short,shorter). + * ``` + * succeeds on a solver initialized with default built-ins and with and empty theory. + * + * ```prolog + * ?- '@<'(foo(b),foo(a)). + * ``` + * fails on a solver initialized with default built-ins and with and empty theory. + * + * ```prolog + * ?- '@<'(X,X). + * ``` + * fails on a solver initialized with default built-ins and with and empty theory. + * + * ```prolog + * ?- '@<'(foo(a,X),foo(b,Y)). + * ``` + * succeeds on a solver initialized with default built-ins and with and empty theory. + */ + fun testTermLessThan() + + /** + * Tests the queries + * ```prolog + * ?- '@=<'(1.0,1). + * ``` + * succeeds on a solver initialized with default built-ins and with and empty theory. + * + * ```prolog + * ?- '@=<'(aardvark,zebra). + * ``` + * succeeds on a solver initialized with default built-ins and with and empty theory. + * + * ```prolog + * ?- '@=<'(short,short). + * ``` + * succeeds on a solver initialized with default built-ins and with and empty theory. + * + * ```prolog + * ?- '@=<'(short,shorter). + * ``` + * succeeds on a solver initialized with default built-ins and with and empty theory. + * + * ```prolog + * ?- '@=<'(foo(b),foo(a)). + * ``` + * fails on a solver initialized with default built-ins and with and empty theory. + * + * ```prolog + * ?- '@=<'(X,X). + * ``` + * succeeds on a solver initialized with default built-ins and with and empty theory. + * + * ```prolog + * ?- '@=<'(foo(a,X),foo(b,Y)). + * ``` + * succeeds on a solver initialized with default built-ins and with and empty theory. + */ + fun testTermLessThanEq() +} \ No newline at end of file diff --git a/test-solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/TestTermImpl.kt b/test-solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/TestTermImpl.kt new file mode 100644 index 000000000..8dac23e9e --- /dev/null +++ b/test-solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/TestTermImpl.kt @@ -0,0 +1,395 @@ +package it.unibo.tuprolog.solve + +import it.unibo.tuprolog.dsl.theory.prolog +import it.unibo.tuprolog.solve.exception.error.InstantiationError +import it.unibo.tuprolog.solve.exception.error.TypeError + +internal class TestTermImpl(private val solverFactory: SolverFactory) : TestTerm { + override fun testTermDiff() { + prolog { + val solver = solverFactory.solverWithDefaultBuiltins() + + var query = "\\=="(1,1) + var solutions = solver.solve(query, mediumDuration).toList() + + assertSolutionEquals( + kotlin.collections.listOf(query.no()), + solutions + ) + + query = "\\=="("X","X") + solutions = solver.solve(query, mediumDuration).toList() + + assertSolutionEquals( + kotlin.collections.listOf(query.no()), + solutions + ) + + query = "\\=="(1,2) + solutions = solver.solve(query, mediumDuration).toList() + + assertSolutionEquals( + kotlin.collections.listOf(query.yes()), + solutions + ) + + query = "\\=="("X",1) + solutions = solver.solve(query, mediumDuration).toList() + + assertSolutionEquals( + kotlin.collections.listOf(query.yes()), + solutions + ) + + query = "\\=="("X","Y") + solutions = solver.solve(query, mediumDuration).toList() + + assertSolutionEquals( + kotlin.collections.listOf(query.yes()), + solutions + ) + + query = "\\=="(`_`,`_`) + solutions = solver.solve(query, mediumDuration).toList() + + assertSolutionEquals( + kotlin.collections.listOf(query.yes()), + solutions + ) + + query = "\\=="("X","a"("X")) + solutions = solver.solve(query, mediumDuration).toList() + + assertSolutionEquals( + kotlin.collections.listOf(query.yes()), + solutions + ) + + query = "\\=="("f"("a"),"f"("a")) + solutions = solver.solve(query, mediumDuration).toList() + + assertSolutionEquals( + kotlin.collections.listOf(query.no()), + solutions + ) + } + } + + override fun testTermEq() { + prolog { + val solver = solverFactory.solverWithDefaultBuiltins() + + var query = "=="(1,1) + var solutions = solver.solve(query, mediumDuration).toList() + + assertSolutionEquals( + kotlin.collections.listOf(query.yes()), + solutions + ) + + query = "=="("X","X") + solutions = solver.solve(query, mediumDuration).toList() + + assertSolutionEquals( + kotlin.collections.listOf(query.yes()), + solutions + ) + + query = "=="(1,2) + solutions = solver.solve(query, mediumDuration).toList() + + assertSolutionEquals( + kotlin.collections.listOf(query.no()), + solutions + ) + + query = "=="("X",1) + solutions = solver.solve(query, mediumDuration).toList() + + assertSolutionEquals( + kotlin.collections.listOf(query.no()), + solutions + ) + + query = "=="("X","Y") + solutions = solver.solve(query, mediumDuration).toList() + + assertSolutionEquals( + kotlin.collections.listOf(query.no()), + solutions + ) + + query = "=="(`_`,`_`) + solutions = solver.solve(query, mediumDuration).toList() + + assertSolutionEquals( + kotlin.collections.listOf(query.no()), + solutions + ) + + query = "=="("X","a"("X")) + solutions = solver.solve(query, mediumDuration).toList() + + assertSolutionEquals( + kotlin.collections.listOf(query.no()), + solutions + ) + + query = "=="("f"("a"),"f"("a")) + solutions = solver.solve(query, mediumDuration).toList() + + assertSolutionEquals( + kotlin.collections.listOf(query.yes()), + solutions + ) + } + } + + override fun testTermGreaterThan() { + prolog { + val solver = solverFactory.solverWithDefaultBuiltins() + + var query = "@>"(1.0,1) + var solutions = solver.solve(query, mediumDuration).toList() + + assertSolutionEquals( + kotlin.collections.listOf(query.no()), + solutions + ) + + query = "@>"("aardvark","zebra") + solutions = solver.solve(query, mediumDuration).toList() + + assertSolutionEquals( + kotlin.collections.listOf(query.no()), + solutions + ) + + query = "@>"("short","short") + solutions = solver.solve(query, mediumDuration).toList() + + assertSolutionEquals( + kotlin.collections.listOf(query.no()), + solutions + ) + + query = "@>"("short","shorter") + solutions = solver.solve(query, mediumDuration).toList() + + assertSolutionEquals( + kotlin.collections.listOf(query.no()), + solutions + ) + + query = "@>"("foo"("b"),"foo"("a")) + solutions = solver.solve(query, mediumDuration).toList() + + assertSolutionEquals( + kotlin.collections.listOf(query.yes()), + solutions + ) + + query = "@>"("X","X") + solutions = solver.solve(query, mediumDuration).toList() + + assertSolutionEquals( + kotlin.collections.listOf(query.no()), + solutions + ) + + query = "@>"("foo"("a", "X"),"foo"("b", "Y")) + solutions = solver.solve(query, mediumDuration).toList() + + assertSolutionEquals( + kotlin.collections.listOf(query.no()), + solutions + ) + } + } + + override fun testTermGreaterThanEq() { + prolog { + val solver = solverFactory.solverWithDefaultBuiltins() + + var query = "@>="(1.0,1) + var solutions = solver.solve(query, mediumDuration).toList() + + assertSolutionEquals( + kotlin.collections.listOf(query.no()), + solutions + ) + + query = "@>="("aardvark","zebra") + solutions = solver.solve(query, mediumDuration).toList() + + assertSolutionEquals( + kotlin.collections.listOf(query.no()), + solutions + ) + + query = "@>="("short","short") + solutions = solver.solve(query, mediumDuration).toList() + + assertSolutionEquals( + kotlin.collections.listOf(query.yes()), + solutions + ) + + query = "@>="("short","shorter") + solutions = solver.solve(query, mediumDuration).toList() + + assertSolutionEquals( + kotlin.collections.listOf(query.no()), + solutions + ) + + query = "@>="("foo"("b"),"foo"("a")) + solutions = solver.solve(query, mediumDuration).toList() + + assertSolutionEquals( + kotlin.collections.listOf(query.yes()), + solutions + ) + + query = "@>="("X","X") + solutions = solver.solve(query, mediumDuration).toList() + + assertSolutionEquals( + kotlin.collections.listOf(query.yes()), + solutions + ) + + query = "@>="("foo"("a", "X"),"foo"("b", "Y")) + solutions = solver.solve(query, mediumDuration).toList() + + assertSolutionEquals( + kotlin.collections.listOf(query.no()), + solutions + ) + } + } + + override fun testTermLessThan() { + prolog { + val solver = solverFactory.solverWithDefaultBuiltins() + + var query = "@<"(1.0,1) + var solutions = solver.solve(query, mediumDuration).toList() + + assertSolutionEquals( + kotlin.collections.listOf(query.yes()), + solutions + ) + + query = "@<"("aardvark","zebra") + solutions = solver.solve(query, mediumDuration).toList() + + assertSolutionEquals( + kotlin.collections.listOf(query.yes()), + solutions + ) + + query = "@<"("short","short") + solutions = solver.solve(query, mediumDuration).toList() + + assertSolutionEquals( + kotlin.collections.listOf(query.no()), + solutions + ) + + query = "@<"("short","shorter") + solutions = solver.solve(query, mediumDuration).toList() + + assertSolutionEquals( + kotlin.collections.listOf(query.yes()), + solutions + ) + + query = "@<"("foo"("b"),"foo"("a")) + solutions = solver.solve(query, mediumDuration).toList() + + assertSolutionEquals( + kotlin.collections.listOf(query.no()), + solutions + ) + + query = "@<"("X","X") + solutions = solver.solve(query, mediumDuration).toList() + + assertSolutionEquals( + kotlin.collections.listOf(query.no()), + solutions + ) + + query = "@<"("foo"("a", "X"),"foo"("b", "Y")) + solutions = solver.solve(query, mediumDuration).toList() + + assertSolutionEquals( + kotlin.collections.listOf(query.yes()), + solutions + ) + } + } + + override fun testTermLessThanEq() { + prolog { + val solver = solverFactory.solverWithDefaultBuiltins() + + var query = "@<="(1.0,1) + var solutions = solver.solve(query, mediumDuration).toList() + + assertSolutionEquals( + kotlin.collections.listOf(query.yes()), + solutions + ) + + query = "@<="("aardvark","zebra") + solutions = solver.solve(query, mediumDuration).toList() + + assertSolutionEquals( + kotlin.collections.listOf(query.yes()), + solutions + ) + + query = "@<="("short","short") + solutions = solver.solve(query, mediumDuration).toList() + + assertSolutionEquals( + kotlin.collections.listOf(query.yes()), + solutions + ) + + query = "@<="("short","shorter") + solutions = solver.solve(query, mediumDuration).toList() + + assertSolutionEquals( + kotlin.collections.listOf(query.yes()), + solutions + ) + + query = "@<="("foo"("b"),"foo"("a")) + solutions = solver.solve(query, mediumDuration).toList() + + assertSolutionEquals( + kotlin.collections.listOf(query.no()), + solutions + ) + + query = "@<="("X","X") + solutions = solver.solve(query, mediumDuration).toList() + + assertSolutionEquals( + kotlin.collections.listOf(query.yes()), + solutions + ) + + query = "@<="("foo"("a", "X"),"foo"("b", "Y")) + solutions = solver.solve(query, mediumDuration).toList() + + assertSolutionEquals( + kotlin.collections.listOf(query.yes()), + solutions + ) + } + } +} \ No newline at end of file