A streaming decoder makes one central promise: the readings it produces depend only on the bytes, never on how those bytes were split into chunks. Ten bytes in one call, ten calls of one byte, or a split of 3, 1 and 6 must all give the same reading, the same consumed counts and the same clean finish. Bugs that break this promise are notoriously hard to find in production, because they only appear when the network happens to split a frame at one particular offset, perhaps once a week.
The fix is to stop relying on luck and test every split. A ten-byte frame has nine internal positions where a chunk can end: after byte 1, after byte 2, and so on up to after byte 9. Each position is either a split or not, independently, so there are 2 to the power 9, which is 512, ways to cut the frame into nonempty contiguous chunks. That is small enough to test all of them on every run.
Predict and run
Read the program before pressing Run. Predict the output, then explain why ten bytes have nine split positions rather than ten.
A split before byte 1 or after byte 10 would create an empty chunk, which says nothing about the frame, so only the nine interior positions count. Each partition is a 9-bit number: if bit k - 1 is set, a chunk ends after byte k. Mask 0 means one ten-byte chunk; mask 511 means ten one-byte chunks. Testing only one-byte chunks would miss bugs that appear when a single call delivers the end of the header and the start of the payload together, which is exactly where the five-byte check runs.
What you will use
A feed function with the project's contract. At most one frame per call, with a status and a consumed count.
A bit mask as a test generator. Looping a mask from 0 to 511 visits every partition once, in a reproducible order.
Sentinel outputs. Each NEED_MORE call must leave a sentinel reading untouched; only the last chunk may change it.
A second, coarser loop. Two concatenated frames fed in chunk sizes 1 to 20, so some chunks end between frames and others cross the boundary.
Plan before you type
For each mask, start a fresh decoder and walk end from 1 to 10.
When end is 10 or bit end - 1 is set, feed the bytes from start to end as one chunk.
Check that consumed equals the chunk length, that every chunk but the last returns NEED_MORE with the sentinel intact, and that the last returns FRAME_READY with sensor 1.
Count a mask as passed only if every check held and finish reports a clean end.
Repeat with two frames and fixed chunk sizes, advancing by consumed inside each chunk.
Prove it can fail
Once both counts read full marks, break the decoder on purpose. First make feed return FRAME_READY with r.consumed - 1: both counts should drop to 0, because every test checks the consumed count. Restore it, then limit each call to nine bytes by changing the loop condition to r.consumed < size && r.consumed < 9. Now the output should be partitions: 511 of 512 passed and chunk sizes: 9 of 20 passed. Only mask 0, the single ten-byte chunk, and chunk sizes of 10 or more expose the bug, which is why one tidy split per test is not enough.
Mistakes worth checking for
512 of 512 pass, but some splits were never tested. Using 1u << end instead of 1u << (end - 1) asks about the wrong bit for every byte. Bit 0 is never looked at, so the masks come in identical pairs, and bit 9 does not exist below 512, so no mask ever splits after byte 9: the case where the checksum arrives alone in its own call is never generated. Only 256 different partitions are tested, each twice. The test still passes; it just covers less than it claims. Print the chunk lengths for a few masks to confirm the generator does what you think.
The count is right, but the sentinel check never runs. The sentinel was set once outside the loop, then overwritten by the first success. Reset it before each chunk.
Two-frame chunk sizes fail only for sizes 11 to 19. The caller fed a chunk once and moved on, dropping the bytes after the first frame. Loop inside each chunk until it is fully consumed.
Finish fails after a correct reading. The decoder did not reset used to 0 after publishing, so it believes a partial frame remains.
A test passes that should not. The test checked only the number of readings. Check status, consumed, output and finish together.
Practice
Exercise
Your turn
The starter contains a decoder with the project's contract. Write int partition_ok(unsigned mask) and int chunks_ok(size_t chunk) as planned, then count how many of the 512 masks and the 20 chunk sizes pass. Print:
Text
partitions: 512 of 512 passedchunk sizes: 20 of 20 passed
Your workspaceC
#include <stdio.h>
#include <string.h>
typedef enum { NEED_MORE, FRAME_READY, DECODE_ERROR } Status;
typedef struct { Status status; size_t consumed; } FeedResult;
typedef struct { unsigned char frame[22]; size_t used; int failed; } Decoder;
static const unsigned char fixture[10] = {0x54, 1, 1, 0, 4, 0, 1, 0, 25, 0x74};
static FeedResult feed(Decoder *d, const unsigned char *bytes, size_t size, unsigned *sensor) {
FeedResult r = {NEED_MORE, 0};
if (d->failed) { r.status = DECODE_ERROR; return r; }
while (r.consumed < size) {
d->frame[d->used++] = bytes[r.consumed++];
int bad = (d->used == 1 && d->frame[0] != 0x54) ||
(d->used == 5 && memcmp(d->frame, fixture, 5) != 0);
if (d->used == 10) {
unsigned char sum = 0;
for (size_t i = 0; i < 9; ++i) { sum = (unsigned char)(sum + d->frame[i]); }
bad = sum != d->frame[9];
}
if (bad) { d->failed = 1; r.status = DECODE_ERROR; return r; }
if (d->used == 10) {
*sensor = ((unsigned)d->frame[5] << 8) | d->frame[6];
d->used = 0; r.status = FRAME_READY; return r;
}
}
return r;
}
static int finished_clean(const Decoder *d) { return !d->failed && d->used == 0; }
int main(void) {
/* Write partition_ok and chunks_ok above, then count passes here. */
return 0;
}
Hint 1: the idea
A partition test is a small loop that remembers where the current chunk started. Whenever it reaches a split point, it feeds fixture + start with length end - start, checks the result, and moves start to end.
Hint 2: a first step
Start with mask 0, which should mean one chunk: the whole fixture. Write the loop so it feeds only at a split point, and treat the end of the fixture as always being one. Then decide which bit of mask means "split after byte end". Before each feed, put a known value in sensor, so you can check that a NEED_MORE left it alone.
Show one solution
Run the solutionC
#include <stdio.h>
#include <string.h>
typedef enum { NEED_MORE, FRAME_READY, DECODE_ERROR } Status;
typedef struct { Status status; size_t consumed; } FeedResult;
typedef struct { unsigned char frame[22]; size_t used; int failed; } Decoder;
static const unsigned char fixture[10] = {0x54, 1, 1, 0, 4, 0, 1, 0, 25, 0x74};
static FeedResult feed(Decoder *d, const unsigned char *bytes, size_t size, unsigned *sensor) {
FeedResult r = {NEED_MORE, 0};
if (d->failed) { r.status = DECODE_ERROR; return r; }
while (r.consumed < size) {
d->frame[d->used++] = bytes[r.consumed++];
int bad = (d->used == 1 && d->frame[0] != 0x54) ||
(d->used == 5 && memcmp(d->frame, fixture, 5) != 0);
if (d->used == 10) {
unsigned char sum = 0;
for (size_t i = 0; i < 9; ++i) { sum = (unsigned char)(sum + d->frame[i]); }
bad = sum != d->frame[9];
}
if (bad) { d->failed = 1; r.status = DECODE_ERROR; return r; }
if (d->used == 10) {
*sensor = ((unsigned)d->frame[5] << 8) | d->frame[6];
d->used = 0; r.status = FRAME_READY; return r;
}
}
return r;
}
static int finished_clean(const Decoder *d) { return !d->failed && d->used == 0; }
int partition_ok(unsigned mask) {
Decoder d = {{0}, 0, 0};
size_t start = 0;
for (size_t end = 1; end <= 10; ++end) {
if (end != 10 && (mask & (1u << (end - 1))) == 0) { continue; }
unsigned sensor = 77;
FeedResult r = feed(&d, fixture + start, end - start, &sensor);
if (r.consumed != end - start) { return 0; }
if (end == 10 && (r.status != FRAME_READY || sensor != 1)) { return 0; }
if (end != 10 && (r.status != NEED_MORE || sensor != 77)) { return 0; }
start = end;
}
return finished_clean(&d);
}
int chunks_ok(size_t chunk) {
unsigned char stream[20];
memcpy(stream, fixture, 10); memcpy(stream + 10, fixture, 10);
Decoder d = {{0}, 0, 0};
size_t offset = 0, frames = 0;
while (offset < 20) {
size_t end = offset + chunk < 20 ? offset + chunk : 20;
while (offset < end) {
unsigned sensor = 77;
FeedResult r = feed(&d, stream + offset, end - offset, &sensor);
if (r.status == DECODE_ERROR || r.consumed == 0 || r.consumed > end - offset) { return 0; }
offset += r.consumed;
if (r.status == FRAME_READY) { if (sensor != 1) { return 0; } ++frames; }
else if (offset != end || sensor != 77) { return 0; }
}
}
return frames == 2 && finished_clean(&d);
}
int main(void) {
unsigned passed = 0;
for (unsigned mask = 0; mask < 512; ++mask) { passed += (unsigned)partition_ok(mask); }
printf("partitions: %u of 512 passed\n", passed);
unsigned sizes = 0;
for (size_t chunk = 1; chunk <= 20; ++chunk) { sizes += (unsigned)chunks_ok(chunk); }
printf("chunk sizes: %u of 20 passed\n", sizes);
return 0;
}
Text
partitions: 512 of 512 passedchunk sizes: 20 of 20 passed
Every one of the 512 partitions exercises the same ten bytes, so any difference in outcome can only come from the split, which is the property under test. The chunk-size loop adds what partitions of one frame cannot: chunks that end mid-frame, exactly between frames, or contain the end of one frame and the start of the next. Below, the project's own suite runs the same two loops against the real decoder, together with every other check you will meet on Days 5 and 6.
Find the mask loop in test_telemetry.c. Each set bit chooses a split. Trace mask 17, binary 000010001, by hand: it splits after byte 1 and after byte 5, so the calls receive 1, 4 and 5 bytes, and the middle call is the one that completes the header. Then compare the loop with decode_chunks, which feeds two frames in fixed chunk sizes. The allocator probe at the top of main is explained on Day 5.
Completion is saved in this browser only. It does not measure mastery.