Skip to content

Commit

Permalink
Updating NumericGenerator to use choose instead of chooseNum. This wi…
Browse files Browse the repository at this point in the history
…ll cause the generated values not to be weighted. Added tests to ensure none of the numeric generators are weighted.
  • Loading branch information
fulrich committed May 9, 2019
1 parent b801629 commit 69f2d40
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import org.scalacheck.Gen.Choose


case class SignGenerator[T : Choose](range: T)(implicit numeric: Numeric[T]) {
lazy val positive: Gen[T] = Gen.chooseNum[T](numeric.zero, range)
lazy val negative: Gen[T] = Gen.chooseNum[T](numeric.negate(range), numeric.zero)
lazy val default: Gen[T] = Gen.chooseNum[T](numeric.negate(range), range)
lazy val positive: Gen[T] = Gen.choose[T](numeric.zero, range)
lazy val negative: Gen[T] = Gen.choose[T](numeric.negate(range), numeric.zero)
lazy val default: Gen[T] = Gen.choose[T](numeric.negate(range), range)
}

object SignGenerator {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.github.fulrich.testcharged.generators.numerics

import org.scalacheck.Gen
import org.scalacheck.Gen.Choose
import org.scalatest.{FunSuite, Matchers}
import org.scalatest.prop.GeneratorDrivenPropertyChecks


class NumericGeneratorDistributionUTest extends FunSuite with Matchers with GeneratorDrivenPropertyChecks {

test("The double generator does not weight extremities") {
ensureExtremitiesNotWeighted(DoubleGenerators)
}

test("The integer generator does not weight extremities") {
ensureExtremitiesNotWeighted(IntGenerators)
}

test("The long generator does not weight extremities") {
ensureExtremitiesNotWeighted(LongGenerators)
}

test("The float generator does not weight extremities") {
ensureExtremitiesNotWeighted(FloatGenerators)
}

test("The short generator does not weight extremities") {
ensureExtremitiesNotWeighted(ShortGenerators)
}

def ensureExtremitiesNotWeighted[A : Choose](generator: NumericGenerator[A])(implicit numeric: Numeric[A]): Unit = {
val sizeOfList: Int = 100
val fivePercent: Int = (sizeOfList * 0.05).toInt
val listGenerator: Gen[Seq[A]] = Gen.listOfN(sizeOfList, generator.default.default)

forAll(listGenerator) { generatedNumerics =>
generatedNumerics.count(_ == generator.DefaultMaximum) should be < fivePercent
generatedNumerics.count(_ == numeric.negate(generator.DefaultMaximum)) should be < fivePercent
}
}
}

0 comments on commit 69f2d40

Please sign in to comment.