IO Operators

This commit is contained in:
Tibo De Peuter 2025-04-27 20:11:15 +02:00
parent b9f419a59d
commit 82a8fccf87
Signed by: tdpeuter
GPG key ID: 38297DE43F75FFE2
22 changed files with 450 additions and 199 deletions

View file

@ -1,8 +1,10 @@
package prolog.builtins
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertInstanceOf
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.ValueSource
@ -13,6 +15,7 @@ import java.io.ByteArrayOutputStream
import java.io.PrintStream
import prolog.ast.arithmetic.Integer
import prolog.ast.terms.Variable
import java.io.ByteArrayInputStream
class IoOperatorsTests {
private var outStream = ByteArrayOutputStream()
@ -44,7 +47,7 @@ class IoOperatorsTests {
assertEquals(1, result.size, "Should return one result")
assertTrue(result[0].isSuccess, "Result should be successful")
assertEquals(name, outStream.toString().trim(), "Output should match the atom")
assertEquals(name, outStream.toString(), "Output should match the atom")
}
@Test
@ -79,7 +82,86 @@ class IoOperatorsTests {
assertEquals(1, result.size, "Should return one result")
assertTrue(result[0].isSuccess, "Result should be successful")
val output = outStream.toString().trim()
val output = outStream.toString()
assertTrue(output == expected1 || output == expected2, "Output should match the arithmetic expression")
}
@Test
fun `write nl`() {
val nl = Nl
val result = nl.satisfy(emptyMap()).toList()
assertEquals(1, result.size, "Should return one result")
assertTrue(result[0].isSuccess, "Result should be successful")
assertTrue(outStream.toString().contains("\n"), "Output should contain a newline")
}
@Test
fun `read term`() {
val inputStream = ByteArrayInputStream("hello.".toByteArray())
System.setIn(inputStream)
val read = Read(Variable("X"))
val result = read.satisfy(emptyMap()).toList()
assertEquals(1, result.size, "Should return one result")
assertTrue(result[0].isSuccess, "Result should be successful")
val answer = result[0].getOrNull()!!
assertTrue(answer.containsKey(Variable("X")), "Result should be successful")
assertInstanceOf(Atom::class.java, answer[Variable("X")], "Output should be an atom")
assertEquals(Atom("hello"), answer[Variable("X")], "Output should match the read term")
}
@Test
fun `read between(1, 2, 3)`() {
val inputStream = ByteArrayInputStream("between(1, 2, 3).".toByteArray())
System.setIn(inputStream)
val read = Read(Variable("X"))
val result = read.satisfy(emptyMap()).toList()
assertEquals(1, result.size, "Should return one result")
assertTrue(result[0].isSuccess, "Result should be successful")
val answer = result[0].getOrNull()!!
assertTrue(answer.containsKey(Variable("X")), "Result should be successful")
assertInstanceOf(CompoundTerm::class.java, answer[Variable("X")], "Output should be a compound term")
assertEquals(
CompoundTerm(Atom("between"), listOf(Integer(1), Integer(2), Integer(3))),
answer[Variable("X")],
"Output should match the read term"
)
}
@Test
fun `read foo(a, X, b, Y, c, Z)`() {
val inputStream = ByteArrayInputStream("foo(A, x, B, y, C, z).".toByteArray())
System.setIn(inputStream)
val read = Read(CompoundTerm(Atom("foo"), listOf(
Atom("a"),
Variable("X"),
Atom("b"),
Variable("Y"),
Atom("c"),
Variable("Z")
)))
val result = read.satisfy(emptyMap()).toList()
assertEquals(1, result.size, "Should return one result")
assertTrue(result[0].isSuccess, "Result should be successful")
val answer = result[0].getOrNull()!!
assertTrue(answer.containsKey(Variable("X")), "Result should be successful")
assertTrue(answer.containsKey(Variable("Y")), "Result should be successful")
assertTrue(answer.containsKey(Variable("Z")), "Result should be successful")
assertEquals(Atom("x"), answer[Variable("X")], "Output should match the read term")
assertEquals(Atom("y"), answer[Variable("Y")], "Output should match the read term")
assertEquals(Atom("z"), answer[Variable("Z")], "Output should match the read term")
}
}