summaryrefslogtreecommitdiff
path: root/test/junit/scala/collection/ReusableBuildersTest.scala
blob: 8dd1a37adf81593d9c2286e8628b2de78af2b362 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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[_, _]])
  }
}