From ce2f9c5a029a65e9f5e0feba95de8d9ab2cfb8f3 Mon Sep 17 00:00:00 2001 From: Iaroslav Postovalov Date: Mon, 7 Dec 2020 02:40:30 +0700 Subject: [PATCH] Update tests of MST-to-WASM --- kmath-ast/src/jsTest/kotlin/Test.kt | 30 ++++++++++++----------------- 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/kmath-ast/src/jsTest/kotlin/Test.kt b/kmath-ast/src/jsTest/kotlin/Test.kt index 32df4631b..748cf6e0d 100644 --- a/kmath-ast/src/jsTest/kotlin/Test.kt +++ b/kmath-ast/src/jsTest/kotlin/Test.kt @@ -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) } }