Thinking Outside the Box
The examples we’ve seen so far can be impressive, but they mostly aimed to directly impact the way data was generated. Targeted properties can help in much more creative ways, but it tends to require approaching problems differently from most other types of testing. For this section, we’ll use targeted properties to weed out a bug we suspect might lie in a quicksort function.
If you’re familiar with quicksort, you know it’s a fast sorting algorithm—especially in imperative languages where in-place sorting can be done to save on memory. It’s also one of the most often used algorithms when demonstrating list comprehensions. The official Erlang documentation gives an example looking like this:
| Erlang code/TargetedProperties/erlang/target/test/prop_target.erl ... |
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.