Need…More..Time

26 11 2010

What a hectic, horrible week… Sometimes life throws a curve ball that hits you square in the, well, soft bits…
But enough bad sport-to-life metaphors; let’s get on with some code. Been writing alot of C# lately and I’ve got lots of stuff to say about the language [and most of it is even good believe it or not] but right now I’d like to take the time to go through the motion of TDD and my Timer class and all in glorious C++, yay!

For my main testing framework I use UnitTest++ which is simple to use and robust testing framework by Noel Llopis and Charles Nicholson. The only hickups I’ve had with it so far are the ++ in the namespace name and the fact that the TEST_FIXTURE macro seems to generate an intellisense error on odd numbers of tests [which is most likely a problem with the intellisense on my copy of Visual Studio 2010 Express rather than the actual code].

Most of the time when I’m about to test or design something the first question I usually ask myself is, what do I want?
So, in short, I want a class that will be able to act as a timer and measure both its total running time and the elapsed time between two ticks [handy for frame timing]. Like a good stopwatch it should also be able to stop, start and reset when needed.

Ok, I know what I want, let’s see what I can get. In a glance I can also see that as far as a full component the Timer isn’t going to be overly complex so I most definitly can finish writing it up it a while and a little bit… But because I’m not sure where all the bits are going to go and the whole point is to practice TDD I’m going to test first and see what happens.

Everything set, everything go. First test.

#include <UnitTest.h>
#include "Timer.h"

TEST(check_timer_name_is_timer1)
{
  Timer timer("timer1");
  CHECK_EQUAL("timer1", timer.Name());
}

Stop, compile, and watch it crash. So many things don’t even exist yet. Before I continue though, most developers I worked with would probably laugh at the level of functionality I’ve just tested. The arguments I usually hear are: It’s too small, it’s testing that the language work and it’s such a small thing it’ll only take a second to write. And to that I usually answer, there’s nothing too small to test because you never know what you might change. Also, I’m not actually testing functionality, I’m designing it. And finally, if it only takes a second to write it’ll only take a second to test and surely it’s better to have a test then not have one.

Now what?
Well, the compiler tells me that I don’t have a file called “timer.h”; so let’s create one.
Now the compiler complains that there’s no such thing as a Timer class. Quick, let’s write one up.

#ifndef _TDD_TIMER_
#define _TDD_TIMER_

class Timer
{

};

#endif/*_TDD_TIMER_*/

Compile, run, more complaints… What a fussy environment. The constructor signature doesn’t match and there’s no function called Name().

#ifndef _TDD_TIMER_
#define _TDD_TIMER_

class Timer
{
  public:
    typedef char* name_type;
    
    name_type Name() const { return "timer1"; }

    Timer(name_type name){}
};

#endif/*_TDD_TIMER_*/

Compile, run and phew… The test passes. The first functionality is in. It doesn’t look like much but the important thing is that it follows the design decisions as documented via the tests. I think this process is far too long for any single post and I’ll run it over the next few days [one post per day] until I get a timer or a headache, depends.

“…and if I bring a little music I can fit right in…”

Advertisements

Actions

Information

One response

26 11 2010
Tatham

I love tests 🙂
One additional thing I would do there is to make the Name() function initially return an empty string. That way, the test fails in the testing system rather than in the compiler. More than a few times I’ve done this, then the test passes! And there’s been something wrong with my testing code that I need to fix so that it works. Then I send it the right value, the test passes, move on 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: