forked from Imagelibrary/littlefs
TEST_PERMUTATION/BENCH_PERMUTATION make it possible to map an integer to a specific permutation efficiently. This is helpful since our testing framework really only parameterizes single integers. The exact implementation took a bit of trial and error. It's based on https://stackoverflow.com/a/7919887 and https://stackoverflow.com/a/24257996, but modified to run in O(n) with no extra memory. In the discussion it seemed like this may not actually be possible for lexicographic ordering of permutations, but fortunately we don't care about the specific ordering, only the reproducibility. Here's how it works: 1. First populate an array with all numbers 0-n. 2. Iterate through each index, selecting only from the remaining numbers based on our current permutation. .- i%rem --. v .----+----. [p0 p1 |-> r0 r1 r2 r3] Normally to maintain lexicographic ordering you should have to do a O(n) shift at this step as you remove each number. But instead we can just swap the removed number and number under the index. This effectively shrinks the remaining part of the array, but permutes the numbers a bit. Fortunately, since each successive permutation swaps at the same location, the resulting permutations will be both exhaustive and reproducible, if unintuitive. Now permutation/fuzz tests can reproduce specific failures by defining either -DPERMUTATION=x or -DSEED=x.