summaryrefslogtreecommitdiff
path: root/test/junit/scala/collection/ReusableBuildersTest.scala
diff options
context:
space:
mode:
authorRex Kerr <ichoran@gmail.com>2016-01-14 15:14:53 -0800
committerRex Kerr <ichoran@gmail.com>2016-01-30 18:32:27 -0800
commit6eaae1b969b68ed3dc65a40613a8168b09246256 (patch)
tree35118267c53111a2f9ce7e2a0ddde0ff903fa2eb /test/junit/scala/collection/ReusableBuildersTest.scala
parent78f0437ac06e71a94a38721751ef5d36ca62c062 (diff)
downloadscala-6eaae1b969b68ed3dc65a40613a8168b09246256.tar.gz
scala-6eaae1b969b68ed3dc65a40613a8168b09246256.tar.bz2
scala-6eaae1b969b68ed3dc65a40613a8168b09246256.zip
Clarified and expanded which Builders were reusable
This additionally fixes both SI-8648 and SI-9564. Added documentation to Builder to clarify that in general Builders are NOT reusable. Altered implementation of GrowingBuilder to use Growable instance's clear (not valid for a reusable builder, but this one isn't reusable). Added a new marker trait ReusableBuilder that specifies that these builders should be reusable. This trait overrides the clear and result methods while leaving them abstract in order to supply appropriate scaladoc. Made all Array builders Reusable in all cases (by setting capacity to 0 if the original array is returned). (Fixed a poor implmentation of Array[Unit] builder along the way.) Documented which other builders were already reusable (maps, sets, Vector, LazyBuilder, StringBuilder, ListBuffer, etc.).
Diffstat (limited to 'test/junit/scala/collection/ReusableBuildersTest.scala')
-rw-r--r--test/junit/scala/collection/ReusableBuildersTest.scala48
1 files changed, 48 insertions, 0 deletions
diff --git a/test/junit/scala/collection/ReusableBuildersTest.scala b/test/junit/scala/collection/ReusableBuildersTest.scala
new file mode 100644
index 0000000000..8dd1a37adf
--- /dev/null
+++ b/test/junit/scala/collection/ReusableBuildersTest.scala
@@ -0,0 +1,48 @@
+package scala.collection
+
+import org.junit.runner.RunWith
+import org.junit.runners.JUnit4
+import org.junit.Test
+
+/* Tests various maps by making sure they all agree on the same answers. */
+@RunWith(classOf[JUnit4])
+class ReusableBuildersTest {
+ // GrowingBuilders are NOT reusable but can clear themselves
+ @Test
+ def test_SI8648() {
+ val b = collection.mutable.HashSet.newBuilder[Int]
+ b += 3
+ b.clear
+ assert(!b.isInstanceOf[collection.mutable.ReusableBuilder[_,_]])
+ assert(b.isInstanceOf[collection.mutable.GrowingBuilder[_,_]])
+ assert(b.result == Set[Int]())
+ }
+
+ // ArrayBuilders ARE reusable, regardless of whether they returned their internal array or not
+ @Test
+ def test_SI9564() {
+ val b = Array.newBuilder[Float]
+ b += 3f
+ val three = b.result
+ b.clear
+ b ++= (1 to 16).map(_.toFloat)
+ val sixteen = b.result
+ b.clear
+ b += 0f
+ val zero = b.result
+ assert(b.isInstanceOf[collection.mutable.ReusableBuilder[_,_]])
+ assert(three.toList == 3 :: Nil)
+ assert(sixteen.toList == (1 to 16))
+ assert(zero.toList == 0 :: Nil)
+ }
+
+ @Test
+ def test_reusability() {
+ val bl = List.newBuilder[String]
+ val bv = Vector.newBuilder[String]
+ val ba = collection.mutable.ArrayBuffer.newBuilder[String]
+ assert(bl.isInstanceOf[collection.mutable.ReusableBuilder[_, _]])
+ assert(bv.isInstanceOf[collection.mutable.ReusableBuilder[_, _]])
+ assert(!ba.isInstanceOf[collection.mutable.ReusableBuilder[_, _]])
+ }
+}