Targeted Properties in Practice

The targeted properties you’ve seen so far are pretty representative of the whole thing. Aside from respecting their limitations (no recursive generators nor stats), not much changes. The path example from early in the chapter was easy for simulated annealing to handle, so in this section, we’ll explore a slightly trickier case: forcing trees to be more or less balanced or skewed, and how to best optimize that.

A binary tree[40] is generally simple to keep balanced when using random data; if the data’s randomness is uniform (or follows a standard distribution), chances are that you’ll get numbers that will naturally distribute themselves on both sides of the tree. But let’s validate that.

We’ll start by writing ...

Get Property-Based Testing with PropEr, Erlang, and Elixir now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.