12#if defined(TUT_USE_SEH)
50 std::logic_error(grp){};
69 std::logic_error(msg){};
78 failure(
const std::string& msg) : std::logic_error(msg){};
87 warning(
const std::string& msg) : std::logic_error(msg){};
93 class seh :
public std::logic_error
96 seh(
const std::string& msg) : std::logic_error(msg){};
154 const std::exception&
ex)
231 typedef std::map<std::string,group_base*>
groups;
254 throw std::invalid_argument(
"group shall be non-null");
257 groups::iterator found =
groups_.find(name);
260 std::string msg(
"attempt to add already existent group "+name);
262 std::cerr << msg << std::endl;
263 throw std::logic_error(msg);
295 ret.push_back(i->first);
316 run_all_tests_in_group_(i);
346 run_all_tests_in_group_(i);
425 extern test_runner_singleton
runner;
432 template <
class Data>
468 if( !cond )
throw failure(
"");
476 void ensure(
const T msg,
bool cond)
478 if( !cond )
throw failure(msg);
488 template <
class T,
class Q>
489 void ensure_equals(
const char* msg,
const Q& actual,
const T& expected)
491 if( expected != actual )
493 std::stringstream ss;
494 ss << (msg?msg:
"") << (msg?
": ":
"") <<
"expected " << expected <<
" actual " <<
actual;
495 throw failure(ss.str().c_str());
499 template <
class T,
class Q>
502 ensure_equals<>(0,actual,expected);
515 void ensure_distance(
const char* msg,
const T& actual,
const T& expected,
const T& distance)
517 if( expected-distance >= actual || expected+distance <= actual )
519 std::stringstream ss;
520 ss << (msg?msg:
"") << (msg?
": ":
"") <<
"expected [" << expected-distance <<
";"
521 << expected+distance <<
"] actual " <<
actual;
522 throw failure(ss.str().c_str());
527 void ensure_distance(
const T& actual,
const T& expected,
const T& distance)
529 ensure_distance<>(0,actual,expected,distance);
535 void fail(
const char* msg=
"")
545 template <
class Test,
class Group,
int n>
548 static void reg(Group& group)
550 group.reg(n,&Test::template test<n>);
555 template<
class Test,
class Group>
558 static void reg(Group&){};
566 template <
class Data,
int MaxTestsInGroup = 50>
572 typedef std::map<int,testmethod> tests;
573 typedef typename tests::iterator tests_iterator;
574 typedef typename tests::const_iterator tests_const_iterator;
575 typedef typename tests::const_reverse_iterator
576 tests_const_reverse_iterator;
577 typedef typename tests::size_type size_type;
580 tests_iterator current_test_;
589 bool permit_throw_in_dtor;
591 safe_holder(
const safe_holder&);
592 safe_holder& operator = (
const safe_holder&);
595 safe_holder() : p_(0),permit_throw_in_dtor(
false)
604 T* operator -> ()
const {
return p_; };
605 T* get()
const {
return p_; };
612 void permit_throw(){ permit_throw_in_dtor =
true; }
624 if( delete_obj() ==
false )
626 throw warning(
"destructor of test object raised an SEH exception");
629 catch(
const std::exception& ex )
631 if( permit_throw_in_dtor )
633 std::string msg =
"destructor of test object raised exception: ";
640 if( permit_throw_in_dtor )
642 throw warning(
"destructor of test object raised an exception");
653 permit_throw_in_dtor =
false;
659#if defined(TUT_USE_SEH)
666#if defined(TUT_USE_SEH)
668 __except(handle_seh_(::GetExceptionCode()))
670 if( permit_throw_in_dtor )
713 void reg(
int n,testmethod tm)
723 current_test_ = tests_.begin();
731 if( current_test_ == tests_.end() )
737 safe_holder<object> obj;
738 while( current_test_ != tests_.end() )
742 return run_test_(current_test_++,obj);
763 tests_iterator ti = tests_.find(n);
766 safe_holder<object> obj;
767 return run_test_(ti,obj);
775 test_result run_test_(
const tests_iterator& ti,safe_holder<object>& obj)
779 if( run_test_seh_(ti->second,obj) ==
false )
782 catch(
const no_such_test&)
786 catch(
const warning& ex)
792 catch(
const failure& ex)
804 catch(
const bad_ctor& ex)
810 catch(
const std::exception& ex)
831 bool run_test_seh_(testmethod tm,safe_holder<object>& obj)
833#if defined(TUT_USE_SEH)
841 obj->called_method_was_a_dummy_test_ =
false;
843#if defined(TUT_USE_SEH)
848#if defined(TUT_USE_SEH)
850 __except(handle_seh_(::GetExceptionCode()))
857 if( obj->called_method_was_a_dummy_test_ )
860 throw no_such_test();
865#if defined(TUT_USE_SEH)
867 __except(handle_seh_(::GetExceptionCode()))
875 void reset_holder_(safe_holder<object>& obj)
881 catch(
const std::exception& ex)
883 throw bad_ctor(ex.what());
887 throw bad_ctor(
"test constructor has generated an exception; group execution is terminated");
892#if defined(TUT_USE_SEH)
896 inline int handle_seh_(DWORD excode)
900 case EXCEPTION_ACCESS_VIOLATION:
901 case EXCEPTION_DATATYPE_MISALIGNMENT:
902 case EXCEPTION_BREAKPOINT:
903 case EXCEPTION_SINGLE_STEP:
904 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
905 case EXCEPTION_FLT_DENORMAL_OPERAND:
906 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
907 case EXCEPTION_FLT_INEXACT_RESULT:
908 case EXCEPTION_FLT_INVALID_OPERATION:
909 case EXCEPTION_FLT_OVERFLOW:
910 case EXCEPTION_FLT_STACK_CHECK:
911 case EXCEPTION_FLT_UNDERFLOW:
912 case EXCEPTION_INT_DIVIDE_BY_ZERO:
913 case EXCEPTION_INT_OVERFLOW:
914 case EXCEPTION_PRIV_INSTRUCTION:
915 case EXCEPTION_IN_PAGE_ERROR:
916 case EXCEPTION_ILLEGAL_INSTRUCTION:
917 case EXCEPTION_NONCONTINUABLE_EXCEPTION:
918 case EXCEPTION_STACK_OVERFLOW:
919 case EXCEPTION_INVALID_DISPOSITION:
920 case EXCEPTION_GUARD_PAGE:
921 case EXCEPTION_INVALID_HANDLE:
922 return EXCEPTION_EXECUTE_HANDLER;
925 return EXCEPTION_CONTINUE_SEARCH;
Exception to be throwed when ensure() fails or fail() called.
Definition tut.h:76
failure(const std::string &msg)
Definition tut.h:78
Exception to be throwed when test issued SEH (Win32)
Definition tut.h:94
seh(const std::string &msg)
Definition tut.h:96
Test group; used to recreate test object instance for each new test since we have to have reinitializ...
Definition tut.h:568
test_result run_test(int n)
Runs one test by position.
Definition tut.h:756
test_group(const char *name)
Creates and registers test group with specified name.
Definition tut.h:686
void reg(int n, testmethod tm)
Registers test method under given number.
Definition tut.h:713
test_group(const char *name, test_runner &another_runner)
This constructor is used in self-test run only.
Definition tut.h:699
test_object< Data > object
Definition tut.h:681
void rewind()
Reset test position before first test.
Definition tut.h:721
test_result run_next()
Runs next test.
Definition tut.h:729
Test object.
Definition tut.h:434
test_object()
Default constructor.
Definition tut.h:439
bool called_method_was_a_dummy_test_
The flag is set to true by default (dummy) test.
Definition tut.h:448
void test()
Default do-nothing test.
Definition tut.h:454
Singleton for test_runner implementation.
Definition tut.h:417
static test_runner & get()
Definition tut.h:419
Test runner.
Definition tut.h:229
groups groups_
Definition tut.h:234
void set_callback(callback *cb)
Stores callback object.
Definition tut.h:272
void run_tests() const
Runs all tests in all groups.
Definition tut.h:305
groups::const_iterator const_iterator
Definition tut.h:233
callback & get_callback() const
Returns callback object.
Definition tut.h:280
groups::iterator iterator
Definition tut.h:232
void register_group(const std::string &name, group_base *gr)
Stores another group for getting by name.
Definition tut.h:250
std::map< std::string, group_base * > groups
Definition tut.h:231
callback * callback_
Definition tut.h:237
test_result run_test(const std::string &group_name, int n) const
Runs one test in specified group.
Definition tut.h:360
test_runner()
Constructor.
Definition tut.h:243
void run_tests(const std::string &group_name) const
Runs all tests in specified group.
Definition tut.h:332
callback default_callback_
Definition tut.h:236
const groupnames list_groups() const
Returns list of known test groups.
Definition tut.h:288
Exception to be throwed when test desctructor throwed an exception.
Definition tut.h:85
warning(const std::string &msg)
Definition tut.h:87
Template Unit Tests Framework for C++.
Definition tut-main.cpp:7
std::vector< std::string > groupnames
Typedef for runner::list_groups()
Definition tut.h:223
test_runner_singleton runner
Definition tut-main.cpp:8
Actual< A > actual(const A &actual)
Definition tests.h:384
Internal exception to be throwed when test constructor has failed.
Definition tut.h:67
bad_ctor(const std::string &msg)
Definition tut.h:68
No such test and passed test number is higher than any test number in current group.
Definition tut.h:40
beyond_last_test()
Definition tut.h:41
Test runner callback interface.
Definition tut.h:185
virtual void run_started()
Called when new test run started.
Definition tut.h:194
virtual ~callback()
Virtual destructor is a must for subclassed types.
Definition tut.h:189
virtual void test_completed(const test_result &)
Called when a test finished.
Definition tut.h:206
virtual void group_completed(const std::string &)
Called when a group is completed.
Definition tut.h:212
virtual void run_completed()
Called when all tests in run completed.
Definition tut.h:217
virtual void group_started(const std::string &)
Called when a group started.
Definition tut.h:200
Interface.
Definition tut.h:166
virtual test_result run_test(int n)=0
virtual ~group_base()
Definition tut.h:167
virtual test_result run_next()=0
Internal exception to be throwed when no more tests left in group or journal.
Definition tut.h:58
no_more_tests()
Definition tut.h:59
Group not found exception.
Definition tut.h:48
no_such_group(const std::string &grp)
Definition tut.h:49
Exception to be throwed when attempted to execute missed test by number.
Definition tut.h:30
no_such_test()
Definition tut.h:31
Return type of runned test/test group.
Definition tut.h:106
std::string exception_typeid
Definition tut.h:131
result_type result
Definition tut.h:125
std::string group
Test group name.
Definition tut.h:110
test_result(const std::string &grp, int pos, result_type res, const std::exception &ex)
Constructor with exception.
Definition tut.h:152
std::string message
Exception message for failed test.
Definition tut.h:130
result_type
ok - test finished successfully fail - test failed with ensure() or fail() methods ex - test throwed ...
Definition tut.h:124
@ term
Definition tut.h:124
@ fail
Definition tut.h:124
@ ex_ctor
Definition tut.h:124
@ warn
Definition tut.h:124
test_result()
Default constructor.
Definition tut.h:136
test_result(const std::string &grp, int pos, result_type res)
Constructor.
Definition tut.h:144
int test
Test number in group.
Definition tut.h:115
static void reg(Group &)
Definition tut.h:558
Walks through test tree and stores address of each test method in group.
Definition tut.h:547
static void reg(Group &group)
Definition tut.h:548
#define ensure(x)
Definition tests.h:101
#define ensure_equals(x, y)
Definition tests.h:105