diff options
author | Adriaan Moors <adriaan@lightbend.com> | 2017-03-10 09:27:23 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-03-10 09:27:23 -0800 |
commit | 680d8661420a6ae4d0c1d9d46145d2b815255ef3 (patch) | |
tree | 6fe27d3ce9c6e51ebc4725a8b7513ae2503212d8 /test | |
parent | 0e38f28e339740d573c87c48c8854ce2085767f8 (diff) | |
parent | 898fa00a76286786427b093f0161ea0d3d3bae29 (diff) | |
download | scala-680d8661420a6ae4d0c1d9d46145d2b815255ef3.tar.gz scala-680d8661420a6ae4d0c1d9d46145d2b815255ef3.tar.bz2 scala-680d8661420a6ae4d0c1d9d46145d2b815255ef3.zip |
Merge pull request #5719 from retronym/ticket/10187
SI-10187 Support mutation of mutable.HashMap in getOrElseUpdate
Diffstat (limited to 'test')
-rw-r--r-- | test/junit/scala/collection/mutable/HashMapTest.scala | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/test/junit/scala/collection/mutable/HashMapTest.scala b/test/junit/scala/collection/mutable/HashMapTest.scala new file mode 100644 index 0000000000..cc1979a920 --- /dev/null +++ b/test/junit/scala/collection/mutable/HashMapTest.scala @@ -0,0 +1,38 @@ +package scala.collection +package mutable + +import org.junit.Assert._ +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 + +@RunWith(classOf[JUnit4]) +class HashMapTest { + + @Test + def getOrElseUpdate_mutationInCallback() { + val hm = new mutable.HashMap[String, String]() + // add enough elements to resize the hash table in the callback + def add() = 1 to 100000 foreach (i => hm(i.toString) = "callback") + hm.getOrElseUpdate("0", { + add() + "" + }) + assertEquals(Some(""), hm.get("0")) + } + + @Test + def getOrElseUpdate_evalOnce(): Unit = { + var i = 0 + val hm = new mutable.HashMap[Int, Int]() + hm.getOrElseUpdate(0, {i += 1; i}) + assertEquals(1, hm(0)) + } + + @Test + def getOrElseUpdate_noEval(): Unit = { + val hm = new mutable.HashMap[Int, Int]() + hm.put(0, 0) + hm.getOrElseUpdate(0, throw new AssertionError()) + } +} |