Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/boost/test/impl/framework.ipp @ 12

Last change on this file since 12 was 12, checked in by landauf, 17 years ago

added boost

File size: 11.6 KB
Line 
1//  (C) Copyright Gennadiy Rozental 2005.
2//  Distributed under the Boost Software License, Version 1.0.
3//  (See accompanying file LICENSE_1_0.txt or copy at
4//  http://www.boost.org/LICENSE_1_0.txt)
5
6//  See http://www.boost.org/libs/test for the library home page.
7//
8//  File        : $RCSfile: framework.ipp,v $
9//
10//  Version     : $Revision: 1.6 $
11//
12//  Description : implements framework songleton - main driver for the test
13// ***************************************************************************
14
15#ifndef BOOST_TEST_FRAMEWORK_IPP_021005GER
16#define BOOST_TEST_FRAMEWORK_IPP_021005GER
17
18// Boost.Test
19#include <boost/test/framework.hpp>
20#include <boost/test/unit_test_suite.hpp>
21#include <boost/test/unit_test_log.hpp>
22#include <boost/test/unit_test_monitor.hpp>
23#include <boost/test/test_observer.hpp>
24#include <boost/test/results_collector.hpp>
25#include <boost/test/progress_monitor.hpp>
26#include <boost/test/results_reporter.hpp>
27#include <boost/test/test_tools.hpp>
28
29#include <boost/test/detail/unit_test_parameters.hpp>
30#include <boost/test/detail/global_typedef.hpp>
31
32#include <boost/test/utils/foreach.hpp>
33
34// Boost
35#include <boost/timer.hpp>
36
37// STL
38#include <map>
39#include <stdexcept>
40#include <cstdlib>
41#include <ctime>
42
43#ifdef BOOST_NO_STDC_NAMESPACE
44namespace std { using ::time; using ::srand; }
45#endif
46
47#include <boost/test/detail/suppress_warnings.hpp>
48
49//____________________________________________________________________________//
50
51// prototype for user's test suite init function
52extern boost::unit_test::test_suite* init_unit_test_suite( int argc, char* argv[] );
53
54namespace boost {
55
56namespace unit_test {
57
58// ************************************************************************** //
59// **************                   framework                  ************** //
60// ************************************************************************** //
61
62class framework_impl : public test_tree_visitor {
63public:
64    framework_impl()
65    : m_master_test_suite( INV_TEST_UNIT_ID )
66    , m_curr_test_case( INV_TEST_UNIT_ID )
67    , m_next_test_case_id( MIN_TEST_CASE_ID )
68    , m_next_test_suite_id( MIN_TEST_SUITE_ID )
69    , m_test_in_progress( false )
70    {}
71
72    ~framework_impl()
73    {
74        BOOST_TEST_FOREACH( test_unit_store::value_type const&, tu, m_test_units ) {
75            if( test_id_2_unit_type( tu.second->p_id ) == tut_suite )
76                delete  (test_suite const*)tu.second;
77            else
78                delete  (test_case const*)tu.second;
79        }
80    }
81
82    void            set_tu_id( test_unit& tu, test_unit_id id ) { tu.p_id.value = id; }
83
84    // test_tree_visitor interface implementation
85    void            visit( test_case const& tc )
86    {
87        if( !tc.check_dependencies() ) {
88            BOOST_TEST_FOREACH( test_observer*, to, m_observers )
89                to->test_unit_skipped( tc );
90
91            return;
92        }
93
94        BOOST_TEST_FOREACH( test_observer*, to, m_observers )
95            to->test_unit_start( tc );
96
97        boost::timer tc_timer;
98        test_unit_id bkup = m_curr_test_case;
99        m_curr_test_case = tc.p_id;
100        unit_test_monitor_t::error_level run_result = unit_test_monitor.execute_and_translate( tc );
101
102        unsigned long elapsed = static_cast<unsigned long>( tc_timer.elapsed() * 1e6 );
103
104        if( unit_test_monitor.is_critical_error( run_result ) ) {
105            BOOST_TEST_FOREACH( test_observer*, to, m_observers )
106                to->test_aborted();
107        }
108
109        BOOST_TEST_FOREACH( test_observer*, to, m_observers )
110            to->test_unit_finish( tc, elapsed );
111
112        m_curr_test_case = bkup;
113
114        if( unit_test_monitor.is_critical_error( run_result ) )
115            throw test_aborted();
116    }
117
118    bool            test_suite_start( test_suite const& ts )
119    {
120        if( !ts.check_dependencies() ) {
121            BOOST_TEST_FOREACH( test_observer*, to, m_observers )
122                to->test_unit_skipped( ts );
123
124            return false;
125        }
126
127        BOOST_TEST_FOREACH( test_observer*, to, m_observers )
128            to->test_unit_start( ts );
129
130        return true;
131    }
132
133    void            test_suite_finish( test_suite const& ts )
134    {
135        BOOST_TEST_FOREACH( test_observer*, to, m_observers )
136            to->test_unit_finish( ts, 0 );
137    }
138
139    //////////////////////////////////////////////////////////////////
140
141    typedef std::map<test_unit_id,test_unit const*> test_unit_store;
142    typedef std::list<test_observer*>               observer_store;
143
144    test_unit_id    m_master_test_suite;
145    test_unit_id    m_curr_test_case;
146    test_unit_store m_test_units;
147
148    test_unit_id    m_next_test_case_id;
149    test_unit_id    m_next_test_suite_id;
150
151    bool            m_test_in_progress;
152
153    observer_store  m_observers;
154
155};
156
157//____________________________________________________________________________//
158
159namespace {
160
161framework_impl& s_frk_impl() { static framework_impl the_inst; return the_inst; }
162
163} // local namespace
164
165//____________________________________________________________________________//
166
167namespace framework {
168
169void
170init( int argc, char* argv[] )
171{
172    runtime_config::init( &argc, argv );
173
174    // set the log level nad format
175    unit_test_log.set_threshold_level( runtime_config::log_level() );
176    unit_test_log.set_format( runtime_config::log_format() );
177
178    // set the report level nad format
179    results_reporter::set_level( runtime_config::report_level() );
180    results_reporter::set_format( runtime_config::report_format() );
181
182    register_observer( results_collector );
183    register_observer( unit_test_log );
184
185    if( runtime_config::show_progress() )
186        register_observer( progress_monitor );
187
188    if( runtime_config::detect_memory_leak() > 0 )
189        detect_memory_leak( runtime_config::detect_memory_leak() );
190
191    // init master unit test suite
192    test_suite const* master_suite = init_unit_test_suite( argc, argv );
193    if( !master_suite )
194        throw std::logic_error( "Fail to initialize test suite" );
195
196    s_frk_impl().m_master_test_suite = master_suite->p_id;
197}
198
199//____________________________________________________________________________//
200
201void
202register_test_unit( test_case* tc )
203{
204    if( tc->p_id != INV_TEST_UNIT_ID )
205        throw std::logic_error( "Test case already registered" );
206
207    test_unit_id new_id = s_frk_impl().m_next_test_case_id;
208
209    if( new_id == MAX_TEST_CASE_ID )
210        throw std::logic_error( "Too many test cases" );
211
212    typedef framework_impl::test_unit_store::value_type map_value_type;
213
214    s_frk_impl().m_test_units.insert( map_value_type( new_id, tc ) );
215    s_frk_impl().m_next_test_case_id++;
216
217    s_frk_impl().set_tu_id( *tc, new_id );
218}
219
220//____________________________________________________________________________//
221
222void
223register_test_unit( test_suite* ts )
224{
225    if( ts->p_id != INV_TEST_UNIT_ID )
226        throw std::logic_error( "Test suite already registered" );
227
228    test_unit_id new_id = s_frk_impl().m_next_test_suite_id;
229
230    if( new_id == MAX_TEST_SUITE_ID )
231        throw std::logic_error( "Too many test suites" );
232
233    typedef framework_impl::test_unit_store::value_type map_value_type;
234    s_frk_impl().m_test_units.insert( map_value_type( new_id, ts ) );
235    s_frk_impl().m_next_test_suite_id++;
236
237    s_frk_impl().set_tu_id( *ts, new_id );
238}
239
240//____________________________________________________________________________//
241
242void
243register_observer( test_observer& to )
244{
245    s_frk_impl().m_observers.push_back( &to );
246}
247
248//____________________________________________________________________________//
249
250void
251reset_observers()
252{
253    s_frk_impl().m_observers.clear();
254}
255
256//____________________________________________________________________________//
257
258test_suite const&
259master_test_suite()
260{
261    return get<test_suite>( s_frk_impl().m_master_test_suite );
262}
263
264//____________________________________________________________________________//
265
266test_case const&
267current_test_case()
268{
269    return get<test_case>( s_frk_impl().m_curr_test_case );
270}
271
272//____________________________________________________________________________//
273
274test_unit const&
275get( test_unit_id id, test_unit_type t )
276{
277    test_unit const* res = s_frk_impl().m_test_units[id];
278
279    if( (res->p_type & t) == 0 )
280        throw std::logic_error( "Invalid test unit type" );
281
282    return *res;
283}
284
285//____________________________________________________________________________//
286
287void
288run( test_unit_id id, bool continue_test )
289{
290    if( id == INV_TEST_UNIT_ID )
291        id = s_frk_impl().m_master_test_suite;
292
293    if( id == INV_TEST_UNIT_ID )
294        throw std::logic_error( "Test unit is initialized" );
295
296    test_case_counter tcc;
297    traverse_test_tree( id, tcc );
298
299    bool call_start_finish = !continue_test || !s_frk_impl().m_test_in_progress;
300    bool was_in_progress = s_frk_impl().m_test_in_progress;
301
302    s_frk_impl().m_test_in_progress = true;
303
304    if( call_start_finish ) {
305        BOOST_TEST_FOREACH( test_observer*, to, s_frk_impl().m_observers )
306            to->test_start( tcc.m_count );
307    }
308
309    switch( runtime_config::random_seed() ) {
310    case 0:
311        break;
312    case 1: {
313        unsigned int seed = std::time( 0 );
314        BOOST_MESSAGE( "Test cases order is shuffled using seed: " << seed );
315        std::srand( seed );
316        break;
317    }
318    default:
319        BOOST_MESSAGE( "Test cases order is shuffled using seed: " << runtime_config::random_seed() );
320        std::srand( runtime_config::random_seed() );
321    }
322
323    try {
324        traverse_test_tree( id, s_frk_impl() );
325    }
326    catch( test_aborted const& ) {
327        // abort already reported
328    }
329
330    if( call_start_finish ) {
331        BOOST_TEST_FOREACH( test_observer*, to, s_frk_impl().m_observers )
332            to->test_finish();
333    }
334
335    s_frk_impl().m_test_in_progress = was_in_progress;
336}
337
338//____________________________________________________________________________//
339
340void
341run( test_unit const* tu, bool continue_test )
342{
343    run( tu->p_id, continue_test );
344}
345
346//____________________________________________________________________________//
347
348void
349assertion_result( bool passed )
350{
351    BOOST_TEST_FOREACH( test_observer*, to, s_frk_impl().m_observers )
352        to->assertion_result( passed );
353}
354
355//____________________________________________________________________________//
356
357void
358exception_caught( execution_exception const& ex )
359{
360    BOOST_TEST_FOREACH( test_observer*, to, s_frk_impl().m_observers )
361        to->exception_caught( ex );
362}
363
364//____________________________________________________________________________//
365
366void
367test_unit_aborted()
368{
369    test_unit const& tu = current_test_case();
370
371    BOOST_TEST_FOREACH( test_observer*, to, s_frk_impl().m_observers )
372        to->test_unit_aborted( tu );
373}
374
375//____________________________________________________________________________//
376
377} // namespace framework
378
379} // namespace unit_test
380
381} // namespace boost
382
383//____________________________________________________________________________//
384
385#include <boost/test/detail/enable_warnings.hpp>
386
387// ***************************************************************************
388//  Revision History :
389//
390//  $Log: framework.ipp,v $
391//  Revision 1.6  2005/05/08 08:55:09  rogeeff
392//  typos and missing descriptions fixed
393//
394//  Revision 1.5  2005/04/05 07:23:20  rogeeff
395//  restore default
396//
397//  Revision 1.4  2005/04/05 06:11:37  rogeeff
398//  memory leak allocation point detection\nextra help with _WIN32_WINNT
399//
400//  Revision 1.3  2005/03/23 21:02:19  rogeeff
401//  Sunpro CC 5.3 fixes
402//
403//  Revision 1.2  2005/02/21 10:12:18  rogeeff
404//  Support for random order of test cases implemented
405//
406//  Revision 1.1  2005/02/20 08:27:07  rogeeff
407//  This a major update for Boost.Test framework. See release docs for complete list of fixes/updates
408//
409// ***************************************************************************
410
411#endif // BOOST_TEST_FRAMEWORK_IPP_021005GER
Note: See TracBrowser for help on using the repository browser.