Jason's Development Blog

Tag: C++

Unit Testing and C++11

by on Jan.16, 2013, under C++, Code, Life, Programming, Technology

TDD - All Code Is Guilty Until Proven Innocent Merry Christmas and Happy New Year to all! Hope you’ve had a blast over the holidays. I know I have. I went to my first Company Christmas Shindig. It was great, I got to wear a dinner suit and drink from an open bar. What more could I want?

Now, on to the coding! Lately I’ve been using a lot of C#. It’s what we predominately use at work, which explains that one. Naturally, we try to deliver the best quality product possible, and one of the techniques we employ to do so is test driven development. To accomplish this we largely use NUnit as our testing framework and Moq for mocking out implementations of interfaces. And on average this works very well.

Now in my spare time I like to occasionally code. I am not willing to give up a hobby just because it’s also a career. Now, C# is great and all but I like to spread my wings, stretch my legs and explore other languages. And most modern languages have pretty good support for TDD. C++ isn’t a modern language. In programming terms it’s steampunk, Victorian fashions realised with modern designs. It’s unit testing and mocking libraries are not quite so easy to use in a TDD context.

I tried with quite a few, and had some reasonable success with UnitTest++ combined with Google Mock. But they never quite matched my needs or expectations, unfortunately. And as the old saying goes, if your wheel is difficult to attach to your car, build a new wheel. Then burn the car down with it. It’s an interesting saying. And it led to the creation of my latest little project, UnitTest11.

UnitTest11 is a testing and mocking framework written in C++11 to take advantage of the latest features provided by the language. It aims to involve less Macros than previous testing frameworks, and allow for code to be written in a more modern style. It’s based inherently around the concepts of “Given, When and Then” (Arrange, Act and Assert), and allows for tests to be expressed in this BDD-like fashion without always requiring you to be encumbered by the strictness of Behaviour-Driven Development.

The actual assertions are written to mimic the “natural language” constraint model of NUnit, namely they adopt an “AssertThat(Value, Predicate)” format, to allow for greater readability and flexibility.

The mocking framework is designed to allow for verifications to be made after the act, as assertions. This is something many other C++ mocking libraries, such as Google Mock, do not (to the best of my knowledge) provide good or even any support for. And since both are built into one library, they integrate with less discrepancies compared to when the Testing and Mocking libraries are unrelated.

At present if you wish to look at the library, feel free to do so (For the curious, the code is under the MIT licence). I’ve not completely finished development of it, the tests driving the library are still a mixture of the new UnitTest11 style and older UnitTest++ tests used to bootstrap the development. Also not every feature is fully implemented. For example, whilst the actual Mocking classes are present support for quickly and easily creating Mock Classes is still not fully implemented. I’m looking into the best way to accomplish this, but fear the Google Mock way of having a series of Macros for varying numbers of parameters may be the only viable option. That makes me a little sad.

Still, it works and I would hope it effectively proves the concept. And if Unit Testing and TDD in C++ interest you, feel free to check it out and let me know what you think. I’ll most likely post again about it, with actual code examples, sometime in the near future.

Quote of the Day
“Cease dependence on mass inspection to achieve quality. Improve the process and build quality into the product in the first place.”
William Edwards Deming

1 Comment :, , , , , , , , , , , more...

Java Event Maps

by on Oct.18, 2010, under Code, Java, Programming

I openly admit to not being fond of coding in Java. It’s just not got the elegance C# has. However, I will use Java if only to remind myself why I don’t like using it.

I recently did just this. Largely so I could see whether the event queue system I made in C++ could easily port to Java and C#. Guess what? It does. This pleases me.

Here’s a code example of how to use the event queue:

package eventtesting;

import JpmEvents.CEventMap;
import JpmEvents.IEvent;
import JpmEvents.IEventHandler;
import JpmEvents.BEvent;

/**
 *
 * @author Jason
 */
public class Main
{
    private class TestEvent1 extends BEvent { public TestEvent1() { super("TestEvent1"); } }
    private class TestEvent2 implements IEvent
    {
        public Integer Hash() { return Name().hashCode(); }
        public String Name() { return "TestEvent2" }
        public TestEvent2() {  }
    }

    private boolean OnEvent(TestEvent1 e)
    {
        System.out.println("Event One Called!!!");
        return false;
    }
    private boolean OnEvent(TestEvent2 e)
    {
        System.out.println("Event Two Called!!!");
        return false;
    }

    protected void Run()
    {
        CEventMap emMap = new CEventMap();

        emMap.Bind(new IEventHandler<TestEvent1>(){public boolean HandleEvent(TestEvent1 event){return OnEvent(event);}}, "TestEvent1");
        emMap.Bind(new IEventHandler<TestEvent2>(){public boolean HandleEvent(TestEvent2 event){return OnEvent(event);}}, "TestEvent2");

        System.out.println("Triggering events...");
        emMap.Trigger(new TestEvent2());

        System.out.println("Queueing events...");
        emMap.Queue(new TestEvent1());
        emMap.Queue(new TestEvent2());
        emMap.Queue(new TestEvent1());

        System.out.println("Flushing events...");
        emMap.Flush();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        Main m = new Main();
        m.Run();
    }
}

And here is the link to the Project Kenai page I’m making the library and it’s source available on, under the MIT Licence

.

Quote of the Day
“I account for morality as an accidental capability produced,
in its boundless stupidity, by a biological process that is normally
opposed to the expression of such a capability”
George C. Williams (May 12, 1926 – September 8, 2010)

Leave a Comment :, , , , , , more...

Refactoring

by on Oct.16, 2010, under Life, Technology

“I don’t like how I did this at all! TO THE REDESIGN ENTIRE SYSTEM FROM GROUND UP MOBILE!!!”

So I basically had the realisation that my for Project Pauper design was modular so would be better fit into multiple Dynamically Linked Libraries than one Statically Linked Library. Unfortunately, my event system relied on RTTI, which in C++ under Windows can’t be relied upon across DLLs. At all. Ever.

So I decided to pull the event system and replace it with a new one. And one thing led to another and suddenly I had decided I wanted to refactor half the code. Joy.

Zip and date old Pauper, move it to backup directory and begin working on new pauper. Most of the start up is just getting what worked in the old to work in the new, copy and paste or rewrite as I go.

Whilst I was at it I decided Window and Graphics Device should not be one item, but separate. This design is one I dropped awhile ago, but fsuck old me, new me says it comes back. I have finally figured a good way to keep window logic in it’s own thread though, via a thread-safe event queue sitting between the window’s events and the events Pauper has to respond to. It just feels cleaner for some reason.

Leave a Comment :, , , , , , , more...

Adverts