summaryrefslogtreecommitdiff
path: root/test/files/jvm/constant-optimization/Test.scala
blob: dc0f8f61035ed8ed84af49c7231572b3149262f7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import scala.tools.partest.BytecodeTest
import scala.tools.asm
import asm.tree.InsnList
import scala.collection.JavaConverters._

object Test extends BytecodeTest {
  val comparisons = Set(asm.Opcodes.IF_ACMPEQ, asm.Opcodes.IF_ACMPNE, asm.Opcodes.IF_ICMPEQ, asm.Opcodes.IF_ICMPGE, asm.Opcodes.IF_ICMPGT, asm.Opcodes.IF_ICMPLE,
    asm.Opcodes.IF_ICMPLT, asm.Opcodes.IF_ICMPNE, asm.Opcodes.IFEQ, asm.Opcodes.IFGE, asm.Opcodes.IFGT, asm.Opcodes.IFLE, asm.Opcodes.IFLT,
    asm.Opcodes.IFNE, asm.Opcodes.IFNONNULL, asm.Opcodes.IFNULL)

  def show: Unit = {
    val classNode = loadClassNode("Foo_1")
    val methodNode = getMethod(classNode, "foo")
    // after optimization there should be no comparisons left
    val expected = 0

    val got = countComparisons(methodNode.instructions)
    assert(got == expected, s"expected $expected but got $got comparisons")
  }

  def countComparisons(insnList: InsnList): Int = {
    def isComparison(node: asm.tree.AbstractInsnNode): Boolean =
      (comparisons contains node.getOpcode)
    insnList.iterator.asScala count isComparison
  }
}