Sunday, June 17, 2012

Object Oriented C - Part 2

Now that I have detailed my existing pattern it's time to start working on an easier to implement/maintain replacement. To do this I need to start thinking about how to test it. After all I am fully embracing test driven development these days.

One thing I forgot of course when detailing the existing pattern is how to use it in an application. I think that part at least is pretty straight forward but just to complete the square, here's an example main.c:
#include "opentv.h"
#include "assert.h"
#include "MyClass.h"
/* This method just waits for a quit message and then exits the application */
static void Main_waitToExit(void)
{
while (TRUE)
{
o_message tMessage;
O_ui_get_event_wait(&tMessage);
if (O_msg_class(&tMessage) == MSG_CLASS_CONTROL)
{
if (O_msg_type(&tMessage) == MSG_TYPE_QUIT)
{
O_exit();
}
}
}
}
void main(void)
{
/*
record the current memory available so we
can detect memory leaks
*/
size_t uMemory = O_heap_available();
MyClass * pInstance = MyClass_create(5);
MyInterface * pInterfaceInstance = MyClass_asMyInterface(pInstance);
/* Method tests */
assert(MyClass_getMyField(pInstance) == 5);
assert(MyInterface_myMethod(pInterfaceInstance, 3) == 8);
/* Memory leak test */
MyClass_destroy(pInstance);
assert(O_heap_available() == uMemory);
/* Stick around so the VSTB does not exit and we know we ran everything */
O_debug("OOOCode: Tests passed\n");
Main_waitToExit();
}
view raw Main.c hosted with ❤ by GitHub

So here are the tests although I haven't used the unit test framework I might use ordinarily (I didn't want to cloud the details and introduce more dependencies). Again I haven't run this code (currently working in OSx and the OpenTV IDE only runs on Windows) but as soon as I do I'll correct any mistakes found and likely add it to my previously created project in GitHub (currently lying empty)

One last thing for this post though, I thought of a better name for my project. From Object Oriented OCode we get OOOCode. That's right... Oooooo, code ;) (or Oh! Oh! Oh! Code - haven't decided)

Previous link is now almost certainly broken so here's a new one.

https://github.com/pghalliday/OOOCode

No comments:

Post a Comment