Fix implementation of Relation.size.

Nothing was actually using it yet, fortunately.
This commit is contained in:
Benjy 2013-11-03 10:08:46 -08:00 committed by Mark Harrah
parent 9fefc18e2d
commit 57f87fe6c1
2 changed files with 8 additions and 1 deletions

View File

@ -118,7 +118,7 @@ private final class MRelation[A,B](fwd: Map[A, Set[B]], rev: Map[B, Set[A]]) ext
def _1s = fwd.keySet
def _2s = rev.keySet
def size = fwd.size
def size = (fwd.valuesIterator map { _.size }).foldLeft(0)(_ + _)
def all: Traversable[(A,B)] = fwd.iterator.flatMap { case (a, bs) => bs.iterator.map( b => (a,b) ) }.toTraversable

View File

@ -60,6 +60,13 @@ object RelationTest extends Properties("Relation")
}
}
property("Computes size correctly") = forAll { (entries: List[(Int, Double)]) =>
val rel = Relation.empty[Int, Double] ++ entries
val expected = rel.all.size // Note: not entries.length, as entries may have duplicates.
val computed = rel.size
"Expected size: %d. Computed size: %d.".format(expected, computed) |: expected == computed
}
def all[T](s: Seq[T])(p: T => Prop): Prop =
if(s.isEmpty) true else s.map(p).reduceLeft(_ && _)
}