From c8ddf01621417ef1e5f07682a360ff1b1ebfd416 Mon Sep 17 00:00:00 2001 From: Paul Phillips Date: Sun, 9 Jan 2011 06:30:35 +0000 Subject: Closes #3984 by the most arduous and indirect r... Closes #3984 by the most arduous and indirect route imaginable: abstracts the common code out of the TrieIterators in HashMap and HashSet. When I raised this flag to find out if anyone would open fire, all was quiet on the western front. Although I wouldn't want to write code like this as an everyday thing, I think it serves as a nice showcase for some of the abstraction challenges we're up against: performance looks the same and I will never again have to fix the same bug in two places. Review by rompf. --- test/files/run/bug3984.scala | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) (limited to 'test/files') diff --git a/test/files/run/bug3984.scala b/test/files/run/bug3984.scala index 3e4d2edf53..0747b0ee25 100644 --- a/test/files/run/bug3984.scala +++ b/test/files/run/bug3984.scala @@ -1,4 +1,4 @@ -object Test { +object SetBug { import scala.collection.immutable.{ Set => ImmutSet } import scala.collection.mutable.{ Set => MutSet } @@ -6,16 +6,47 @@ object Test { override def hashCode: Int = h } - def main (args: Array[String]) { + def run() { var is = ImmutSet.empty[IH] var ms = MutSet.empty[IH] for (ih <- List(IH(2,0),IH(0,0),IH(4,4),IH(6,4),IH(-8,1520786080))) { is = is + ih ms = ms + ih } + assert(is == ms) val x = IH(6,4) is = is - x ms = ms - x assert(is == ms) } } + +object MapBug { + import scala.collection.immutable.{ Map => ImmutMap } + import scala.collection.mutable.{ Map => MutMap } + + case class IH (i: Int, h: Int) { + override def hashCode: Int = h + } + + def run() { + var im = ImmutMap.empty[IH,IH] + var mm = MutMap.empty[IH,IH] + for (ih <- List(IH(2,0),IH(0,0),IH(4,4),IH(6,4),IH(-8,1520786080))) { + im = im + ((ih,ih)) + mm = mm + ((ih,ih)) + } + assert(im == mm) + val x = IH(6,4) + im = im - x + mm = mm - x + assert(im == mm) + } +} + +object Test { + def main(args: Array[String]): Unit = { + SetBug.run() + MapBug.run() + } +} -- cgit v1.2.3