Unit Tests in Pelles C on Windows

I said before that I might try and do some unit testing in C, rather than use the features in Visual Studio. It would mean that for H2D2 I can do everything inside Pelles C and won't need to have two IDEs running. But I've never done any unit testing in C before (at least if I have it was so long ago I've forgotten). So I needed to go off and find some means of doing that.

So I found the libtap library which is a unit testing framework for C programs. Well, it is something that implements the Test Anything Protocol which you can read about here. I'm no expert on the Test Anything Protocol, this is the first time I've used it, but for my purposes it looks like it will do fine. As long as I can run code and check for expected results and have the Passes and Failures shown in some sort of output then I'll be happy... But for the moment it looks ideal. I've had to make some minor modifications to get it working in my project (in Pelles C for Windows) but it was not very much effort really. I've just made my own version of tap.c and included it in my project. I had to include an implementation of vasprintf() which I found here rather than rolling my own, but that seems to work quite well for me.

I found libtap after searching for C unit testing on stack overflow in case you're wondering how I found it in the first place.

What I've ended up with is a #define which tells the compiler which version of main() to include. One version is my usual code and the other runs my test suite. I haven't began porting my actual H2D2 unit tests back over from C# yet, but I do have a trivial example running just to check that I can run some unit tests. So this this is all I've got so far:

I've also set it up so that the content of tap.c is not included when I compile normally, so this means that my code isn't bloated with the test framework when I'm not using it.

I've resisted the temptation to mess around with libtap too much, I've only made enough changes to get it working in my project. Thanks need to go to Nik Clayton, who wrote the library and to whoever River Tarnell-7 is for posting his vasprintf() implementation. Thanks! In case it helps any other people using Pelles C who would like to add units tests to their project, here are the files:

Download libtap, adapted for Pelles C on Windows.

I'll probably try this out on the Raspberry Pi soon, because I think it may be useful to have a lightweight unit testing framework for GCC on the Raspberry Pi too. I'll post an update if anything comes of that idea.