Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions test-tests/shared/src/test/scala/zio/test/GenSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ object GenSpec extends ZIOBaseSpec {
testM("anyOffsetDateTime generates OffsetDateTime values") {
checkSample(Gen.anyOffsetDateTime)(isNonEmpty)
},
testM("bigDecimal generates values in range") {
val min = BigDecimal("1.414213562373095048801688724209698")
val max = BigDecimal("2.0")
val bigDecimal = Gen.bigDecimal(min, max)
checkSample(bigDecimal)(forall(isGreaterThanEqualTo(min) && isLessThanEqualTo(max)))
},
testM("bigInt generates values in range") {
val min = BigInt("1")
val max = BigInt("265252859812191058636308480000000")
Expand Down
14 changes: 14 additions & 0 deletions test/shared/src/main/scala/zio/test/Gen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,20 @@ object Gen extends GenZIO with FunctionVariants with TimeVariants {
(leastSigBits & ~(0xC0000000L << 32)) | (0x80000000L << 32)
)

/**
* A generator of big decimals inside the specified range: [start, end].
* The shrinker will shrink toward the lower end of the range ("smallest").
*
* The values generated will have a precision equal to the precision of the
* difference between `max` and `min`.
*/
def bigDecimal(min: BigDecimal, max: BigDecimal): Gen[Random, BigDecimal] = {
val difference = max - min
val decimals = difference.scale max 0
val bigInt = (difference * BigDecimal(10).pow(decimals)).toBigInt
Gen.bigInt(0, bigInt).map(bigInt => min + BigDecimal(bigInt) / BigDecimal(10).pow(decimals))
}

/**
* A generator of big integers inside the specified range: [start, end].
* The shrinker will shrink toward the lower end of the range ("smallest").
Expand Down