All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.it.unibo.tuprolog.solve.TestAtomLengthImpl.kt Maven / Gradle / Ivy

package it.unibo.tuprolog.solve

import it.unibo.tuprolog.dsl.theory.prolog
import it.unibo.tuprolog.solve.exception.error.TypeError

class TestAtomLengthImpl(private val solverFactory: SolverFactory) : TestAtomLength {

    override fun testAtomLengthNoVar() {
        prolog {
            val solver = solverFactory.solverWithDefaultBuiltins()
            val query = atom_length("test", 4)
            val solutions = solver.solve(query, mediumDuration).toList()

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

    override fun testAtomLengthSecondIsVar() {
        prolog {
            val solver = solverFactory.solverWithDefaultBuiltins()
            val query = atom_length("test", X)
            val solutions = solver.solve(query, mediumDuration).toList()

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

    override fun testAtomLengthFirstIsVar() {
        prolog {
            val solver = solverFactory.solverWithDefaultBuiltins()
            val query = char_code("X", "a")
            val solutions = solver.solve(query, mediumDuration).toList()

            assertSolutionEquals(
                kotlin.collections.listOf(
                    query.halt(
                        TypeError.forArgument(
                            DummyInstances.executionContext,
                            Signature("char_code", 2),
                            TypeError.Expected.INTEGER,
                            atomOf("a"),
                            index = 1
                        )
                    )
                ),
                solutions
            )
        }
    }

    override fun testAtomLengthSecondIsVar2() {
        prolog {
            val solver = solverFactory.solverWithDefaultBuiltins()
            val query = atom_length("testLength", X)
            val solutions = solver.solve(query, mediumDuration).toList()

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

    override fun testAtomLengthFail() {
        prolog {
            val solver = solverFactory.solverWithDefaultBuiltins()
            val query = atom_length("test", 5)
            val solutions = solver.solve(query, mediumDuration).toList()

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy