-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add .oneOf as syntax extension on the Arbitrary companion object
- Loading branch information
Showing
3 changed files
with
57 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package at.ac.oeaw.imba.gerlich.gerlib | ||
package testing | ||
|
||
import cats.Alternative | ||
import cats.syntax.all.* | ||
|
||
import org.scalacheck.{Arbitrary, Gen} | ||
import org.scalacheck.Arbitrary.arbitrary | ||
import org.scalatest.funsuite.AnyFunSuite | ||
import org.scalatest.matchers.should | ||
import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks | ||
import at.ac.oeaw.imba.gerlich.gerlib.testing.syntax.SyntaxForScalacheck | ||
|
||
/** Tests for for the syntax extensions we provide to Scalacheck instances and | ||
* companion objects | ||
*/ | ||
class TestSyntaxExtensions | ||
extends AnyFunSuite, | ||
ScalaCheckPropertyChecks, | ||
should.Matchers, | ||
SyntaxForScalacheck // the trait under test | ||
: | ||
test( | ||
"Arbitrary.oneOf produces values of each type, in plausible relative proportions." | ||
) { | ||
given Arbitrary[Boolean | Int] = Arbitrary.oneOf[Boolean, Int] | ||
val n = 10000 | ||
val (lowerBound, upperBound) = | ||
val zStar = 3.719016 | ||
val p = 0.5 | ||
val sd = scala.math.sqrt(n * p * p) | ||
val exp = n * p | ||
(-zStar * sd + exp, zStar * sd + exp) | ||
forAll(Gen.listOfN(n, arbitrary[Boolean | Int])) { values => | ||
val (bools, ints): (List[Boolean], List[Int]) = | ||
Alternative[List].separate( | ||
values map { | ||
case p: Boolean => p.asLeft | ||
case z: Int => z.asRight | ||
} | ||
) | ||
bools.length > lowerBound shouldBe true | ||
bools.length < upperBound shouldBe true | ||
ints.length > lowerBound shouldBe true | ||
ints.length < upperBound shouldBe true | ||
} | ||
} | ||
end TestSyntaxExtensions |