60 lines
No EOL
1.8 KiB
Kotlin
60 lines
No EOL
1.8 KiB
Kotlin
package prolog.builtins
|
|
|
|
import org.junit.jupiter.api.Assertions.assertEquals
|
|
import org.junit.jupiter.api.Assertions.assertTrue
|
|
import org.junit.jupiter.api.Disabled
|
|
import org.junit.jupiter.api.Test
|
|
import prolog.ast.lists.List.Cons
|
|
import prolog.ast.lists.List.Empty
|
|
import prolog.ast.terms.Atom
|
|
|
|
class ListOperatorsTests {
|
|
@Test
|
|
fun `size empty list is 0`() {
|
|
assertEquals(0, Empty.size.value, "Expected size of empty list to be 0")
|
|
}
|
|
|
|
@Test
|
|
fun `size of singleton is 1`() {
|
|
val list = Cons(Atom("a"), Empty)
|
|
|
|
assertEquals(1, list.size.value, "Expected size of singleton list to be 1")
|
|
}
|
|
|
|
@Test
|
|
fun `size of list with five elements is 5`() {
|
|
val list = Cons(Atom("a"), Cons(Atom("b"), Cons(Atom("c"), Cons(Atom("d"), Cons(Atom("e"), Empty)))))
|
|
|
|
assertEquals(5, list.size.value, "Expected size of list with five elements to be 5")
|
|
}
|
|
|
|
@Test
|
|
fun `member(a, list of a)`() {
|
|
val atom = Atom("a")
|
|
val list = Cons(atom, Empty)
|
|
|
|
val member = Member(atom, list)
|
|
|
|
val result = member.satisfy(emptyMap()).toList()
|
|
|
|
assertEquals(1, result.size, "Expected one solution")
|
|
assertTrue(result[0].isSuccess, "Expected success")
|
|
assertTrue(result[0].getOrNull()!!.isEmpty(), "Expected empty substitution map")
|
|
}
|
|
|
|
@Disabled("Not required functionality")
|
|
@Test
|
|
fun `member with variable in list`() {
|
|
val atom = Atom("a")
|
|
val variable = Atom("X")
|
|
val list = Cons(variable, Empty)
|
|
|
|
val member = Member(atom, list)
|
|
|
|
val result = member.satisfy(emptyMap()).toList()
|
|
|
|
assertEquals(1, result.size, "Expected one solution")
|
|
assertTrue(result[0].isSuccess, "Expected success")
|
|
assertEquals(atom, result[0].getOrNull()!![variable], "Expected variable to be unified with atom")
|
|
}
|
|
} |