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
*.pdb
*.swp
/*.sublime-*

View File

@ -62,35 +62,21 @@ public:
};
class BasicAssemblerTest : public Test {
public:
BasicAssemblerTest():
Test("BasicAssembler")
{}
TEST(BasicAssembler) {
BasicEnv env;
Asm a(env);
}
virtual void run() {
BasicEnv env;
Asm a(env);
TEST(ArchitecturePlan) {
BasicEnv env;
for(int op = (int)lir::Call; op < (int)lir::AlignedJump; op++) {
bool thunk;
OperandMask mask;
env.arch->plan((lir::UnaryOperation)op, vm::TargetBytesPerWord, mask, &thunk);
assertFalse(thunk);
assertNotEqual(static_cast<uint8_t>(0), mask.typeMask);
assertNotEqual(static_cast<uint64_t>(0), mask.registerMask);
}
} basicAssemblerTest;
class ArchitecturePlanTest : public Test {
public:
ArchitecturePlanTest():
Test("ArchitecturePlan")
{}
virtual void run() {
BasicEnv env;
for(int op = (int)lir::Call; op < (int)lir::AlignedJump; op++) {
bool thunk;
OperandMask mask;
env.arch->plan((lir::UnaryOperation)op, vm::TargetBytesPerWord, mask, &thunk);
assertFalse(thunk);
assertNotEqual(static_cast<uint8_t>(0), mask.typeMask);
assertNotEqual(static_cast<uint64_t>(0), mask.registerMask);
}
}
} architecturePlanTest;
}

View File

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

View File

@ -15,38 +15,41 @@
#include <stdio.h>
class Test {
private:
private:
Test* next;
static Test* first;
static Test** last;
friend int main(int argc, char** argv);
void print(uint64_t value) {
void print(uint64_t 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));
}
void print(uint8_t value) {
void print(uint8_t value)
{
print(static_cast<uint32_t>(value));
}
void print(bool value) {
void print(bool value)
{
fprintf(stderr, "%s", value ? "true" : "false");
}
int failures;
int runs;
protected:
template<class T>
void assertEqual(T expected, T actual) {
if(expected != actual) {
protected:
template <class T>
void assertEqual(T expected, T actual)
{
if (expected != actual) {
fprintf(stderr, "assertion failure, expected: ");
print(expected);
fprintf(stderr, ", actual: ");
@ -57,17 +60,23 @@ protected:
runs++;
}
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);
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);
failures++;
}
runs++;
}
template<class T>
void assertNotEqual(T expected, T actual) {
if(expected == actual) {
template <class T>
void assertNotEqual(T expected, T actual)
{
if (expected == actual) {
fprintf(stderr, "assertion failure, expected: not ");
print(expected);
fprintf(stderr, ", actual: ");
@ -78,15 +87,17 @@ protected:
runs++;
}
void assertTrue(bool value) {
void assertTrue(bool value)
{
assertEqual(true, value);
}
void assertFalse(bool value) {
void assertFalse(bool value)
{
assertEqual(false, value);
}
public:
public:
const char* const name;
Test(const char* name);
@ -95,4 +106,14 @@ public:
static bool runAll();
};
#endif // TEST_HARNESS_H
#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

View File

@ -19,51 +19,44 @@
using namespace avian::util;
class ArgParserTest : public Test {
public:
ArgParserTest():
Test("ArgParser")
{}
virtual void run() {
{
ArgParser parser;
Arg arg1(parser, false, "arg1", "<value>");
Arg required2(parser, true, "required2", "<value>");
const char* args[] = {
"myExecutable",
"-arg1", "myValue1",
"-required2", "myRequired2",
0
};
assertTrue(parser.parse(sizeof(args) / sizeof(char*) - 1, args));
assertEqual("myValue1", arg1.value);
assertEqual("myRequired2", required2.value);
}
{
ArgParser parser;
Arg arg1(parser, false, "arg1", "<value>");
Arg required2(parser, true, "required2", "<value>");
const char* args[] = {
"myExecutable",
"-arg1", "myValue1",
"-required2",
0
};
assertFalse(parser.parse(sizeof(args) / sizeof(char*) - 1, args));
}
{
ArgParser parser;
Arg arg1(parser, false, "arg1", "<value>");
Arg required2(parser, true, "required2", "<value>");
const char* args[] = {
"myExecutable",
"-arg1", "myValue1",
0
};
assertFalse(parser.parse(sizeof(args) / sizeof(char*) - 1, args));
}
TEST(ArgParser) {
{
ArgParser parser;
Arg arg1(parser, false, "arg1", "<value>");
Arg required2(parser, true, "required2", "<value>");
const char* args[] = {
"myExecutable",
"-arg1", "myValue1",
"-required2", "myRequired2",
0
};
assertTrue(parser.parse(sizeof(args) / sizeof(char*) - 1, args));
assertEqual("myValue1", arg1.value);
assertEqual("myRequired2", required2.value);
}
} argParserTest;
{
ArgParser parser;
Arg arg1(parser, false, "arg1", "<value>");
Arg required2(parser, true, "required2", "<value>");
const char* args[] = {
"myExecutable",
"-arg1", "myValue1",
"-required2",
0
};
assertFalse(parser.parse(sizeof(args) / sizeof(char*) - 1, args));
}
{
ArgParser parser;
Arg arg1(parser, false, "arg1", "<value>");
Arg required2(parser, true, "required2", "<value>");
const char* args[] = {
"myExecutable",
"-arg1", "myValue1",
0
};
assertFalse(parser.parse(sizeof(args) / sizeof(char*) - 1, args));
}
}