Resizing Generators
Understand the concept of resizing and how to use the resize function in properties.
We'll cover the following
What is resizing?
Resizing is the process that makes generators grow bigger:
- Make an integer larger.
- Make a list longer.
- Make a string have larger codepoints.
This is the simplest thing to do with custom generators.
In fact, PropEr makes that happen for us on its own as the tests run. The way generator growth works is that PropEr internally uses a size parameter. Its value is usually small at first, but as tests run, the value is increased, and the data generated grows in complexity along with it. This allows the framework to start testing our systems with initially small data, and to then progressively make it more complex.
Resizing is part of a strategy to find easy edge conditions such as 0, empty containers, or strings, handling negative or positive numbers (which turn out to cover a large set of bugs) early on. Then, as each early test passes against these expected edge conditions, the framework grows the data it generates to find trickier bugs. The more tests pass in a run, the larger the data generated becomes.
Why use resizing?
It’s possible that our codebase is pretty solid or that we suspect it may just fail on edge cases that can only be detected through very complex cases. It may not be worth our time to wait hundreds or thousands of tests before the data generated gets intricate enough to be interesting, so it would be good to be able to speed this up. For this, we’ll use the resize
function.
Resize function
The resize(Size, Generator)
function can be used to force a given size onto a generator. To learn about the function let’s recap the prop_collect2
property we made in the Collecting lesson. In the property we counted the size of binaries which had results similar to the following:
56% {0,10}
27% {10,20}
13% {20,30}
3% {30,40}
1% {40,50}
By resizing the generator to some arbitrary value, the data size can be increased.
Let’s take a look at a basic property using the resize
function:
prop_resize() ->
?FORALL(Bin, resize(150, binary()), % <= resized here
collect(to_range(10, byte_size(Bin)), is_binary(Bin))).
The following terminal will run and show the result of this property.
Get hands-on with 1400+ tech skills courses.