|
| 1 | +object Test { |
| 2 | + |
| 3 | + import scala.collection.mutable.{ArrayBuffer, Buffer, BufferProxy, ListBuffer} |
| 4 | + |
| 5 | + def main(args: Array[String]): Unit = { |
| 6 | + test(collection.mutable.ArrayBuffer[String]()) |
| 7 | + test(collection.mutable.ListBuffer[String]()) |
| 8 | + class BBuf(z:ListBuffer[String]) extends BufferProxy[String] { // @odersky - bug here in scala 2.12 trait encoding seems like... |
| 9 | + def self = z |
| 10 | + } |
| 11 | + test(new BBuf(collection.mutable.ListBuffer[String]())) |
| 12 | + } |
| 13 | + |
| 14 | + def test(x: Buffer[String]): Unit = { |
| 15 | + // testing method += |
| 16 | + x += "one" |
| 17 | + assert(x(0) == "one", "retrieving 'one'") |
| 18 | + assert(x.length == 1, "length A") |
| 19 | + x += "two" |
| 20 | + assert(x(1) == "two", "retrieving 'two'") |
| 21 | + assert(x.length == 2, "length B") |
| 22 | + |
| 23 | + // testing method -= (removing last element) |
| 24 | + x -= "two" |
| 25 | + |
| 26 | + assert(x.length == 1, "length C") |
| 27 | + |
| 28 | + try { x(1); sys.error("no exception for removed element") } |
| 29 | + catch { case i:IndexOutOfBoundsException => } |
| 30 | + |
| 31 | + try { x.remove(1); sys.error("no exception for removed element") } |
| 32 | + catch { case i:IndexOutOfBoundsException => } |
| 33 | + |
| 34 | + x += "two2" |
| 35 | + assert(x.length == 2, "length D") |
| 36 | + |
| 37 | + // removing first element |
| 38 | + x.remove(0) |
| 39 | + assert(x.length == 1, "length E") |
| 40 | + |
| 41 | + // toList |
| 42 | + assert(x.toList == List("two2"), "toList") |
| 43 | + |
| 44 | + // clear |
| 45 | + x.clear() |
| 46 | + assert(x.length == 0, "length 0") |
| 47 | + assert(x.isEmpty, "isEmpty") |
| 48 | + |
| 49 | + // copyToBuffer |
| 50 | + x += "a" |
| 51 | + x += "b" |
| 52 | + val dest = new ArrayBuffer[String] |
| 53 | + x.copyToBuffer(dest) |
| 54 | + assert(List("a", "b") == dest.toList, "dest") |
| 55 | + assert(List("a", "b") == x.toList, "source") |
| 56 | + } |
| 57 | + |
| 58 | +} |
0 commit comments