Merge pull request #166 from joshuawarner32/master

Improve c++ unit test harness
This commit is contained in:
Joel Dice 2014-02-09 15:16:38 -07:00
commit 07abed9c65
5 changed files with 114 additions and 120 deletions

1
.gitignore vendored
View File

@ -9,3 +9,4 @@ bin
/distrib /distrib
*.pdb *.pdb
*.swp *.swp
/*.sublime-*

View File

@ -62,25 +62,12 @@ public:
}; };
class BasicAssemblerTest : public Test { TEST(BasicAssembler) {
public:
BasicAssemblerTest():
Test("BasicAssembler")
{}
virtual void run() {
BasicEnv env; BasicEnv env;
Asm a(env); Asm a(env);
} }
} basicAssemblerTest;
class ArchitecturePlanTest : public Test { TEST(ArchitecturePlan) {
public:
ArchitecturePlanTest():
Test("ArchitecturePlan")
{}
virtual void run() {
BasicEnv env; BasicEnv env;
for(int op = (int)lir::Call; op < (int)lir::AlignedJump; op++) { for(int op = (int)lir::Call; op < (int)lir::AlignedJump; op++) {
@ -93,4 +80,3 @@ public:
} }
} }
} architecturePlanTest;

View File

@ -19,13 +19,7 @@ using namespace avian::codegen;
using namespace vm; using namespace vm;
class RegisterIteratorTest : public Test { TEST(RegisterIterator) {
public:
RegisterIteratorTest():
Test("RegisterIterator")
{}
virtual void run() {
RegisterMask regs(0x55); RegisterMask regs(0x55);
assertEqual<unsigned>(0, regs.start); assertEqual<unsigned>(0, regs.start);
assertEqual<unsigned>(7, regs.limit); assertEqual<unsigned>(7, regs.limit);
@ -41,4 +35,3 @@ public:
assertEqual<unsigned>(6, it.next()); assertEqual<unsigned>(6, it.next());
assertFalse(it.hasNext()); assertFalse(it.hasNext());
} }
} registerIteratorTest;

View File

@ -22,21 +22,23 @@ private:
friend int main(int argc, char** argv); friend int main(int argc, char** argv);
void print(uint64_t value)
void print(uint64_t value) { {
fprintf(stderr, "%p", reinterpret_cast<void*>(value)); fprintf(stderr, "%p", reinterpret_cast<void*>(value));
} }
void print(uint32_t value) { void print(uint32_t value)
{
fprintf(stderr, "%p", reinterpret_cast<void*>(value)); fprintf(stderr, "%p", reinterpret_cast<void*>(value));
} }
void print(uint8_t value)
void print(uint8_t value) { {
print(static_cast<uint32_t>(value)); print(static_cast<uint32_t>(value));
} }
void print(bool value) { void print(bool value)
{
fprintf(stderr, "%s", value ? "true" : "false"); fprintf(stderr, "%s", value ? "true" : "false");
} }
@ -45,7 +47,8 @@ private:
protected: protected:
template <class T> template <class T>
void assertEqual(T expected, T actual) { void assertEqual(T expected, T actual)
{
if (expected != actual) { if (expected != actual) {
fprintf(stderr, "assertion failure, expected: "); fprintf(stderr, "assertion failure, expected: ");
print(expected); print(expected);
@ -57,16 +60,22 @@ protected:
runs++; runs++;
} }
void assertEqual(const char* expected, const char* actual) { void assertEqual(const char* expected, const char* actual)
if((expected == 0 && actual != 0) || (expected != 0 && actual == 0) || strcmp(expected, actual) != 0) { {
fprintf(stderr, "assertion failure, expected: \"%s\", actual: \"%s\"\n", expected, actual); if ((expected == 0 && actual != 0) || (expected != 0 && actual == 0)
|| strcmp(expected, actual) != 0) {
fprintf(stderr,
"assertion failure, expected: \"%s\", actual: \"%s\"\n",
expected,
actual);
failures++; failures++;
} }
runs++; runs++;
} }
template <class T> template <class T>
void assertNotEqual(T expected, T actual) { void assertNotEqual(T expected, T actual)
{
if (expected == actual) { if (expected == actual) {
fprintf(stderr, "assertion failure, expected: not "); fprintf(stderr, "assertion failure, expected: not ");
print(expected); print(expected);
@ -78,11 +87,13 @@ protected:
runs++; runs++;
} }
void assertTrue(bool value) { void assertTrue(bool value)
{
assertEqual(true, value); assertEqual(true, value);
} }
void assertFalse(bool value) { void assertFalse(bool value)
{
assertEqual(false, value); assertEqual(false, value);
} }
@ -95,4 +106,14 @@ public:
static bool runAll(); static bool runAll();
}; };
#define TEST(name) \
class name##TestClass : public Test { \
public: \
name##TestClass() : Test(#name) \
{ \
} \
virtual void run(); \
} name##TestInstance; \
void name##TestClass::run()
#endif // TEST_HARNESS_H #endif // TEST_HARNESS_H

View File

@ -19,13 +19,7 @@
using namespace avian::util; using namespace avian::util;
class ArgParserTest : public Test { TEST(ArgParser) {
public:
ArgParserTest():
Test("ArgParser")
{}
virtual void run() {
{ {
ArgParser parser; ArgParser parser;
Arg arg1(parser, false, "arg1", "<value>"); Arg arg1(parser, false, "arg1", "<value>");
@ -66,4 +60,3 @@ public:
assertFalse(parser.parse(sizeof(args) / sizeof(char*) - 1, args)); assertFalse(parser.parse(sizeof(args) / sizeof(char*) - 1, args));
} }
} }
} argParserTest;