alexlipa
alexlipa

Reputation: 1291

ForAll in scala check skips some input and do not respect containers size

I am new to scala check and I want to test the following piece of my application. I want to generate 30 and 20 random events and check if my application code correctly computes a result

// generate 30 random events
val eventGenerator: Gen[Event] = for {
  d <- Gen.oneOf[String](Seq("es1", "es2", "es3"))
  t <- Gen.choose[Long](minEvent.getTime, maxEvent.getTime)
  s <- Gen.oneOf[String](Seq("s1", "s2", "s3", "s4", "s5", "s6", "s7"))} yield Event(d, t, s)
val eventsGenerator: Gen[List[VpSearchLog]] = Gen.containerOfN[List, VpSearchLog](30, eventGenerator)

// generate 20 random instances
val instanceGenerator: Gen[Instance] = for {
  d <- Gen.oneOf[String](Seq("es1", "es2", "es3"))
  t <- Gen.choose[Long](minInstance.getTime, maxInstance.getTime)} yield Instance(d, new Timestamp(t))
val instancesGenerator: Gen[List[Instance]] = Gen.containerOfN[List, Instance](20, instanceGenerator)

val p: Prop = forAll(instancesGenerator, eventsGenerator) { (i, e) =>

  println(i.size)
  println(e.size)
  println()
  val instancesWithFeature = computeExpected(instance)

  isEqual(transform(instance), instanceWithFeature)
}

For some reason I see this in the stdout

  20
  15

  20
  7

  20
  3

  20
  1

  20
  0
  starting to compute expected:

Basically it looks like the forAll generates a couple of inputs with a certain size and then skips them. For some reaon, it starts to compute things when one of the input has size 0 and then it starts the proper check. My questions are:

Upvotes: 0

Views: 216

Answers (1)

ashawley
ashawley

Reputation: 4283

You may need to use forAllNoShrink to avoid the known defect in ScalaCheck where shrinking fails to respect generators

val thirtyInts: Gen[List[Int]] =
  Gen.listOfN[Int](30, Gen.const(99))

val twentyLongs: Gen[List[Long]] =
  Gen.listOfN[Long](20, Gen.const(44L))

property("listOfN") = {
  Prop.forAllNoShrink(thirtyInts, twentyLongs) { (ii, ll) =>
    ii.size == 30 && ll.size == 20
  }
}

Upvotes: 1

Related Questions