Skip to content

Commit 0dd8a13

Browse files
committed
Add tests for primitive construction as well as reference and boxed unit arrays
1 parent 5ef03c4 commit 0dd8a13

File tree

3 files changed

+100
-3
lines changed

3 files changed

+100
-3
lines changed

test/test/AsmConverters.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import asm._
55
import asm.tree._
66
import scala.collection.JavaConverters._
77

8-
/** Makes using ASM from ByteCodeTests more convenient.
8+
/** Makes using ASM from tests more convenient.
99
*
1010
* Wraps ASM instructions in case classes so that equals and toString work
1111
* for the purpose of bytecode diffing and pretty printing.

test/test/DottyBytecodeTest.scala

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,24 @@ trait DottyBytecodeTest extends DottyTest {
2727
import AsmNode._
2828
import ASMConverters._
2929

30+
protected object Opcode {
31+
val newarray = 188
32+
val anewarray = 189
33+
val multianewarray = 197
34+
35+
val boolean = 4
36+
val char = 5
37+
val float = 6
38+
val double = 7
39+
val byte = 8
40+
val short = 9
41+
val int = 10
42+
val long = 11
43+
44+
val boxedUnit = "scala/runtime/BoxedUnit"
45+
val javaString = "java/lang/String"
46+
}
47+
3048
private def bCodeCheckingComp(phase: TestGenBCode)(check: Directory => Unit) =
3149
new Compiler {
3250
override def phases = {

test/test/DottyBytecodeTests.scala

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class TestBCode extends DottyBytecodeTest {
8686
/** Make sure that creating multidim arrays reduces to "multinewarray"
8787
* instruction
8888
*/
89-
@Test def multidimArrays = {
89+
@Test def multidimArraysFromOfDim = {
9090
val source = """
9191
|object Arr {
9292
| def arr = Array.ofDim[Int](2, 1)
@@ -98,12 +98,91 @@ class TestBCode extends DottyBytecodeTest {
9898

9999
val hadCorrectInstr =
100100
instructionsFromMethod(method)
101-
.collect { case x @ NewArray(op, _, dims) if op == 197 && dims == 2 => x }
101+
.collect {
102+
case x @ NewArray(op, _, dims)
103+
if op == Opcode.multianewarray && dims == 2 => x
104+
}
102105
.length > 0
103106

104107
assert(hadCorrectInstr,
105108
"Did not contain \"multianewarray\" instruction in:\n" +
106109
instructionsFromMethod(method).mkString("\n"))
107110
}
108111
}
112+
113+
@Test def arraysFromOfDim = {
114+
val source = """
115+
|object Arr {
116+
| def arr1 = Array.ofDim[Int](2)
117+
| def arr2 = Array.ofDim[Unit](2)
118+
| def arr3 = Array.ofDim[String](2)
119+
| def arr4 = Array.ofDim[Map[String, String]](2)
120+
|}""".stripMargin
121+
checkBCode(source) { dir =>
122+
val moduleIn = dir.lookupName("Arr$.class", directory = false)
123+
val moduleNode = loadClassNode(moduleIn.input)
124+
val arr1 = getMethod(moduleNode, "arr1")
125+
val arr2 = getMethod(moduleNode, "arr2")
126+
val arr3 = getMethod(moduleNode, "arr3")
127+
128+
val arr1CorrectInstr =
129+
instructionsFromMethod(arr1)
130+
.collect {
131+
case x @ IntOp(op, oprnd)
132+
if op == Opcode.newarray && oprnd == Opcode.int => x
133+
}
134+
.length > 0
135+
136+
assert(arr1CorrectInstr,
137+
"Did not contain \"multianewarray\" instruction in:\n" +
138+
instructionsFromMethod(arr1).mkString("\n"))
139+
140+
val arr2CorrectInstr =
141+
instructionsFromMethod(arr2)
142+
.collect {
143+
case x @ TypeOp(op, oprnd)
144+
if op == Opcode.anewarray && oprnd == Opcode.boxedUnit => x
145+
}
146+
.length > 0
147+
148+
assert(arr2CorrectInstr,
149+
"arr2 bytecode did not contain correct `anewarray` instruction:\n" +
150+
instructionsFromMethod(arr2)mkString("\n"))
151+
152+
val arr3CorrectInstr =
153+
instructionsFromMethod(arr3)
154+
.collect {
155+
case x @ TypeOp(op, oprnd)
156+
if op == Opcode.anewarray && oprnd == Opcode.javaString => x
157+
}
158+
.length > 0
159+
160+
assert(arr3CorrectInstr,
161+
"arr3 bytecode did not contain correct `anewarray` instruction:\n" +
162+
instructionsFromMethod(arr3).mkString("\n"))
163+
}
164+
}
165+
166+
@Test def arraysFromDimAndFromNewEqual = {
167+
val source = """
168+
|object Arr {
169+
| def arr1 = Array.ofDim[Int](2)
170+
| def arr2 = new Array[Int](2)
171+
|}""".stripMargin
172+
173+
checkBCode(source) { dir =>
174+
val moduleIn = dir.lookupName("Arr$.class", directory = false)
175+
val moduleNode = loadClassNode(moduleIn.input)
176+
val arr1 = getMethod(moduleNode, "arr1")
177+
val arr2 = getMethod(moduleNode, "arr2")
178+
179+
// First two instructions of `arr1` fetch the static reference to `Array`
180+
val instructions1 = instructionsFromMethod(arr1).drop(2)
181+
val instructions2 = instructionsFromMethod(arr2)
182+
183+
assert(instructions1 == instructions2,
184+
"Creating arrays using `Array.ofDim[Int](2)` did not equal bytecode for `new Array[Int](2)`\n" +
185+
diffInstructions(instructions1, instructions2))
186+
}
187+
}
109188
}

0 commit comments

Comments
 (0)