aboutsummaryrefslogtreecommitdiff
path: root/tests/run/unittest_collection.scala
blob: d10845475b745ebb3a0328a8c37fbdc01aa25272 (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
49
50
51
52
53
54
55
56
57
58
object Test {

  import scala.collection.mutable.{ArrayBuffer, Buffer, BufferProxy, ListBuffer}

  def main(args: Array[String]): Unit = {
    test(collection.mutable.ArrayBuffer[String]())
    test(collection.mutable.ListBuffer[String]())
    class BBuf(z:ListBuffer[String]) extends BufferProxy[String] {
      def self = z
    }
    test(new BBuf(collection.mutable.ListBuffer[String]()))
  }

  def test(x: Buffer[String]): Unit = {
    // testing method +=
    x += "one"
    assert(x(0) == "one", "retrieving 'one'")
    assert(x.length == 1, "length A")
    x += "two"
    assert(x(1) == "two", "retrieving 'two'")
    assert(x.length == 2, "length B")

    // testing method -= (removing last element)
    x -=  "two"

    assert(x.length == 1, "length C")

    try { x(1); sys.error("no exception for removed element") }
    catch { case i:IndexOutOfBoundsException => }

    try { x.remove(1); sys.error("no exception for removed element") }
    catch { case i:IndexOutOfBoundsException => }

    x += "two2"
    assert(x.length == 2, "length D")

    // removing first element
    x.remove(0)
    assert(x.length == 1, "length E")

    // toList
    assert(x.toList == List("two2"), "toList")

    // clear
    x.clear()
    assert(x.length == 0, "length 0")
    assert(x.isEmpty, "isEmpty")

    // copyToBuffer
    x += "a"
    x += "b"
    val dest = new ArrayBuffer[String]
    x.copyToBuffer(dest)
    assert(List("a", "b") == dest.toList, "dest")
    assert(List("a", "b") == x.toList, "source")
  }

}