Write tests that catch the bugs
Write a test suite for a scaling function. It is graded by running it against buggy versions it must reject. A medium embedded C problem (Testing and code…
medium · Testing and code coverage · testing · test-design · boundaries · mutation-testing
This time you write the tests. test_percent(f) must call the function f with inputs of your choice, check its results against the specification in the comment above, and return true only if every check passes.
Your suite is graded the way mutation testing grades a test suite. It is run against a correct implementation, which it must pass, and against versions with one bug each, which it must reject. The visible test shows three of them. The hidden test has more: think about each line of the specification and what could go wrong with it.
Starter code
bool test_percent(percent_fn f) {
return f(2048, 0, 4095) == 50;
}Example tests
The correct version, and three bugs
judge("correct", good);
judge("no top clamp", no_top_clamp);
judge("no bottom clamp", no_bottom_clamp);
judge("divides first", divides_first);Expected output:
correct: passes
no top clamp: caught
no bottom clamp: caught
divides first: caughtLearn it first
- Testing and code coverage · Unit tests on the host, mocks, HIL, coverage and static analysis.
More Testing and code coverage problems
- Test with a fake GPIO · Write a fake that records pin writes and delays, so code can be tested off target.
- Write a CMock-style mock · A mock that checks a driver makes exactly the bus writes a test expects, in order, and reports what went wrong.
- A fake I2C device for driver tests · Write a fake I2C bus with one register-mapped device, so a sensor driver can be tested on a PC.