47degrees / scalacheck-toolbox

A helping hand for generating sensible data with ScalaCheck
https://47degrees.github.io/scalacheck-toolbox/
Apache License 2.0
120 stars 7 forks source link

Filter is failing with ZonedDateTime generator #38

Open hghina0 opened 7 years ago

hghina0 commented 7 years ago

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

noelmarkham commented 7 years ago

Hi there,

Have you seen the genDateTimeWithinRange function? This should be able to constrain the year for you.

hghina0 commented 7 years ago

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.