From fffc75215313dd580e1b23ddd32eca04e84cc7b1 Mon Sep 17 00:00:00 2001 From: Iaroslav Date: Wed, 24 Jun 2020 21:17:06 +0700 Subject: [PATCH] Add more tests and improve current, fix type stack underflow exception --- .../kmath/asm/internal/AsmBuilder.kt | 2 +- .../scietifik/kmath/asm/TestAsmAlgebras.kt | 123 +++++++++--------- .../scietifik/kmath/asm/TestAsmExpressions.kt | 7 + .../kotlin/scietifik/kmath/ast/AsmTest.kt | 15 ++- .../kotlin/scietifik/kmath/ast/ParserTest.kt | 12 +- 5 files changed, 92 insertions(+), 67 deletions(-) diff --git a/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/internal/AsmBuilder.kt b/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/internal/AsmBuilder.kt index 2c9cd48d9..8f45c4044 100644 --- a/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/internal/AsmBuilder.kt +++ b/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/internal/AsmBuilder.kt @@ -368,7 +368,7 @@ internal class AsmBuilder internal constructor( tArity: Int, opcode: Int = Opcodes.INVOKEINTERFACE ) { - repeat(tArity) { typeStack.pop() } + repeat(tArity) { if (!typeStack.empty()) typeStack.pop() } invokeMethodVisitor.visitMethodInsn( opcode, diff --git a/kmath-ast/src/jvmTest/kotlin/scietifik/kmath/asm/TestAsmAlgebras.kt b/kmath-ast/src/jvmTest/kotlin/scietifik/kmath/asm/TestAsmAlgebras.kt index ce5b7cbf9..4c2be811e 100644 --- a/kmath-ast/src/jvmTest/kotlin/scietifik/kmath/asm/TestAsmAlgebras.kt +++ b/kmath-ast/src/jvmTest/kotlin/scietifik/kmath/asm/TestAsmAlgebras.kt @@ -1,9 +1,12 @@ package scietifik.kmath.asm import scientifik.kmath.asm.compile +import scientifik.kmath.ast.mstInField +import scientifik.kmath.ast.mstInRing import scientifik.kmath.ast.mstInSpace import scientifik.kmath.expressions.invoke import scientifik.kmath.operations.ByteRing +import scientifik.kmath.operations.RealField import kotlin.test.Test import kotlin.test.assertEquals @@ -45,65 +48,63 @@ class TestAsmAlgebras { assertEquals(res1, res2) } -// @Test -// fun space() { -// val res = ByteRing.asm { -// binaryOperation( -// "+", -// -// unaryOperation( -// "+", -// 3.toByte() - (2.toByte() + (multiply( -// add(number(1), number(1)), -// 2 -// ) + 1.toByte()) * 3.toByte() - 1.toByte()) -// ), -// -// number(1) -// ) + symbol("x") + zero -// }("x" to 2.toByte()) -// -// assertEquals(16, res) -// } -// -// @Test -// fun ring() { -// val res = ByteRing.asmInRing { -// binaryOperation( -// "+", -// -// unaryOperation( -// "+", -// (3.toByte() - (2.toByte() + (multiply( -// add(const(1), const(1)), -// 2 -// ) + 1.toByte()))) * 3.0 - 1.toByte() -// ), -// -// number(1) -// ) * const(2) -// }() -// -// assertEquals(24, res) -// } -// -// @Test -// fun field() { -// val res = RealField.asmInField { -// +(3 - 2 + 2*(number(1)+1.0) -// -// unaryOperation( -// "+", -// (3.0 - (2.0 + (multiply( -// add((1.0), const(1.0)), -// 2 -// ) + 1.0))) * 3 - 1.0 -// )+ -// -// number(1) -// ) / 2, const(2.0)) * one -// }() -// -// assertEquals(3.0, res) -// } + @Test + fun ring() { + val res1 = ByteRing.mstInRing { + binaryOperation( + "+", + + unaryOperation( + "+", + (symbol("x") - (2.toByte() + (multiply( + add(number(1), number(1)), + 2 + ) + 1.toByte()))) * 3.0 - 1.toByte() + ), + + number(1) + ) * number(2) + }("x" to 3.toByte()) + + val res2 = ByteRing.mstInRing { + binaryOperation( + "+", + + unaryOperation( + "+", + (symbol("x") - (2.toByte() + (multiply( + add(number(1), number(1)), + 2 + ) + 1.toByte()))) * 3.0 - 1.toByte() + ), + + number(1) + ) * number(2) + }.compile()("x" to 3.toByte()) + + assertEquals(res1, res2) + } + + @Test + fun field() { + val res1 = RealField.mstInField { + +(3 - 2 + 2 * number(1) + 1.0) + binaryOperation( + "+", + (3.0 - (symbol("x") + (multiply(add(number(1.0), number(1.0)), 2) + 1.0))) * 3 - 1.0 + + number(1), + 1 / 2 + number(2.0) * one + ) + }("x" to 2.0) + + val res2 = RealField.mstInField { + +(3 - 2 + 2 * number(1) + 1.0) + binaryOperation( + "+", + (3.0 - (symbol("x") + (multiply(add(number(1.0), number(1.0)), 2) + 1.0))) * 3 - 1.0 + + number(1), + 1 / 2 + number(2.0) * one + ) + }.compile()("x" to 2.0) + + assertEquals(res1, res2) + } } diff --git a/kmath-ast/src/jvmTest/kotlin/scietifik/kmath/asm/TestAsmExpressions.kt b/kmath-ast/src/jvmTest/kotlin/scietifik/kmath/asm/TestAsmExpressions.kt index 82af1a927..824201aa7 100644 --- a/kmath-ast/src/jvmTest/kotlin/scietifik/kmath/asm/TestAsmExpressions.kt +++ b/kmath-ast/src/jvmTest/kotlin/scietifik/kmath/asm/TestAsmExpressions.kt @@ -16,6 +16,13 @@ class TestAsmExpressions { assertEquals(-2.0, res) } + @Test + fun testBinaryOperationInvocation() { + val expression = RealField.mstInSpace { -symbol("x") + number(1.0) }.compile() + val res = expression("x" to 2.0) + assertEquals(-1.0, res) + } + @Test fun testConstProductInvocation() { val res = RealField.mstInField { symbol("x") * 2 }("x" to 2.0) diff --git a/kmath-ast/src/jvmTest/kotlin/scietifik/kmath/ast/AsmTest.kt b/kmath-ast/src/jvmTest/kotlin/scietifik/kmath/ast/AsmTest.kt index f0f9a8bc1..08d7fff47 100644 --- a/kmath-ast/src/jvmTest/kotlin/scietifik/kmath/ast/AsmTest.kt +++ b/kmath-ast/src/jvmTest/kotlin/scietifik/kmath/ast/AsmTest.kt @@ -1,7 +1,10 @@ package scietifik.kmath.ast -import scientifik.kmath.ast.evaluate +import scientifik.kmath.asm.compile +import scientifik.kmath.asm.expression +import scientifik.kmath.ast.mstInField import scientifik.kmath.ast.parseMath +import scientifik.kmath.expressions.invoke import scientifik.kmath.operations.Complex import scientifik.kmath.operations.ComplexField import kotlin.test.Test @@ -9,9 +12,15 @@ import kotlin.test.assertEquals class AsmTest { @Test - fun parsedExpression() { + fun `compile MST`() { val mst = "2+2*(2+2)".parseMath() - val res = ComplexField.evaluate(mst) + val res = ComplexField.expression(mst)() + assertEquals(Complex(10.0, 0.0), res) + } + + @Test + fun `compile MSTExpression`() { + val res = ComplexField.mstInField { number(2) + number(2) * (number(2) + number(2)) }.compile()() assertEquals(Complex(10.0, 0.0), res) } } diff --git a/kmath-ast/src/jvmTest/kotlin/scietifik/kmath/ast/ParserTest.kt b/kmath-ast/src/jvmTest/kotlin/scietifik/kmath/ast/ParserTest.kt index 06546302e..5394a4b00 100644 --- a/kmath-ast/src/jvmTest/kotlin/scietifik/kmath/ast/ParserTest.kt +++ b/kmath-ast/src/jvmTest/kotlin/scietifik/kmath/ast/ParserTest.kt @@ -1,17 +1,25 @@ package scietifik.kmath.ast import scientifik.kmath.ast.evaluate +import scientifik.kmath.ast.mstInField import scientifik.kmath.ast.parseMath +import scientifik.kmath.expressions.invoke import scientifik.kmath.operations.Complex import scientifik.kmath.operations.ComplexField -import kotlin.test.assertEquals import kotlin.test.Test +import kotlin.test.assertEquals internal class ParserTest { @Test - fun parsedExpression() { + fun `evaluate MST`() { val mst = "2+2*(2+2)".parseMath() val res = ComplexField.evaluate(mst) assertEquals(Complex(10.0, 0.0), res) } + + @Test + fun `evaluate MSTExpression`() { + val res = ComplexField.mstInField { number(2) + number(2) * (number(2) + number(2)) }() + assertEquals(Complex(10.0, 0.0), res) + } }