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: unit_test_suite.ipp,v $ |
---|
9 | // |
---|
10 | // Version : $Revision: 1.13 $ |
---|
11 | // |
---|
12 | // Description : privide core implementation for Unit Test Framework. |
---|
13 | // Extensions could be provided in separate files |
---|
14 | // *************************************************************************** |
---|
15 | |
---|
16 | #ifndef BOOST_TEST_UNIT_TEST_SUITE_IPP_012205GER |
---|
17 | #define BOOST_TEST_UNIT_TEST_SUITE_IPP_012205GER |
---|
18 | |
---|
19 | // Boost.Test |
---|
20 | #include <boost/detail/workaround.hpp> |
---|
21 | #include <boost/test/unit_test_suite_impl.hpp> |
---|
22 | #include <boost/test/framework.hpp> |
---|
23 | #include <boost/test/utils/foreach.hpp> |
---|
24 | #include <boost/test/results_collector.hpp> |
---|
25 | #include <boost/test/detail/unit_test_parameters.hpp> |
---|
26 | |
---|
27 | // Boost |
---|
28 | #include <boost/timer.hpp> |
---|
29 | |
---|
30 | // STL |
---|
31 | #include <algorithm> |
---|
32 | #include <vector> |
---|
33 | |
---|
34 | #include <boost/test/detail/suppress_warnings.hpp> |
---|
35 | |
---|
36 | #if BOOST_WORKAROUND(__BORLANDC__, < 0x600) && \ |
---|
37 | BOOST_WORKAROUND(_STLPORT_VERSION, <= 0x450) \ |
---|
38 | /**/ |
---|
39 | using std::rand; // rand is in std and random_shuffle is in _STL |
---|
40 | #endif |
---|
41 | |
---|
42 | //____________________________________________________________________________// |
---|
43 | |
---|
44 | namespace boost { |
---|
45 | |
---|
46 | namespace unit_test { |
---|
47 | |
---|
48 | // ************************************************************************** // |
---|
49 | // ************** test_unit ************** // |
---|
50 | // ************************************************************************** // |
---|
51 | |
---|
52 | test_unit::test_unit( const_string name, test_unit_type t ) |
---|
53 | : p_type( t ) |
---|
54 | , p_type_name( t == tut_case ? "case" : "suite" ) |
---|
55 | , p_id( INV_TEST_UNIT_ID ) |
---|
56 | , p_name( std::string( name.begin(), name.size() ) ) |
---|
57 | { |
---|
58 | } |
---|
59 | |
---|
60 | //____________________________________________________________________________// |
---|
61 | |
---|
62 | void |
---|
63 | test_unit::depends_on( test_unit* tu ) |
---|
64 | { |
---|
65 | m_dependencies.push_back( tu->p_id ); |
---|
66 | } |
---|
67 | |
---|
68 | //____________________________________________________________________________// |
---|
69 | |
---|
70 | bool |
---|
71 | test_unit::check_dependencies() const |
---|
72 | { |
---|
73 | #if BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x530) ) |
---|
74 | BOOST_TEST_FOREACH( test_unit_id, tu_id, const_cast<test_unit*>(this)->m_dependencies ) { |
---|
75 | #else |
---|
76 | BOOST_TEST_FOREACH( test_unit_id, tu_id, m_dependencies ) { |
---|
77 | #endif |
---|
78 | if( !unit_test::results_collector.results( tu_id ).passed() ) |
---|
79 | return false; |
---|
80 | } |
---|
81 | |
---|
82 | return true; |
---|
83 | } |
---|
84 | |
---|
85 | //____________________________________________________________________________// |
---|
86 | |
---|
87 | // ************************************************************************** // |
---|
88 | // ************** test_case ************** // |
---|
89 | // ************************************************************************** // |
---|
90 | |
---|
91 | test_case::test_case( const_string name, callback0<> const& test_func ) |
---|
92 | : test_unit( name, (test_unit_type)type ) |
---|
93 | , m_test_func( test_func ) |
---|
94 | { |
---|
95 | // !! weirdest MSVC BUG; try to remove this statement; looks like it eats first token of next statement |
---|
96 | #if BOOST_WORKAROUND(BOOST_MSVC,<1300) |
---|
97 | 0; |
---|
98 | #endif |
---|
99 | framework::register_test_unit( this ); |
---|
100 | } |
---|
101 | |
---|
102 | //____________________________________________________________________________// |
---|
103 | |
---|
104 | // ************************************************************************** // |
---|
105 | // ************** test_suite ************** // |
---|
106 | // ************************************************************************** // |
---|
107 | |
---|
108 | //____________________________________________________________________________// |
---|
109 | |
---|
110 | test_suite::test_suite( const_string name ) |
---|
111 | : test_unit( name, (test_unit_type)type ) |
---|
112 | { |
---|
113 | framework::register_test_unit( this ); |
---|
114 | } |
---|
115 | |
---|
116 | //____________________________________________________________________________// |
---|
117 | |
---|
118 | // !! need to prevent modifing test unit once it is added to tree |
---|
119 | |
---|
120 | void |
---|
121 | test_suite::add( test_unit* tu, counter_t expected_failures, unsigned timeout ) |
---|
122 | { |
---|
123 | if( expected_failures != 0 ) |
---|
124 | tu->p_expected_failures.value = expected_failures; |
---|
125 | |
---|
126 | p_expected_failures.value += tu->p_expected_failures; |
---|
127 | |
---|
128 | if( timeout != 0 ) |
---|
129 | tu->p_timeout.value = timeout; |
---|
130 | |
---|
131 | m_members.push_back( tu->p_id ); |
---|
132 | tu->p_parent_id.value = p_id; |
---|
133 | } |
---|
134 | |
---|
135 | //____________________________________________________________________________// |
---|
136 | |
---|
137 | void |
---|
138 | test_suite::add( test_unit_generator const& gen, unsigned timeout ) |
---|
139 | { |
---|
140 | test_unit* tu; |
---|
141 | while((tu = gen.next(), tu)) |
---|
142 | add( tu, 0, timeout ); |
---|
143 | } |
---|
144 | |
---|
145 | //____________________________________________________________________________// |
---|
146 | |
---|
147 | // ************************************************************************** // |
---|
148 | // ************** traverse_test_tree ************** // |
---|
149 | // ************************************************************************** // |
---|
150 | |
---|
151 | void |
---|
152 | traverse_test_tree( test_case const& tc, test_tree_visitor& V ) |
---|
153 | { |
---|
154 | V.visit( tc ); |
---|
155 | } |
---|
156 | |
---|
157 | //____________________________________________________________________________// |
---|
158 | |
---|
159 | void |
---|
160 | traverse_test_tree( test_suite const& suite, test_tree_visitor& V ) |
---|
161 | { |
---|
162 | if( !V.test_suite_start( suite ) ) |
---|
163 | return; |
---|
164 | |
---|
165 | try { |
---|
166 | if( runtime_config::random_seed() == 0 ) { |
---|
167 | BOOST_TEST_FOREACH( test_unit_id, id, suite.m_members ) |
---|
168 | traverse_test_tree( id, V ); |
---|
169 | } |
---|
170 | else { |
---|
171 | std::vector<test_unit_id> members( suite.m_members ); |
---|
172 | std::random_shuffle( members.begin(), members.end() ); |
---|
173 | BOOST_TEST_FOREACH( test_unit_id, id, members ) |
---|
174 | traverse_test_tree( id, V ); |
---|
175 | } |
---|
176 | |
---|
177 | } catch( test_being_aborted const& ) { |
---|
178 | V.test_suite_finish( suite ); |
---|
179 | framework::test_unit_aborted( suite ); |
---|
180 | |
---|
181 | throw; |
---|
182 | } |
---|
183 | |
---|
184 | V.test_suite_finish( suite ); |
---|
185 | } |
---|
186 | |
---|
187 | //____________________________________________________________________________// |
---|
188 | |
---|
189 | void |
---|
190 | traverse_test_tree( test_unit_id id, test_tree_visitor& V ) |
---|
191 | { |
---|
192 | if( test_id_2_unit_type( id ) == tut_case ) |
---|
193 | traverse_test_tree( framework::get<test_case>( id ), V ); |
---|
194 | else |
---|
195 | traverse_test_tree( framework::get<test_suite>( id ), V ); |
---|
196 | } |
---|
197 | |
---|
198 | //____________________________________________________________________________// |
---|
199 | |
---|
200 | // ************************************************************************** // |
---|
201 | // ************** object generators ************** // |
---|
202 | // ************************************************************************** // |
---|
203 | |
---|
204 | namespace ut_detail { |
---|
205 | |
---|
206 | std::string |
---|
207 | normalize_test_case_name( const_string name ) |
---|
208 | { |
---|
209 | return ( name[0] == '&' |
---|
210 | ? std::string( name.begin()+1, name.size()-1 ) |
---|
211 | : std::string( name.begin(), name.size() ) ); |
---|
212 | } |
---|
213 | |
---|
214 | //____________________________________________________________________________// |
---|
215 | |
---|
216 | } // namespace ut_detail |
---|
217 | |
---|
218 | } // namespace unit_test |
---|
219 | |
---|
220 | } // namespace boost |
---|
221 | |
---|
222 | //____________________________________________________________________________// |
---|
223 | |
---|
224 | #include <boost/test/detail/enable_warnings.hpp> |
---|
225 | |
---|
226 | // *************************************************************************** |
---|
227 | // Revision History : |
---|
228 | // |
---|
229 | // $Log: unit_test_suite.ipp,v $ |
---|
230 | // Revision 1.13 2006/02/23 15:33:15 rogeeff |
---|
231 | // workaround restored |
---|
232 | // |
---|
233 | // Revision 1.12 2006/01/28 08:53:57 rogeeff |
---|
234 | // VC6.0 workaround removed |
---|
235 | // |
---|
236 | // Revision 1.11 2005/12/14 05:54:41 rogeeff |
---|
237 | // *** empty log message *** |
---|
238 | // |
---|
239 | // Revision 1.10 2005/04/18 04:55:36 rogeeff |
---|
240 | // test unit name made read/write |
---|
241 | // |
---|
242 | // Revision 1.9 2005/03/23 21:02:25 rogeeff |
---|
243 | // Sunpro CC 5.3 fixes |
---|
244 | // |
---|
245 | // Revision 1.8 2005/03/21 15:33:15 rogeeff |
---|
246 | // check reworked |
---|
247 | // |
---|
248 | // Revision 1.7 2005/02/25 21:27:44 turkanis |
---|
249 | // fix for random_shuffle on Borland 5.x w/ STLPort |
---|
250 | // |
---|
251 | // Revision 1.6 2005/02/21 10:12:24 rogeeff |
---|
252 | // Support for random order of test cases implemented |
---|
253 | // |
---|
254 | // Revision 1.5 2005/02/20 08:27:07 rogeeff |
---|
255 | // This a major update for Boost.Test framework. See release docs for complete list of fixes/updates |
---|
256 | // |
---|
257 | // *************************************************************************** |
---|
258 | |
---|
259 | #endif // BOOST_TEST_UNIT_TEST_SUITE_IPP_012205GER |
---|