Permalink
Browse files

Fix SI-5880.

Add a ChiSquare test for the new hash code.
  • Loading branch information...
axel22 committed Jun 6, 2012
1 parent db1b777 commit daca24d8e7050eab9551b123cef010d7bf33f803
Showing with 49 additions and 2 deletions.
  1. +8 −2 src/library/scala/collection/convert/Wrappers.scala
  2. +41 −0 test/files/run/t5880.scala
@@ -171,15 +171,21 @@ private[collection] trait Wrappers {
var prev : Option[A] = None
def hasNext = ui.hasNext
def improve(hc: Int) = {
var i = hc * 0x9e3775cd
i = java.lang.Integer.reverseBytes(i)
i * 0x9e3775c
}
def next() = {
val (k, v) = ui.next
prev = Some(k)
new ju.Map.Entry[A, B] {
def getKey = k
def getValue = v
def setValue(v1 : B) = self.put(k, v1)
override def hashCode = k.hashCode + v.hashCode
override def hashCode = improve(k.hashCode) + (improve(v.hashCode) << 16)
override def equals(other: Any) = other match {
case e: ju.Map.Entry[_, _] => k == e.getKey && v == e.getValue
case _ => false
View
@@ -0,0 +1,41 @@
import scala.collection.JavaConversions._
object Test {
def main(args:Array[String]) = {
val tests = 5000
val jm: java.util.Map[Int, Int] = scala.collection.mutable.Map((0 until tests) zip (0 until tests).reverse: _*)
val es = jm.entrySet()
val it = es.iterator
// chi square test
val groups = 10
val hits = new Array[Int](groups)
def hit(hc: Int) {
val bucket = math.abs(hc) / (Int.MaxValue / groups)
hits(bucket) += 1
}
def expected = tests / groups
def Dstat = {
val diffs = for (i <- 0 until groups) yield math.abs(hits(i) - expected)
diffs.sum.toDouble / expected
}
def ChiSquare = {
val diffs = for (i <- 0 until groups) yield (hits(i) - expected) * (hits(i) - expected)
diffs.sum.toDouble / expected
}
while (it.hasNext) {
val x = it.next()
hit(x.##)
}
// println(hits.toBuffer)
// println(ChiSquare)
assert(ChiSquare < 2.0)
}
}

0 comments on commit daca24d

Please sign in to comment.