Add more tests and improve current, fix type stack underflow exception

This commit is contained in:
Iaroslav 2020-06-24 21:17:06 +07:00
parent b565b7f312
commit fffc752153
No known key found for this signature in database
GPG Key ID: 46E15E4A31B3BCD7
5 changed files with 92 additions and 67 deletions

View File

@ -368,7 +368,7 @@ internal class AsmBuilder<T> internal constructor(
tArity: Int, tArity: Int,
opcode: Int = Opcodes.INVOKEINTERFACE opcode: Int = Opcodes.INVOKEINTERFACE
) { ) {
repeat(tArity) { typeStack.pop() } repeat(tArity) { if (!typeStack.empty()) typeStack.pop() }
invokeMethodVisitor.visitMethodInsn( invokeMethodVisitor.visitMethodInsn(
opcode, opcode,

View File

@ -1,9 +1,12 @@
package scietifik.kmath.asm package scietifik.kmath.asm
import scientifik.kmath.asm.compile import scientifik.kmath.asm.compile
import scientifik.kmath.ast.mstInField
import scientifik.kmath.ast.mstInRing
import scientifik.kmath.ast.mstInSpace import scientifik.kmath.ast.mstInSpace
import scientifik.kmath.expressions.invoke import scientifik.kmath.expressions.invoke
import scientifik.kmath.operations.ByteRing import scientifik.kmath.operations.ByteRing
import scientifik.kmath.operations.RealField
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals
@ -45,65 +48,63 @@ class TestAsmAlgebras {
assertEquals(res1, res2) assertEquals(res1, res2)
} }
// @Test @Test
// fun space() { fun ring() {
// val res = ByteRing.asm { val res1 = ByteRing.mstInRing {
// binaryOperation( binaryOperation(
// "+", "+",
//
// unaryOperation( unaryOperation(
// "+", "+",
// 3.toByte() - (2.toByte() + (multiply( (symbol("x") - (2.toByte() + (multiply(
// add(number(1), number(1)), add(number(1), number(1)),
// 2 2
// ) + 1.toByte()) * 3.toByte() - 1.toByte()) ) + 1.toByte()))) * 3.0 - 1.toByte()
// ), ),
//
// number(1) number(1)
// ) + symbol("x") + zero ) * number(2)
// }("x" to 2.toByte()) }("x" to 3.toByte())
//
// assertEquals(16, res) val res2 = ByteRing.mstInRing {
// } binaryOperation(
// "+",
// @Test
// fun ring() { unaryOperation(
// val res = ByteRing.asmInRing { "+",
// binaryOperation( (symbol("x") - (2.toByte() + (multiply(
// "+", add(number(1), number(1)),
// 2
// unaryOperation( ) + 1.toByte()))) * 3.0 - 1.toByte()
// "+", ),
// (3.toByte() - (2.toByte() + (multiply(
// add(const(1), const(1)), number(1)
// 2 ) * number(2)
// ) + 1.toByte()))) * 3.0 - 1.toByte() }.compile()("x" to 3.toByte())
// ),
// assertEquals(res1, res2)
// number(1) }
// ) * const(2)
// }() @Test
// fun field() {
// assertEquals(24, res) val res1 = RealField.mstInField {
// } +(3 - 2 + 2 * number(1) + 1.0) + binaryOperation(
// "+",
// @Test (3.0 - (symbol("x") + (multiply(add(number(1.0), number(1.0)), 2) + 1.0))) * 3 - 1.0
// fun field() { + number(1),
// val res = RealField.asmInField { 1 / 2 + number(2.0) * one
// +(3 - 2 + 2*(number(1)+1.0) )
// }("x" to 2.0)
// unaryOperation(
// "+", val res2 = RealField.mstInField {
// (3.0 - (2.0 + (multiply( +(3 - 2 + 2 * number(1) + 1.0) + binaryOperation(
// add((1.0), const(1.0)), "+",
// 2 (3.0 - (symbol("x") + (multiply(add(number(1.0), number(1.0)), 2) + 1.0))) * 3 - 1.0
// ) + 1.0))) * 3 - 1.0 + number(1),
// )+ 1 / 2 + number(2.0) * one
// )
// number(1) }.compile()("x" to 2.0)
// ) / 2, const(2.0)) * one
// }() assertEquals(res1, res2)
// }
// assertEquals(3.0, res)
// }
} }

View File

@ -16,6 +16,13 @@ class TestAsmExpressions {
assertEquals(-2.0, res) 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 @Test
fun testConstProductInvocation() { fun testConstProductInvocation() {
val res = RealField.mstInField { symbol("x") * 2 }("x" to 2.0) val res = RealField.mstInField { symbol("x") * 2 }("x" to 2.0)

View File

@ -1,7 +1,10 @@
package scietifik.kmath.ast 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.ast.parseMath
import scientifik.kmath.expressions.invoke
import scientifik.kmath.operations.Complex import scientifik.kmath.operations.Complex
import scientifik.kmath.operations.ComplexField import scientifik.kmath.operations.ComplexField
import kotlin.test.Test import kotlin.test.Test
@ -9,9 +12,15 @@ import kotlin.test.assertEquals
class AsmTest { class AsmTest {
@Test @Test
fun parsedExpression() { fun `compile MST`() {
val mst = "2+2*(2+2)".parseMath() 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) assertEquals(Complex(10.0, 0.0), res)
} }
} }

View File

@ -1,17 +1,25 @@
package scietifik.kmath.ast package scietifik.kmath.ast
import scientifik.kmath.ast.evaluate import scientifik.kmath.ast.evaluate
import scientifik.kmath.ast.mstInField
import scientifik.kmath.ast.parseMath import scientifik.kmath.ast.parseMath
import scientifik.kmath.expressions.invoke
import scientifik.kmath.operations.Complex import scientifik.kmath.operations.Complex
import scientifik.kmath.operations.ComplexField import scientifik.kmath.operations.ComplexField
import kotlin.test.assertEquals
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertEquals
internal class ParserTest { internal class ParserTest {
@Test @Test
fun parsedExpression() { fun `evaluate MST`() {
val mst = "2+2*(2+2)".parseMath() val mst = "2+2*(2+2)".parseMath()
val res = ComplexField.evaluate(mst) val res = ComplexField.evaluate(mst)
assertEquals(Complex(10.0, 0.0), res) 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)
}
} }