diff options
author | Jason Zaugg <jzaugg@gmail.com> | 2017-02-10 08:22:14 +1000 |
---|---|---|
committer | Jason Zaugg <jzaugg@gmail.com> | 2017-02-10 10:14:36 +1000 |
commit | d1dbf1864c4853854701c1b0c51c0e5e21eda9d8 (patch) | |
tree | 2b1416b3717ef5f1ccd46b4bbcd93d4aac2c16cb /test | |
parent | b9d4089d19ead36d07c2d6cdda283c9b678d515e (diff) | |
download | scala-d1dbf1864c4853854701c1b0c51c0e5e21eda9d8.tar.gz scala-d1dbf1864c4853854701c1b0c51c0e5e21eda9d8.tar.bz2 scala-d1dbf1864c4853854701c1b0c51c0e5e21eda9d8.zip |
SI-10177 Override lazy operations to preserve TrieMap's semantics
Calling .iterator on a TrieMap gives a read-only snapshot. This then
extends to most inherited implementations written in terms of .iterator.
However, some inherited methods, such as .values, either defer the call
to .iterator or call it more than once. This results in subsequent
mutations to the original map being visible
I reviewed the inherited implementations from MapLike and found we
needed overrides of `values`, `keySet`, `filterKeys`, and `mapValues`.
Like `iterator`, these now create a read-only snapshot.
Diffstat (limited to 'test')
-rw-r--r-- | test/junit/scala/collection/concurrent/TrieMapTest.scala | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/test/junit/scala/collection/concurrent/TrieMapTest.scala b/test/junit/scala/collection/concurrent/TrieMapTest.scala new file mode 100644 index 0000000000..ed67f3e9a9 --- /dev/null +++ b/test/junit/scala/collection/concurrent/TrieMapTest.scala @@ -0,0 +1,54 @@ +package scala.collection.concurrent + +import org.junit.{Assert, Test} + +class TrieMapTest { + + private def check[T](result2: List[Any])(f: TrieMap[String, String] => TraversableOnce[Any]) = { + val m = TrieMap[String, String]() + val values = f(m) + m.put("k", "v") + Assert.assertEquals(Nil, values.toList) + Assert.assertEquals(result2, f(m).toList) + } + + @Test + def iterator(): Unit = { + check(List(("k", "v")))(_.iterator) + } + + @Test + def values(): Unit = { + check(List("v"))(_.values) + } + + @Test + def valuesIterator(): Unit = { + check(List("v"))(_.valuesIterator) + } + + @Test + def keySet(): Unit = { + check(List("k"))(_.keySet) + } + + @Test + def keysIterator(): Unit = { + check(List("k"))(_.keysIterator) + } + + @Test + def keys(): Unit = { + check(List("k"))(_.keys) + } + + @Test + def filterKeys(): Unit = { + check(List(("k", "v")))(_.filterKeys(_ => true)) + } + + @Test + def mapValues(): Unit = { + check(List(("k", "v")))(_.mapValues(x => x)) + } +} |