Code: Select all
#pragma once
#ifndef __UNITTEST_H_
#define __UNITTEST_H_
#include <iostream>
#include <string>
#if defined (__GNUC__)
// Test for GCC compiler so we can use the typeof keyword
#define TEST(function_call, equality, value) { \
typeof(value) var = function_call; \
if (var equality (value)) \
std::cout << #function_call " passed." << std::endl; \
else \
std::cout << #function_call " failed: " << value \
<< " " << #equality << " " << var << "." << std::endl; }
#elif defined(_MSC_VER) && (_MSC_VER >= 1600)
// Check for Visual Studio 2010, because it has decltype
#define TEST(function_call, equality, value) { \
decltype(value) var = function_call; \
if (var equality (value)) \
std::cout << #function_call " passed." << std::endl; \
else \
std::cout << #function_call " failed: " << value \
<< " " << #equality << " " << var << "." << std::endl; }
#else
// Screw it, no pretty failed message for you.
#define TEST(function_call, equality, value) { \
typeof(value) var = function_call; \
if (var equality (value)) \
std::cout << #function_call " passed." << std::endl; \
else \
std::cout << #function_call " failed: " << value << "." << std::endl; }
#endif
#endif
An example usage (taken directly from my Vec3 unit testing):
Code: Select all
// Unit tests
#if 0
#include "../UnitTest.h"
#define SDL_main main
// Testing area
int main(int argc, char* argv[])
{
float3 v(1, 2, 3);
float3 u(4, 5, 6);
float3 w(4, 3, 2);
std::cout << "Max floating point epsilon is: " << epsilon << std::endl;
TEST(v+u, ==, float3(5, 7, 9));
TEST(w-u, ==, float3(0, -2, -4));
TEST(u*w, ==, 43.f);
TEST(3*v, ==, float3(3, 6, 9));
TEST(v*2, == , float3(2, 4, 6));
TEST(v/2, == , float3(0.5f, 1.f, 1.5f));
TEST(v, ==, float4(1, 2, 3));
TEST(v, !=, u);
TEST(v.length(), ==, sqrt(14.f));
TEST(v.cross(u), ==, float3(-3, 6, -3));
TEST(u^v, ==, float3(3, -6, 3));
TEST(v.normalised(), ==, float3(0.2672612419124244f, 0.5345224838248488f, 0.8017837257372732f));
TEST(w.isZero(), !=, true);
TEST(-u, !=, float3(-4, -5, -6));
#ifdef _WIN32
system("pause"); // Not the best practice, but good enough for now.
#else
getchar();
#endif
}
#endif
Code: Select all
Max floating point epsilon is: 1.19209e-007
v+u passed.
w-u passed.
u*w passed.
3*v passed.
v*2 passed.
v/2 passed.
v passed.
v passed.
v.length() passed.
v.cross(u) passed.
u^v passed.
v.normalised() passed.
w.isZero() passed.
-u failed: (-4, -5, -6) != (-4, -5, -6).