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(
// run = hedgehog.core.GenT$$Lambda$11272/0x00007f0539e262d8@656d6575
// )

val longPairGen: Gen[(Long, Long)] =
NumGens.genLongMinMaxPair(-1000L, 1000L)
// longPairGen: GenT[Tuple2[Long, Long]] = GenT(
// run = hedgehog.core.GenT$$Lambda$11272/0x00007f0539e262d8@2d9b970f
// )
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: 135862695457
// intPair: (-92, -86)
// intPair: (-76, -32)
// intPair: (-19, -12)
// intPair: (-94, -19)
// intPair: (-60, 67)
// + repl.MdocSession$MdocApp$OrderedPairGeneratorsExampleSpec$.Example for intPairGen: OK, passed 5 tests
// longPair: (-626, -620)
// longPair: (-688, -480)
// longPair: (-738, -603)
// longPair: (-994, -361)
// longPair: (-851, -702)
// + 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(
// run = hedgehog.core.GenT$$Lambda$11272/0x00007f0539e262d8@18b1fcb5
// )
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(
// run = hedgehog.core.GenT$$Lambda$11272/0x00007f0539e262d8@182d2f74
// )
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: 135870395415
// (min, max, value): (2, 10, 2)
// (min, max, value): (14, 18, 14)
// (min, max, value): (24, 29, 24)
// (min, max, value): (29, 36, 29)
// (min, max, value): (16, 33, 30)
// + repl.MdocSession$MdocApp$BoundedIntGeneratorExampleSpec$.Example for boundedIntGen: OK, passed 5 tests