test: Meta examples

This commit is contained in:
Tibo De Peuter 2025-05-08 19:14:48 +02:00
parent 1feb3893c5
commit 9b454a9669
Signed by: tdpeuter
GPG key ID: 38297DE43F75FFE2
5 changed files with 62 additions and 3 deletions

View file

@ -0,0 +1,48 @@
% Old mag steeds door New vervangen worden
mib(Old, New, Old, New).
% Een term blijft enkel behouden als het een attoom is
% (dat is, niet compound), en als het niet Old is
mib(Old, _, Term, Term) :-
atomic(Term),
Term \= Old.
% Voor een samengestelde Term
mib(Old, New, Pre, Post) :-
compound(Pre),
functor(Pre, F, N), % Pre heeft naam F en arriteit N
functor(Post, F, N), % Post is term met zelfde naam (F) en arriteit (N)
mib(N, Old, New, Pre, Post).
% Extra predicaat om de argumenten te vervangen van een samengestelde term
%
% N = het nr van het argument (strikt positief)
mib(0, _, _, _, _) :- !. % Argument 0 bestaat niet, als we hier komen zijn we klaar.
mib(N, Old, New, Pre, Post) :-
arg(N, Pre, ArgPre), % neem het N-de argument
mib(Old, New, ArgPre, ArgPost), % vertaal het
arg(N, Post, ArgPost), % unificeer het met het N-de argument van output
N1 is N-1,
mib(N1, Old, New, Pre, Post). % Herhaal voor argument N-1
example1 :-
mib(a, b, a, Result),
write(Result), nl.
example2 :-
mib(a, b, f(a), Result),
write(Result), nl.
example3 :-
mib(b, a, f(g(a, b), h(c, d), i(e, f)), Result),
write(Result), nl,
mib(d, c, Result, Result2),
write(Result2), nl,
mib(f, e, Result2, Result3),
write(Result3), nl.
main :-
example1,
example2,
example3.
:- initialization(main).

View file

@ -21,7 +21,7 @@ open class Structure(val name: Atom, var arguments: List<Argument>) : Goal(), He
override fun toString(): String { override fun toString(): String {
return when { return when {
arguments.isEmpty() -> name.name arguments.isEmpty() -> name.name
else -> "${name.name}(${arguments.joinToString(", ")})" else -> "${name.name}(${arguments.joinToString(",")})"
} }
} }

View file

@ -37,6 +37,13 @@ class Examples {
assertEquals(expected, outStream.toString()) assertEquals(expected, outStream.toString())
} }
@ParameterizedTest
@MethodSource("meta")
fun `Identical output for meta`(inputFile: String, expected: String) {
loader.load("examples/meta/$inputFile")
assertEquals(expected, outStream.toString())
}
@ParameterizedTest @ParameterizedTest
@MethodSource("other") @MethodSource("other")
fun `Identical output for other`(inputFile: String, expected: String) { fun `Identical output for other`(inputFile: String, expected: String) {
@ -57,6 +64,10 @@ class Examples {
Arguments.of("write.pl", "gpl zegt: dag(wereld)\n"), Arguments.of("write.pl", "gpl zegt: dag(wereld)\n"),
) )
fun meta() = listOf(
Arguments.of("mib_voorbeelden.pl", "b\nf(b)\nf(g(a,a),h(c,d),i(e,f))\nf(g(a,a),h(c,c),i(e,f))\nf(g(a,a),h(c,c),i(e,e))\n")
)
fun other() = listOf( fun other() = listOf(
Arguments.of("program.pl", "10\nhello(world)") Arguments.of("program.pl", "10\nhello(world)")
) )

View file

@ -32,7 +32,7 @@ class LogicGrammarTests {
"male(jimmy).", "male(jimmy).",
"female(mary).", "female(mary).",
"not(not(true)).", "not(not(true)).",
"not(a, not(b, c), d, not(not(a)))." "not(a,not(b,c),d,not(not(a)))."
]) ])
fun `parse simple fact`(input: String) { fun `parse simple fact`(input: String) {
val result = parser.parseToEnd(input) val result = parser.parseToEnd(input)

View file

@ -55,7 +55,7 @@ class IoOperatorsTests {
assertEquals(1, result.size, "Should return one result") assertEquals(1, result.size, "Should return one result")
assertTrue(result[0].isSuccess, "Result should be successful") assertTrue(result[0].isSuccess, "Result should be successful")
assertEquals("person(john, doe)", outStream.toString().trim(), "Output should match the structure") assertEquals("person(john,doe)", outStream.toString().trim(), "Output should match the structure")
} }
@Test @Test