Skip to content

Instantly share code, notes, and snippets.

@Helco
Last active March 14, 2018 13:18
Show Gist options
  • Save Helco/5fa53be0e5c0b970a108a219e2e4a071 to your computer and use it in GitHub Desktop.
Save Helco/5fa53be0e5c0b970a108a219e2e4a071 to your computer and use it in GitHub Desktop.
NGFX Test Framework styles
typedef struct {
bool success;
const char* message;
const char* file;
unsigned int line;
} TestResult;
#define NGFX_ASSERT_MSG(cond, msg) do { \
if (!(cond)) return (TestResult) { \
.success = false, \
.message = msg, \
.file = __FILE__, \
.line = __LINE__ \
}; \
}while(0)
#define NGFX_ASSERT(cond) NGFX_ASSERT_MSG(cond, #cond)
#define NGFX_ASSERT_EQ(actual, expected) NGFX_ASSERT(actual == expected)
#define NGFX_ASSERT_EQSTR(actual, expected) NGFX_ASSERT(strcmp(actual, expected) == 0)
#define NGFX_ASSERT_PIXEL_EQ(point, expected) NGFX_ASSERT(...)
#define NGFX_ASSERT_RECT_EQ(rect, resource_expected) NGFX_ASSERT(...)
#define NGFX_ASSERT_SCREEN_EQ(resource_expected) NGFX_ASSERT(...)
// ...
// testframework.h
// tl;dr module has to be identifier, name does not, only one point to add tests
#define NGFX_BIG_TEST(module, name, code) // stub
#define NGFX_SMALL_TEST(module, name, code) // stub
#define NGFX_ASSERT(cond, msg) // see assert.h
// some_test.h
NGFX_TEST(GBitmap, Some important test, {
NGFX_ASSERT_EQ(true, false);
})
// tests.h
#include "some_test.h"
// testframework.c
// the test function implementations (shortened)
#define NGFX_TEST(module, name, code) TestResult test_##module##_##__LINE__ () { \
{ code } \
return (TestResult) { .success = true }; \
}
#include "tests.h"
// the test metadata
#define NGFX_TEST(module, name, code) { \
.module = #module, \
.test = #name, \
.func = test_module_##__LINE__ \
},
static const Test tests[] = {
#include "tests.h"
{ .func = NULL }
};
// testframework.h
// tl;dr both module and name have to be identifiers, to add tests add at two points
#define NGFX_TEST(module, name) TestResult test_##module##_##name ()
#define NGFX_ASSERT(cond, msg) // see assert.h
// some_test.h
NGFX_TEST(GBitmap, SomeImportantTest);
// some_test.c
NGFX_TEST(GBitmap, SomeImportantTest) {
NGFX_ASSERT_EQ(true, false)
}
// testframework.c
#define NGFX_TEST(module, name) { \
.module = module, \
.name = name, \
.func = test_##module##_##name \
},
static const Test tests[] = {
#include "tests.h"
{ .func = NULL }
};
//testframework.h
// tl;dr there is no module nor name and there are three points to add tests
#define NGFX_ASSERT(cond, msg) // see assert.h
// some_test.h
TestResult test_GBitmap_SomeImportantTest();
// some_test.c
TestResult test_GBitmap_SomeImportantTest() {
NGFX_ASSERT_EQ(true, false);
}
// tests.c
const Test tests[] = {
{
.module = "GBitmap",
.name = "Some important test",
.func = test_GBitmap_SomeImportantTest
},
{ .func = NULL }
};
// testframework.c
extern const Test tests[];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment