diff options
-rw-r--r-- | sources/scala/collection/immutable/Queue.scala | 31 | ||||
-rw-r--r-- | test/files/run/iq.check | 11 | ||||
-rw-r--r-- | test/files/run/iq.scala | 76 |
3 files changed, 112 insertions, 6 deletions
diff --git a/sources/scala/collection/immutable/Queue.scala b/sources/scala/collection/immutable/Queue.scala index e1338052d8..59b489ff90 100644 --- a/sources/scala/collection/immutable/Queue.scala +++ b/sources/scala/collection/immutable/Queue.scala @@ -131,8 +131,37 @@ class Queue[+A](in:List[A],out:List[A]) extends Seq[A] { /** Returns a string representation of this queue. */ - override def toString() = (out:::(in.reverse)).toString(); + override def toString() = (out:::(in.reverse)).mkString("Queue(", ",", ")"); + /** Compares two queues for equality by comparing + * each element in the queues. + */ + override def equals(o :Any) = { + /* Make sure o is a Queue. */ + if (o is Queue[Any]) { + /* o is a queue so we cast it and store it in q. */ + val q:Queue[Any] = o as Queue[Any]; + /* A function that compares the element at + position index in q with the element at + the same position in this (queue). + If they are equal the next element is + compared. + */ + def eqe(index: int):Boolean = { + /* If all elements are compared + the queues are equal. */ + index >= this.length || + /* Otherwise: compare the elements */ + (q.apply(index) == this.apply(index) && + /* if they are equal compare the rest. */ + eqe(index+1)) + } + /* If the length of the ques are the same, + compare each element, starting at index 0. */ + (q.length == this.length) && eqe(0); + + } else false; /* o is not a queue: not equal to this. */ + } } diff --git a/test/files/run/iq.check b/test/files/run/iq.check index a2b2750392..8e794a8629 100644 --- a/test/files/run/iq.check +++ b/test/files/run/iq.check @@ -1,3 +1,12 @@ Empty Head: 42 -q5: List(0,1,2,3,4,5,6,7,8,9) +q5: Queue(0,1,2,3,4,5,6,7,8,9) +q5[5]: 5 +q5 == q5c: true +q5c == q5: true +q8: Queue(2,3,4,5,6,7,8,9,10,11) +q8 == q9: true +Elements: 1 2 3 4 5 6 7 8 9 +String: <1-2-3-4-5-6-7-8-9> +Length: 9 +Front: 1 diff --git a/test/files/run/iq.scala b/test/files/run/iq.scala index e6c571227c..a7f87409f3 100644 --- a/test/files/run/iq.scala +++ b/test/files/run/iq.scala @@ -1,15 +1,27 @@ -/* $Id$ */ +/* $Id$ + * Test file for immutable queues. + */ + +import scala.collection.immutable.Queue; object iq { def main = { - val q:scala.collection.immutable.Queue[Int] = - scala.collection.immutable.Queue.Empty; + /* Create an empty queue. */ + val q:Queue[Int] = Queue.Empty; + + /* Test isEmpty. + * Expected: Empty + */ if(q.isEmpty) { java.lang.System.out.println("Empty"); } + /* Test infix enqueing. */ val q2 = q + 42 + 0; + /* Test is empty and dequeue. + * Expected: Head: 42 + */ val q4 = if(q2.isEmpty) { java.lang.System.out.println("Empty"); @@ -20,8 +32,64 @@ object iq { q3; }; - val q5 = q4.enqueue(1,2,3,4,5,6,7,8,9); + /* Test sequence enqueing. */ + val q5:Queue[Any] = q4.enqueue(1,2,3,4,5,6,7,8,9); + /* Test toString. + * Expected: Head: q5: Queue(0,1,2,3,4,5,6,7,8,9) + */ java.lang.System.out.println("q5: " + q5); + /* Test apply + * Expected: q5[5]: 5 + */ + java.lang.System.out.println("q5[5]: " + q5.apply(5)); + + val q5c:Queue[char] = Queue.Empty.enqueue(0 as char, 1 as char, + 2 as char, 3 as char, + 4 as char, 5 as char, + 6 as char, 7 as char, + 8 as char, 9 as char); + /* Testing == + * Expected: q5 == q9: true + * q9 == q5: true + */ + java.lang.System.out.println("q5 == q5c: " + (q5 == q5c)); + java.lang.System.out.println("q5c == q5: " + (q5c == q5)); + + val Pair(_,q6) = q5.dequeue; + val Pair(_,q7) = q6.dequeue; + val q8 = q7 + 10 + 11; + /* Test dequeu + * Expected: q8: Queue(2,3,4,5,6,7,8,9,10,11) + */ + java.lang.System.out.println("q8: " + q8); + val q9 = Queue.Empty.enqueue(2,3,4,5,6,7,8,9,10,11); + + /* Testing == + * Expected: q8 == q9: true + */ + java.lang.System.out.println("q8 == q9: " + (q8 == q9)); + + /* Testing elements + * Expected: Elements: 1 2 3 4 5 6 7 8 9 + */ + java.lang.System.out.print("Elements: "); + q6.elements.foreach(e => java.lang.System.out.print(" "+ e + " ")); + java.lang.System.out.println(); + + /* Testing mkString + * Expected: String: <1-2-3-4-5-6-7-8-9> + */ + java.lang.System.out.println("String: " + q6.mkString("<","-",">")); + + /* Testing length + * Expected: Length: 9 + */ + java.lang.System.out.println("Length: " + q6.length); + + /* Testing front + * Expected: Front: 1 + */ + java.lang.System.out.println("Front: " + q6.front); } } |