diff options
Diffstat (limited to 'test/junit')
-rw-r--r-- | test/junit/scala/collection/immutable/HashMapTest.scala | 48 | ||||
-rw-r--r-- | test/junit/scala/collection/immutable/StringLikeTest.scala | 31 | ||||
-rw-r--r-- | test/junit/scala/lang/primitives/NaNTest.scala | 38 |
3 files changed, 117 insertions, 0 deletions
diff --git a/test/junit/scala/collection/immutable/HashMapTest.scala b/test/junit/scala/collection/immutable/HashMapTest.scala new file mode 100644 index 0000000000..a970786455 --- /dev/null +++ b/test/junit/scala/collection/immutable/HashMapTest.scala @@ -0,0 +1,48 @@ +package scala.collection.immutable + +import org.junit.Assert.assertEquals +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 + +@RunWith(classOf[JUnit4]) +class HashMapTest { + + private val computeHashF = { + HashMap.empty.computeHash _ + } + + @Test + def canMergeIdenticalHashMap1sWithNullKvs() { + def m = new HashMap.HashMap1(1, computeHashF(1), 1, null) + val merged = m.merged(m)(null) + assertEquals(m, merged) + } + + @Test + def canMergeIdenticalHashMap1sWithNullKvsCustomMerge() { + def m = new HashMap.HashMap1(1, computeHashF(1), 1, null) + val merged = m.merged(m) { + case ((k1, v1), (k2, v2)) => + (k1, v1 + v2) + } + assertEquals(new HashMap.HashMap1(1, computeHashF(1), 2, null), merged) + } + + @Test + def canMergeHashMap1sWithNullKvsHashCollision() { + val key1 = 1000L * 1000 * 1000 * 10 + val key2 = key1.##.toLong + assert(key1.## == key2.##) + + val m1 = new HashMap.HashMap1(key1, computeHashF(key1.##), 1, null) + val m2 = new HashMap.HashMap1(key2, computeHashF(key2.##), 1, null) + val expected = HashMap(key1 -> 1, key2 -> 1) + val merged = m1.merged(m2)(null) + assertEquals(expected, merged) + val mergedWithMergeFunction = m1.merged(m2) { (kv1, kv2) => + throw new RuntimeException("Should not be reached.") + } + assertEquals(expected, mergedWithMergeFunction) + } +}
\ No newline at end of file diff --git a/test/junit/scala/collection/immutable/StringLikeTest.scala b/test/junit/scala/collection/immutable/StringLikeTest.scala index 50be638b89..44bade860e 100644 --- a/test/junit/scala/collection/immutable/StringLikeTest.scala +++ b/test/junit/scala/collection/immutable/StringLikeTest.scala @@ -1,5 +1,6 @@ package scala.collection.immutable +import org.junit.Assert._ import org.junit.Test import org.junit.runner.RunWith import org.junit.runners.JUnit4 @@ -40,4 +41,34 @@ class StringLikeTest { AssertUtil.assertSameElements("--ch--omp--".split("-"), Array("", "", "ch", "", "omp")) // All the cases! AssertUtil.assertSameElements(twopairs.split(high), Array(twopairs)) //don't split on characters that are half a surrogate pair } + + /* Test for SI-9767 */ + @Test + def testNumericConversion: Unit = { + val sOne = " \t\n 1 \n\r\t " + val sOk = "2" + val sNull:String = null + + AssertUtil.assertThrows[java.lang.NumberFormatException](sOne.toInt) + AssertUtil.assertThrows[java.lang.NumberFormatException](sOne.toLong) + AssertUtil.assertThrows[java.lang.NumberFormatException](sOne.toShort) + AssertUtil.assertThrows[java.lang.NumberFormatException](sOne.toByte) + assertTrue("trim toDouble", sOne.toDouble == 1.0d) + assertTrue("trim toFloat", sOne.toFloat == 1.0f) + + assertTrue("no trim toInt", sOk.toInt == 2) + assertTrue("no trim toLong", sOk.toLong == 2L) + assertTrue("no trim toShort", sOk.toShort == 2.toShort) + assertTrue("no trim toByte", sOk.toByte == 2.toByte) + assertTrue("no trim toDouble", sOk.toDouble == 2.0d) + assertTrue("no trim toFloat", sOk.toFloat == 2.0f) + + AssertUtil.assertThrows[java.lang.NumberFormatException](sNull.toInt, {s => s == "null"}) + AssertUtil.assertThrows[java.lang.NumberFormatException](sNull.toLong, {s => s == "null"}) + AssertUtil.assertThrows[java.lang.NumberFormatException](sNull.toShort, {s => s == "null"}) + AssertUtil.assertThrows[java.lang.NumberFormatException](sNull.toByte, {s => s == "null"}) + + AssertUtil.assertThrows[java.lang.NullPointerException](sNull.toDouble) + AssertUtil.assertThrows[java.lang.NullPointerException](sNull.toFloat) + } } diff --git a/test/junit/scala/lang/primitives/NaNTest.scala b/test/junit/scala/lang/primitives/NaNTest.scala new file mode 100644 index 0000000000..f4c4258395 --- /dev/null +++ b/test/junit/scala/lang/primitives/NaNTest.scala @@ -0,0 +1,38 @@ +package scala.lang.primitives + +import org.junit.Assert._ +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 + +import scala.tools.testing.RunTesting + +@RunWith(classOf[JUnit4]) +class NaNTest extends RunTesting { + + @Test + def compNaNFalse(): Unit = { + def code(tp: String) = + s"""val n = $tp.NaN + |def ne(x: $tp, y: $tp) = x != y + |val fs: List[($tp, $tp) => Boolean] = List(_ < _, _ <= _, _ > _, _ >= _, _ == _, (x, y) => !ne(x, y)) + |val vs = List[$tp](n, 1, -1, 0) + |for (f <- fs; v <- vs; (x, y) <- List((n, v), (v, n))) yield f(x, y) + """.stripMargin + + runner.run[List[Boolean]](code("Double")).foreach(assertFalse) + runner.run[List[Boolean]](code("Float")).foreach(assertFalse) + } + + @Test + def genericEqNe(): Unit = { + def code(tp: String) = + s"""def a[T](x: T, y: T) = x == y + |def b[T](x: T, y: T) = x != y + |val n = $tp.NaN + |a(n, n) :: a(n, 0) :: a (0, n) :: !b(n, n) :: !b(n, 0) :: !b(0, n) :: Nil + """.stripMargin + runner.run[List[Boolean]](code("Double")).foreach(assertFalse) + runner.run[List[Boolean]](code("Float")).foreach(assertFalse) + } +} |