Unit tests...
Essentially none, but writing unit tests ...
For writing, compiling and running unit tests, a unit testing framework is required.
There are many concurring unit testing frameworks for C.
At the time of this writing, Criterion offers the best features.
#include <stdint.h>
// Return the sum of all values from 1 to n.
uint64_t add_to(uint32_t n) {
uint64_t sum = 0;
for (uint32_t i = 1; i <= n; ++i) {
sum += i;
}
return sum;
}
Download before_refactoring.c
After experiencing performance problems, we want to use a faster algorithm
#include <stdint.h>
// Return the sum of all values from 1 to n.
uint64_t add_to(uint32_t n) {
return (uint64_t) (n / 2.0) * (n + 1);
}
Download after_refactoring.c
How can we apply the change without risking to destroy our application?
Using pencil and paper / brainstorming: define relevant test cases
25 is a possible common case.
Add 26 as an even input.
The edge cases should
include 0 and 4294967295.
Expected results for these are 325, 351, 0 and 9223372034707292160.
Let's also add 500000000 (expected result 125000000250000000) as 4294967295 does not terminate with the current implementation.
#include <criterion/criterion.h>
#include <criterion/new/assert.h>
unsigned long long add_to(unsigned n); // usually in header file
Test(sum_to, fast_test) {
cr_assert(eq(u64, add_to(0), 0), "Sum of 0 is 0.");
cr_assert(eq(u64, add_to(25), 325), "Sum of 1..25 is 325.");
cr_assert(eq(u64, add_to(26), 351), "Sum of 1..26 is 351.");
cr_assert(eq(u64, add_to(500000000), 125000000250000000),
"This function must work for large unsigned integers.");
}
// does not terminate for the old code version due to an overflow of the
// loop counter (the loop would terminate only at UINT_MAX + 1)
Test(sum_to, slow_test) {
cr_assert(eq(u64, add_to(4294967295), 9223372034707292160),
"The function must work for the largest unsigned integer.");
}
Download test_sum_to.c
clang before_refactoring.c test_sum_to.c -l criterion -o run_tests
./run_tests # does not terminate; interrupt via [Ctrl]+[C]
What to do now?
./run_tests --help
./run_tests -l # list all available tests
./run_tests --filter sum_to/fast_test
For the sake of the presentation, instead of applying the change, we use another source file.
clang after_refactoring.c test_sum_to.c -l criterion -o run_tests
./run_tests
Now the tests are fast, but they are failing. Fix the code, then compile and run the tests again.
The comprehensive documentation can be found on readthedocs.
The installation is trivial, if it's available in your package manager.
Otherwise, follow the setup guide for building from source.
A slightly more comprehensive example also demonstrates text fixtures.
Whenever you are tempted to type something into a print
statement or a debugger expression, write it as a test instead.