fork download
  1. // kjellkod.wordpress.com
  2. // simple 'test frameork' in case you want to unit test your ideone code snippet
  3. // enjoy, Kjell
  4. //
  5. // USAGE:
  6. // CREATE_TESTSUITE() should be called first in your testfunction
  7. // it will simply generate an output of the test functions name
  8. // and number of tests in it (that were successful)
  9. //
  10. // IMPORTANT:
  11. // --- unsuccessful test stops the test execution
  12. //
  13. // VERIFY(boolean expression) : does NOT depend on CREATE_TESTSUITE() ---
  14. // handy if you just want to test quickly w/out the other setup
  15. //
  16. //
  17. // CHECK(boolean_expression) :
  18. // which will trigger an exception if false
  19. //
  20. // CHECK_EQ(first, second) : same as CHECK but will print the actual values if not equal
  21. // CHECK_STREQ(str1, str2) : same as CHECK_EQ but for strings
  22. //
  23. // EXPECT_FAIL(function_call) :
  24. // expects the call to trigger an exception which it then 'swallows'
  25. // if no exception comes then it triggers a new exception
  26. //
  27.  
  28.  
  29.  
  30. #include <iostream>
  31. #include <sstream>
  32. #include <stdexcept>
  33. #include <string>
  34.  
  35. // For now VERIFY does depend on prior setup of
  36. // CREATE_TESTSUITE() ... I just added it for an easy way to use it wherever
  37. #define VERIFY(expression) \
  38.   if( false == (expression)) \
  39.   { std::ostringstream oss; \
  40.   oss << "CHECK failed for [" << #expression << "] at " << __FUNCTION__ << ":" << __LINE__ << "\n"; \
  41.   std::cerr << oss.str() << endl; \
  42.   throw std::runtime_error(oss.str()); \
  43.   }
  44.  
  45.  
  46. #define CHECK(expression) \
  47.   if( false == (expression)) \
  48.   { std::ostringstream oss; \
  49.   oss << "CHECK failed for [" << #expression << "] at " << __FUNCTION__ << ":" << __LINE__ << "\n"; \
  50.   throw std::runtime_error(oss.str()); \
  51.   } _create_testsuite_raii_result._count++;
  52.  
  53. #define CHECK_EQ(first, second) \
  54.   { auto f = first; auto s = second; \
  55.   if( !(f == s)) \
  56.   { std::ostringstream oss; \
  57.   oss << "CHECK_EQ("<< f << "," << s << "] at " << __FUNCTION__ << ":" << __LINE__ << "\n"; \
  58.   throw std::runtime_error(oss.str()); \
  59.   } \
  60.   } _create_testsuite_raii_result._count++;
  61.  
  62.  
  63. #define CHECK_STREQ(first, second) \
  64.   { std::string f(first); std::string s(second); \
  65.   if(f != s ) \
  66.   { std::ostringstream oss; \
  67.   oss << "CHECK_STREQ: \n["<< f << "]\n[" << s << "] at " << __FUNCTION__ << ":" << __LINE__ << "\n"; \
  68.   throw std::runtime_error(oss.str()); \
  69.   } \
  70.   } _create_testsuite_raii_result._count++;
  71.  
  72.  
  73. #define EXPECT_FAIL( generate_check_exception ) \
  74.   { \
  75.   bool _exception_thrown = false; \
  76.   try {generate_check_exception;} \
  77.   catch(std::exception const& e) \
  78.   { \
  79.   _exception_thrown = true; \
  80.   } \
  81.   if (!_exception_thrown) \
  82.   { \
  83.   std::ostringstream oss; \
  84.   oss << "EXPECTED_FAIL [" << #generate_check_exception << "]"; \
  85.   oss << "] did NOT throw as expected. EXPECTED_FAIL error at at "; \
  86.   oss << __FUNCTION__ << ":" << __LINE__ << "\n"; \
  87.   throw std::runtime_error(oss.str()); \
  88.   } _create_testsuite_raii_result._count++; \
  89. }
  90.  
  91. namespace KjellKod_SimpleTest {
  92. struct RaiiTestResult
  93. { RaiiTestResult(std::string name): _name(name), _count(0){}
  94. ~RaiiTestResult(){ std::cout <<"TESTING\t[" << _name << "] ---> " << _count << " tests: OK\n";}
  95. std::string _name; std::size_t _count;
  96. };
  97. }
  98.  
  99. #define CREATE_TESTSUITE() KjellKod_SimpleTest::RaiiTestResult _create_testsuite_raii_result(__FUNCTION__)
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108. // --------------------------------
  109. // BELOW Example Usage
  110. // -------------------------------
  111.  
  112. size_t first = 1;
  113. size_t second = 2;
  114.  
  115. void test__CHECK() {
  116. CREATE_TESTSUITE();
  117. CHECK(first != second);
  118. }
  119.  
  120.  
  121. void test__EXPECT_FAIL() {
  122. CREATE_TESTSUITE();
  123. EXPECT_FAIL(throw std::runtime_error("yalla"));
  124. EXPECT_FAIL( CHECK(first == second));
  125. }
  126.  
  127. void test__CHECK_EQ() {
  128. CREATE_TESTSUITE();
  129. EXPECT_FAIL( CHECK_EQ(first, second));
  130. }
  131.  
  132. void test__CHECK_STREQ() {
  133. CREATE_TESTSUITE();
  134.  
  135. std::string str1("1");
  136. std::string str2("2");
  137. EXPECT_FAIL( CHECK_STREQ(str1, str2));
  138. }
  139.  
  140. int main() {
  141.  
  142.  
  143. test__CHECK();
  144. test__EXPECT_FAIL();
  145. test__CHECK_EQ();
  146. test__CHECK_STREQ();
  147. return 0;
  148. }
  149.  
Success #stdin #stdout 0s 2992KB
stdin
Standard input is empty
stdout
TESTING	[test__CHECK] ---> 1 tests: OK
TESTING	[test__EXPECT_FAIL] ---> 2 tests: OK
TESTING	[test__CHECK_EQ] ---> 1 tests: OK
TESTING	[test__CHECK_STREQ] ---> 1 tests: OK