From ba9df9f6871007c86b163ef0574d4b25f65e0945 Mon Sep 17 00:00:00 2001 From: Erik Erlandson Date: Wed, 2 Dec 2015 09:51:07 -0700 Subject: [PATCH] unit testing for semigroup variants --- .../algebird/AppendAggregatorTest.scala | 47 +++++++++++++++---- 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/algebird-test/src/test/scala/com/twitter/algebird/AppendAggregatorTest.scala b/algebird-test/src/test/scala/com/twitter/algebird/AppendAggregatorTest.scala index 1dcab3585..64769a2f6 100644 --- a/algebird-test/src/test/scala/com/twitter/algebird/AppendAggregatorTest.scala +++ b/algebird-test/src/test/scala/com/twitter/algebird/AppendAggregatorTest.scala @@ -6,16 +6,16 @@ class AppendAggregatorTest extends WordSpec with Matchers { val data = Vector.fill(100) { scala.util.Random.nextInt(100) } val mpty = Vector.empty[Int] - // test the methods that appendMonoid method defines or overrides - def testMethods[E, M, P]( - agg1: MonoidAggregator[E, M, P], - agg2: MonoidAggregator[E, M, P], + // test the methods that appendSemigroup method defines or overrides + def testMethodsSemigroup[E, M, P]( + agg1: Aggregator[E, M, P], + agg2: Aggregator[E, M, P], data: Seq[E], empty: Seq[E]) { val n = data.length val (half1, half2) = data.splitAt(n / 2) - val lhs = agg1.appendAll(half1) + val lhs = agg1.appendAll(agg1.prepare(half1.head), half1.tail) data.foreach { e => agg1.prepare(e) should be(agg2.prepare(e)) @@ -24,7 +24,6 @@ class AppendAggregatorTest extends WordSpec with Matchers { agg1.present(lhs) should be(agg2.present(lhs)) agg1(data) should be (agg2(data)) - agg1(empty) should be (agg2(empty)) agg1.applyOption(data) should be(agg2.applyOption(data)) agg1.applyOption(empty) should be(agg2.applyOption(empty)) @@ -34,15 +33,26 @@ class AppendAggregatorTest extends WordSpec with Matchers { } agg1.appendAll(lhs, half2) should be(agg2.appendAll(lhs, half2)) + } + + // test the methods that appendMonoid method defines or overrides + def testMethodsMonoid[E, M, P]( + agg1: MonoidAggregator[E, M, P], + agg2: MonoidAggregator[E, M, P], + data: Seq[E], + empty: Seq[E]) { - agg2.appendAll(data) should be(agg2.appendAll(data)) + testMethodsSemigroup(agg1, agg2, data, empty) + + agg1(empty) should be (agg2(empty)) + agg1.appendAll(data) should be(agg2.appendAll(data)) } "appendMonoid" should { "be equivalent to integer monoid aggregator" in { val agg1 = Aggregator.fromMonoid[Int] val agg2 = Aggregator.appendMonoid((m: Int, e: Int) => m + e) - testMethods(agg1, agg2, data, mpty) + testMethodsMonoid(agg1, agg2, data, mpty) } "be equivalent to set monoid aggregator" in { @@ -54,7 +64,26 @@ class AppendAggregatorTest extends WordSpec with Matchers { val agg1 = Aggregator.prepareMonoid((e: Int) => Set(e))(setMonoid) val agg2 = Aggregator.appendMonoid((m: Set[Int], e: Int) => m + e)(setMonoid) - testMethods(agg1, agg2, data, mpty) + testMethodsMonoid(agg1, agg2, data, mpty) + } + } + + "appendSemigroup" should { + "be equivalent to integer semigroup aggregator" in { + val agg1 = Aggregator.fromSemigroup[Int] + val agg2 = Aggregator.appendSemigroup(identity[Int]_, (m: Int, e: Int) => m + e) + testMethodsSemigroup(agg1, agg2, data, mpty) + } + + "be equivalent to set semigroup aggregator" in { + object setSemigroup extends Semigroup[Set[Int]] { + def plus(m1: Set[Int], m2: Set[Int]) = m1 ++ m2 + } + + val agg1 = Aggregator.prepareSemigroup((e: Int) => Set(e))(setSemigroup) + val agg2 = Aggregator.appendSemigroup((e: Int) => Set(e), (m: Set[Int], e: Int) => m + e)(setSemigroup) + + testMethodsSemigroup(agg1, agg2, data, mpty) } } }