From e7079ca36aef1b74696f50fbdfe11d99273274d7 Mon Sep 17 00:00:00 2001 From: Rui Gonçalves Date: Wed, 21 Oct 2015 21:22:18 +0100 Subject: SI-9497 Fix SetLike#clear() default implementation When dealing with mutable collections, it is not safe to assume iterators will remain consistent when the collection is modified mid-traversal. The bug reported in SI-9497 is very similar to SI-7269, "ConcurrentModificationException when filtering converted Java HashMap". Then, only the `retain` method was fixed. This commit fixes `clear`, which had the same problem. --- .../scala/collection/mutable/SetLikeTest.scala | 26 ++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 test/junit/scala/collection/mutable/SetLikeTest.scala (limited to 'test') diff --git a/test/junit/scala/collection/mutable/SetLikeTest.scala b/test/junit/scala/collection/mutable/SetLikeTest.scala new file mode 100644 index 0000000000..c819024558 --- /dev/null +++ b/test/junit/scala/collection/mutable/SetLikeTest.scala @@ -0,0 +1,26 @@ +package scala.collection.mutable + +import org.junit.Assert._ +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 + +@RunWith(classOf[JUnit4]) +class SetLikeTest { + + class MySet(self: Set[String]) extends Set[String] with SetLike[String, MySet] { + override def -=(elem: String) = { self -= elem; this } + override def +=(elem: String) = { self += elem; this } + + override def empty = new MySet(self.empty) + override def iterator = self.iterator + override def contains(elem: String) = self.contains(elem) + } + + @Test + def hasCorrectClear() { + val s = new MySet(Set("EXPOSEDNODE", "CONNECTABLE")) + s.clear() + assertEquals(new MySet(Set()), s) + } +} -- cgit v1.2.3