Added TEST_PERMUTATION, made it easier to reproduce perm/fuzz failures
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.
This commit is contained in:
@@ -80,6 +80,13 @@ uint32_t bench_prng(uint32_t *state);
|
||||
|
||||
#define BENCH_PRNG(state) bench_prng(state)
|
||||
|
||||
// generation of specific permutations of an array for exhaustive benching
|
||||
size_t bench_factorial(size_t x);
|
||||
void bench_permutation(size_t i, uint32_t *buffer, size_t size);
|
||||
|
||||
#define BENCH_FACTORIAL(x) bench_factorial(x)
|
||||
#define BENCH_PERMUTATION(i, buffer, size) bench_permutation(i, buffer, size)
|
||||
|
||||
|
||||
// access generated bench defines
|
||||
intmax_t bench_define(size_t define);
|
||||
|
||||
Reference in New Issue
Block a user