From 90cd1f9ccd272e436b23af70aa1465c673aab25e Mon Sep 17 00:00:00 2001 From: Aleksandar Prokopec Date: Wed, 6 Jun 2012 17:29:02 +0200 Subject: 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. --- test/files/run/t5879.scala | 74 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 test/files/run/t5879.scala (limited to 'test/files/run/t5879.scala') 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)) + } + +} -- cgit v1.2.3