Skip to content

Commit 7042270

Browse files
committed
update: address compiler errors and warnings
1 parent 6784882 commit 7042270

32 files changed

+293
-177
lines changed

algebird-test/src/main/scala/com/twitter/algebird/scalacheck/Gen.scala

+4-4
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ object gen extends ExpHistGen with IntervalGen {
4747
for {
4848
m0 <- choose(1L, Int.MaxValue.toLong)
4949
m1 <- choose(-1e50, 1e50)
50-
m2 <- choose(0, 1e50)
50+
m2 <- choose[Double](0, 1e50)
5151
m3 <- choose(-1e10, 1e50)
52-
m4 <- choose(0, 1e50)
52+
m4 <- choose[Double](0, 1e50)
5353
} yield new Moments(m0, m1, m2, m3, m4)
5454

5555
private val genLongString: Gen[String] = for {
@@ -91,8 +91,8 @@ object gen extends ExpHistGen with IntervalGen {
9191
val genRandom: Gen[Correlation] =
9292
for {
9393
c2 <- choose(-1e10, 1e10)
94-
m2x <- choose(0, 1e10)
95-
m2y <- choose(0, 1e10)
94+
m2x <- choose[Double](0, 1e10)
95+
m2y <- choose[Double](0, 1e10)
9696
m1x <- choose(-1e10, 1e10)
9797
m1y <- choose(-1e10, 1e10)
9898
m0 <- choose(-1e10, 1e10)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.twitter.algebird
2+
3+
import org.scalatestplus.scalacheck.{ScalaCheckDrivenPropertyChecks, ScalaCheckPropertyChecks}
4+
import org.scalacheck.{Arbitrary, Gen}
5+
6+
import scala.util.Random
7+
import CMSHasherImplicits.CMSHasherBigInt
8+
import org.scalatest.matchers.should.Matchers
9+
import org.scalatest.propspec.AnyPropSpec
10+
import org.scalatest.wordspec.AnyWordSpec
11+
12+
trait CMSContraMapSpecCompat { self: AnyWordSpec with Matchers with ScalaCheckDrivenPropertyChecks =>
13+
"translates CMSHasher[K] into CMSHasher[L], given a function f: L => K" in {
14+
// Given a "source" CMSHasher[K]
15+
val sourceHasher: CMSHasher[String] = CMSHasher.CMSHasherString
16+
// and a translation function from an unsupported type L (here: Seq[Byte]) to K
17+
def f(bytes: Seq[Byte]): String = new String(bytes.toArray[Byte], "UTF-8")
18+
19+
// When we run contramap on a CMSHasher[K] supplying f,
20+
// then the result should be a CMSHasher[L]...
21+
val targetHasher: CMSHasher[Seq[Byte]] =
22+
sourceHasher.contramap((d: Seq[Byte]) => f(d))
23+
targetHasher shouldBe an[CMSHasher[
24+
_
25+
]] // Can't test CMSHasher[Seq[Byte]] specifically because of type erasure.
26+
27+
// ...and hashing should work correctly (this is only a smoke test).
28+
val a = 4
29+
val b = 0
30+
val width = 1234
31+
val x = Array(113.toByte).toSeq // same as Seq(133.toByte)
32+
val result = targetHasher.hash(a, b, width)(x)
33+
val expected = sourceHasher.hash(a, b, width)("q")
34+
result should be(expected)
35+
result should be(434)
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.twitter.algebird
2+
3+
import org.scalacheck.Arbitrary
4+
import org.scalacheck.Prop._
5+
import org.scalacheck.Prop
6+
import org.scalatest.funsuite.AnyFunSuite
7+
8+
trait IntervalLawsCompat { self: CheckProperties =>
9+
import com.twitter.algebird.scalacheck.arbitrary._
10+
import com.twitter.algebird.Interval.GenIntersection
11+
property("if boundedLeast is none, we are Universe, Upper or isEmpty is true") {
12+
forAll { (a: Interval[Long]) =>
13+
a.boundedLeast match {
14+
case Some(_) => true
15+
case None =>
16+
a.isEmpty || (a match {
17+
case _: Upper[_] => true
18+
case Universe() => true
19+
case _ => false
20+
})
21+
}
22+
}
23+
}
24+
property("if boundedGreatest is none, we are Universe, Lower or isEmpty is true") {
25+
forAll { (a: Interval[Long]) =>
26+
a.boundedGreatest match {
27+
case Some(_) => true
28+
case None =>
29+
a.isEmpty || (a match {
30+
case _: Lower[_] => true
31+
case Universe() => true
32+
case _ => false
33+
})
34+
}
35+
}
36+
}
37+
38+
}

algebird-test/src/test/scala-2.12+/com/twitter/algebird/AggregatorLaws.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ class AggregatorLaws extends CheckProperties {
126126
}
127127

128128
def checkNumericSum[T: Arbitrary](implicit num: Numeric[T]): Prop =
129-
forAll { in: List[T] =>
129+
forAll { (in: List[T]) =>
130130
val aggregator = Aggregator.numericSum[T]
131131
val ares = aggregator(in)
132132
val sres = in.map(num.toDouble).sum
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.twitter.algebird
2+
3+
import org.scalatestplus.scalacheck.{ScalaCheckDrivenPropertyChecks, ScalaCheckPropertyChecks}
4+
import org.scalacheck.{Arbitrary, Gen}
5+
6+
import scala.util.Random
7+
import CMSHasherImplicits.CMSHasherBigInt
8+
import org.scalatest.matchers.should.Matchers
9+
import org.scalatest.propspec.AnyPropSpec
10+
import org.scalatest.wordspec.AnyWordSpec
11+
12+
trait CMSContraMapSpecCompat { self: AnyWordSpec with Matchers with ScalaCheckDrivenPropertyChecks =>
13+
"translates CMSHasher[K] into CMSHasher[L], given a function f: L => K" in {
14+
// Given a "source" CMSHasher[K]
15+
val sourceHasher: CMSHasher[String] = CMSHasher.CMSHasherString
16+
// and a translation function from an unsupported type L (here: Seq[Byte]) to K
17+
def f(bytes: Seq[Byte]): String = new String(bytes.toArray[Byte], "UTF-8")
18+
19+
// When we run contramap on a CMSHasher[K] supplying f,
20+
// then the result should be a CMSHasher[L]...
21+
val targetHasher: CMSHasher[Seq[Byte]] =
22+
sourceHasher.contramap((d: Seq[Byte]) => f(d))
23+
targetHasher shouldBe an[CMSHasher[
24+
?
25+
]] // Can't test CMSHasher[Seq[Byte]] specifically because of type erasure.
26+
27+
// ...and hashing should work correctly (this is only a smoke test).
28+
val a = 4
29+
val b = 0
30+
val width = 1234
31+
val x = Array(113.toByte).toSeq // same as Seq(133.toByte)
32+
val result = targetHasher.hash(a, b, width)(x)
33+
val expected = sourceHasher.hash(a, b, width)("q")
34+
result should be(expected)
35+
result should be(434)
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.twitter.algebird
2+
3+
import org.scalacheck.Arbitrary
4+
import org.scalacheck.Prop._
5+
import org.scalacheck.Prop
6+
import org.scalatest.funsuite.AnyFunSuite
7+
8+
trait IntervalLawsCompat { self: CheckProperties =>
9+
import com.twitter.algebird.scalacheck.arbitrary._
10+
import com.twitter.algebird.Interval.GenIntersection
11+
property("if boundedLeast is none, we are Universe, Upper or isEmpty is true") {
12+
forAll { (a: Interval[Long]) =>
13+
a.boundedLeast match {
14+
case Some(_) => true
15+
case None =>
16+
a.isEmpty || (a match {
17+
case _: Upper[?] => true
18+
case Universe() => true
19+
case _ => false
20+
})
21+
}
22+
}
23+
}
24+
property("if boundedGreatest is none, we are Universe, Lower or isEmpty is true") {
25+
forAll { (a: Interval[Long]) =>
26+
a.boundedGreatest match {
27+
case Some(_) => true
28+
case None =>
29+
a.isEmpty || (a match {
30+
case _: Lower[?] => true
31+
case Universe() => true
32+
case _ => false
33+
})
34+
}
35+
}
36+
}
37+
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.twitter.algebird
2+
3+
import org.scalatestplus.scalacheck.Checkers
4+
import org.scalatest.propspec.AnyPropSpec
5+
6+
/**
7+
* @author
8+
* Mansur Ashraf.
9+
*/
10+
trait CheckProperties extends AnyPropSpec with Checkers

algebird-test/src/test/scala/com/twitter/algebird/AbstractAlgebraTest.scala

+12-8
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ class AbstractAlgebraTest extends CheckProperties with Matchers {
99

1010
property("A Monoid should be able to sum") {
1111
val monoid = implicitly[Monoid[Int]]
12-
forAll { intList: List[Int] => intList.sum == monoid.sum(intList) }
12+
forAll((intList: List[Int]) => intList.sum == monoid.sum(intList))
1313
}
1414

1515
property("A Ring should be able to product") {
1616
val ring = implicitly[Ring[Int]]
17-
forAll { intList: List[Int] => intList.product == ring.product(intList) }
17+
forAll((intList: List[Int]) => intList.product == ring.product(intList))
1818
}
1919

2020
property("An OptionMonoid should be able to sum") {
2121
val monoid = implicitly[Monoid[Option[Int]]]
2222

23-
forAll { intList: List[Option[Int]] =>
23+
forAll { (intList: List[Option[Int]]) =>
2424
val flattenedList = intList.flatten
2525
val expectedResult =
2626
if (flattenedList.nonEmpty) Some(flattenedList.sum) else None
@@ -31,7 +31,7 @@ class AbstractAlgebraTest extends CheckProperties with Matchers {
3131
property("An OptionMonoid based on a Semigroup should be able to sum") {
3232
val maxMonoid = implicitly[Monoid[Option[Max[Int]]]]
3333
val minMonoid = implicitly[Monoid[Option[Min[Int]]]]
34-
forAll { intList: List[Option[Int]] =>
34+
forAll { (intList: List[Option[Int]]) =>
3535
val minList = intList.map {
3636
case Some(x) => Some(Min(x))
3737
case None => None
@@ -84,7 +84,7 @@ class AbstractAlgebraTest extends CheckProperties with Matchers {
8484

8585
property("An ArrayMonoid should sum") {
8686
val monoid = new ArrayMonoid[Int]
87-
forAll { intList: List[Int] =>
87+
forAll { (intList: List[Int]) =>
8888
val (l, r) = intList.splitAt(intList.size / 2)
8989
val left = l.padTo(math.max(l.size, r.size), 0)
9090
val right = r.padTo(math.max(l.size, r.size), 0)
@@ -97,7 +97,7 @@ class AbstractAlgebraTest extends CheckProperties with Matchers {
9797

9898
property("An ArrayGroup should negate") {
9999
val arrayGroup = new ArrayGroup[Int]
100-
forAll { intList: List[Int] =>
100+
forAll { (intList: List[Int]) =>
101101
intList.map(-1 * _).toSeq == arrayGroup
102102
.negate(intList.toArray)
103103
.toSeq
@@ -106,8 +106,12 @@ class AbstractAlgebraTest extends CheckProperties with Matchers {
106106

107107
property("a user-defined product monoid should work") {
108108
case class Metrics(count: Int, largestValue: Option[Max[Int]])
109-
implicit val MetricsMonoid = Monoid(Metrics.apply _, Metrics.unapply _)
110-
implicit val metricsGen = Arbitrary {
109+
val unapply: Metrics => Option[(Int, Option[Max[Int]])] = { case Metrics(count, largestValue) =>
110+
Some((count, largestValue))
111+
}
112+
implicit val MetricsMonoid: Monoid[Metrics] =
113+
Monoid.apply[Metrics, Int, Option[Max[Int]]](Metrics.apply _, unapply)
114+
implicit val metricsGen: Arbitrary[Metrics] = Arbitrary[Metrics] {
111115
for {
112116
count <- Gen.choose(0, 10000)
113117
largest <- Gen.oneOf[Option[Max[Int]]](None, Gen.choose(1, 100).map(n => Some(Max(n))))

algebird-test/src/test/scala/com/twitter/algebird/ApplicativeProperties.scala

+7-7
Original file line numberDiff line numberDiff line change
@@ -54,35 +54,35 @@ class ApplicativeProperties extends CheckProperties {
5454
property("sequenceGen") {
5555
// This follows from the laws, so we are just testing
5656
// the implementation of sequenceGen against sequence here
57-
forAll { ls: List[Option[Int]] =>
57+
forAll { (ls: List[Option[Int]]) =>
5858
val res: Option[Vector[Int]] = Applicative.sequenceGen(ls)
5959
Applicative.sequence(ls).map(_.toVector) == res
6060
}
6161
}
6262
// Applicative algebras:
6363
import BaseProperties._
6464
property("Applicative Semigroup") {
65-
implicit val optSg = new ApplicativeSemigroup[Int, Option]
66-
implicit val listSg = new ApplicativeSemigroup[String, List]
65+
implicit val optSg: Semigroup[Option[Int]] = new ApplicativeSemigroup[Int, Option]
66+
implicit val listSg: Semigroup[List[String]] = new ApplicativeSemigroup[String, List]
6767
// the + here is actually a cross-product, and testing sumOption blows up
6868
semigroupLaws[Option[Int]] && isAssociative[List[String]]
6969
}
7070

7171
property("Applicative Monoid") {
72-
implicit val optSg = new ApplicativeMonoid[Int, Option]
73-
implicit val listSg = new ApplicativeMonoid[String, List]
72+
implicit val optSg: Monoid[Option[Int]] = new ApplicativeMonoid[Int, Option]
73+
implicit val listSg: Monoid[List[String]] = new ApplicativeMonoid[String, List]
7474
// the + here is actually a cross-product, and testing sumOption blows up
7575
monoidLaws[Option[Int]] && validZero[List[String]]
7676
}
7777

7878
// These laws work for only "non-empty" monads
7979
property("Applicative Group") {
80-
implicit val optSg = new ApplicativeGroup[Int, Some]
80+
implicit val optSg: ApplicativeGroup[Int, Some] = new ApplicativeGroup[Int, Some]
8181
groupLaws[Some[Int]]
8282
}
8383

8484
property("Applicative Ring") {
85-
implicit val optSg = new ApplicativeRing[Int, Some]
85+
implicit val optSg: ApplicativeRing[Int, Some] = new ApplicativeRing[Int, Some]
8686
ringLaws[Some[Int]]
8787
}
8888
}

algebird-test/src/test/scala/com/twitter/algebird/AveragedValueLaws.scala

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class AveragedValueLaws extends CheckProperties {
2626
}
2727

2828
property("AveragedValue.aggregator returns the average") {
29-
forAll { v: NonEmptyVector[Double] => approxEq(1e-10)(avg(v.items), AveragedValue.aggregator(v.items)) }
29+
forAll { (v: NonEmptyVector[Double]) => approxEq(1e-10)(avg(v.items), AveragedValue.aggregator(v.items)) }
3030
}
3131

3232
property("AveragedValue instances subtract") {
@@ -42,7 +42,7 @@ class AveragedValueLaws extends CheckProperties {
4242
}
4343

4444
property("AveragedValue works by + or sumOption") {
45-
forAll { v: NonEmptyVector[Double] =>
45+
forAll { (v: NonEmptyVector[Double]) =>
4646
val avgs = v.items.map(AveragedValue(_))
4747
val sumOpt = Semigroup.sumOption[AveragedValue](avgs).get
4848
val byPlus = avgs.reduce(_ + _)
@@ -53,7 +53,7 @@ class AveragedValueLaws extends CheckProperties {
5353
}
5454

5555
def numericAggregatorTest[T: Numeric: Arbitrary]: Prop =
56-
forAll { v: NonEmptyVector[T] =>
56+
forAll { (v: NonEmptyVector[T]) =>
5757
val averaged = AveragedValue.numericAggregator[T].apply(v.items)
5858
approxEq(1e-10)(avg(v.items), averaged)
5959
}

algebird-test/src/test/scala/com/twitter/algebird/BloomFilterTest.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -250,13 +250,13 @@ class BloomFilterProperties extends ApproximateProperties("BloomFilter") {
250250

251251
for (falsePositiveRate <- List(0.1, 0.01, 0.001)) {
252252
property(s"has small false positive rate with false positive rate = $falsePositiveRate") = {
253-
implicit val intGen = Gen.choose(1, 1000)
253+
implicit val intGen:Gen[Int] = Gen.choose(1, 1000)
254254
toProp(new BloomFilterFalsePositives[Int](falsePositiveRate), 50, 50, 0.01)
255255
}
256256
}
257257

258258
property("approximate cardinality") = {
259-
implicit val intGen = Gen.choose(1, 1000)
259+
implicit val intGen:Gen[Int] = Gen.choose(1, 1000)
260260
toProp(new BloomFilterCardinality[Int], 50, 1, 0.01)
261261
}
262262
}

0 commit comments

Comments
 (0)