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){};
145 : group(grp),test(pos),result(res)
154 const std::exception& ex)
155 : group(grp),test(pos),result(res),
156 message(ex.what()),exception_typeid(typeid(ex).name())
170 virtual void rewind() = 0;
231 typedef std::map<std::string,group_base*>
groups;
254 throw std::invalid_argument(
"group shall be non-null");
258 if( found != groups_.end() )
260 std::string msg(
"attempt to add already existent group "+name);
262 std::cerr << msg << std::endl;
263 throw std::logic_error(msg);
274 callback_ = cb==0? &default_callback_:cb;
291 const_iterator i = groups_.begin();
292 const_iterator e = groups_.end();
295 ret.push_back(i->first);
309 const_iterator i = groups_.begin();
310 const_iterator e = groups_.end();
316 run_all_tests_in_group_(i);
336 const_iterator i = groups_.find(group_name);
337 if( i == groups_.end() )
346 run_all_tests_in_group_(i);
364 const_iterator i = groups_.find(group_name);
365 if( i == groups_.end() )
395 void run_all_tests_in_group_(const_iterator i)
const 432 template <
class Data>
448 bool called_method_was_a_dummy_test_;
456 called_method_was_a_dummy_test_ =
true;
476 void ensure(
const T msg,
bool cond)
478 if( !cond )
throw failure(msg);
488 template <
class T,
class Q>
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;
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 )
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_ )
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)
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;
static void reg(Group &)
Definition: tut.h:558
Iterator< typename I::value_type > iterator(I i)
Definition: iterator.h:123
test_result()
Default constructor.
Definition: tut.h:136
Test runner.
Definition: tut.h:228
no_such_test()
Definition: tut.h:31
void run_tests() const
Runs all tests in all groups.
Definition: tut.h:305
test_group(const char *name)
Creates and registers test group with specified name.
Definition: tut.h:686
std::string group
Test group name.
Definition: tut.h:110
Test group; used to recreate test object instance for each new test since we have to have reinitializ...
Definition: tut.h:567
Walks through test tree and stores address of each test method in group.
Definition: tut.h:546
Exception to be throwed when attempted to execute missed test by number.
Definition: tut.h:29
static test_runner & get()
Definition: tut.h:419
void Test
Definition: test.h:178
std::string exception_typeid
Definition: tut.h:131
groups groups_
Definition: tut.h:234
Test object.
Definition: tut.h:433
void run_tests(const std::string &group_name) const
Runs all tests in specified group.
Definition: tut.h:332
Group not found exception.
Definition: tut.h:47
virtual void run_completed()
Called when all tests in run completed.
Definition: tut.h:217
static void reg(Group &group)
Definition: tut.h:548
failure(const std::string &msg)
Definition: tut.h:78
groups::const_iterator const_iterator
Definition: tut.h:233
test_result(const std::string &grp, int pos, result_type res, const std::exception &ex)
Constructor with exception.
Definition: tut.h:152
result_type
ok - test finished successfully fail - test failed with ensure() or fail() methods ex - test throwed ...
Definition: tut.h:124
const groupnames list_groups() const
Returns list of known test groups.
Definition: tut.h:288
callback & get_callback() const
Returns callback object.
Definition: tut.h:280
Exception to be throwed when ensure() fails or fail() called.
Definition: tut.h:75
Exception to be throwed when test desctructor throwed an exception.
Definition: tut.h:84
beyond_last_test()
Definition: tut.h:41
int test
Test number in group.
Definition: tut.h:115
warning(const std::string &msg)
Definition: tut.h:87
test_result run_test(const std::string &group_name, int n) const
Runs one test in specified group.
Definition: tut.h:360
test_result run_test(int n)
Runs one test by position.
Definition: tut.h:756
virtual void test_completed(const test_result &)
Called when a test finished.
Definition: tut.h:206
result_type result
Definition: tut.h:125
#define ensure(x)
Definition: tests.h:101
no_such_group(const std::string &grp)
Definition: tut.h:49
Template Unit Tests Framework for C++.
Definition: tut-main.cpp:6
callback * callback_
Definition: tut.h:237
void rewind()
Reset test position before first test.
Definition: tut.h:721
std::map< std::string, group_base * > groups
Definition: tut.h:231
virtual void group_completed(const std::string &)
Called when a group is completed.
Definition: tut.h:212
Internal exception to be throwed when test constructor has failed.
Definition: tut.h:66
test_runner_singleton runner
Definition: tut-main.cpp:8
void set_callback(callback *cb)
Stores callback object.
Definition: tut.h:272
std::vector< std::string > groupnames
Typedef for runner::list_groups()
Definition: tut.h:223
Singleton for test_runner implementation.
Definition: tut.h:416
test_object< Data > object
Definition: tut.h:681
test_group(const char *name, test_runner &another_runner)
This constructor is used in self-test run only.
Definition: tut.h:699
callback default_callback_
Definition: tut.h:236
Internal exception to be throwed when no more tests left in group or journal.
Definition: tut.h:57
void register_group(const std::string &name, group_base *gr)
Stores another group for getting by name.
Definition: tut.h:250
Interface.
Definition: tut.h:165
void reg(int n, testmethod tm)
Registers test method under given number.
Definition: tut.h:713
virtual ~group_base()
Definition: tut.h:167
void test()
Default do-nothing test.
Definition: tut.h:454
std::string message
Exception message for failed test.
Definition: tut.h:130
Return type of runned test/test group.
Definition: tut.h:105
test_result(const std::string &grp, int pos, result_type res)
Constructor.
Definition: tut.h:144
#define ensure_equals(x, y)
Definition: tests.h:105
virtual void group_started(const std::string &)
Called when a group started.
Definition: tut.h:200
Exception to be throwed when test issued SEH (Win32)
Definition: tut.h:93
no_more_tests()
Definition: tut.h:59
test_runner()
Constructor.
Definition: tut.h:243
test_result run_next()
Runs next test.
Definition: tut.h:729
virtual ~callback()
Virtual destructor is a must for subclassed types.
Definition: tut.h:189
groups::iterator iterator
Definition: tut.h:232
Actual< A > actual(const A &actual)
Definition: tests.h:384
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:39
Test runner callback interface.
Definition: tut.h:184
test_object()
Default constructor.
Definition: tut.h:439
virtual void run_started()
Called when new test run started.
Definition: tut.h:194
seh(const std::string &msg)
Definition: tut.h:96