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.

Farewell Honda S2000 ... it's been fun

I won't go into all the boring details, but after very nearly 5 years it has become necessary for me to get a more practical car. I'm afraid that I've recently sold my lovely Honda S2000.

It's a bit sad because this is a fantastic car. I have to admit that sometimes I've even felt it to be a bit of a responsibility to look after this car because it's so special. It's a great piece of engineering, and fun to drive as well. It deserves to be taken care of.

One of the nice things about owning an S2000 is that people come and talk to you about it. These cars have a great reputation amongst car enthusiasts. Although one time a young lady walked up to me when I was filling it up with fuel and said: "I love your car, it's so very ... erm ... ahh ... blue!" - she was obviously lost for words. That incident still makes me chuckle. But strangely she was right - the blue ones are the best :-)

I will also miss waving at any other S2000 owner I see when driving around. I can't remember any time when I've seen another S2000 and not been given a smile and a wave. It seems that S2000 owners are a friendly and happy bunch. Maybe it helps when you drive a brilliant car which is unlikely to let you down.

But I am sticking with Honda though, I reckon I've had 5 different Hondas now and they've all been really good cars. So it's on to Honda #6... I've also converted my Dad to Honda too, he has a couple of them at the moment.

A friend of mine who also drives an S2000 once said "people who have not owned an S2000 just don't understand". I can go along with that.

The joy of tests

Since my H2D2 project has quite a lot of code in it now, I've been using .Net interop as a quick way to write some unit tests for the H2D2 programming language.

I have written a C# unit test project in Visual Studio which tests much of the existing H2D2 language syntax. This means that I can add new features and quickly check that I've not broken any of the existing code. So I can very easily make sure that programs which used to run continue to work as expected.

