A test of gcd program.
This commit is contained in:
parent
c64bbfb240
commit
bc7f266997
|
|
@ -10,6 +10,8 @@ import java.util.*
|
|||
*/
|
||||
|
||||
|
||||
fun <T: Any> anon(value: T) = TestLogical(value)
|
||||
|
||||
fun <T: Any> logical(name: String) = TestLogical<T>(name)
|
||||
|
||||
fun <T: Any> logical(name1: String, name2: String) = Pair(TestLogical<T>(name1), TestLogical<T>(name2))
|
||||
|
|
@ -39,6 +41,12 @@ fun <T: Any> Logical<T>.set(t: T) {
|
|||
|
||||
data class TestLogical<T>(val name: String, var value: T?, var parent: TestLogical<T>?) : Logical<T> {
|
||||
|
||||
companion object {
|
||||
var anonIdx = 0
|
||||
}
|
||||
|
||||
constructor(value: T) : this("$${anonIdx++}", value, null)
|
||||
|
||||
constructor(name: String) : this(name, null, null) {}
|
||||
|
||||
override fun name(): String = name
|
||||
|
|
|
|||
|
|
@ -123,4 +123,51 @@ class TestProgram {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun gcd() {
|
||||
val (M, N, TMP) = logicalPattern<Int>("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<Int>).get())
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue