Update tests of MST-to-WASM

This commit is contained in:
Iaroslav Postovalov 2020-12-07 02:40:30 +07:00
parent d9859eeb47
commit ce2f9c5a02
No known key found for this signature in database
GPG Key ID: 46E15E4A31B3BCD7

View File

@ -1,8 +1,9 @@
package kscience.kmath.ast
import kscience.kmath.expressions.invoke
import kscience.kmath.operations.IntRing
import kscience.kmath.operations.RealField
import kscience.kmath.operations.invoke
import kscience.kmath.wasm.compile
import kotlin.random.Random
import kotlin.test.Test
import kotlin.test.assertEquals
@ -11,27 +12,28 @@ import kotlin.time.measureTime
internal class Test {
@Test
fun int() {
val res = IntWasmBuilder(MstRing { number(100000000) + number(10000000) }).instance()
val res = IntRing.mstInRing { number(100000000) + number(10000000) }.compile()()
assertEquals(110000000, res)
}
@Test
fun real() {
val res = RealWasmBuilder(MstExtendedField { number(100000000) + number(2).pow(10) }).instance()
val res = RealField.mstInExtendedField { number(100000000) + number(2).pow(10) }.compile()()
assertEquals(100001024.0, res)
}
@Test
fun argsPassing() {
val res = RealWasmBuilder(MstExtendedField { symbol("y") + symbol("x").pow(10) })
.instance("x" to 2.0, "y" to 100000000.0)
val res = RealField
.mstInExtendedField { symbol("y") + symbol("x").pow(10) }
.compile()("x" to 2.0, "y" to 100000000.0)
assertEquals(100001024.0, res)
}
@Test
fun powFunction() {
val expr = RealWasmBuilder(MstExtendedField { symbol("x").pow(1.0 / 6.0) }).instance
val expr = RealField.mstInExtendedField { symbol("x").pow(1.0 / 6.0) }.compile()
assertEquals(0.9730585187140817, expr("x" to 0.8488554755054833))
}
@ -42,20 +44,12 @@ internal class Test {
var rng = Random(0)
var sum1 = 0.0
var sum2 = 0.0
measureTime {
val res = RealWasmBuilder(MstExtendedField { symbol("x").pow(1.0 / 6.0) }).instance
repeat(times) { sum1 += res("x" to rng.nextDouble()) }
}.also(::println)
val e2 = RealField.mstInExtendedField { symbol("x").pow(1.0 / 6.0) }
val e1 = e2.compile()
measureTime { repeat(times) { sum1 += e1("x" to rng.nextDouble()) } }.also(::println)
println("MST")
rng = Random(0)
measureTime {
val res = RealField.mstInExtendedField { symbol("x").pow(1.0 / 6.0) }
repeat(times) { sum2 += res("x" to rng.nextDouble()) }
}.also(::println)
measureTime { repeat(times) { sum2 += e2("x" to rng.nextDouble()) } }.also(::println)
assertEquals(sum1, sum2)
}
}