aboutsummaryrefslogblamecommitdiff
path: root/tests/run/unittest_collection.scala
blob: 5db9b56d2bdc06793275956d9460c75aaae1c587 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11



                                                                                
                                         
                                                  




                                                                    

   
                                       











































                                                                      
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")
  }

}