// kjellkod.wordpress.com
// simple 'test frameork' in case you want to unit test your ideone code snippet
// enjoy, Kjell
//
// USAGE:
// CREATE_TESTSUITE() should be called first in your testfunction
// it will simply generate an output of the test functions name
// and number of tests in it (that were successful)
//
// IMPORTANT:
// --- unsuccessful test stops the test execution
//
// VERIFY(boolean expression) : does NOT depend on CREATE_TESTSUITE() ---
// handy if you just want to test quickly w/out the other setup
//
//
// CHECK(boolean_expression) :
// which will trigger an exception if false
//
// CHECK_EQ(first, second) : same as CHECK but will print the actual values if not equal
// CHECK_STREQ(str1, str2) : same as CHECK_EQ but for strings
//
// EXPECT_FAIL(function_call) :
// expects the call to trigger an exception which it then 'swallows'
// if no exception comes then it triggers a new exception
//
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <string>
// For now VERIFY does depend on prior setup of
// CREATE_TESTSUITE() ... I just added it for an easy way to use it wherever
#define VERIFY(expression) \
if( false == (expression)) \
{ std::ostringstream oss; \
oss << "CHECK failed for [" << #expression << "] at " << __FUNCTION__ << ":" << __LINE__ << "\n"; \
std::cerr << oss.str() << endl; \
throw std::runtime_error(oss.str()); \
}
#define CHECK(expression) \
if( false == (expression)) \
{ std::ostringstream oss; \
oss << "CHECK failed for [" << #expression << "] at " << __FUNCTION__ << ":" << __LINE__ << "\n"; \
throw std::runtime_error(oss.str()); \
} _create_testsuite_raii_result._count++;
#define CHECK_EQ(first, second) \
{ auto f = first; auto s = second; \
if( !(f == s)) \
{ std::ostringstream oss; \
oss << "CHECK_EQ("<< f << "," << s << "] at " << __FUNCTION__ << ":" << __LINE__ << "\n"; \
throw std::runtime_error(oss.str()); \
} \
} _create_testsuite_raii_result._count++;
#define CHECK_STREQ(first, second) \
{ std::string f(first); std::string s(second); \
if(f != s ) \
{ std::ostringstream oss; \
oss << "CHECK_STREQ: \n["<< f << "]\n[" << s << "] at " << __FUNCTION__ << ":" << __LINE__ << "\n"; \
throw std::runtime_error(oss.str()); \
} \
} _create_testsuite_raii_result._count++;
#define EXPECT_FAIL( generate_check_exception ) \
{ \
bool _exception_thrown = false; \
try {generate_check_exception;} \
catch(std::exception const& e) \
{ \
_exception_thrown = true; \
} \
if (!_exception_thrown) \
{ \
std::ostringstream oss; \
oss << "EXPECTED_FAIL [" << #generate_check_exception << "]"; \
oss << "] did NOT throw as expected. EXPECTED_FAIL error at at "; \
oss << __FUNCTION__ << ":" << __LINE__ << "\n"; \
throw std::runtime_error(oss.str()); \
} _create_testsuite_raii_result._count++; \
}
namespace KjellKod_SimpleTest {
struct RaiiTestResult
{ RaiiTestResult(std::string name): _name(name), _count(0){}
~RaiiTestResult(){ std::cout <<"TESTING\t[" << _name << "] ---> " << _count << " tests: OK\n";}
std::string _name; std::size_t _count;
};
}
#define CREATE_TESTSUITE() KjellKod_SimpleTest::RaiiTestResult _create_testsuite_raii_result(__FUNCTION__)
// --------------------------------
// BELOW Example Usage
// -------------------------------
size_t first = 1;
size_t second = 2;
void test__CHECK() {
CREATE_TESTSUITE();
CHECK(first != second);
}
void test__EXPECT_FAIL() {
CREATE_TESTSUITE();
EXPECT_FAIL(throw std::runtime_error("yalla"));
EXPECT_FAIL( CHECK(first == second));
}
void test__CHECK_EQ() {
CREATE_TESTSUITE();
EXPECT_FAIL( CHECK_EQ(first, second));
}
void test__CHECK_STREQ() {
CREATE_TESTSUITE();
std::string str1("1");
std::string str2("2");
EXPECT_FAIL( CHECK_STREQ(str1, str2));
}
int main() {
test__CHECK();
test__EXPECT_FAIL();
test__CHECK_EQ();
test__CHECK_STREQ();
return 0;
}