-
Notifications
You must be signed in to change notification settings - Fork 405
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Behavior of Gen.listOfN behaves different to what I was assuming based on the documentation #844
Comments
I think the scaladoc here is quite misleading. The
That basically means that if the supplied
Apparently this generator will never produce more than 10 elements since the inner generator produces chars in range 0..9. But I cannot come up with any example where |
It does what I expect from the name and not what I expect from the docs and I usually end up sampling to remember what it really does. I don't know that it should be a primitive, but I've had luck mixing it with Poisson when there's no hard max. It gives me outliers without killing test performance:
|
One concern with fixing is that people have written generators and tests that also do what they "expected", and what ScalaCheck does(!), and not what the doc describes. However, we could probably try making this change in a minor release, which would be 1.16.0, and add release notes and see how it goes. |
Indeed, there's probably not a way to change |
I opened an initial PR, #845, to give more variation to the size for There were tests even in ScalaCheck's test suite that were disrupted by this change. Changing a popular method like |
Sergey commented on the PR and suggested that these generator methods shouldn't behave inconsistently.
|
It's a valid point and why I did plan on investigating the other collection types besides |
Georgi Krastev mades a similar conclusion on the PR on what it is that could be done:
|
Sounds like I was tricked by the documentation. In fact, the name of the method is pretty accurate and the scaladoc is not.
That sounds like exactly what I was looking for, I'd love to see them in scalacheck :) If that's something the maintainers want and nobody else would like to work on that (and if it's a good first issue), I'd be happy to spend some time on implementing them.
I'll give that one a try for now and see if it helps in my particular case |
@L7R7 do you think this issue is safe to be closed now? |
Yes, can be closed. |
I have a Generator for a list of elements. To limit the number of elements in that list, I used
Gen.listOfN
instead ofGen.listOf
.The Scaladoc says "Generates a list with at most the given number of elements", so I was assuming that the generated lists will have different lengths, but never more than the provided value for the argument
n
.But instead what I see is that the lists always seem to be of length
n
.I would assume that this is the intended behavior and not a bug (based on a quick look at
buildableOfN
, it seems to be intended to aim forn
elements in the result), so probably I was fooled by the docs.Theoretically I could go with
Gen.list(...).map(_.take(n))
, but that wouldn't be efficient because the first generator would generate a lot of values that are discarded bytake
.Or something like:
which feels primitive enough to make me expect it in scalacheck directly.
Am I missing something here? Is there a different way to write an efficient generator for a list that produces lists of different lengths with a given maximum length?
The text was updated successfully, but these errors were encountered: