diff --git a/reactor/Test/test/LogicalHelper.kt b/reactor/Test/test/LogicalHelper.kt index 215d6e2e..75310932 100644 --- a/reactor/Test/test/LogicalHelper.kt +++ b/reactor/Test/test/LogicalHelper.kt @@ -10,6 +10,8 @@ import java.util.* */ +fun anon(value: T) = TestLogical(value) + fun logical(name: String) = TestLogical(name) fun logical(name1: String, name2: String) = Pair(TestLogical(name1), TestLogical(name2)) @@ -39,6 +41,12 @@ fun Logical.set(t: T) { data class TestLogical(val name: String, var value: T?, var parent: TestLogical?) : Logical { + companion object { + var anonIdx = 0 + } + + constructor(value: T) : this("$${anonIdx++}", value, null) + constructor(name: String) : this(name, null, null) {} override fun name(): String = name diff --git a/reactor/Test/test/TestProgram.kt b/reactor/Test/test/TestProgram.kt index 16a49ffb..12ec6648 100644 --- a/reactor/Test/test/TestProgram.kt +++ b/reactor/Test/test/TestProgram.kt @@ -123,4 +123,51 @@ class TestProgram { } } + @Test + fun gcd() { + val (M, N, TMP) = logicalPattern("M", "N", "TMP") + program( + rule("main", + headReplaced( + constraint("main") + ), + body( + statement({ m, n -> m.set(21); n.set(35) }, M, N), + constraint("gcd", M), + constraint("gcd", N) + ) + ), + rule("trivial", + headReplaced( + constraint("gcd", M) + ), + guard( + expression({ x -> x.get() == 0 }, M) + ), + body( + statement { } // nothing + ) + ), + rule("step", + headKept( + constraint("gcd", N) + ), + headReplaced( + constraint("gcd", M) + ), + guard( + expression({ m, n -> m.get() >= n.get()}, M, N) + ), + body( + statement({ m, n, tmp -> tmp.set(m.get() - n.get())}, M, N, TMP), + constraint("gcd", TMP) + ) + ) + ).session("gcd").run { + assertEquals(1, constraintOccurrences().count()) + val arg = constraintOccurrences().first().arguments().first() + assertEquals(7, (arg as Logical).get()) + } + } + }