diff options
author | Łukasz Gieroń <lgieron@gmail.com> | 2016-05-23 15:17:24 +0200 |
---|---|---|
committer | Lukas Rytz <lukas.rytz@typesafe.com> | 2016-05-23 15:17:24 +0200 |
commit | 41965695b71bc00ea60003c39c72a0e10bfd621f (patch) | |
tree | a9fa9f77ba80dfd3b9d5ba2ae47c228de87a5733 /test/junit | |
parent | 0d0671ae10ef552d66861248fa087306c960520e (diff) | |
download | scala-41965695b71bc00ea60003c39c72a0e10bfd621f.tar.gz scala-41965695b71bc00ea60003c39c72a0e10bfd621f.tar.bz2 scala-41965695b71bc00ea60003c39c72a0e10bfd621f.zip |
SI-9688 Make merge in immutable HashMap1 work with null kv.
The kv field of scala.collection.immutable.HashMap.HashMap1 can be null. This
commit corrects the behavior of updated0 (which is on call path for merged) to
work in such cases, instead of throwing NPE.
Commit contains regression test.
Diffstat (limited to 'test/junit')
-rw-r--r-- | test/junit/scala/collection/immutable/HashMapTest.scala | 48 |
1 files changed, 48 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 |