From fdf5354eef3d2a75458cf646c00616ca39a4e56b Mon Sep 17 00:00:00 2001 From: Felix Mulder Date: Tue, 17 May 2016 14:54:53 +0200 Subject: Add switch verification test --- test/test/DottyBytecodeTest.scala | 16 +++++++++++++ test/test/DottyBytecodeTests.scala | 48 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 62 insertions(+), 2 deletions(-) (limited to 'test') diff --git a/test/test/DottyBytecodeTest.scala b/test/test/DottyBytecodeTest.scala index 6b39116f8..3814b03bd 100644 --- a/test/test/DottyBytecodeTest.scala +++ b/test/test/DottyBytecodeTest.scala @@ -94,6 +94,22 @@ trait DottyBytecodeTest extends DottyTest { } /**************************** Comparison Methods ****************************/ + def verifySwitch(method: MethodNode, shouldFail: Boolean = false, debug: Boolean = false): Boolean = { + val instructions = instructionsFromMethod(method) + + val succ = instructions + .collect { + case x: TableSwitch => x + case x: LookupSwitch => x + } + .length > 0 + + if (debug || !succ && !shouldFail || succ && shouldFail) + instructions.foreach(Console.err.println) + + succ + } + def sameBytecode(methA: MethodNode, methB: MethodNode) = { val isa = instructionsFromMethod(methA) val isb = instructionsFromMethod(methB) diff --git a/test/test/DottyBytecodeTests.scala b/test/test/DottyBytecodeTests.scala index 3b8308b0a..6b1562741 100644 --- a/test/test/DottyBytecodeTests.scala +++ b/test/test/DottyBytecodeTests.scala @@ -4,6 +4,7 @@ import org.junit.Assert._ import org.junit.Test class TestBCode extends DottyBytecodeTest { + import ASMConverters._ @Test def nullChecks = { val source = """ |class Foo { @@ -15,11 +16,54 @@ class TestBCode extends DottyBytecodeTest { |} """.stripMargin - checkBCode(source) { file => - val clsIn = file.lookupName("Foo.class", directory = false).input + checkBCode(source) { dir => + val clsIn = dir.lookupName("Foo.class", directory = false).input val clsNode = loadClassNode(clsIn) val methodNode = getMethod(clsNode, "foo") correctNumberOfNullChecks(2, methodNode.instructions) } } + + /** This test verifies that simple matches are transformed if possible + * despite no annotation + */ + @Test def basicTransformNonAnnotated = { + val source = """ + |object Foo { + | def foo(i: Int) = i match { + | case 2 => println(2) + | case 1 => println(1) + | } + |}""".stripMargin + + checkBCode(source) { dir => + val moduleIn = dir.lookupName("Foo$.class", directory = false) + val moduleNode = loadClassNode(moduleIn.input) + val methodNode = getMethod(moduleNode, "foo") + assert(verifySwitch(methodNode)) + } + } + + /** This test verifies that simple matches with `@switch` annotations are + * indeed transformed to a switch + * + * FIXME: once issue#1258 is resolved, this should be enabled! + */ + //@Test def basicTransfromAnnotated = { + // val source = """ + // |object Foo { + // | import scala.annotation.switch + // | def foo(i: Int) = (i: @switch) match { + // | case 2 => println(2) + // | case 1 => println(1) + // | } + // |}""".stripMargin + + // checkBCode(source) { dir => + // val moduleIn = dir.lookupName("Foo$.class", directory = false) + // val moduleNode = loadClassNode(moduleIn.input) + // val methodNode = getMethod(moduleNode, "foo") + // assert(verifySwitch(methodNode)) + // } + //} } -- cgit v1.2.3