From 57f87fe6c12d11bfb9e2c8b219e2d4317ee92c85 Mon Sep 17 00:00:00 2001 From: Benjy Date: Sun, 3 Nov 2013 10:08:46 -0800 Subject: [PATCH] Fix implementation of Relation.size. Nothing was actually using it yet, fortunately. --- util/relation/src/main/scala/sbt/Relation.scala | 2 +- util/relation/src/test/scala/RelationTest.scala | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/util/relation/src/main/scala/sbt/Relation.scala b/util/relation/src/main/scala/sbt/Relation.scala index d97ee2321..725512d0b 100644 --- a/util/relation/src/main/scala/sbt/Relation.scala +++ b/util/relation/src/main/scala/sbt/Relation.scala @@ -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 diff --git a/util/relation/src/test/scala/RelationTest.scala b/util/relation/src/test/scala/RelationTest.scala index 03b728915..3dcc03f38 100644 --- a/util/relation/src/test/scala/RelationTest.scala +++ b/util/relation/src/test/scala/RelationTest.scala @@ -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(_ && _) }