File size: 3,786 Bytes
26d5b81 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | #ifndef NEUROFLOW_TEST_FRAMEWORK_HPP
#define NEUROFLOW_TEST_FRAMEWORK_HPP
#include <cmath>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <string>
#include <vector>
namespace neuroflow_test {
struct TestCase {
std::string name;
std::function<void()> func;
};
static std::vector<TestCase>& get_tests() {
static std::vector<TestCase> tests;
return tests;
}
static int& fail_count() {
static int count = 0;
return count;
}
struct TestRegistrar {
TestRegistrar(const char* name, std::function<void()> func) {
get_tests().push_back({name, func});
}
};
#define TEST(group, name) \
void test_##group##_##name(); \
static neuroflow_test::TestRegistrar reg_##group##_##name(#group "." #name, test_##group##_##name); \
void test_##group##_##name()
#define EXPECT_EQ(a, b) do { \
auto _a = (a); auto _b = (b); \
if (!(_a == _b)) { \
std::cerr << " FAIL: " << #a << " == " << #b \
<< " (got " << _a << " vs " << _b << ")" << std::endl; \
neuroflow_test::fail_count()++; \
} \
} while(0)
#define EXPECT_NE(a, b) do { \
auto _a = (a); auto _b = (b); \
if (_a == _b) { \
std::cerr << " FAIL: " << #a << " != " << #b << std::endl; \
neuroflow_test::fail_count()++; \
} \
} while(0)
#define EXPECT_GT(a, b) do { \
auto _a = (a); auto _b = (b); \
if (!(_a > _b)) { \
std::cerr << " FAIL: " << #a << " > " << #b \
<< " (got " << _a << " vs " << _b << ")" << std::endl; \
neuroflow_test::fail_count()++; \
} \
} while(0)
#define EXPECT_LT(a, b) do { \
auto _a = (a); auto _b = (b); \
if (!(_a < _b)) { \
std::cerr << " FAIL: " << #a << " < " << #b \
<< " (got " << _a << " vs " << _b << ")" << std::endl; \
neuroflow_test::fail_count()++; \
} \
} while(0)
#define EXPECT_NEAR(a, b, tol) do { \
auto _a = static_cast<double>(a); auto _b = static_cast<double>(b); auto _t = static_cast<double>(tol); \
if (std::abs(_a - _b) > _t) { \
std::cerr << " FAIL: " << #a << " ~= " << #b \
<< " (got " << _a << " vs " << _b << ", tol=" << _t << ")" << std::endl; \
neuroflow_test::fail_count()++; \
} \
} while(0)
#define EXPECT_TRUE(cond) do { \
if (!(cond)) { \
std::cerr << " FAIL: " << #cond << std::endl; \
neuroflow_test::fail_count()++; \
} \
} while(0)
#define EXPECT_FALSE(cond) do { \
if (cond) { \
std::cerr << " FAIL: NOT(" << #cond << ")" << std::endl; \
neuroflow_test::fail_count()++; \
} \
} while(0)
#define EXPECT_THROW(expr, exc_type) do { \
bool _caught = false; \
try { expr; } catch (const exc_type&) { _caught = true; } \
if (!_caught) { \
std::cerr << " FAIL: " << #expr << " did not throw " << #exc_type << std::endl; \
neuroflow_test::fail_count()++; \
} \
} while(0)
#define RUN_ALL_TESTS() do { \
int _passed = 0; int _failed = 0; \
for (auto& _tc : neuroflow_test::get_tests()) { \
neuroflow_test::fail_count() = 0; \
std::cout << "[ RUN ] " << _tc.name << std::endl; \
_tc.func(); \
if (neuroflow_test::fail_count() == 0) { \
std::cout << "[ OK ] " << _tc.name << std::endl; \
_passed++; \
} else { \
std::cout << "[ FAILED ] " << _tc.name << std::endl; \
_failed++; \
} \
} \
std::cout << "\n=== " << _passed << " passed, " << _failed << " failed ===" << std::endl; \
return _failed > 0 ? 1 : 0; \
} while(0)
} // namespace neuroflow_test
#endif |