32/34
Part 7 of 7 · Follow-upsLearn and do
The tests you'd write
- A model with one slot per unit, slow and obviously right, run side by side with the real allocator on a few hundred random calls in an arena of 64 units. Compare every return value and the free runs after every call. The practice problem's tests do exactly that.
- The worked example's rebuild after every call, as an invariant check.
- Then the edges by hand: a request of exactly the arena's size, and one unit too big. A freed block with a free run on each side, on one side and on neither. Two runs one used unit apart. A double free, and a free inside a block. An owner whose block was freed by its address. An aligned request whose run is too short after rounding. A grow that must fail and change nothing.
A call here is a method name and its argument, such as ("alloc", 4). getattr(obj, "alloc") looks up the method by its name, so getattr(obj, name)(arg) calls it. outcome(obj, call) and first_disagreement(calls, real, model) are the harness from the first bullet above, which runs an allocator beside a slot model like SlotAllocator. The tests bring both allocators, so the print line tries outcome on a plain list.