scalacheck-toolbox
scalacheck-toolbox copied to clipboard
Filter is failing with ZonedDateTime generator
genZonedDateTime has very big space for year which is preventing strict date formatting like yyyyMMdd.
year <- Gen.choose(-292278994, 292278994)
With above range below filter mostly fails
year <- Gen.choose(-292278994, 292278994) suchThat{r => r.getYear()>1970 && r.getYear()<3000}
I know the range is due to
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
Timestamp t1 = new Timestamp(Long.MIN_VALUE);
assertEquals("292278994-08-17 07:12:55.192", t1.toString());
However, it is better if library can provide some way to constraint year range for rational generator
Hi there,
Have you seen the genDateTimeWithinRange function? This should be able to constrain the year for you.
Hello,
Thanks for pointing into right direction. However I'm facing below issue with genDateTimeWithinRange and jdk8.
import java.time.ZonedDateTime
import org.scalacheck.{Gen, Prop}
import com.fortysevendeg.scalacheck.datetime.GenDateTime.genDateTimeWithinRange
import com.fortysevendeg.scalacheck.datetime.jdk8.ArbitraryJdk8.{genDuration, genZonedDateTime}
import com.fortysevendeg.scalacheck.datetime.instances.jdk8.jdk8ForDuration
import com.fortysevendeg.scalacheck.datetime.jdk8.granularity.days
val startAndEndDateTimeGen : Gen[(ZonedDateTime,ZonedDateTime)] = for {
startZonedDateTime <- genZonedDateTime
duration <- genDuration //BTW This gen should be parametrized to control future vs past date
endZonedDateTime <- genDateTimeWithinRange(startZonedDateTime,duration)
} yield (startZonedDateTime, endZonedDateTime)
Prop.forAll(startAndEndDateTimeGen) { case (start,end ) => true }.check
! Exception raised on property evaluation.
> Exception: java.lang.ArithmeticException: long overflow
java.lang.Math.multiplyExact(Math.java:892)
Please advise.