Altifigence Academy

15 / 24 · Lab

Lab: check 1024 stall patterns

Exercise hold, replacement, and emptying with all ten-cycle ready patterns.

Preserve values and ordering under stalls

Manually create elastic-model.mjs, save the following code, and run node elastic-model.mjs. The source offers four ordered items and advances only when accepted. Pattern bit zero controls edge 0, bit one controls edge 1, and so on. Exhaust all 1024 ten-bit ready patterns, then keep the sink ready to drain outstanding work.

Deliver from old slot before deciding the replacement. An empty slot accepts one item even with a stalled sink. A full stalled slot cannot advance the source. Incrementing next every edge would discard unaccepted items and make the test source incorrect even if the buffer were correct.

javascript
import assert from 'node:assert/strict';
const values = [0, 9, 255, 12];
let deliveries = 0;
for (let pattern = 0; pattern < 1024; pattern++) {
  let slot = null, next = 0;
  const seen = [];
  for (let edge = 0; edge < 20; edge++) {
    const ready = edge >= 10 || ((pattern >>> edge) & 1) === 1;
    const old = slot;
    const inReady = old === null || ready;
    if (old !== null && ready) seen.push(old);
    if (inReady) {
      slot = next < values.length ? { tag: next, y: values[next] } : null;
      if (slot !== null) next++;
    } else {
      assert.deepEqual(slot, old);
    }
    assert.equal(next - seen.length, Number(slot !== null));
  }
  assert.deepEqual(seen, values.map((y, tag) => ({ tag, y })));
  assert.equal(slot, null);
  deliveries += seen.length;
}
console.log(`ELASTIC PASS: 1024 patterns, ${deliveries} deliveries`);

For pattern zero, the sink stalls on the first ten edges. Tag0,y0 enters the empty slot at edge 0 and remains there through edge 9. Edge 10 delivers it and stores tag1,y9. Tags 1,2,3 then transfer at edges 11,12,13. A valid zero must not be mistaken for null. Success reports 1024 patterns and 4096 deliveries.

Accepted minus delivered equals current occupancy after every edge. This local accounting narrows failures earlier than a final output comparison. Hold checks cover payload preservation, though comparing a real implementation requires observing its actual output rather than only this model's unchanged variable. Exhausting ten-cycle patterns is a bounded test, not proof for every possible stall length. Eventual completion additionally assumes the sink eventually becomes ready; indefinite backpressure requires preservation, not guaranteed delivery.

Try it yourself

For pattern zero, how far does source next advance over edges 0 through 9? What are occupancy and next-seen.length after edge 10?

Read the explanation

Only the edge-0 item is accepted, so next=1. At edge 10, tag0 transfers and tag1 enters: next=2, seen.length=1, and both the difference and slot occupancy are one.

Lesson files

elastic-model.mjs ↓

Your choice applies to this browser. Change it any time using the footer.