diff options
author | Jason Zaugg <jzaugg@gmail.com> | 2012-12-08 09:30:42 +0100 |
---|---|---|
committer | Jason Zaugg <jzaugg@gmail.com> | 2012-12-08 11:47:03 +0100 |
commit | d526f8bd74a3a6b878dda77bf19beb60dbc28f81 (patch) | |
tree | 15696ceae54177d44e428fe73873b0d3f8c45a8a /test/files | |
parent | fd57069a3a49de1757a518b573a0cd8cb98bbbd5 (diff) | |
download | scala-d526f8bd74a3a6b878dda77bf19beb60dbc28f81.tar.gz scala-d526f8bd74a3a6b878dda77bf19beb60dbc28f81.tar.bz2 scala-d526f8bd74a3a6b878dda77bf19beb60dbc28f81.zip |
SI-6690 Release reference to last dequeued element.
Avoids leaks in MutableList and its more better known child,
mutable.Queue, when the last element is dequeued or when we
take the tail of a one element collection.
Refactors copy/pasted code between the two implementations of
tail.
Diffstat (limited to 'test/files')
-rw-r--r-- | test/files/run/t6690.scala | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/test/files/run/t6690.scala b/test/files/run/t6690.scala new file mode 100644 index 0000000000..43ede967a0 --- /dev/null +++ b/test/files/run/t6690.scala @@ -0,0 +1,62 @@ +import scala.collection.mutable + +object Test extends App { + def last0(ml: mutable.MutableList[Int]) = + ml.asInstanceOf[{def last0: mutable.LinkedList[Int]}].last0 + + def first0(ml: mutable.MutableList[Int]) = + ml.asInstanceOf[{def first0: mutable.LinkedList[Int]}].first0 + + val f = mutable.Queue[Int]() + def check(desc: String) { + assert(f.length == 0, s"$desc: non empty: $f") + assert(last0(f).isEmpty, s"$desc: last0 leak: ${last0(f)}") + assert(first0(f).isEmpty, s"$desc: first0 leak: ${last0(f)}") + } + + f.enqueue(1) + f.dequeue() + check("dequeue 1") + + f.enqueue(1) + f.enqueue(2) + f.dequeue() + assert(last0(f).toList == List(2), last0(f)) + f.dequeue() + check("dequeue 2") + + f.enqueue(1) + f.dequeueAll(_ => false) + f.dequeueAll(_ => true) + check("dequeueAll") + + f.enqueue(1) + f.dequeueFirst(_ => true) + check("dequeueFirst") + + { + f.enqueue(1) + val tail = f.tail + assert(last0(tail).isEmpty, last0(tail)) + assert(first0(tail).isEmpty, first0(tail)) + } + + { + val ml = mutable.MutableList[Int]() + 1 +=: ml + val tail = ml.tail + assert(last0(tail).isEmpty, last0(tail)) + assert(first0(tail).isEmpty, first0(tail)) + } + + { + val ml = mutable.MutableList[Int]() + 1 +=: ml + ml += 2 + val tail = ml.tail + assert(last0(tail).toList == List(2), last0(tail)) + assert(first0(tail) == last0(tail).toList, first0(tail)) + assert(last0(tail.tail).toList == Nil, last0(tail.tail).toList) + assert(first0(tail.tail) == Nil, first0(tail.tail)) + } +} |