Generating list of lists with custom value limitations with Hypothesis
<p><strong>The Story:</strong></p>

<p>Currently, I have a function-under-test that expects a <em>list of lists of integers</em> with the following rules:</p>

<ol>
<li>number of sublists (let's call it <code>N</code>) can be from 1 to 50</li>
<li>number of values inside sublists is the same for all sublists (rectangular form) and should be >= 0 and &lt;= 5</li>
<li>values inside sublists cannot be more than or equal to the total number of sublists. In other words, each value inside a sublist is an integer >= 0 and &lt; <code>N</code></li>
</ol>

<p>Sample valid inputs:</p>

<pre><code>[[0]]
[[2, 1], [2, 0], [3, 1], [1, 0]]
[[1], [0]]
</code></pre>

<p>Sample invalid inputs:</p>

<pre><code>[[2]]  # 2 is more than N=1 (total number of sublists)
[[0, 1], [2, 0]]  # 2 is equal to N=2 (total number of sublists)
</code></pre>

<p>I'm trying to approach it with <em>property-based-testing</em> and generate different valid inputs with <a href="http://hypothesis.readthedocs.io/en/latest/index.html"><code>hypothesis</code> library</a> and trying to wrap my head around <code>lists()</code> and <code>integers()</code>, but cannot make it work:</p>

<ul>
<li>the condition #1 is easy to approach with <code>lists()</code> and <code>min_size</code> and <code>max_size</code> arguments</li>
<li>the condition #2 is covered under <a href="http://hypothesis.readthedocs.io/en/latest/data.html#chaining-strategies-together"><code>Chaining strategies together</code></a></li>
<li>the condition #3 is what I'm struggling with - cause, if we use the <code>rectangle_lists</code> from the above example, we don't have a reference to the length of the "parent" list inside <code>integers()</code> </li>
</ul>

<p><strong>The Question:</strong></p>

<p>How can I limit the integer values inside sublists to be less than the total number of sublists?</p>

<hr>

<p>Some of my attempts:</p>

<pre><code>from hypothesis import given
from hypothesis.strategies import lists, integers

@given(lists(lists(integers(min_value=0, max_value=5), min_size=1, max_size=5), min_size=1, max_size=50))
def test(l):
    # ...
</code></pre>

<p>This one was very far from meeting the requirements - list is not strictly of a rectangular form and generated integer values can go over the generated size of the list.</p>

<pre><code>from hypothesis import given
from hypothesis.strategies import lists, integers

@given(integers(min_value=0, max_value=5).flatmap(lambda n: lists(lists(integers(min_value=1, max_value=5), min_size=n, max_size=n), min_size=1, max_size=50)))
def test(l):
    # ...
</code></pre>

<p>Here, the #1 and #2 are requirements were being met, but the integer values can go larger than the size of the list - requirement #3 is not met.</p>

<p>There's a good general technique that is often useful when trying to solve tricky constraints like this: try to build something that looks a bit like what you want but doesn't satisfy all the constraints and then compose it with a function that modifies it (e.g. by throwing away the bad bits or patching up bits that don't quite work) to make it satisfy the constraints.</p>

<p>For your case, you could do something like the following:</p>

<pre><code>from hypothesis.strategies import builds, lists, integers

def prune_list(ls):
    n = len(ls)
    return [
       [i for i in sublist if i &lt; n][:5]
       for sublist in ls
    ]

limited_list_strategy = builds(
   prune_list,
   lists(lists(integers(0, 49), average_size=5), max_size=50, min_size=1)
)
</code></pre>

<p>In this we:</p>

<ol>
<li>Generate a list that looks roughly right (it's a list of list of integers and the integers are in the same range as all <em>possible</em> indices that could be valid).</li>
<li>Prune out any invalid indices from the sublists</li>
<li>Truncate any sublists that still have more than 5 elements in them</li>
</ol>

<p>The result should satisfy all three conditions you needed.</p>

<p>The average_size parameter isn't strictly necessary but in experimenting with this I found it was a bit too prone to producing empty sublists otherwise. </p>

<p>ETA: Apologies. I've just realised that I misread one of your conditions - this doesn't actually do quite what you want because it doesn't ensure each list is the same length. Here's a way to modify this to fix that (it gets a bit more complicated, so I've switched to using composite instead of builds):</p>

<pre><code>from hypothesis.strategies import composite, lists, integers, permutations


@composite
def limisted_lists(draw):
    ls = draw(
        lists(lists(integers(0, 49), average_size=5), max_size=50, min_size=1)
    )
    filler = draw(permutations(range(50)))
    sublist_length = draw(integers(0, 5))

    n = len(ls)
    pruned = [
       [i for i in sublist if i &lt; n][:sublist_length]
       for sublist in ls
    ]

    for sublist in pruned:
        for i in filler:
            if len(sublist) == sublist_length:
               break
            elif i &lt; n:
               sublist.append(i)
    return pruned
</code></pre>

<p>The idea is that we generate a "filler" list that provides the defaults for what a sublist looks like (so they will tend to shrink in the direction of being more similar to eachother) and then draw the length of the sublists to prune to to get that consistency.</p>

<p>This has got pretty complicated I admit. You might want to use RecursivelyIronic's flatmap based version. The main reason I prefer this over that is that it will tend to shrink better, so you'll get nicer examples out of it.</p>

python unit-testing testing property-based-testing python-hypothesis