[complement-svn] SF.net SVN: complement: [1601] trunk/complement/explore/app/exam
Status: Pre-Alpha
Brought to you by:
complement
From: <com...@us...> - 2007-07-10 17:12:26
|
Revision: 1601 http://svn.sourceforge.net/complement/?rev=1601&view=rev Author: complement Date: 2007-07-10 10:12:18 -0700 (Tue, 10 Jul 2007) Log Message: ----------- function and class function work Modified Paths: -------------- trunk/complement/explore/app/exam/suite.cc trunk/complement/explore/app/exam/suite.h trunk/complement/explore/app/exam/zero.cc Modified: trunk/complement/explore/app/exam/suite.cc =================================================================== --- trunk/complement/explore/app/exam/suite.cc 2007-07-06 06:29:26 UTC (rev 1600) +++ trunk/complement/explore/app/exam/suite.cc 2007-07-10 17:12:18 UTC (rev 1601) @@ -1,9 +1,12 @@ -// -*- C++ -*- Time-stamp: <07/07/06 09:55:44 ptr> +// -*- C++ -*- Time-stamp: <07/07/08 23:39:10 ptr> #include "suite.h" #include <boost/graph/breadth_first_search.hpp> #include <stack> +#include <cstdio> +#include <iostream> + namespace exam { using namespace std; @@ -39,8 +42,15 @@ _test[root] = 0; } -void test_suite::girdle( int start ) +test_suite::~test_suite() { + for ( test_case_map_type::iterator i = _test.begin(); i != _test.end(); ++i ) { + delete i->second; + } +} + +void test_suite::girdle() +{ stack<vertex_t> buffer; list<vertex_t> v; breadth_first_visit( g, root, buffer, @@ -58,6 +68,26 @@ } } +void test_suite::girdle( test_suite::test_case_type start ) +{ + stack<vertex_t> buffer; + list<vertex_t> v; + breadth_first_visit( g, start, buffer, + make_bfs_visitor(record_vertexes(v,on_discover_vertex())), + color ); + + v.pop_front(); // remove root, it empty + + for ( list<vertex_t>::const_iterator i = v.begin(); i != v.end(); ++i ) { + try { + (*_test[*i])(); + } + catch ( ... ) { + } + } +} + + test_suite::test_case_type test_suite::add( test_suite::func_type f ) { vertex_t v = add_vertex( white_color, g); @@ -76,4 +106,38 @@ return v; } +int test_suite::flags() +{ + return _flags; +} + +bool test_suite::is_trace() +{ + return (_flags & trace) != 0; +} + +void _report0( const char *file, int line, bool cnd, const char *expr ) +{ + std::cerr << file << ":" << line << ": " << (cnd ? "pass" : "fail" ) << ": " << expr + << std::endl; +} + +void _report1( const char *file, int line, bool cnd, const char *expr ) +{ + printf( "%s:%d: %s: %s\n", file, line, (cnd ? "pass" : "fail"), expr ); +} + +void _report2( const char *file, int line, bool cnd, const char *expr ) +{ + fprintf( stderr, "%s:%d: %s: %s\n", file, line, (cnd ? "pass" : "fail"), expr ); +} + +int test_suite::_flags = 0; +void (*test_suite::_report)( const char *, int, bool, const char * ) = _report0; + +void test_suite::report( const char *file, int line, bool cnd, const char *expr ) +{ + (*test_suite::_report)( file, line, cnd, expr ); +} + } // namespace exam Modified: trunk/complement/explore/app/exam/suite.h =================================================================== --- trunk/complement/explore/app/exam/suite.h 2007-07-06 06:29:26 UTC (rev 1600) +++ trunk/complement/explore/app/exam/suite.h 2007-07-10 17:12:18 UTC (rev 1601) @@ -1,4 +1,4 @@ -// -*- C++ -*- Time-stamp: <07/07/06 09:58:06 ptr> +// -*- C++ -*- Time-stamp: <07/07/08 23:40:05 ptr> #ifndef __suite_h #define __suite_h @@ -18,15 +18,6 @@ namespace detail { -/* -struct invoker -{ - template <typename F> - void invoke( F& f ) - { f(); } -}; -*/ - struct call_impl { virtual ~call_impl() @@ -44,28 +35,20 @@ { } virtual void invoke() - { /* invoker().invoke( _f ); */ _f(); } + { _f(); } private: F _f; }; -class call +class dummy { public: - call() + virtual void f() { } - - template <class F> - call( F f ) : - _f( new call_impl_t<F>(f) ) + private: + virtual ~dummy() { } - - void operator()() - { _f->invoke(); } - - private: - call_impl *_f; }; template <class TC> @@ -73,19 +56,47 @@ { public: typedef void (TC::*mf_type)(); - method_invoker( TC& instance, mf_type f ) : + + explicit method_invoker( TC& instance, mf_type f ) : _inst(instance), _func(f) { } + method_invoker( const method_invoker<TC>& m ) : + _inst( m._inst ), + _func( m._func ) + { } + void operator()() - { _inst.*_func(); } + { (_inst.*_func)(); } private: + method_invoker& operator =( const method_invoker<TC>& ) + { return *this; } + TC& _inst; mf_type _func; }; +class call +{ + public: + call() + { } + + template <class F> + call( F f ) + { new (&_buf[0]) call_impl_t<F>(f); } + + void operator()() + { reinterpret_cast<call_impl *>(&_buf[0])->invoke(); } + + private: + // call_impl *_f; + char _buf[((sizeof(call_impl_t<method_invoker<dummy> >)+64) / 64) << 6]; +}; + + class test_case { public: @@ -131,24 +142,73 @@ typedef vertex_t test_case_type; test_suite(); + ~test_suite(); test_case_type add( func_type ); test_case_type add( func_type, test_case_type ); - void girdle( int start ); + template <class TC> + test_case_type add( void (TC::*)(), TC& ); + template <class TC> + test_case_type add( void (TC::*)(), TC&, test_case_type ); + + void girdle(); + void girdle( test_case_type start ); + + enum { + trace = 1 + }; + + static int flags(); + static bool is_trace(); + static void report( const char *, int, bool, const char * ); + private: graph_t g; vertex_t root; vertex_color_map_t color; vertex_testcase_map_t testcase; - std::map<vertex_t,detail::test_case *> _test; + typedef std::map<vertex_t,detail::test_case *> test_case_map_type; + test_case_map_type _test; + + static int _flags; + static void (*_report)( const char *, int, bool, const char * ); }; +template <class TC> +test_suite::test_case_type test_suite::add( void (TC::*f)(), TC& instance ) +{ + vertex_t v = boost::add_vertex( boost::white_color, g); + boost::add_edge( root, v, g ); + _test[v] = detail::make_test_case( f, instance ); + + return v; +} + +template <class TC> +test_suite::test_case_type test_suite::add( void (TC::*f)(), TC& instance, test_suite::test_case_type depends ) +{ + vertex_t v = boost::add_vertex( boost::white_color, g); + boost::add_edge( depends, v, g ); + _test[v] = detail::make_test_case( f, instance ); + + return v; +} + typedef test_suite::test_case_type test_case_type; } // namespace exam +#ifdef FIT_EXAM +# define EXAM_CHECK(C) if ( !(C) ) { exam::test_suite::report( __FILE__, __LINE__, false, #C ); } else if ( exam::test_suite::is_trace() ) { exam::test_suite::report( __FILE__, __LINE__, true, #C ); } +# define EXAM_MESSAGE(M) +#else +# define EXAM_CHECK(C) +# define EXAM_MESSAGE(M) +#endif + + #endif // __suite_h Modified: trunk/complement/explore/app/exam/zero.cc =================================================================== --- trunk/complement/explore/app/exam/zero.cc 2007-07-06 06:29:26 UTC (rev 1600) +++ trunk/complement/explore/app/exam/zero.cc 2007-07-10 17:12:18 UTC (rev 1601) @@ -1,66 +1,31 @@ #define FIT_EXAM -#ifdef FIT_EXAM -# define EXAM_CHECK(C) if ( !(C) ) { exam::report( __FILE__, __LINE__, false, #C ); } else if ( exam::base::is_trace() ) { exam::report( __FILE__, __LINE__, true, #C ); } -# define EXAM_MESSAGE(M) -#else -# define EXAM_CHECK(C) -# define EXAM_MESSAGE(M) -#endif - - -#include <cstdio> -#include <iostream> - #include "suite.h" -namespace exam { +void func() +{ + EXAM_CHECK(false); +} -class base +class test_x { public: - enum { - trace = 1 - }; - - static int flags(); - static bool is_trace(); - private: - - static int _flags; + void f() + { + EXAM_CHECK(false); + } }; -void _report0( const char *file, int line, bool cnd, const char *expr ) -{ - std::cerr << file << ":" << line << ": " << (cnd ? "pass" : "fail" ) << " \"" << expr << "\"" - << std::endl; -} -void _report1( const char *file, int line, bool cnd, const char *expr ) -{ - printf( "%s:%d: %s \"%s\"\n", file, line, (cnd ? "pass" : "fail"), expr ); -} - -void _report2( const char *file, int line, bool cnd, const char *expr ) -{ - fprintf( stderr, "%s:%d: %s \"%s\"\n", file, line, (cnd ? "pass" : "fail"), expr ); -} - -void (*report)( const char *, int, bool, const char * ) = _report0; - -} // namespec exam - -void func() -{ - EXAM_CHECK(false); -} - int main( int argc, char **argv ) { exam::test_suite t; + test_x tx; + t.add( func ); + t.add( &test_x::f, tx ); t.girdle( 0 ); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |