Aladdin - Scala Bugtracking
[#978] project: api priority: high category: bug
submitter assigned to status date submitted
Iulian Martin fixed 2007-03-05 11:48:04.0
subject HashSet bug: 'contains' is wrong about some elements
code
class Foo(val n: Int) {
  override def hashCode = n % 2 // pretty bad hash
  override def equals(other: Any): Boolean = other match {
    case f: Foo => f.n == n
    case _ => false
  }

  override def toString = "" + n
}

object Main extends Application {
  val set = new collection.mutable.HashSet[Foo]
//  val set = new collection.jcl.HashSet[Foo]

  val max = 200
  for (val x <- 1 to max)
    set += new Foo(x)

  testRemove(2)
  testExists(2)

  def testRemove(m: Int) {
    for (val x <- 1 to max; x % m == 0) {
      val f = new Foo(x)
      set -= f
      assert(!(set contains f))
    }
  }

  def testExists(m: Int) {
    for (val x <- 1 to max; x % m == 1) {
      val f = new Foo(x)
      assert(set contains f, "For element: " + f + " set: " + set)
    }
  }

}

what happened
uneven element Foo(131) is not found in the set. (jcl.HashSet passes the test). I think the same problem makes '\
remove' not remove some elements (I couldn't find an small example to exhibit it, but it happened in my own code\
). I made this a high-priority bug because the standard library is the last place you'd search for a bug (at lea\
st that's true for me).
what expected Correct behavior.
[back to overview]
Changes of this bug report
Iulian  edited on  2007-03-05 11:48:36.0
Martin  edited on  2007-03-05 16:22:15.0