summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorDenys Shabalin <denys.shabalin@epfl.ch>2015-10-08 14:03:00 +0200
committerDenys Shabalin <denys.shabalin@epfl.ch>2015-10-08 14:47:32 +0200
commit37eacec819e38cc29357a31ee99b592f31e0702f (patch)
tree54d6609e37ec82c331ac16c4d01e0e8abc44b5e9 /test
parent5614baf08b2da56532b580c7e2f70cf357832970 (diff)
downloadscala-37eacec819e38cc29357a31ee99b592f31e0702f.tar.gz
scala-37eacec819e38cc29357a31ee99b592f31e0702f.tar.bz2
scala-37eacec819e38cc29357a31ee99b592f31e0702f.zip
Fixes an inconsistency between BoxesRunTime and Predef's autoboxing
Previously autoboxing implicits in Predef were inconsistent with BoxesRunTime box/unbox due to different treatment of unboxing of nulls. Implicits didn't check for null and would crash with NPE unlike the BoxesRunTime which correctly returned zero value of given type. The fix is trivial: lets just use asInstanceOfs to implement implicits in Predef. This would ensure that both have the same behaviour and that the two would not diverge again in the future.
Diffstat (limited to 'test')
-rw-r--r--test/junit/scala/PredefAutoboxingTest.scala35
1 files changed, 35 insertions, 0 deletions
diff --git a/test/junit/scala/PredefAutoboxingTest.scala b/test/junit/scala/PredefAutoboxingTest.scala
new file mode 100644
index 0000000000..e5d8ded5d4
--- /dev/null
+++ b/test/junit/scala/PredefAutoboxingTest.scala
@@ -0,0 +1,35 @@
+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 PredefAutoboxingTest {
+ @Test def unboxNullByte() =
+ assertEquals(Predef.Byte2byte(null), 0.toByte)
+
+ @Test def unboxNullShort() =
+ assertEquals(Predef.Short2short(null), 0.toShort)
+
+ @Test def unboxNullCharacter() =
+ assertEquals(Predef.Character2char(null), 0.toChar)
+
+ @Test def unboxNullInteger() =
+ assertEquals(Predef.Integer2int(null), 0)
+
+ @Test def unboxNullLong() =
+ assertEquals(Predef.Long2long(null), 0L)
+
+ @Test def unboxNullFloat() =
+ assertEquals(Predef.Float2float(null), 0F, 0F)
+
+ @Test def unboxNullDouble() =
+ assertEquals(Predef.Double2double(null), 0D, 0D)
+
+ @Test def unboxNullBoolean() =
+ assertEquals(Predef.Boolean2boolean(null), false)
+}