summaryrefslogtreecommitdiff
path: root/test/junit
diff options
context:
space:
mode:
authorLukas Rytz <lukas.rytz@gmail.com>2016-04-15 16:41:03 +0200
committerLukas Rytz <lukas.rytz@gmail.com>2016-04-20 10:13:02 +0200
commitc600c15488b97e7b1f72e6b7811f53f03a493c1c (patch)
tree7bdba1077d32d370b166beca1de1c427d8821aff /test/junit
parentd6f66ec0f38e42c19f79cbe9d32d29c65dee1e05 (diff)
downloadscala-c600c15488b97e7b1f72e6b7811f53f03a493c1c.tar.gz
scala-c600c15488b97e7b1f72e6b7811f53f03a493c1c.tar.bz2
scala-c600c15488b97e7b1f72e6b7811f53f03a493c1c.zip
SI-6710 / PR 5072 follow-up: fix Unit.box / Unit.unbox
The backend replaces .box / .unbox methods by corresponding invocations to BoxesRunTime, but not for Unit. This commit restores the body of `Unit.box` and `Unit.unbox`.
Diffstat (limited to 'test/junit')
-rw-r--r--test/junit/scala/BoxUnboxTest.scala119
1 files changed, 119 insertions, 0 deletions
diff --git a/test/junit/scala/BoxUnboxTest.scala b/test/junit/scala/BoxUnboxTest.scala
new file mode 100644
index 0000000000..162d805a6b
--- /dev/null
+++ b/test/junit/scala/BoxUnboxTest.scala
@@ -0,0 +1,119 @@
+package scala
+
+import org.junit.Test
+import org.junit.Assert._
+import org.junit.runner.RunWith
+import org.junit.runners.JUnit4
+
+import scala.tools.testing.AssertUtil._
+
+@RunWith(classOf[JUnit4])
+class BoxUnboxTest {
+ def genericNull[T] = null.asInstanceOf[T] // allowed, see SI-4437, point 2
+
+ @Test
+ def boxUnboxInt(): Unit = {
+ val b = new Integer(1)
+ val u = 1
+
+ assertEquals(1.toInt, u)
+
+ assertEquals(Predef.int2Integer(1), b)
+ assertEquals(1: Integer, b)
+ assertEquals(Int.box(1), b)
+ assertEquals(1.asInstanceOf[Object], b)
+
+ assertThrows[ClassCastException]("".asInstanceOf[Integer])
+
+ assertEquals(Predef.Integer2int(b), u)
+ assertEquals(b: Int, u)
+ assertEquals(Int.unbox(b), u)
+ assertEquals(b.asInstanceOf[Int], u)
+ assertEquals(b.intValue, u)
+ assertEquals(b.toInt, u)
+ intWrapper(b).toInt
+
+ assertThrows[ClassCastException](Int.unbox(""))
+ assertThrows[ClassCastException]("".asInstanceOf[Int])
+
+ // null unboxing in various positions
+
+ val n1 = Int.unbox(null)
+ assertEquals(n1, 0)
+ val n2 = Predef.Integer2int(null)
+ assertEquals(n2, 0)
+ val n3 = (null: Integer): Int
+ assertEquals(n3, 0)
+ val n4 = null.asInstanceOf[Int]
+ assertEquals(n4, 0)
+ val n5 = null.asInstanceOf[Int] == 0
+ assertTrue(n5)
+ val n6 = null.asInstanceOf[Int] == null // SI-9671 -- should be false, but is true
+ assertThrows[AssertionError](assertFalse(n6)) // should not throw
+ val n7 = null.asInstanceOf[Int] != 0
+ assertFalse(n7)
+ val n8 = null.asInstanceOf[Int] != null // SI-9671 -- should be true, but is false
+ assertThrows[AssertionError](assertTrue(n8)) // should not throw
+
+ val mp = new java.util.HashMap[Int, Int]
+ val n9 = mp.get(0)
+ assertEquals(n9, 0)
+ val n10 = mp.get(0) == null // SI-602 -- maybe related to SI-9671 (test above)?
+ assertThrows[AssertionError](assertFalse(n10)) // should not throw
+
+ def f(a: Any) = "" + a
+ val n11 = f(null.asInstanceOf[Int]) // "null", should be "0". probably same cause as SI-602.
+ assertThrows[AssertionError](assertEquals(n11, "0")) // should not throw
+
+ def n12 = genericNull[Int]
+ assertEquals(n12, 0)
+ }
+
+ @Test
+ def numericConversions(): Unit = {
+ val i1 = 1L.asInstanceOf[Int]
+ assertEquals(i1, 1)
+ assertThrows[ClassCastException] {
+ val i2 = (1L: Any).asInstanceOf[Int] // SI-1448, should not throw. see also SI-4437 point 1.
+ assertEquals(i2, 1)
+ }
+ }
+
+ @Test
+ def boxUnboxBoolean(): Unit = {
+ val n1 = Option(null.asInstanceOf[Boolean]) // SI-7397 -- should be Some(false), but is None
+ assertThrows[AssertionError](assertEquals(n1, Some(false))) // should not throw
+ }
+
+ @Test
+ def boxUnboxUnit(): Unit = {
+ // should not use assertEquals in this test: it takes two Object parameters. normally, Unit does
+ // not conform to Object, but for Java-defined methods scalac makes an exception and treats them
+ // as Any. passing a Unit as Any makes the compiler go through another layer of boxing, so it
+ // can hide some bugs (where we actually have a null, but the compiler makes it a ()).
+
+ var v = 0
+ def eff() = { v = 1 }
+ def chk() = { assert(v == 1); v = 0 }
+
+ val b = runtime.BoxedUnit.UNIT
+
+ assert(eff() == b); chk()
+ assert(Unit.box(eff()) == b); chk()
+ assert(().asInstanceOf[Object] == b)
+
+ Unit.unbox({eff(); b}); chk()
+ Unit.unbox({eff(); null}); chk()
+ assertThrows[ClassCastException](Unit.unbox({eff(); ""})); chk()
+
+ val n1 = null.asInstanceOf[Unit] // SI-9066: should be UNIT, but currently null
+ assertThrows[AssertionError](assert(n1 == b)) // should not throw
+
+ val n2 = null.asInstanceOf[Unit] == b // SI-9066: should be true, but currently false
+ assertThrows[AssertionError](assert(n2)) // should not throw
+
+ def f(a: Any) = "" + a
+ val n3 = f(null.asInstanceOf[Unit]) // "null", should be "()". probably same cause as SI-602.
+ assertThrows[AssertionError](assertEquals(n3, "()")) // should not throw
+ }
+}