Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/boost/test/impl/unit_test_parameters.ipp @ 44

Last change on this file since 44 was 29, checked in by landauf, 16 years ago

updated boost from 1_33_1 to 1_34_1

File size: 11.1 KB
Line 
1//  (C) Copyright Gennadiy Rozental 2001-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: unit_test_parameters.ipp,v $
9//
10//  Version     : $Revision: 1.10 $
11//
12//  Description : simple implementation for Unit Test Framework parameter
13//  handling routines. May be rewritten in future to use some kind of
14//  command-line arguments parsing facility and environment variable handling
15//  facility
16// ***************************************************************************
17
18#ifndef BOOST_TEST_UNIT_TEST_PARAMETERS_IPP_012205GER
19#define BOOST_TEST_UNIT_TEST_PARAMETERS_IPP_012205GER
20
21// Boost.Test
22#include <boost/test/detail/unit_test_parameters.hpp>
23#include <boost/test/utils/basic_cstring/basic_cstring.hpp>
24#include <boost/test/utils/basic_cstring/compare.hpp>
25#include <boost/test/utils/basic_cstring/io.hpp>
26#include <boost/test/utils/fixed_mapping.hpp>
27
28// Boost
29#include <boost/config.hpp>
30#include <boost/test/detail/suppress_warnings.hpp>
31#include <boost/lexical_cast.hpp>
32#include <boost/test/detail/enable_warnings.hpp>
33
34// STL
35#include <map>
36#include <cstdlib>
37
38#include <boost/test/detail/suppress_warnings.hpp>
39
40//____________________________________________________________________________//
41
42# ifdef BOOST_NO_STDC_NAMESPACE
43namespace std { using ::getenv; using ::strncmp; using ::strcmp; }
44# endif
45
46namespace boost {
47
48namespace unit_test {
49
50namespace {
51
52// framework parameters and there corresponding command-line arguments
53literal_string LOG_LEVEL         = "BOOST_TEST_LOG_LEVEL";
54literal_string NO_RESULT_CODE    = "BOOST_TEST_RESULT_CODE";
55literal_string REPORT_LEVEL      = "BOOST_TEST_REPORT_LEVEL";
56literal_string TESTS_TO_RUN      = "BOOST_TESTS_TO_RUN";
57literal_string SAVE_TEST_PATTERN = "BOOST_TEST_SAVE_PATTERN";
58literal_string BUILD_INFO        = "BOOST_TEST_BUILD_INFO";
59literal_string SHOW_PROGRESS     = "BOOST_TEST_SHOW_PROGRESS";
60literal_string CATCH_SYS_ERRORS  = "BOOST_TEST_CATCH_SYSTEM_ERRORS";
61literal_string REPORT_FORMAT     = "BOOST_TEST_REPORT_FORMAT";
62literal_string LOG_FORMAT        = "BOOST_TEST_LOG_FORMAT";
63literal_string OUTPUT_FORMAT     = "BOOST_TEST_OUTPUT_FORMAT";
64literal_string DETECT_MEM_LEAK   = "BOOST_TEST_DETECT_MEMORY_LEAK";
65literal_string RANDOM_SEED       = "BOOST_TEST_RANDOM";
66literal_string BREAK_EXEC_PATH   = "BOOST_TEST_BREAK_EXEC_PATH";
67
68unit_test::log_level     s_log_level;
69bool                     s_no_result_code;
70unit_test::report_level  s_report_level;
71const_string             s_tests_to_run;
72const_string             s_exec_path_to_break;
73bool                     s_save_pattern;
74bool                     s_show_build_info;
75bool                     s_show_progress;
76bool                     s_catch_sys_errors;
77output_format            s_report_format;
78output_format            s_log_format;
79long                     s_detect_mem_leaks;
80unsigned int             s_random_seed;
81
82// ************************************************************************** //
83// **************                 runtime_config               ************** //
84// ************************************************************************** //
85
86const_string
87retrieve_framework_parameter( const_string parameter_name, int* argc, char** argv )
88{
89    static fixed_mapping<const_string,const_string> parameter_2_cla_name_map(
90        LOG_LEVEL         , "--log_level",
91        NO_RESULT_CODE    , "--result_code",
92        REPORT_LEVEL      , "--report_level",
93        TESTS_TO_RUN      , "--run_test",
94        SAVE_TEST_PATTERN , "--save_pattern",
95        BUILD_INFO        , "--build_info",
96        SHOW_PROGRESS     , "--show_progress",
97        CATCH_SYS_ERRORS  , "--catch_system_errors",
98        REPORT_FORMAT     , "--report_format",
99        LOG_FORMAT        , "--log_format",
100        OUTPUT_FORMAT     , "--output_format",
101        DETECT_MEM_LEAK   , "--detect_memory_leaks",
102        RANDOM_SEED       , "--random",
103        BREAK_EXEC_PATH   , "--break_exec_path",
104       
105        ""
106    );
107
108    // first try to find parameter among command line arguments if present
109    if( argc ) {
110        // locate corresponding cla name
111        const_string cla_name = parameter_2_cla_name_map[parameter_name];
112
113        if( !cla_name.is_empty() ) {
114            for( int i = 1; i < *argc; ++i ) {
115                if( cla_name == const_string( argv[i], cla_name.size() ) && argv[i][cla_name.size()] == '=' ) {
116                    const_string result = argv[i] + cla_name.size() + 1;
117
118                    for( int j = i; j < *argc; ++j ) {
119                        argv[j] = argv[j+1];
120                    }
121                    --(*argc);
122
123                    return result;
124                }
125            }
126        }
127    }
128
129    return std::getenv( parameter_name.begin() );
130}
131
132long interpret_long( const_string from )
133{
134    bool negative = false;
135    long res = 0;
136
137    if( first_char( from ) == '-' ) {
138        negative = true;
139        from.trim_left( 1 );
140    }
141
142    const_string::iterator it = from.begin();
143    for( ;it != from.end(); ++it ) {
144        int d = *it - '0';
145
146        res = 10 * res + d;
147    }
148
149    if( negative )
150        res = -res;
151
152    return res;
153}
154
155} // local namespace
156
157//____________________________________________________________________________//
158
159namespace runtime_config {
160
161void
162init( int* argc, char** argv )
163{
164    fixed_mapping<const_string,unit_test::log_level,case_ins_less<char const> > log_level_name(
165        "all"           , log_successful_tests,
166        "success"       , log_successful_tests,
167        "test_suite"    , log_test_suites,
168        "message"       , log_messages,
169        "warning"       , log_warnings,
170        "error"         , log_all_errors,
171        "cpp_exception" , log_cpp_exception_errors,
172        "system_error"  , log_system_errors,
173        "fatal_error"   , log_fatal_errors,
174        "nothing"       , log_nothing,
175
176        invalid_log_level
177    );
178
179    fixed_mapping<const_string,unit_test::report_level,case_ins_less<char const> > report_level_name (
180        "confirm",  CONFIRMATION_REPORT,
181        "short",    SHORT_REPORT,
182        "detailed", DETAILED_REPORT,
183        "no",       NO_REPORT,
184
185        INV_REPORT_LEVEL
186    );
187
188    fixed_mapping<const_string,output_format,case_ins_less<char const> > output_format_name (
189        "HRF", CLF,
190        "CLF", CLF,
191        "XML", XML,
192
193        CLF
194    );
195
196    s_no_result_code    = retrieve_framework_parameter( NO_RESULT_CODE, argc, argv ) == "no";
197    s_save_pattern      = retrieve_framework_parameter( SAVE_TEST_PATTERN, argc, argv ) == "yes";
198    s_show_build_info   = retrieve_framework_parameter( BUILD_INFO, argc, argv ) == "yes";
199    s_show_progress     = retrieve_framework_parameter( SHOW_PROGRESS, argc, argv ) == "yes";
200    s_catch_sys_errors  = retrieve_framework_parameter( CATCH_SYS_ERRORS, argc, argv ) != "no";
201    s_tests_to_run      = retrieve_framework_parameter( TESTS_TO_RUN, argc, argv );
202    s_exec_path_to_break= retrieve_framework_parameter( BREAK_EXEC_PATH, argc, argv );
203
204    const_string rs_str = retrieve_framework_parameter( RANDOM_SEED, argc, argv );
205    s_random_seed       = rs_str.is_empty() ? 0 : lexical_cast<unsigned int>( rs_str );
206   
207    s_log_level         = log_level_name[retrieve_framework_parameter( LOG_LEVEL, argc, argv )];
208    s_report_level      = report_level_name[retrieve_framework_parameter( REPORT_LEVEL, argc, argv )];
209
210    s_report_format     = output_format_name[retrieve_framework_parameter( REPORT_FORMAT, argc, argv )];
211    s_log_format        = output_format_name[retrieve_framework_parameter( LOG_FORMAT, argc, argv )];
212
213    const_string output_format = retrieve_framework_parameter( OUTPUT_FORMAT, argc, argv );
214    if( !output_format.is_empty() ) {
215        s_report_format     = output_format_name[output_format];
216        s_log_format        = output_format_name[output_format];
217    }
218
219    const_string ml_str = retrieve_framework_parameter( DETECT_MEM_LEAK, argc, argv );
220    s_detect_mem_leaks  =  ml_str.is_empty() ? 1 : interpret_long( ml_str );
221}
222
223//____________________________________________________________________________//
224
225unit_test::log_level
226log_level()
227{
228    return s_log_level;
229}
230
231//____________________________________________________________________________//
232
233bool
234no_result_code()
235{
236    return s_no_result_code;
237}
238
239//____________________________________________________________________________//
240
241unit_test::report_level
242report_level()
243{
244    return s_report_level;
245}
246
247//____________________________________________________________________________//
248
249const_string
250test_to_run()
251{
252    return s_tests_to_run;
253}
254
255//____________________________________________________________________________//
256
257const_string
258break_exec_path()
259{
260    return s_exec_path_to_break;
261}
262
263//____________________________________________________________________________//
264
265bool
266save_pattern()
267{
268    return s_save_pattern;
269}
270
271//____________________________________________________________________________//
272
273bool
274show_progress()
275{
276    return s_show_progress;
277}
278
279//____________________________________________________________________________//
280
281bool
282show_build_info()
283{
284    return s_show_build_info;
285}
286
287//____________________________________________________________________________//
288
289bool
290catch_sys_errors()
291{
292    return s_catch_sys_errors;
293}
294
295//____________________________________________________________________________//
296
297output_format
298report_format()
299{
300    return s_report_format;
301}
302
303//____________________________________________________________________________//
304
305output_format
306log_format()
307{
308    return s_log_format;
309}
310
311//____________________________________________________________________________//
312
313long
314detect_memory_leaks()
315{
316    return s_detect_mem_leaks;
317}
318
319//____________________________________________________________________________//
320
321int
322random_seed()
323{
324    return s_random_seed;
325}
326
327//____________________________________________________________________________//
328
329} // namespace runtime_config
330
331} // namespace unit_test
332
333} // namespace boost
334
335//____________________________________________________________________________//
336
337#include <boost/test/detail/enable_warnings.hpp>
338
339// ***************************************************************************
340//  Revision History :
341//
342//  $Log: unit_test_parameters.ipp,v $
343//  Revision 1.10  2006/01/30 07:29:49  rogeeff
344//  split memory leaks detection API in two to get more functions with better defined roles
345//
346//  Revision 1.9  2005/12/14 05:38:47  rogeeff
347//  new parameter break_exec_path() is introduced
348//
349//  Revision 1.8  2005/05/08 08:55:09  rogeeff
350//  typos and missing descriptions fixed
351//
352//  Revision 1.7  2005/04/05 07:23:21  rogeeff
353//  restore default
354//
355//  Revision 1.6  2005/04/05 06:11:37  rogeeff
356//  memory leak allocation point detection\nextra help with _WIN32_WINNT
357//
358//  Revision 1.5  2005/02/21 10:12:22  rogeeff
359//  Support for random order of test cases implemented
360//
361//  Revision 1.4  2005/02/20 08:27:07  rogeeff
362//  This a major update for Boost.Test framework. See release docs for complete list of fixes/updates
363//
364// ***************************************************************************
365
366#endif // BOOST_TEST_UNIT_TEST_PARAMETERS_IPP_012205GER
Note: See TracBrowser for help on using the repository browser.