summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBurak Emir <emir@epfl.ch>2006-10-04 15:44:42 +0000
committerBurak Emir <emir@epfl.ch>2006-10-04 15:44:42 +0000
commit93275f2d34ec534049baf529493dc6f2836a85ea (patch)
tree146f98f9953acf406c3b1e2c9bb2e28bca789440
parent7d72618b375a21387e18fc42b07a834cee3f2616 (diff)
downloadscala-93275f2d34ec534049baf529493dc6f2836a85ea.tar.gz
scala-93275f2d34ec534049baf529493dc6f2836a85ea.tar.bz2
scala-93275f2d34ec534049baf529493dc6f2836a85ea.zip
fixed bugs in ListBuffer and ArrayBuffer, added...
fixed bugs in ListBuffer and ArrayBuffer, added unit test
-rw-r--r--src/library/scala/collection/mutable/ArrayBuffer.scala51
-rw-r--r--src/library/scala/collection/mutable/ListBuffer.scala29
-rw-r--r--test/files/run/unittest_collection.scala95
-rwxr-xr-xtest/scalatest4
4 files changed, 144 insertions, 35 deletions
diff --git a/src/library/scala/collection/mutable/ArrayBuffer.scala b/src/library/scala/collection/mutable/ArrayBuffer.scala
index f33256782c..97bfe642a4 100644
--- a/src/library/scala/collection/mutable/ArrayBuffer.scala
+++ b/src/library/scala/collection/mutable/ArrayBuffer.scala
@@ -55,6 +55,16 @@ class ArrayBuffer[A] extends Buffer[A] with ResizableArray[A] {
this
}
+ /** returns the i-th element of this ArrayBuffer. Throws IndexOutOfBoundException if
+ * i is out of bounds.
+ */
+ override def apply(i: Int) = {
+ if ((i < 0) || (i >= size))
+ throw new IndexOutOfBoundsException(i.toString())
+ else
+ array(i)
+ }
+
/** Prepends a number of elements provided by an iterable object
* via its <code>elements</code> method. The identity of the
* buffer is returned.
@@ -72,13 +82,13 @@ class ArrayBuffer[A] extends Buffer[A] with ResizableArray[A] {
*/
def insertAll(n: Int, iter: Iterable[A]): Unit = {
if ((n < 0) || (n > size))
- error("cannot insert element " + n + " in ListBuffer")
- val xs = iter.elements.toList
- val len = xs.length
- ensureSize(size + len)
- copy(n, n + len, size - n)
- xs.copyToArray(array, n)
- size = size + len
+ throw new IndexOutOfBoundsException("cannot insert element at " + n);
+ val xs = iter.elements.toList;
+ val len = xs.length;
+ ensureSize(size+len);
+ copy(n, n + len, size - n);
+ xs.copyToArray(array, n);
+ size = size + len;
}
/** Replace element at index <code>n</code> with the new element
@@ -87,14 +97,15 @@ class ArrayBuffer[A] extends Buffer[A] with ResizableArray[A] {
* @param n the index of the element to replace.
* @param newelem the new element.
*/
- def update(n: Int, newelem: A): Unit =
+ def update(n: Int, newelem: A): Unit = {
if ((n < 0) || (n >= size))
- error("cannot update element " + n + " in ArrayBuffer")
+ throw new IndexOutOfBoundsException("cannot update element at " + n);
else {
val res = array(n)
array(n) = newelem
res
}
+ }
/** Removes the element on a given index position.
*
@@ -102,24 +113,26 @@ class ArrayBuffer[A] extends Buffer[A] with ResizableArray[A] {
*/
def remove(n: Int): A = {
if ((n < 0) || (n >= size))
- error("cannot remove element " + n + " in Buffer")
- val res = array(n)
- copy(n + 1, n, size - n - 1)
- size = size - 1
+ throw new IndexOutOfBoundsException("cannot remove element at " + n);
+ val res = array(n);
+ copy(n + 1, n, size - n - 1);
+ size = size - 1;
res
}
/** Clears the buffer contents.
*/
- def clear: Unit = { size = 0 }
+ def clear: Unit = {
+ size = 0;
+ }
/** Return a clone of this buffer.
*
* @return an <code>ArrayBuffer</code> with the same elements.
*/
override def clone(): Buffer[A] = {
- val res = new ArrayBuffer[A]
- res ++= this
+ val res = new ArrayBuffer[A];
+ res ++= this;
res
}
@@ -130,9 +143,9 @@ class ArrayBuffer[A] extends Buffer[A] with ResizableArray[A] {
override def equals(obj: Any): Boolean = obj match {
case that: ArrayBuffer[A] =>
this.length == that.length &&
- elements.zip(that.elements).forall {
- case Pair(thiselem, thatelem) => thiselem == thatelem
- }
+ elements.zip(that.elements).forall {
+ case Pair(thiselem, thatelem) => thiselem == thatelem
+ }
case _ =>
false
}
diff --git a/src/library/scala/collection/mutable/ListBuffer.scala b/src/library/scala/collection/mutable/ListBuffer.scala
index b031a4a9aa..e1f1ff4d72 100644
--- a/src/library/scala/collection/mutable/ListBuffer.scala
+++ b/src/library/scala/collection/mutable/ListBuffer.scala
@@ -66,10 +66,13 @@ final class ListBuffer[A] extends Buffer[A] {
else if (start.head == x) start = start.tail
else {
var cursor = start
- while (!cursor.tail.isEmpty && cursor.tail.head != x)
- cursor = cursor.tail
- if (!cursor.tail.isEmpty)
- cursor.asInstanceOf[scala.::[A]].tl = cursor.tail.tail
+ while (!cursor.tail.isEmpty && cursor.tail.head != x) { cursor = cursor.tail }
+ if (!cursor.tail.isEmpty) {
+ val z = cursor.asInstanceOf[scala.::[A]]
+ if(z.tl == last)
+ last = z
+ z.tl = cursor.tail.tail
+ }
}
}
@@ -110,21 +113,18 @@ final class ListBuffer[A] extends Buffer[A] {
*/
def length: int = start.length
- private def noElem(n: int): All =
- error("element " + n + " does not exist in buffer")
-
/** Returns the <code>n</code>th element of this list. This method
* yields an error if the element does not exist.
*/
def apply(n: Int): A = try {
start(n)
} catch {
- case ex: Error => noElem(n)
+ case ex: Error => throw new IndexOutOfBoundsException(n.toString())
}
/** Replace element at index <code>n</code> with the new element
- * <code>newelem</code>.
- *
+ * <code>newelem</code>. Throws IndexOutOfBoundsException if
+ * n is out of bounds.
* @param n the index of the element to replace.
* @param x the new element.
*/
@@ -146,13 +146,13 @@ final class ListBuffer[A] extends Buffer[A] {
cursor.asInstanceOf[scala.::[A]].tl = newElem
}
} catch {
- case ex: Error => noElem(n)
+ case ex: Error => throw new IndexOutOfBoundsException(n.toString())
}
/** Inserts new elements at the index <code>n</code>. Opposed to method
* <code>update</code>, this method will not replace an element with a new
* one. Instead, it will insert a new element at index <code>n</code>.
- *
+ * Throws IndexOutOfBoundsException if n is out of bounds.
* @param n the index where a new element will be inserted.
* @param iter the iterable object providing all elements to insert.
*/
@@ -181,9 +181,10 @@ final class ListBuffer[A] extends Buffer[A] {
}
}
} catch {
- case ex: Error => noElem(n)
+ case ex: Error => throw new IndexOutOfBoundsException(n.toString())
}
+
/** Removes the element on a given index position.
*
* @param n the index which refers to the element to delete.
@@ -208,7 +209,7 @@ final class ListBuffer[A] extends Buffer[A] {
}
old
} catch {
- case ex: Error => noElem(n)
+ case ex: Error => throw new IndexOutOfBoundsException(n.toString())
}
/** Returns an iterator over all elements of this list.
diff --git a/test/files/run/unittest_collection.scala b/test/files/run/unittest_collection.scala
new file mode 100644
index 0000000000..6d2a34cfb9
--- /dev/null
+++ b/test/files/run/unittest_collection.scala
@@ -0,0 +1,95 @@
+
+object Test {
+
+ import scala.testing.SUnit._
+ import scala.collection.mutable.{ArrayBuffer, Buffer, BufferProxy, ListBuffer}
+
+ trait BufferTest extends Assert {
+ def doTest(x:Buffer[String]) = {
+ // testing method +=
+ x += "one"
+ assertEquals("retrieving 'one'", x(0), "one")
+ assertEquals("length A ", x.length, 1)
+ x += "two"
+ assertEquals("retrieving 'two'", x(1), "two")
+ assertEquals("length B ", x.length, 2)
+
+ // testing method -= (removing last element)
+ x -= "two"
+
+ assertEquals("length C ", x.length, 1)
+
+ try { x(1); fail("no exception for removed element") }
+ catch { case i:IndexOutOfBoundsException => }
+
+ try { x.remove(1); fail("no exception for removed element") }
+ catch { case i:IndexOutOfBoundsException => }
+
+ x += "two2"
+ assertEquals("length D ", x.length, 2)
+
+ // removing first element
+ x.remove(0)
+ assertEquals("length E ", x.length, 1)
+
+ // toList
+ assertEquals("toList ", x.toList, List("two2"))
+
+ // clear
+ x.clear
+ assertEquals("length F ", x.length, 0)
+ }
+ }
+
+ class TArrayBuffer extends TestCase("collection.mutable.ArrayBuffer") with Assert with BufferTest {
+
+ var x: ArrayBuffer[String] = _
+
+ override def runTest = { setUp; doTest(x); tearDown }
+
+ override def setUp = { x = new scala.collection.mutable.ArrayBuffer }
+
+ override def tearDown = { x.clear; x = null }
+ }
+
+ class TListBuffer extends TestCase("collection.mutable.ListBuffer") with Assert with BufferTest {
+
+ var x: ListBuffer[String] = _
+
+ override def runTest = { setUp; doTest(x); tearDown }
+
+ override def setUp = { x = new scala.collection.mutable.ListBuffer }
+
+ override def tearDown = { x.clear; x = null }
+
+ }
+
+ class TBufferProxy extends TestCase("collection.mutable.BufferProxy") with Assert with BufferTest {
+
+ class BBuf(z:ListBuffer[String]) extends BufferProxy[String] {
+ def self = z
+ }
+
+ var x: BufferProxy[String] = _
+
+ override def runTest = { setUp; doTest(x); tearDown }
+
+ override def setUp = { x = new BBuf(new scala.collection.mutable.ListBuffer) }
+
+ override def tearDown = { x.clear; x = null }
+
+ }
+
+ def main(args:Array[String]) = {
+ val ts = new TestSuite(
+ //new TArrayBuffer,
+ new TListBuffer//,
+ //new TBufferProxy
+ )
+ val tr = new TestResult()
+ ts.run(tr)
+ for(val failure <- tr.failures) {
+ Console.println(failure)
+ }
+ }
+}
diff --git a/test/scalatest b/test/scalatest
index 804495e677..841ce0a106 100755
--- a/test/scalatest
+++ b/test/scalatest
@@ -508,8 +508,8 @@ FILES_MSIL="";
FILES_SCRIPT="";
FILES_DIS="";
-QUICK="$PREFIX/bin/quick/bin"
-QUICK_LIB="$PREFIX/bin/quick/lib/library"
+QUICK="$PREFIX/build/quick/bin"
+QUICK_LIB="$PREFIX/build/quick/lib/library"
if [ -d "$PREFIX/dists" ]; then
LATEST="$PREFIX/dists/latest/bin";