0

I'm looking into testing a heap data structure via property-based testing. After watching John Hughes I'm starting with testing the invariants, namely the heap invariant that any sub-tree rooted at x will be of lesser priority (using a min-heap) than its parent.

So far I set the heap up myself by inserting a whole bunch of arbitrary data, but after all, I've read and seen this feels to manual.

Is there a way to specify a heap-based on the framework using my heap insert() function to insert it's arbitrary supplied values?

heap.heap in the code sample is just accessing the underlying array of the heap for ease of testing

import * as fc from 'fast-check';
import { Heap } from '../src/Heap';

test('Every sub-root is less than its parent', () => {
    
    fc.assert(
        fc.property(fc.array(fc.tuple(fc.string(6), fc.nat())), (a:[string, number][]) => {
            let heap:Heap<String> = new Heap();

            for (let index = 0; index < a.length; index++) {
                const [ val, priority ] = a[index];
                heap.insert(val, priority);
            }

            return heap.heap.every((node, index) => {
                let parent = heap[heap.parentIndex(index)];
                if (parent != null) {
                    return node.value < parent.value;
                } else {
                    return true;
                }
            });
        })
    )
});
SuperJumbo
  • 519
  • 3
  • 13
  • "*after all I've read and seen, this feels to manual.*" - looks fine to me. I'm not sure what you mean by "*Is there a way to specify a heap-based on the framework*"? – Bergi Jul 15 '20 at 14:39
  • I just mean constructing the heap within the test seems counter to what I've seen from examples so far; I'd have to repeat this for _every_ test. Is there not a way to specify an arbitrary heap as an argument to my property function? – SuperJumbo Jul 15 '20 at 23:31
  • Yes, you can [create a custom `Arbitrary`](https://github.com/dubzzz/fast-check/blob/master/documentation/1-Guides/AdvancedArbitraries.md) for that. – Bergi Jul 16 '20 at 12:29

0 Answers0