corda/sgx-jvm/avian/unittest/test-harness.h

120 lines
2.5 KiB
C
Raw Normal View History

2015-03-13 18:52:59 +00:00
/* Copyright (c) 2008-2015, Avian Contributors
2013-02-15 03:54:20 +00:00
Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.
There is NO WARRANTY for this software. See license.txt for
details. */
2013-02-15 16:54:05 +00:00
#ifndef TEST_HARNESS_H
#define TEST_HARNESS_H
2013-02-15 03:54:20 +00:00
#include "avian/common.h"
2013-02-17 19:10:18 +00:00
#include <stdio.h>
2013-02-15 03:54:20 +00:00
class Test {
2014-01-31 00:34:59 +00:00
private:
2013-02-15 03:54:20 +00:00
Test* next;
static Test* first;
static Test** last;
friend int main(int argc, char** argv);
2014-01-31 00:34:59 +00:00
void print(uint64_t value)
{
2013-02-15 03:54:20 +00:00
fprintf(stderr, "%p", reinterpret_cast<void*>(value));
}
2014-01-31 00:34:59 +00:00
void print(uint32_t value)
{
2013-02-15 16:54:05 +00:00
fprintf(stderr, "%p", reinterpret_cast<void*>(value));
2013-02-15 03:54:20 +00:00
}
2014-01-31 00:34:59 +00:00
void print(uint8_t value)
{
2013-02-15 16:54:05 +00:00
print(static_cast<uint32_t>(value));
2013-02-15 14:35:17 +00:00
}
2014-01-31 00:34:59 +00:00
void print(bool value)
{
2013-02-15 16:54:05 +00:00
fprintf(stderr, "%s", value ? "true" : "false");
2013-02-15 14:35:17 +00:00
}
2013-02-15 03:54:20 +00:00
int failures;
int runs;
2014-01-31 00:34:59 +00:00
protected:
template <class T>
void assertEqual(T expected, T actual)
{
if (expected != actual) {
2013-02-15 03:54:20 +00:00
fprintf(stderr, "assertion failure, expected: ");
print(expected);
fprintf(stderr, ", actual: ");
print(actual);
fprintf(stderr, "\n");
failures++;
}
runs++;
}
2013-02-17 19:10:18 +00:00
2014-01-31 00:34:59 +00:00
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);
2013-02-17 19:10:18 +00:00
failures++;
}
runs++;
}
2014-01-31 00:34:59 +00:00
template <class T>
void assertNotEqual(T expected, T actual)
{
if (expected == actual) {
2013-02-15 14:35:17 +00:00
fprintf(stderr, "assertion failure, expected: not ");
print(expected);
fprintf(stderr, ", actual: ");
print(actual);
fprintf(stderr, "\n");
failures++;
}
runs++;
}
2013-02-15 03:54:20 +00:00
2014-01-31 00:34:59 +00:00
void assertTrue(bool value)
{
2013-02-15 14:35:17 +00:00
assertEqual(true, value);
}
2014-01-31 00:34:59 +00:00
void assertFalse(bool value)
{
2013-02-15 14:35:17 +00:00
assertEqual(false, value);
2013-02-15 03:54:20 +00:00
}
2014-01-31 00:34:59 +00:00
public:
2013-02-15 03:54:20 +00:00
const char* const name;
Test(const char* name);
virtual void run() = 0;
static bool runAll();
};
2014-01-31 00:34:59 +00:00
#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