12/27
Part 3 of 7 · The shape of the treeLearn and do
Test a protocol against the one-machine answer
How do you know the protocol works? The hand-picked trees are a start. They can't show you every order a network might pick, or every tree shape. So write two helpers:
random_tree(n, seed): machines 0 to n - 1, where machine k gets a parent picked from the machines before it. Picking only earlier machines means every machine hangs off machine 0, and there can't be a cycle. A seededrandom.Randommakes every tree repeatable, so a failing seed can be rerun.shape(tree, node): the answer worked out on one machine, by the recursion from the first step. This is your oracle, the slow version you trust.
Then the test is a loop: for many seeds, build a tree, run the protocol in fifo, lifo and several shuffles, and compare the root's count and shape with the oracle's. A bug that depends on order or shape shows up within a few seeds.
In an interview, say this plan out loud once level 1 passes. It's the answer to "how would you test this?" before anyone asks it.
The editor holds random_tree and shape, both to write. Write them, and the tests check that the same seed gives the same tree.