Skip to content

Test Every Short Input

The Course
•
40 min study

Most decoder bugs live at the edges of the input, not in the middle. A frame cut off after byte 4, a frame whose last byte is damaged, a stream that ends exactly between two frames: each takes a different path through the code, and each path needs its own test. The good news is that a ten-byte frame has very few edges, so you can test all of them instead of a sample.

There are two families. Truncation cuts the valid fixture short. Every prefix of length 1 to 9 must be accepted as NEED_MORE, because more bytes could still arrive, and must then fail at decoder_finish, because they did not. The empty prefix, length 0, is a clean empty stream. After a truncation, a further feed must be refused with zero bytes consumed and the output unchanged. Corruption keeps the length but changes a byte. Flipping any single bit in any of the ten bytes must produce DECODE_ERROR, never a reading.

Predict and run

Read the program before pressing Run. Predict the count, then explain why treating the empty prefix as truncated would make an empty stream unusable.

Predict before running C
#include <stdio.h>

int main(void) {
    unsigned truncated = 0;
    for (size_t length = 1; length < 10; ++length) { ++truncated; }
    printf("%u\n", truncated);
    return 0;
}
Check your prediction
Text
9

Lengths 1 through 9 are the nine truncated prefixes. Length 0 is excluded on purpose: a source that sent nothing has not sent a broken frame. If an empty stream failed at finish, every caller would need a special case for "no data", and the application could not print Frames: 0 for empty input.

Which check catches which corruption

The consumed count on an error tells you which check fired, because each check runs at a known byte. That makes a corruption table a precise test, not just a pass or fail:

Flip one bit in each byte C
#include <stdio.h>
#include <string.h>

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};

/* Returns 1 for a reading, 0 for need more, -1 for error; *consumed reports progress. */
static int feed(Decoder *d, const unsigned char *bytes, size_t size, size_t *consumed) {
    *consumed = 0;
    if (d->failed) { return -1; }
    while (*consumed < size) {
        d->frame[d->used++] = bytes[(*consumed)++];
        int bad = (d->used == 1 && d->frame[0] != 0x54) ||
                  (d->used == 5 && (d->frame[1] != 1 || d->frame[2] != 1 || d->frame[3] != 0 || d->frame[4] != 4));
        if (d->used == 10) {
            unsigned char sum = 0;
            for (size_t i = 0; i < 9; ++i) { sum = (unsigned char)(sum + d->frame[i]); }
            unsigned sensor = ((unsigned)d->frame[5] << 8) | d->frame[6];
            bad = sum != d->frame[9] || sensor < 1 || sensor > 4095;
        }
        if (bad) { d->failed = 1; return -1; }
        if (d->used == 10) { d->used = 0; return 1; }
    }
    return 0;
}

int main(void) {
    for (size_t byte = 0; byte < 10; ++byte) {
        unsigned char corrupt[10];
        memcpy(corrupt, fixture, 10);
        corrupt[byte] ^= 1;
        Decoder d = {{0}, 0, 0};
        size_t consumed = 0;
        int result = feed(&d, corrupt, 10, &consumed);
        printf("flip byte %zu: %s at %zu\n", byte, result < 0 ? "error" : "ACCEPTED", consumed);
    }
    return 0;
}
Text
flip byte 0: error at 1
flip byte 1: error at 5
flip byte 2: error at 5
flip byte 3: error at 5
flip byte 4: error at 5
flip byte 5: error at 10
flip byte 6: error at 10
flip byte 7: error at 10
flip byte 8: error at 10
flip byte 9: error at 10

A damaged magic byte fails immediately. Damaged header bytes fail when the header completes. Damaged payload and checksum bytes fail at the end. If a refactoring moved the magic check to byte 5, every line would still say "error", but the first would say at 5, so a test that asserts the consumed count catches a change that a pass or fail test would miss.

Separate the failure paths

Flipping a payload bit makes the checksum wrong, so the corruption tests only prove that the checksum works. They say nothing about the domain check, because the checksum rejects those frames first. To test the domain check on its own, build a frame that is wrong only in its meaning: set the sensor to 0 and repair the checksum, so the sum is correct. The project does exactly this, and also repairs the checksum after changing the magic, version, type and length bytes, so each header rule is proven to reject a frame on its own. A test suite that only ever fails one way leaves the other checks unverified, and this day's mutation checkpoint shows what that costs.

