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;
}
});
})
)
});