|
| 1 | +package scala |
| 2 | + |
| 3 | +import org.junit.Test |
| 4 | +import org.junit.Assert._ |
| 5 | +import org.junit.runner.RunWith |
| 6 | +import org.junit.runners.JUnit4 |
| 7 | + |
| 8 | +import scala.tools.testing.AssertUtil._ |
| 9 | + |
| 10 | +@RunWith(classOf[JUnit4]) |
| 11 | +class BoxUnboxTest { |
| 12 | + def genericNull[T] = null.asInstanceOf[T] // allowed, see SI-4437, point 2 |
| 13 | + |
| 14 | + @Test |
| 15 | + def boxUnboxInt(): Unit = { |
| 16 | + val b = new Integer(1) |
| 17 | + val u = 1 |
| 18 | + |
| 19 | + assertEquals(1.toInt, u) |
| 20 | + |
| 21 | + assertEquals(Predef.int2Integer(1), b) |
| 22 | + assertEquals(1: Integer, b) |
| 23 | + assertEquals(Int.box(1), b) |
| 24 | + assertEquals(1.asInstanceOf[Object], b) |
| 25 | + |
| 26 | + assertThrows[ClassCastException]("".asInstanceOf[Integer]) |
| 27 | + |
| 28 | + assertEquals(Predef.Integer2int(b), u) |
| 29 | + assertEquals(b: Int, u) |
| 30 | + assertEquals(Int.unbox(b), u) |
| 31 | + assertEquals(b.asInstanceOf[Int], u) |
| 32 | + assertEquals(b.intValue, u) |
| 33 | + assertEquals(b.toInt, u) |
| 34 | + intWrapper(b).toInt |
| 35 | + |
| 36 | + assertThrows[ClassCastException](Int.unbox("")) |
| 37 | + assertThrows[ClassCastException]("".asInstanceOf[Int]) |
| 38 | + |
| 39 | + // null unboxing in various positions |
| 40 | + |
| 41 | + val n1 = Int.unbox(null) |
| 42 | + assertEquals(n1, 0) |
| 43 | + val n2 = Predef.Integer2int(null) |
| 44 | + assertEquals(n2, 0) |
| 45 | + val n3 = (null: Integer): Int |
| 46 | + assertEquals(n3, 0) |
| 47 | + val n4 = null.asInstanceOf[Int] |
| 48 | + assertEquals(n4, 0) |
| 49 | + val n5 = null.asInstanceOf[Int] == 0 |
| 50 | + assertTrue(n5) |
| 51 | + val n6 = null.asInstanceOf[Int] == null // SI-9671 -- should be false, but is true |
| 52 | + assertThrows[AssertionError](assertFalse(n6)) // should not throw |
| 53 | + val n7 = null.asInstanceOf[Int] != 0 |
| 54 | + assertFalse(n7) |
| 55 | + val n8 = null.asInstanceOf[Int] != null // SI-9671 -- should be true, but is false |
| 56 | + assertThrows[AssertionError](assertTrue(n8)) // should not throw |
| 57 | + |
| 58 | + val mp = new java.util.HashMap[Int, Int] |
| 59 | + val n9 = mp.get(0) |
| 60 | + assertEquals(n9, 0) |
| 61 | + val n10 = mp.get(0) == null // SI-602 -- maybe related to SI-9671 (test above)? |
| 62 | + assertThrows[AssertionError](assertFalse(n10)) // should not throw |
| 63 | + |
| 64 | + def f(a: Any) = "" + a |
| 65 | + val n11 = f(null.asInstanceOf[Int]) // "null", should be "0". probably same cause as SI-602. |
| 66 | + assertThrows[AssertionError](assertEquals(n11, "0")) // should not throw |
| 67 | + |
| 68 | + def n12 = genericNull[Int] |
| 69 | + assertEquals(n12, 0) |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + def numericConversions(): Unit = { |
| 74 | + val i1 = 1L.asInstanceOf[Int] |
| 75 | + assertEquals(i1, 1) |
| 76 | + assertThrows[ClassCastException] { |
| 77 | + val i2 = (1L: Any).asInstanceOf[Int] // SI-1448, should not throw. see also SI-4437 point 1. |
| 78 | + assertEquals(i2, 1) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + def boxUnboxBoolean(): Unit = { |
| 84 | + val n1 = Option(null.asInstanceOf[Boolean]) // SI-7397 -- should be Some(false), but is None |
| 85 | + assertThrows[AssertionError](assertEquals(n1, Some(false))) // should not throw |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + def boxUnboxUnit(): Unit = { |
| 90 | + // should not use assertEquals in this test: it takes two Object parameters. normally, Unit does |
| 91 | + // not conform to Object, but for Java-defined methods scalac makes an exception and treats them |
| 92 | + // as Any. passing a Unit as Any makes the compiler go through another layer of boxing, so it |
| 93 | + // can hide some bugs (where we actually have a null, but the compiler makes it a ()). |
| 94 | + |
| 95 | + var v = 0 |
| 96 | + def eff() = { v = 1 } |
| 97 | + def chk() = { assert(v == 1); v = 0 } |
| 98 | + |
| 99 | + val b = runtime.BoxedUnit.UNIT |
| 100 | + |
| 101 | + assert(eff() == b); chk() |
| 102 | + assert(Unit.box(eff()) == b); chk() |
| 103 | + assert(().asInstanceOf[Object] == b) |
| 104 | + |
| 105 | + Unit.unbox({eff(); b}); chk() |
| 106 | + Unit.unbox({eff(); null}); chk() |
| 107 | + assertThrows[ClassCastException](Unit.unbox({eff(); ""})); chk() |
| 108 | + |
| 109 | + val n1 = null.asInstanceOf[Unit] // SI-9066: should be UNIT, but currently null |
| 110 | + assertThrows[AssertionError](assert(n1 == b)) // should not throw |
| 111 | + |
| 112 | + val n2 = null.asInstanceOf[Unit] == b // SI-9066: should be true, but currently false |
| 113 | + assertThrows[AssertionError](assert(n2)) // should not throw |
| 114 | + |
| 115 | + def f(a: Any) = "" + a |
| 116 | + val n3 = f(null.asInstanceOf[Unit]) // "null", should be "()". probably same cause as SI-602. |
| 117 | + assertThrows[AssertionError](assertEquals(n3, "()")) // should not throw |
| 118 | + } |
| 119 | +} |
0 commit comments