Practice

Exercise

Your turn

Using the feed above, add a finish with sticky error, then write a truncation test. For every prefix length 1 to 9, check that feeding the prefix returns need-more with the whole prefix consumed, that finish fails, and that feeding the full fixture afterward is refused with zero consumed. Also check the empty prefix. Print:

Text
prefix 0: clean finish
prefixes 1-9: 9 of 9 behaved
Your workspace C
#include <stdio.h>
#include <string.h>

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 int feed(Decoder *d, const unsigned char *bytes, size_t size, size_t *consumed) {
    *consumed = 0;
    if (d->failed) { return -1; }
    while (*consumed < size) {
        d->frame[d->used++] = bytes[(*consumed)++];
        int bad = (d->used == 1 && d->frame[0] != 0x54) ||
                  (d->used == 5 && (d->frame[1] != 1 || d->frame[2] != 1 || d->frame[3] != 0 || d->frame[4] != 4));
        if (d->used == 10) {
            unsigned char sum = 0;
            for (size_t i = 0; i < 9; ++i) { sum = (unsigned char)(sum + d->frame[i]); }
            unsigned sensor = ((unsigned)d->frame[5] << 8) | d->frame[6];
            bad = sum != d->frame[9] || sensor < 1 || sensor > 4095;
        }
        if (bad) { d->failed = 1; return -1; }
        if (d->used == 10) { d->used = 0; return 1; }
    }
    return 0;
}

int main(void) {
    /* Write finish and prefix_ok above, then run the checks here. */
    return 0;
}
Hint 1: the idea

Each prefix gets a fresh decoder, so one failing length cannot affect the next. Three facts must all hold for a length to count: need-more with everything consumed, a failed finish, and a refused feed.

Hint 2: a first step

Start with the empty prefix alone: a fresh decoder, no feed, then finish. It must fail, which tells you the two conditions under which finish refuses. Add the loop over the other lengths after that one passes.

Show one solution
Run the solution C
#include <stdio.h>
#include <string.h>

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 int feed(Decoder *d, const unsigned char *bytes, size_t size, size_t *consumed) {
    *consumed = 0;
    if (d->failed) { return -1; }
    while (*consumed < size) {
        d->frame[d->used++] = bytes[(*consumed)++];
        int bad = (d->used == 1 && d->frame[0] != 0x54) ||
                  (d->used == 5 && (d->frame[1] != 1 || d->frame[2] != 1 || d->frame[3] != 0 || d->frame[4] != 4));
        if (d->used == 10) {
            unsigned char sum = 0;
            for (size_t i = 0; i < 9; ++i) { sum = (unsigned char)(sum + d->frame[i]); }
            unsigned sensor = ((unsigned)d->frame[5] << 8) | d->frame[6];
            bad = sum != d->frame[9] || sensor < 1 || sensor > 4095;
        }
        if (bad) { d->failed = 1; return -1; }
        if (d->used == 10) { d->used = 0; return 1; }
    }
    return 0;
}

static int finish(Decoder *d) {
    if (d->failed || d->used != 0) { d->failed = 1; return 0; }
    return 1;
}

int prefix_ok(size_t length) {
    Decoder d = {{0}, 0, 0};
    size_t consumed = 0;
    if (feed(&d, fixture, length, &consumed) != 0 || consumed != length) { return 0; }
    if (finish(&d)) { return 0; }
    return feed(&d, fixture, 10, &consumed) == -1 && consumed == 0;
}

int main(void) {
    Decoder empty = {{0}, 0, 0};
    printf("prefix 0: %s\n", finish(&empty) ? "clean finish" : "unexpected error");
    unsigned behaved = 0;
    for (size_t length = 1; length < 10; ++length) { behaved += (unsigned)prefix_ok(length); }
    printf("prefixes 1-9: %u of 9 behaved\n", behaved);
    return 0;
}
Text
prefix 0: clean finish
prefixes 1-9: 9 of 9 behaved

Every prefix exercises a different resting point of the state machine, from one byte of header to a frame missing only its checksum, and each one is checked on all three behaviors. Remove the d->used != 0 clause from finish and the count drops to 0, which is the project's "accept truncated end" mutation. The checkpoint next runs every mutation in the manifest against the real suite.

Was this article helpful?

Your response is saved on this device.