Archive for October, 2007

A mini test framework in a single header file

Wednesday, October 31st, 2007

After trying it on a number of projects, I’m now very enthusiastic about test driven development. At home I’ve rather missed the minimal support code that I had at my old job, so I’ve rewritten a miniature test framework in a single header file.

Only this time, it’s better. Framework implies something rather big. This isn’t. The design goal was to make something really simple and lightweight that just makes the process of writing tests as simple as possible with as few overheads as possible.

In this framework a test function is just a void function returning void. If the function doesn’t throw an exception when it is called then it has passed.

Here are some example tests, which show the three different type of assert macros provided. (Yes all the tests are broken!)

void strlen_test()
{
    HSHG_ASSERT( strlen( "1245" ) == 5 );
}

void sum_test()
{
    int total = 0;

    for( int i = 1; i <= 15; ++i )
    {
        total += 1;
        total += 7;
    }

    for( int i = 2; i <= 7; ++i )
    {
        total += i;
    }

    HSHG_ASSERT_DESC( total == 148, "Maximum break is 148" );
}

class MyThrowable
{
public:
    static void ThrowMe()
    {
        throw MyThrowable();
    }
};

void MyThrowable_test()
{
    HSHG_ASSERT_THROWS( true ? 0 : (MyThrowable::ThrowMe(), 0), MyThrowable );
}

The test namespace is HSHGTest and there is a struct TestFn for a test function which contains a char* for the function name and a pointer to the function itself.

There is then an inline function called RunTests that takes an array of these structs (terminated by one with a null function pointer); it runs each test in the array and reports to a given std::ostream; it then returns EXIT_SUCCESS if it ran some tests and they all passed, and EXIT_FAILURE otherwise. This makes it suitable for returning the result directly from a main function. Here is an example report.

testtest.cc:6: Test strlen_test failed. ( strlen( "1245" ) == 5 )
testtest.cc:24: Test sum_test failed. ( Maximum break is 148 )
testtest.cc:38: Test MyThrowable_test failed. ( Exception MyThrowable expected. )

If this sounds a bit laborious then there are some helper macros to set a suitable array up, and even a macro for a default main function:

HSHG_BEGIN_TESTS
HSHG_TEST_ENTRY( strlen_test )
HSHG_TEST_ENTRY( sum_test )
HSHG_TEST_ENTRY( MyThrowable_test )
HSHG_END_TESTS

HSHG_TEST_MAIN

These macros translate (roughly) into:

namespace
{

HSHGTest::TestFn tests[] =
{
    { "strlen_test", strlen_test },
    { "sum_test", sum_test },
    { "MyThrowable_test", MyThrowable_test },
    { NULL, NULL }
};

}

int main()
{
    return HSHGTest::RunTests( tests, std::cout );
}

The "framework" is available for download here. HSHGTest frawework

Address Space Monitor vs Google

Sunday, October 28th, 2007

In an unusual coincidence, googlebot visited my homepage the day after I put the link to Address Space Monitor live, and the next day it was the top search result for the query: ‘Address Space Monitor’. It stayed there for a few days but now the asm homepage is not in Google’s index at all. I’ve logged in to Google’s Webmaster Tools but so far they haven’t shed any light on the situation.

[Edit: Monday morning] And now it’s back in at #1. It must be a glitch in the google…

Address Space Monitor

Tuesday, October 23rd, 2007

Finally, I manage to release software on the unsuspecting world!

I wrote this tool in response to spending some painful time debugging a process which seemed unable to allocate a chunk of memory when most conventional tools were showing that the process wasn’t at the limit in terms of memory usage and the system hadn’t run out of swap space. The problem was virtual address space fragmentation.

Address Space Monitor is a windows tool that shows graphically how a process’ address space has been carved up and how big and where the biggest blobs of contiguous free memory are in the address space.

Naturally, if you are using the tool in earnest, the process which is giving you trouble will inevitably be resource heavy and slow. Hence, Address Space Monitor (ASM hence forth – not to be confused with assembly language source files) has been written to minimise its own resource usage while retaining boredom alleviating features such as fun colours and a bouncy CPU meter. You cannot yet use it to read mail, so it is still classed as “in development”. Oh yes, and the ‘a’ at the end of 0.5a means that it is alpha software.

Oh, where is it?

<c3po>Over Here!</c3po>