summaryrefslogtreecommitdiff
path: root/test/files/run/t5879.scala
diff options
context:
space:
mode:
authorAleksandar Prokopec <axel22@gmail.com>2012-06-06 17:29:02 +0200
committerAleksandar Prokopec <axel22@gmail.com>2012-06-06 17:32:25 +0200
commit90cd1f9ccd272e436b23af70aa1465c673aab25e (patch)
treeff3fb6246ce15163e9e908b82a35bfa5964614dd /test/files/run/t5879.scala
parentdb1b777372a6883f9c474159804b8f7798c0ec49 (diff)
downloadscala-90cd1f9ccd272e436b23af70aa1465c673aab25e.tar.gz
scala-90cd1f9ccd272e436b23af70aa1465c673aab25e.tar.bz2
scala-90cd1f9ccd272e436b23af70aa1465c673aab25e.zip
Fix SI-5879.
Fix a bug where a key in an immutable hash map have the corresponding value different in the iteration than when doing lookup. This use to happen after calling `merge`. Fix the order in which a key-value pair appears in the collision resolution function - the first argument always comes from the `this` hash map. Deprecate `merge` in favour of `merged`, as this is a pure method. As an added benefit, the syntax for invoking `merge` is now nicer.
Diffstat (limited to 'test/files/run/t5879.scala')
-rw-r--r--test/files/run/t5879.scala74
1 files changed, 74 insertions, 0 deletions
diff --git a/test/files/run/t5879.scala b/test/files/run/t5879.scala
new file mode 100644
index 0000000000..e1c07fc4c2
--- /dev/null
+++ b/test/files/run/t5879.scala
@@ -0,0 +1,74 @@
+import collection.immutable.HashMap
+
+
+object Test {
+
+ def main(args: Array[String]) {
+ resolveDefault()
+ resolveFirst()
+ resolveSecond()
+ resolveMany()
+ }
+
+ def resolveDefault() {
+ val a = HashMap(1 -> "1")
+ val b = HashMap(1 -> "2")
+
+ val r = a.merged(b)(null)
+ println(r)
+ println(r(1))
+
+ val rold = a.merge(b)
+ println(rold)
+ println(rold(1))
+ }
+
+ def resolveFirst() {
+ val a = HashMap(1 -> "1")
+ val b = HashMap(1 -> "2")
+ def collision(a: (Int, String), b: (Int, String)) = {
+ println(a)
+ a
+ }
+
+ val r = a.merged(b) { collision }
+ println(r)
+ println(r(1))
+
+ val rold = a.merge(b, collision)
+ println(rold)
+ println(rold(1))
+ }
+
+ def resolveSecond() {
+ val a = HashMap(1 -> "1")
+ val b = HashMap(1 -> "2")
+ def collision(a: (Int, String), b: (Int, String)) = {
+ println(b)
+ b
+ }
+
+ val r = a.merged(b) { collision }
+ println(r)
+ println(r(1))
+
+ val rold = a.merge(b, collision)
+ println(rold)
+ println(rold(1))
+ }
+
+ def resolveMany() {
+ val a = HashMap((0 until 100) zip (0 until 100): _*)
+ val b = HashMap((0 until 100) zip (100 until 200): _*)
+ def collision(a: (Int, Int), b: (Int, Int)) = {
+ (a._1, a._2 + b._2)
+ }
+
+ val r = a.merged(b) { collision }
+ for ((k, v) <- r) assert(v == 100 + 2 * k, (k, v))
+
+ val rold = a.merge(b, collision)
+ for ((k, v) <- r) assert(v == 100 + 2 * k, (k, v))
+ }
+
+}