## Where communities thrive

• Join over 1.5M+ people
• Join over 100K+ communities
• Free without limits
• Create your own community
##### Activity
João Costa
@jd557:matrix.org
[m]
fwiw, you have a typo in your example (you have written "Old shrink" twice) :P
Nicolas Rinaudo
@nrinaudo
yeah I realised afterwards, and was kind of hoping no one would pick up on it. Clearly, that was in vain :)
a couple of important things to note:
• this implementation can be centered on any number, not just 0, which can be useful when you're working with, say, ints between 50 and 100.
• it starts by attempting the destination (0, here), because that's the largest possible shrink and will often end up short-circuiting the entire process
• I take exactly 0 credit for it, it's really just me ripping it from Hedgehog (which, itself, ripped it from QuickCheck)
Nicolas Rinaudo
@nrinaudo
I'd have pasted a sample shrink tree, but since this is so much more thorough than the default shrink, they end up being huge and not terribly useful.
Yuliban
@yupegom_gitlab

Hi everyone. I'm trying to generate an ADT that is previously validated. So, instead of returning the actual ADT it is returning a Validate[ADT]. This is the generator:

val coordinatesGen: Gen[ValidationResult[Coordinates]] = for {
latitude <- arbitrary[Double].suchThat(BigDecimal(_).scale <= 8).suchThat(_.abs < 90)
longitude <- arbitrary[Double].suchThat(BigDecimal(_).scale <= 8).suchThat(_.abs < 180)
} yield {
Coordinates(
latitude = latitude,
longitude = longitude
)
}

implicit val arbCoordinatesGen: Arbitrary[ValidationResult[Coordinates]] = Arbitrary(coordinatesGen)

And this is the test:

property("coordinates") = forAll { coordinates: ValidationResult[Coordinates] =>
coordinates.isValid
}

This is not working tho, it seems like no test is passing: Generating coordinates.coordinates: Gave up after only 0 passed tests. 501 tests were discarded.. Is this related to the fact that I'm returning a ValidationResult instead of the actual ADT?

Christopher Davenport
@ChristopherDavenport
You want to use restricted generators rather than using filters like that .
Gen.between I think is the one I'm thinking of
Yuliban
@yupegom_gitlab
Thanks @ChristopherDavenport . When you say "rather than using filters like that" you mean this filter: coordinates.isValid?
Yuliban
@yupegom_gitlab
Oh I got it you meant avoiding using suchThat filters basically. Why those wouldn't work?
Christopher Davenport
@ChristopherDavenport
Because those just throw away the value, if it's too strict then it will fail like your case did above
Aaron S. Hawley
@ashawley
@nrinaudo Yes, feel free to open a PR for improved numeric shrinking. Missed your message earlier. I'm not sure why ScalaCheck has a different shrink from Quickcheck. It could be that Scalacheck fell behind the times.
Nicolas Rinaudo
@nrinaudo
@ashawley will do. Am I ok to basically port the quickcheck version, or do you want to retain the scalacheck twist of swapping signs?
On an unrelated subject, does anyone know why Arbitrary exists? I assumed because it does in QuickCheck, but in QuickCheck, it’s purpose is to aggregate Gen and Shrink, which I don’t think ScalaCheck does?
Aaron S. Hawley
@ashawley
No, it wouldn't seem that it's necessary to arbitrarily swap signs if the halving strategy is smartly shrinking across the negative domain.
Nicolas Rinaudo
@nrinaudo
It does not - if the initial failing test case is positive, and the “center” of the gen’s interval is 0+, you’ll never explore negative values. But that seems desirable to me
Exploring values on the other side of the interval’s “zero” feels like looking for a different failure symptom. But it can definitely be argued both ways, and it’s easy enough to implement sign swapping, so if you’d rather keep it for scenarios where zero is 0, the smallest positive failing test case is 123456789 and the smallest negative one -1, then I can definitely do that
Anton Sviridov
@velvetbaldmime:matrix.org
[m]

Apologies in advance for the super vague question - still investigating.

We have some tests that were setting Seed(0) generating several instances of a fairly large generator.

After doing some rounds of upgrades (Scala 2.12 patch from 12 to 13), we noticed that the values generated are no longer the same.

So my question is - are all Seeds born equal? Could there be something special about our poor choice of seed 0?

Scalacheck is 1.14.0, but 1.15.4 has the same behaviour. I'm really clutching at straws here, everything else failed :)

(note that we don't use the generated values in a property, we just generate the values and compare them against a pre-defined set. It's not a good test.)
Aaron S. Hawley
@ashawley
@velvetbaldmime:matrix.org Using seed in a property test is usually an anti-pattern. ScalaCheck does it in its test suite to verify its implementation. Now that ScalaCheck prints the failing seed, it seems less necessary for a user to use it.
I don't recall what happened to the Seed implementation in 1.14.0 or earlier. I know in 1.15.0 there was #674 and #651.
Anton Sviridov
@velvetbaldmime:matrix.org
[m]
Yeah, this whole test is sadly an anti-pattern and we're trying to convert it, while attempting to understand what could possibly have affected it :D
Artem Nikiforov
@nikiforo

Hi all,
I want generated values to be unique by some field.
For instance,

case class Entity(id: Int, code: String)

check { entities: List[Entity] =>
// I want every generated entity in a list to have unique code
}

Is it possible?

Aaron S. Hawley
@ashawley
@nikiforo This is one way to do it
  property("genEntity") = {
val genEntity: Gen[List[Entity]] = {
for {
id <- Arbitrary.arbitrary[Int]
codes <- Gen.containerOf[Set,String](Arbitrary.arbitrary[String])
code <- codes.toList
} yield {
Entity(id, code)
}
}
Prop.forAllNoShrink(genEntity) { (entities: List[Entity]) =>
Prop.collect(entities.size) {
entities.size >= 0
}
}
}
Artem Nikiforov
@nikiforo

Hi all,
Consider this code:

  var seen = 0
var filtered = 0
private val genInt =
Arbitrary
.arbInt
.arbitrary
.map { v => seen += 1; v }
.filter(_ % 2 == 1)
.map { v => filtered += 1; v }

private implicit val arbInt: Arbitrary[Int] = Arbitrary(genInt)

test("uniqueGen") {
var listed = 0
check { ints: List[Int] =>
listed += ints.length
println(s"$seen/$filtered/\$listed")
true
}
}

running tests results in Gave up after 5 successful property evaluations. 51 evaluations were discarded.

And the logs are:

0/0/0
4/3/3
114/43/3
310/100/4
0/0/0
0/0/0
4/1/1
19/7/7
155/47/11

From my POV this check should succeed, shoudn't it?

Aaron S. Hawley
@ashawley
It should, but creating a collection (i.e. List) of Ints has a filter for size (see definition of Gen.buildableOfN) and then your Gen[Int] has a filter for oddsthat causes ScalaCheck to exhaust the number of attempts. You might try retryUntil instead of filterbut that suggests a different implementation.
Actually the error message is from Scalatest, which has its own property checking implementation now, so my advice may be off, although I believe Scalatest property testing is similar to ScalaCheck.
Maatary
@Maatary
Hi, I need to generate some random json sample, compliant to a schema supplied dynamically. Meaning that the input would be a schema (e.g. json-schema) and the output would be a json that complies to it.
So far i have no clue how to go about it. Hence I'm looking for pointers ? Any suggestion on this ?
4 replies
Hi, I have a third party lib that has its own random id generateId method based on instance of Random. I want to create a generator that produces valid values using that method. But as far as I can see all access to constructors is private in Gen, so I wonder how can I create that one (I will need to access Seed to provide Random based on it to the generateId method)?