Raphaël Hertzog: Test Driven Development with CppUTest, now in Debian
I have recently read Test Driven Development with Embedded C by James W. Grenning and published by Pragmatic Programmers.
I really enjoyed the book: while I was aware of the huge benefits of having a comprehensive test suite, I never studied seriously the principles behind Test Driven Development (TDD) and this book makes a good introduction to the topic. At the same time it focuses on the C language and contains lots of examples on how you can create tests even for projects which have to interact with hardware or other unpredictable components (the key is to create many abstractions) using all the possibilities that C offers.
The author convincingly argues that developing code with TDD forces you to create a modular design that is easier to evolve when the underlying requirements change. He also highlights how the tests serve as reference documentation of the API.
James W. Grenning recommends CppUTest as his xUnit test framework of choice. When I wanted to try this test framework, I discovered that it was not available in Debian. I decided to package it because it has some interesting features not offered by the contenders (at least not to my knowledge). It s now available in Debian and in Ubuntu.
First, it doesn t require any explicit registration of tests and has a very lightweight syntax. The small downside is that CppUTest requires the usage of C++ for the tests. But C++ is compatible with C so it doesn t matter much if you have a C++ compiler for your target. On the contrary, usage of variables and methods scoped to the test group makes it easy to write clear tests. Here s a short sample of test code:
extern "C" #include "timer.h" #include "timefn.h" #include "CppUTest/TestHarness.h" static Time the_time; static const int start_sec = 123; static const int start_nsec = 456789000; static const int delay_sec = 8; static const int delay_nsec = 111111000; // start_nsec + delay_nsec < 10^9 TEST_GROUP(Timer) /* Class variables available to all tests in the group */ Timer timer; Delay remaining; /* Standard setup/teardown methods of xUnit tests */ void setup() timer = timer_new(); time_set(&the_time, start_sec, start_nsec); /* [...] */ void teardown() timer_free(timer); /* [...] */ /* Helper functions specific to the test group */ void start_timer_with_delay(long sec, long nsec) timer_set_real_delay(timer, sec, nsec); timer_start(timer); void ensure_remaining_is(long sec, long nsec) CHECK_EQUAL(sec, delay_get_seconds(remaining)); CHECK_EQUAL(nsec, delay_get_nanoseconds(remaining)); ; TEST(Timer, NewIsNotStarted) CHECK(!timer->started); /* [...] */ TEST(Timer, GetRemainingTimeWithNanosecondPrecision_ShiftOfSeconds) start_timer_with_delay(delay_sec, delay_nsec); time_set(&the_time, start_sec + delay_sec - 5, start_nsec + delay_nsec + 1000); remaining = timer_get_remaining_time(timer); ensure_remaining_is(4, 999999000);
#include "CppUTest/CommandLineTestRunner.h" int main(int argc, char** argv) return CommandLineTestRunner::RunAllTests(argc, argv);
The upstream developers have made some unusual choices (static library only, installation in a private directory) but this will likely change with the switch to an automake and autoconf-based build system. I have reported the oddities that I found and I requested them to provide a pkg-config file to make it easier to compile and link unit tests exploiting CppUTest.
I already used CppUTest to develop a small application running on an embedded Linux. At some point, I might try to use CppUTest for dpkg development. I believe that it makes for a good fit. dpkg is already C++ ready since dselect is written in C++ and reuses a good part of dpkg s code.
In any case, if you like Test Driven Development and are writing C or C++ based applications, I invite you to try CppUTest.
No comment Liked this article? Click here. My blog is Flattr-enabled.
(This blog post is a more firm version of a series of tweets and forum posts I made a few weeks ago. This should also be considered a refresh of

In case you were wondering what had happened to my blog over the past
few weeks: the server on which it runs, samba.nixsys.be, has been down
for that time, since 
Usually in life we focus upon the things we do, and not the things we don't do, or avoid.
I don't have many "rules" for the way I behave, the things I do, but there are some.
The second highlight is more on the internal structure camp. As
mentioned last month, I've discussed with the tech-ctte insisting a
bit to set up periodic IRC meetings, to ensure outstanding issues
get periodically reviewed. At the end of May the first IRC meeting
has happened, and has been very productive. See the
The other complaint about UEFI Secure Boot is that it doesn't add any security. There's two aspects to this - people either think it'll be quickly broken, or people think that the public availability of signing services will render it useless.
...should be banned. On the flight back from Hong Kong to London, a
collection of about half a dozen small children conspired to cry,
shout, wail and shriek nigh-on constantly. For twelve hours. :-(
I think it's incredibly selfish to take kids on planes when they're
too young to understand what's going on, or to behave
reliably. Children make noise, that's natural and expected. Forcing
hundreds of other people around you to deal with that noise in an
enclosed space for extended periods with no way to escape is just
wrong, in my opinion.
</rant>
When packaging software for Debian, there exist two important assumptions:
Dpkg
Like last month, I did almost nothing concerning dpkg. This will probably change in June now that the book is out
The only thing worth noting is that I have helped Carey Underwood who was trying to diagnose why btrfs was performing so badly when unpacking Debian packages (compared to ext4). Apparently this already resulted in some btrfs improvements.
But not as much as what could be hoped. The sync_file_range() calls that dpkg are doing only force the writeback of the underlying data and not of the meta-data. So the numerous fsync() that follow still create many journal transactions that would be better handled as one big transaction. As a proof of this, replacing the fsync() with a sync() brings the performance on par with ext4.
(Beware this is my own recollection of the discussion, while it should be close to the truth, it s probably not 100% accurate when speaking of the brtfs behaviour)
Packaging
I uploaded new versions of smarty-gettext and smarty-validate because they were uninstallable after the removal of smarty. The whole history of smarty in Debian/Ubuntu has been a big FAIL since the start.
Once upon a time, there was a
I ve expanded a bit on my idea (that is, the sbuilder wrapper to help aid with
managing more then one sbuild chroot), and it got out of hand very quickly.
It s gone from one small shell script to a set of 8, and growing. Quickly.
Until I spin these off into their own repo, they ll be in my
There has been much made of the pheonmen of