Skip to content

Commit 295bba2

Browse files
authored
Merge pull request #380 from ashawley/double-infinity
Fix Gen.choose for Double infinity
2 parents d094b9c + 3aa9442 commit 295bba2

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

jvm/src/test/scala/org/scalacheck/GenSpecification.scala

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,20 @@ object GenSpecification extends Properties("Gen") {
103103

104104
import Double.{MinValue, MaxValue}
105105
property("choose-large-double") = forAll(choose(MinValue, MaxValue)) { x =>
106-
MinValue <= x && x <= MaxValue
106+
MinValue <= x && x <= MaxValue && !x.isNaN
107+
}
108+
109+
import Double.{NegativeInfinity, PositiveInfinity}
110+
property("choose-infinite-double") = {
111+
forAll(Gen.choose(NegativeInfinity, PositiveInfinity)) { x =>
112+
NegativeInfinity <= x && x <= PositiveInfinity && !x.isNaN
113+
}
114+
}
115+
116+
property("choose-infinite-double-fix-zero-defect-379") = {
117+
Prop.forAllNoShrink(listOfN(3, choose(NegativeInfinity, PositiveInfinity))) { xs =>
118+
xs.exists(_ != 0d)
119+
}
107120
}
108121

109122
property("choose-xmap") = {

src/main/scala/org/scalacheck/Gen.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,12 @@ object Gen extends GenArities{
399399
new Choose[Double] {
400400
def choose(low: Double, high: Double) =
401401
if (low > high) throw new IllegalBoundsError(low, high)
402+
else if (low == Double.NegativeInfinity)
403+
frequency(1 -> const(Double.NegativeInfinity),
404+
9 -> choose(Double.MinValue, high))
405+
else if (high == Double.PositiveInfinity)
406+
frequency(1 -> const(Double.PositiveInfinity),
407+
9 -> choose(low, Double.MaxValue))
402408
else gen(chDbl(low,high))
403409
}
404410

0 commit comments

Comments
 (0)