Good Code
The good version turns the expected label into assertions. The test fails automatically when the formatter returns an error or writes the wrong text.
Lesson 10
Write small C tests that assert boundary behavior directly, instead of printing output for a human to inspect.
#include <assert.h>
#include <string.h>
static void test_review_label_fits_buffer(void)
{
char buffer[16];
// Test data names the boundary condition under review.
assert(format_review_label(buffer, sizeof buffer, 42) == 0);
assert(strcmp(buffer, "review-42") == 0);
}
int main(void)
{
test_review_label_fits_buffer();
return 0;
}#include <stdio.h>
int main(void)
{
char buffer[16];
format_review_label(buffer, sizeof buffer, 42);
// Printing output asks humans to judge the test result.
printf("%s\n", buffer);
return 0;
}The good version turns the expected label into assertions. The test fails automatically when the formatter returns an error or writes the wrong text.
The bad version prints the result and exits successfully. CI will pass even if the output is wrong, unless a human reads the log.