Recently, I've been making H2D2 support function calls, and so it's been handy to write tests for various different types of function call (passing parameters, reading return values, allowing functions that don't return a value, etc). The unit tests have made it much easier to ensure that everything works after I've made changes.

Since I've written more unit tests in C# than anything else, I thought it would be the quickest way to get my tests up and running. And because I had already compiled H2D2 into a windows dll (the unmanaged kind), it is not very hard to run H2D2 programs from .Net - even easier because I had already written some basic wrapper code for all the interop stuff. It's not much harder than putting some H2D2 source code into a string and calling a Run() method. Obviously all this has been done 'just for fun', since it's only one of my hobbyist projects anyway.

But when I mentioned to one of my colleagues that I had spent some of my free time writing unit tests for fun he said that I was clearly stark raving mad. There's a possibility he's correct.

However, when I think of how I'd feel if I broke some of my existing H2D2 code and had to go back and unpick where I went wrong, I reckon that these unit tests are really less about having fun and more about preventing a severe loss of fun if I ended up scratching my head and trying to figure out what I'd done to make something break.

So I hope that's a suitable explanation for why I've been writing unit tests in my free time. It's the best excuse I've got.

Although ultimately, I may end up looking for a unit testing framework for C so that I only need one IDE loaded to run my tests... The .Net interop solution may just be a stepping stone.

Easy Windows Service Development

I've just discovered Topshelf for creating Windows Services in .Net. It makes building Windows Services a breeze, and you can also run them in the Visual Studio debugger easily too. Just look at the simple code example here ... and you only need to create a console app to make this example work.
Adding Topshelf to your project is easy thanks to NuGet. Your service code just needs to be a plain object and doesn't need to inherit from any special base class. It makes the whole thing extremely simple.

When you've compiled your console app, you can run it in the debugger to check it, and when you want to install it as a proper service, you just pass "install" as a command line argument when you run the exe. To remove the service you re-run the console app and pass "uninstall" on the command line.

Rubbish music in Cambridge

Back in the summer, on a Saturday in Cambridge I saw this busker playing guitar and singing whilst *inside* a litter bin. It was very entertaining, it really made me laugh:

... it was made better somehow because he was playing songs from Jungle Book.

An update on libpigpio

In the last couple of weeks, a few people have said that my libpigpio GPIO shared library for the Raspberry Pi only works if they recompile it from source, like here for example. (although a chap called Brendan mentioned it first, thanks Brendan).

So I thought that it was worth making people aware of that. Since I compiled the library on the initial Debian Squeeze release, there’s probably some difference with other Debian distributions that have come out since.

However, compiling the library from source on your own Raspberry Pi seems to work, so at some point I’ll update the zip file without the .so file, so that people can just compile their own version. In the meantime, the instructions are as follows:

  1. Download the zip file from here.
  2. Copy the zip file to the Raspberry Pi and extract the files to a folder
  3. Open a terminal window and use the 'cd' command to change directory to where you extracted the files
  4. Now type these commands (press enter after each line):
    gcc -c -fpic libpigpio.c
    gcc -shared -o libpigpio.so libpigpio.o
    sudo cp libpigpio.so /lib
  5. That will have compiled and installed the library into /lib
  6. I have also included a test program which you could try, like this...
  7. In the same terminal window enter these commands
    (note the spelling of -lpigpio is important):
    gcc -Wall -o test main.c -lpigpio
    sudo ./test
  8. Now the GPIO pins 7 to 11 should be turning on an off
    (use CTRL+C when you’re fed up)

I hope that helps...

Hacking the Universe

On my recent holiday to Fuerteventura, I decided that I should take the opportunity to understand more about Quantum Mechanics, as you do, so I took the book In Search Of Schrodinger's Cat with me. I really enjoyed it, I can recommend it if you need a bit more wierdness in your life.

I suppose that I'm bound to try and think about it in terms of computing; but I couldn't help feeling that since it seems impossible to measure certain pairs of properties simultaneously, it's like the universe has built in security protocols which only allow us to go so far. For example, it's not possible to measure both the position and momentum of a particle like an electron simultaneously, if you're certain about one of those properties, the other becomes more uncertain.

So, to me, the whole of Quantum Mechanics seems to be about Hacking the Universe. Nice.

Javascript Mandelbrot plotter - part 2

I've made a few tweaks to my javascript mandelbrot drawing code, so there's a new version uploaded here. You can now select three different colours and a palette will be interpolated made up from in-between colours.

So, here's an example that I came up with using the new version:

There's a couple more simple options in this version, like a choice of fractal sizes and a reset button. It would be handy to have an 'undo' option for those times when you've made a slip of the mouse...

So, don't be surprised if there are more changes to come.

Javascript Mandelbrot Plotter

Since I seem to like my Mandelbrot drawing test program so much, I thought that I should create an easy way to experiment with different parameters, then I can stop drawing the same old thing all the time... Plus it would be nice to see the Mandelbrot set in all it's graphical glory for a change.

But I've also been wanting an excuse to play around with some more html5 canvas stuff, so I decided that this was a good opportunity.

So I set myself these goals:

  1. draw the fractal in a reasonable amount of time (try not to make it too slow in other words)
  2. observe the fractal whilst in progress, so you don't have to stare at a blank page until the whole thing has rendered
  3. be able to select a region of the fractal (with a mouse click) and redraw it (zoomed in) showing the new parameters

That was the minimum functionality I was aiming for. I decided that if I could do that in javascript then it's something I could share with other Mandelbrot addicts, sorry, enthusiasts - and also use it myself of course. Perhaps I'll find some good new coordinates for future renderings in ASCII art.

I've also used it as an excuse to mess around with things like Twitter Bootstrap, which is a handy way to start a project from scratch.

I even tried to get it to work on older versions of IE which don't support HTML5, but whilst I found that I could use things like ExCanvas to bring canvas support to older versions of IE, the performance was poor when running my fractal code. So I gave up on that idea, it was locking up older versions of IE.

The other thing that I included in the code was a javascript colour palette generator, where you pass in two colours (as RGB values) and then linear interpolation is used to create a range of colours in between. Quite a nifty way to make the end result look a bit nicer.

The final code was then washed through the google closure javascript compiler for good measure.

So I got to play with a few different toys along the way... which was fun and I learned some new things.

So I'm reasonably happy with the result. It even works on iOS, (although it did lock up once on my iPhone) I'd probably have to tinker with a few things to optimise it for proper mobile use (but I doubt that I'll bother). Even on my older iPhone 3GS the speed is acceptable. Mental note: I should try it on the Raspberry Pi and see how fast (or slow) it is, or if it works at all...

So here is some example output created whilst I have been playing:


And you can try it for yourself here.

Happy fractaling. Enjoy!

MODE_W - a minimal desktop in javascript

I decided that since I've been writing my new H2D2 programming language, it would be good to have some kind of web-based environment to run some H2D2 programs in. So I started writing something in javascript to try and do that. I started calling it MODE (Minimal Operation Desktop Environment) but then I decided to add 'W' for web as well, so I'm naming it mode_w for the moment.

The main idea was for it to be minimalistic - so it's a character based system only. I always remember thinking that the work Microsoft did with MinWin was cool, so that's one of my influences. I always remember that Windows flag logo animated from ASCII characters...

Since I wanted to be able to run several things at the same time, I built a primitive window based environment - but one that works with keyboard control only, and in a browser.

Already I've got something which can move, resize, open and close windows. Everything is simply rendered into a <pre> tag with a monospace font, so theres no mucking about with <div> blocks or anything, just an array of strings that represent the screen - one string for each line on the screen.

Each 'window' draws its own content (which scrolls when it runs out of space) and this means that I can do different things in different windows. So each window becomes a kind of widget, in a text-only kind of way.

I have also allowed character input into each window which can post back a command to a webservice, and this means you can have a command processor running as a web service somewhere else. Of course, different windows could point to different command processors, so a variety of tasks can be done from the one environment.

Here's a screenshot of how it looks so far:

So before I get H2D2 running in mode_w I'm building a few experimental widgets, like the google search widget, just to see how well it works and to iron out any issues. The google search widget allows you to type in a search term and see the top matching URLs.

And maybe I'll build an expression parsing widget which could also be used as a calculator. But after that I will try and get it to host some H2D2 programs, since that is why I made it in the first place...