A fixture tells you the codec is right for one carefully chosen input. There are 4095 times 10001 valid readings, about 41 million, and you cannot hand-derive bytes for all of them. What you can do is state rules that must hold for every one of them, and then check those rules over many generated inputs. Such a rule is called a property. For this decoder, four properties carry most of the contract:
- Round trip. For every valid reading
r, decodingencode(r)gives backr. - Chunk independence. For one fixed byte stream, every way of splitting it into chunks produces the same readings and the same final status.
- Progress accounting. Across a run, the sum of all
consumedcounts equals the number of bytes the decoder accepted, and no single count exceeds what was supplied. - Output preservation. Any call that does not return
FRAME_READYleaves the caller's output unchanged.
Properties complement fixtures; they do not replace them. A round-trip property is exactly the test that matching mistakes pass, as the previous lesson showed. Fixtures anchor the format, and properties spread that confidence across the input space.
Predict and run
Read the program before pressing Run. Predict the count, then explain why a small grid like this supplements, rather than replaces, endpoint tests at sensor 4095 and value 10000.
#include <stdio.h>
int main(void) {
unsigned cases = 0;
for (unsigned sensor = 1; sensor <= 3; ++sensor) {
for (unsigned value = 0; value <= 2; ++value) { ++cases; }
}
printf("%u\n", cases);
return 0;
}Check your prediction
9Three sensors times three values gives nine cases. A grid checks a property at evenly spread points, but it only hits an endpoint if the step happens to land on it. The project's own grid shows the trap.
Check what your grid actually covers
The project's suite steps sensors by 683 and values by 2500. Print the values that loop visits:
#include <stdio.h>
int main(void) {
unsigned sensors = 0, last_sensor = 0, values = 0, last_value = 0;
for (unsigned sensor = 1; sensor <= 4095; sensor += 683) { ++sensors; last_sensor = sensor; }
for (unsigned value = 0; value <= 10000; value += 2500) { ++values; last_value = value; }
printf("sensors: %u, last %u\n", sensors, last_sensor);
printf("values: %u, last %u\n", values, last_value);
printf("grid cases: %u\n", sensors * values);
return 0;
}sensors: 6, last 3416
values: 5, last 10000
grid cases: 30The value loop lands on 10000 exactly, because 10000 is a multiple of 2500. The sensor loop stops at 3416 and never tests 4095. That is why the suite has a separate, explicit test for sensor 4095 with value 10000. Whenever you write a generator, check its coverage the way you would check any other code: print a few of the cases, and confirm the boundaries are in the set.
Deterministic generation
Random inputs find bugs that no grid anticipates, but a test that fails once and passes on the rerun is nearly useless. Make generated tests reproducible: use a fixed seed, print it when a case fails, and generate with a function whose output is the same on every platform. The standard rand is a poor choice for this, because the C standard does not specify its algorithm, so the same seed gives different sequences on different C libraries. A tiny linear congruential generator with explicit 32-bit arithmetic is enough for tests:
#include <stdio.h>
typedef struct { unsigned long state; } Rng;
static unsigned long next(Rng *rng) {
rng->state = (rng->state * 1103515245UL + 12345UL) & 0xFFFFFFFFUL;
return rng->state >> 16;
}
static unsigned in_range(Rng *rng, unsigned low, unsigned high) {
return low + (unsigned)(next(rng) % (high - low + 1u));
}
int main(void) {
Rng rng = {2024};
for (int i = 0; i < 3; ++i) {
unsigned sensor = in_range(&rng, 1, 4095);
unsigned value = in_range(&rng, 0, 10000);
printf("case %d: sensor %u, value %u\n", i + 1, sensor, value);
}
return 0;
}case 1: sensor 2013, value 3372
case 2: sensor 2345, value 7549
case 3: sensor 206, value 1249The mask & 0xFFFFFFFFUL keeps the state at 32 bits even where unsigned long is 64 bits wide, so these exact three cases appear everywhere. Unsigned multiplication may wrap, and unsigned wraparound is well defined, so there is no overflow hazard. The modulo introduces a slight bias toward small values, which does not matter for testing. It would matter for cryptography or statistics.
The project's native suite uses the fixed grid plus exhaustive partitions of one frame. It does not claim to cover every possible byte stream, and a test report should not either. "All 512 partitions of the fixture" is a precise claim; "all inputs" would be false.
Practice
Your turn
Using the generator above and the encoder from the starter, check the round-trip property for the 30 grid cases, the explicit endpoint 4095/10000, and 100 seeded random cases with seed 2024. If any case fails, print it with the seed and stop. Print:
grid: 30 passed
endpoint: passed
random (seed 2024): 100 passed#include <stdio.h>
typedef struct { unsigned long state; } Rng;
static unsigned long next(Rng *rng) {
rng->state = (rng->state * 1103515245UL + 12345UL) & 0xFFFFFFFFUL;
return rng->state >> 16;
}
static unsigned in_range(Rng *rng, unsigned low, unsigned high) {
return low + (unsigned)(next(rng) % (high - low + 1u));
}
static void encode(unsigned sensor, unsigned value, unsigned char f[10]) {
const unsigned char header[5] = {0x54, 1, 1, 0, 4};
unsigned char sum = 0;
for (size_t i = 0; i < 5; ++i) { f[i] = header[i]; }
f[5] = (unsigned char)(sensor >> 8); f[6] = (unsigned char)(sensor & 0xFFu);
f[7] = (unsigned char)(value >> 8); f[8] = (unsigned char)(value & 0xFFu);
for (size_t i = 0; i < 9; ++i) { sum = (unsigned char)(sum + f[i]); }
f[9] = sum;
}
int main(void) {
/* Write decode and round_trip_ok above, then check the three groups here. */
return 0;
}Hint 1: the idea
All three groups test the same property, so write it once as int round_trip_ok(unsigned sensor, unsigned value) and call it from three loops.
Hint 2: a first step
decode should verify the checksum and the fixed header before reading the fields, so a broken encoder cannot pass by accident. In the random loop, keep the two in_range calls as separate statements so their order is fixed.
Show one solution
#include <stdio.h>
typedef struct { unsigned long state; } Rng;
static unsigned long next(Rng *rng) {
rng->state = (rng->state * 1103515245UL + 12345UL) & 0xFFFFFFFFUL;
return rng->state >> 16;
}
static unsigned in_range(Rng *rng, unsigned low, unsigned high) {
return low + (unsigned)(next(rng) % (high - low + 1u));
}
static void encode(unsigned sensor, unsigned value, unsigned char f[10]) {
const unsigned char header[5] = {0x54, 1, 1, 0, 4};
unsigned char sum = 0;
for (size_t i = 0; i < 5; ++i) { f[i] = header[i]; }
f[5] = (unsigned char)(sensor >> 8); f[6] = (unsigned char)(sensor & 0xFFu);
f[7] = (unsigned char)(value >> 8); f[8] = (unsigned char)(value & 0xFFu);
for (size_t i = 0; i < 9; ++i) { sum = (unsigned char)(sum + f[i]); }
f[9] = sum;
}
static int decode(const unsigned char f[10], unsigned *sensor, unsigned *value) {
unsigned char sum = 0;
for (size_t i = 0; i < 9; ++i) { sum = (unsigned char)(sum + f[i]); }
if (f[0] != 0x54 || f[1] != 1 || f[2] != 1 || f[3] != 0 || f[4] != 4 || sum != f[9]) { return 0; }
*sensor = ((unsigned)f[5] << 8) | f[6];
*value = ((unsigned)f[7] << 8) | f[8];
return 1;
}
int round_trip_ok(unsigned sensor, unsigned value) {
unsigned char f[10];
unsigned s = 0, v = 0;
encode(sensor, value, f);
return decode(f, &s, &v) && s == sensor && v == value;
}
int main(void) {
unsigned grid = 0;
for (unsigned sensor = 1; sensor <= 4095; sensor += 683) {
for (unsigned value = 0; value <= 10000; value += 2500) {
if (!round_trip_ok(sensor, value)) { printf("grid failed: %u/%u\n", sensor, value); return 1; }
++grid;
}
}
printf("grid: %u passed\n", grid);
printf("endpoint: %s\n", round_trip_ok(4095, 10000) ? "passed" : "failed");
const unsigned long seed = 2024;
Rng rng = {seed};
unsigned random = 0;
for (int i = 0; i < 100; ++i) {
unsigned sensor = in_range(&rng, 1, 4095);
unsigned value = in_range(&rng, 0, 10000);
if (!round_trip_ok(sensor, value)) {
printf("seed %lu case %d failed: %u/%u\n", seed, i + 1, sensor, value);
return 1;
}
++random;
}
printf("random (seed %lu): %u passed\n", seed, random);
return 0;
}grid: 30 passed
endpoint: passed
random (seed 2024): 100 passedA failure message that names the seed and the case number lets anyone reproduce the exact input on any machine, which turns a flaky report into a debuggable one. The three groups each cover something the others miss: the grid is evenly spread, the endpoint is the boundary, and the random cases hit values nobody chose on purpose. Next, you test the inputs that should fail: every short prefix of a frame.
Completion is saved in this browser only. It does not measure mastery.