Skip to content
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

Add continuously growing collection Generators #849

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mdedetrich
Copy link

This PR adds generators which allow you to generate a collection/list that continuously grows until a condition/predicate is fullfilled (with the input of the condition being the current state of the collection being generated).

I am creating this PR because I currently have a usecase where I need to create a collection that is of specific size (in bytes, not number of elements) due to testing multipart uploads and having to make sure that generated test data is of least a specific chunk size but I suppose the problem is generic enough that it can apply to other issues.

Due to Gen being a sealed trait and certain methods inside Gen being private its not that easy to extend Gen with your own generator methods.

The naming of the methods can probably be done better, also not sure if I need to add tests for these and if so what I should be testing for/what they should look like. The failureHint's approach is also an open question due to the fact that its obviously not possible to figure out the size of the resulting collection.

@mdedetrich mdedetrich force-pushed the add-continuously-growing-collection-generators branch from 3cb4ff2 to 1b9c0da Compare October 2, 2022 08:45
@mdedetrich
Copy link
Author

@armanbilge Would it be possible to look into this?

@satorg
Copy link
Contributor

satorg commented Oct 2, 2022

@mdedetrich but is it really necessary to extend Gen for that? There's a public method infiniteLazyList (or infitineStream) in Gen that (I think) can be used to achieve pretty much the same functionality, e.g.:

  def buildableOfCondGen[C, T](fillCondition: C => Boolean, g: Gen[T])(
      implicit
      evb: Buildable[T, C],
      evt: C => Iterable[T]
  ): Gen[C] =
    Gen.infiniteLazyList(g).map { ll =>
      val it = ll.iterator
      val bldr = evb.builder
      while (!fillCondition(bldr.result()))
        bldr += it.next()
      bldr.result() // sub-optimal: is called twice for the same result, can be improved!
    }

  val vgen: Gen[Vector[Int]] = buildableOfCondGen[Vector[Int], Int](_.size == 123, Gen.chooseNum(456, 789))

  Prop.forAll(vgen) { v =>
    v.size == 123
  }.check()

And perhaps you even don't have to worry about failureHint with it.

@mdedetrich
Copy link
Author

@satorg

Perfect thanks, I didn't realize there was a flexible infiniteLazyList that can theoretically be used to do the same. Will look into this and report back!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants