Skip to main content

NumGens

NumGens provides generators that always produce ordered min/max pairs.

Import​

import hedgehog.*
import hedgehog.extra.NumGens

Ordered pair generators​

val intPairGen: Gen[(Int, Int)] =
NumGens.genIntMinMaxPair(-100, 100)
// intPairGen: GenT[Tuple2[Int, Int]] = GenT(
// hedgehog.core.GenT$$Lambda$11547/0x00007fa1b9e8b7f0@2cd43e4f
// )

val longPairGen: Gen[(Long, Long)] =
NumGens.genLongMinMaxPair(-1000L, 1000L)
// longPairGen: GenT[Tuple2[Long, Long]] = GenT(
// hedgehog.core.GenT$$Lambda$11547/0x00007fa1b9e8b7f0@50f754d0
// )
import hedgehog.*
import hedgehog.runner.*

object OrderedPairGeneratorsExampleSpec extends Properties {
override def tests: List[Test] = List(
property(
"Example for intPairGen",
testExampleForIntPairGen
).withTests(5), // only for documentation to show fewer logs
property(
"Example for longPairGen",
testExampleForLongPairGen
).withTests(5) // only for documentation to show fewer logs
)

def testExampleForIntPairGen: Property =
for {
pair <- intPairGen.log("pair")
(min, max) = pair
} yield {
println(s"intPair: ($min, $max)") // only for documentation to show the value
Result.assert(min <= max).log("Int min/max pair should satisfy min <= max")
}

def testExampleForLongPairGen: Property =
for {
pair <- longPairGen.log("pair")
(min, max) = pair
} yield {
println(s"longPair: ($min, $max)") // only for documentation to show the value
Result.assert(min <= max).log("Long min/max pair should satisfy min <= max")
}
}

// This is only for doc. Your test code should be run by sbt (or maybe another build tool)
OrderedPairGeneratorsExampleSpec.main(Array.empty)
// Using random seed: 80741980739
// intPair: (-76, -60)
// intPair: (-97, -50)
// intPair: (-99, -92)
// intPair: (-2, 39)
// intPair: (-57, 51)
// + repl.MdocSession$MdocApp$OrderedPairGeneratorsExampleSpec$.Example for intPairGen: OK, passed 5 tests
// longPair: (-760, -744)
// longPair: (-877, -764)
// longPair: (-30, 85)
// longPair: (-182, -123)
// longPair: (-989, 525)
// + repl.MdocSession$MdocApp$OrderedPairGeneratorsExampleSpec$.Example for longPairGen: OK, passed 5 tests

Functional composition with Range​

val boundedIntGen: Gen[Int] =
NumGens
.genIntMinMaxPair(1, 50)
.flatMap { case (min, max) =>
Gen.int(Range.linear(min, max))
}
// boundedIntGen: GenT[Int] = GenT(
// hedgehog.core.GenT$$Lambda$11547/0x00007fa1b9e8b7f0@21d9d5b3
// )
val boundedIntWithRangeGen: Gen[(Int, Int, Int)] =
NumGens
.genIntMinMaxPair(1, 50)
.flatMap { case (min, max) =>
Gen.int(Range.linear(min, max)).map { value =>
(min, max, value)
}
}
// boundedIntWithRangeGen: GenT[Tuple3[Int, Int, Int]] = GenT(
// hedgehog.core.GenT$$Lambda$11547/0x00007fa1b9e8b7f0@20ec0c1d
// )
import hedgehog.*
import hedgehog.runner.*

object BoundedIntGeneratorExampleSpec extends Properties {
override def tests: List[Test] = List(
property(
"Example for boundedIntGen",
testExampleForBoundedIntGen
).withTests(5) // only for documentation to show fewer logs
)

def testExampleForBoundedIntGen: Property =
for {
tuple <- boundedIntWithRangeGen.log("(min, max, value)")
(min, max, value) = tuple
} yield {
println(s"(min, max, value): ($min, $max, $value)") // only for documentation to show the value
Result
.assert(value >= min && value <= max)
.log("Generated value should stay within its generated range")
}
}

// This is only for doc. Your test code should be run by sbt (or maybe another build tool)
BoundedIntGeneratorExampleSpec.main(Array.empty)
// Using random seed: 80752585493
// (min, max, value): (2, 8, 2)
// (min, max, value): (1, 12, 1)
// (min, max, value): (1, 22, 6)
// (min, max, value): (9, 28, 18)
// (min, max, value): (19, 28, 20)
// + repl.MdocSession$MdocApp$BoundedIntGeneratorExampleSpec$.Example for boundedIntGen: OK, passed 5 tests