]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/test/test/writing-test-ts/assertion-construction-test.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / test / test / writing-test-ts / assertion-construction-test.cpp
1 // (C) Copyright Gennadiy Rozental 2011-2015.
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$
9 //
10 // Version : $Revision: 62023 $
11 //
12 // Description : unit test for new assertion construction based on input expression
13 // ***************************************************************************
14
15 // Boost.Test
16 #define BOOST_TEST_MODULE Boost.Test assertion consruction test
17 #include <boost/test/unit_test.hpp>
18 #include <boost/test/tools/assertion.hpp>
19 #include <boost/test/utils/is_forward_iterable.hpp>
20
21 #include <boost/noncopyable.hpp>
22
23 #include <map>
24 #include <set>
25
26 namespace utf = boost::unit_test;
27
28 //____________________________________________________________________________//
29
30 #define EXPR_TYPE( expr ) ( assertion::seed() ->* expr )
31
32 // some broken compilers do not implement properly decltype on expressions
33 // partial implementation of is_forward_iterable when decltype not available
34 struct not_fwd_iterable_1 {
35 typedef int const_iterator;
36 typedef int value_type;
37
38 bool size();
39 };
40
41 struct not_fwd_iterable_2 {
42 typedef int const_iterator;
43 typedef int value_type;
44
45 bool begin();
46 };
47
48 struct not_fwd_iterable_3 {
49 typedef int value_type;
50 bool begin();
51 bool size();
52 };
53
54 BOOST_AUTO_TEST_CASE( test_forward_iterable_concept )
55 {
56 {
57 typedef std::vector<int> type;
58 BOOST_CHECK_MESSAGE(utf::ut_detail::has_member_size<type>::value, "has_member_size failed");
59 BOOST_CHECK_MESSAGE(utf::ut_detail::has_member_begin<type>::value, "has_member_begin failed");
60 BOOST_CHECK_MESSAGE(utf::is_forward_iterable< type >::value, "is_forward_iterable failed");
61 }
62
63 {
64 // should also work for references, but from is_forward_iterable
65 typedef std::vector<int>& type;
66 BOOST_CHECK_MESSAGE(utf::is_forward_iterable< type >::value, "is_forward_iterable failed");
67 }
68
69
70 {
71 typedef std::list<int> type;
72 BOOST_CHECK_MESSAGE(utf::ut_detail::has_member_size<type>::value, "has_member_size failed");
73 BOOST_CHECK_MESSAGE(utf::ut_detail::has_member_begin<type>::value, "has_member_begin failed");
74 BOOST_CHECK_MESSAGE(utf::is_forward_iterable< type >::value, "is_forward_iterable failed");
75 }
76
77 {
78 typedef std::map<int, int> type;
79 BOOST_CHECK_MESSAGE(utf::ut_detail::has_member_size<type>::value, "has_member_size failed");
80 BOOST_CHECK_MESSAGE(utf::ut_detail::has_member_begin<type>::value, "has_member_begin failed");
81 BOOST_CHECK_MESSAGE(utf::is_forward_iterable< type >::value, "is_forward_iterable failed");
82 }
83
84 {
85 typedef std::set<int, int> type;
86 BOOST_CHECK_MESSAGE(utf::ut_detail::has_member_size<type>::value, "has_member_size failed");
87 BOOST_CHECK_MESSAGE(utf::ut_detail::has_member_begin<type>::value, "has_member_begin failed");
88 BOOST_CHECK_MESSAGE(utf::is_forward_iterable< type >::value, "is_forward_iterable failed");
89 }
90
91
92 {
93 typedef float type;
94 BOOST_CHECK_MESSAGE(!utf::ut_detail::has_member_size<type>::value, "has_member_size failed");
95 BOOST_CHECK_MESSAGE(!utf::ut_detail::has_member_begin<type>::value, "has_member_begin failed");
96 BOOST_CHECK_MESSAGE(!utf::is_forward_iterable< type >::value, "is_forward_iterable failed");
97 }
98
99 {
100 typedef not_fwd_iterable_1 type;
101 BOOST_CHECK_MESSAGE(utf::ut_detail::has_member_size<type>::value, "has_member_size failed");
102 BOOST_CHECK_MESSAGE(!utf::ut_detail::has_member_begin<type>::value, "has_member_begin failed");
103 BOOST_CHECK_MESSAGE(!utf::is_forward_iterable< type >::value, "is_forward_iterable failed");
104 }
105
106 {
107 typedef not_fwd_iterable_2 type;
108 BOOST_CHECK_MESSAGE(!utf::ut_detail::has_member_size<type>::value, "has_member_size failed");
109 BOOST_CHECK_MESSAGE(utf::ut_detail::has_member_begin<type>::value, "has_member_begin failed");
110 BOOST_CHECK_MESSAGE(!utf::is_forward_iterable< type >::value, "is_forward_iterable failed");
111 }
112
113 {
114 typedef not_fwd_iterable_3 type;
115 BOOST_CHECK_MESSAGE(utf::ut_detail::has_member_size<type>::value, "has_member_size failed");
116 BOOST_CHECK_MESSAGE(utf::ut_detail::has_member_begin<type>::value, "has_member_begin failed");
117 BOOST_CHECK_MESSAGE(!utf::is_forward_iterable< type >::value, "is_forward_iterable failed");
118 }
119
120 {
121 typedef char type;
122 BOOST_CHECK_MESSAGE(!utf::ut_detail::has_member_size<type>::value, "has_member_size failed");
123 BOOST_CHECK_MESSAGE(!utf::ut_detail::has_member_begin<type>::value, "has_member_begin failed");
124 BOOST_CHECK_MESSAGE(!utf::is_forward_iterable< type >::value, "is_forward_iterable failed");
125 }
126
127 {
128 // tables are not in the forward_iterable concept
129 typedef int type[10];
130 BOOST_CHECK_MESSAGE(!utf::ut_detail::has_member_size<type>::value, "has_member_size failed");
131 BOOST_CHECK_MESSAGE(!utf::ut_detail::has_member_begin<type>::value, "has_member_begin failed");
132 BOOST_CHECK_MESSAGE(!utf::is_forward_iterable< type >::value, "is_forward_iterable failed");
133 }
134 }
135
136 BOOST_AUTO_TEST_CASE( test_basic_value_expression_construction )
137 {
138 using namespace boost::test_tools;
139
140 {
141 predicate_result const& res = EXPR_TYPE( 1 ).evaluate();
142 BOOST_TEST( res );
143 BOOST_TEST( res.message().is_empty() );
144 }
145
146 {
147 predicate_result const& res = EXPR_TYPE( 0 ).evaluate();
148 BOOST_TEST( !res );
149 BOOST_TEST( res.message() == " [(bool)0 is false]" );
150 }
151
152 {
153 predicate_result const& res = EXPR_TYPE( true ).evaluate();
154 BOOST_TEST( res );
155 BOOST_TEST( res.message().is_empty() );
156 }
157
158 {
159 predicate_result const& res = EXPR_TYPE( 1.5 ).evaluate();
160 BOOST_TEST( res );
161 }
162
163 {
164 predicate_result const& res = EXPR_TYPE( "abc" ).evaluate();
165 BOOST_TEST( res );
166 }
167
168 {
169 predicate_result const& res = EXPR_TYPE( 1>2 ).evaluate();
170 BOOST_TEST( !res );
171 BOOST_TEST( res.message() == " [1 <= 2]" );
172 }
173
174 }
175
176 //____________________________________________________________________________//
177
178 BOOST_AUTO_TEST_CASE( test_comparison_expression )
179 {
180 using namespace boost::test_tools;
181
182 {
183 predicate_result const& res = EXPR_TYPE( 1>2 ).evaluate();
184 BOOST_TEST( !res );
185 BOOST_TEST( res.message() == " [1 <= 2]" );
186 }
187
188 {
189 predicate_result const& res = EXPR_TYPE( 100 < 50 ).evaluate();
190 BOOST_TEST( !res );
191 BOOST_TEST( res.message() == " [100 >= 50]" );
192 }
193
194 {
195 predicate_result const& res = EXPR_TYPE( 5 <= 4 ).evaluate();
196 BOOST_TEST( !res );
197 BOOST_TEST( res.message() == " [5 > 4]" );
198 }
199
200 {
201 predicate_result const& res = EXPR_TYPE( 10>=20 ).evaluate();
202 BOOST_TEST( !res );
203 BOOST_TEST( res.message() == " [10 < 20]" );
204 }
205
206 {
207 int i = 10;
208 predicate_result const& res = EXPR_TYPE( i != 10 ).evaluate();
209 BOOST_TEST( !res );
210 BOOST_TEST( res.message() == " [10 == 10]" );
211 }
212
213 {
214 int i = 5;
215 predicate_result const& res = EXPR_TYPE( i == 3 ).evaluate();
216 BOOST_TEST( !res );
217 BOOST_TEST( res.message() == " [5 != 3]" );
218 }
219 }
220
221 //____________________________________________________________________________//
222
223 BOOST_AUTO_TEST_CASE( test_arithmetic_ops )
224 {
225 using namespace boost::test_tools;
226
227 {
228 int i = 3;
229 int j = 5;
230 predicate_result const& res = EXPR_TYPE( i+j !=8 ).evaluate();
231 BOOST_TEST( !res );
232 BOOST_TEST( res.message() == " [3 + 5 == 8]" );
233 }
234
235 {
236 int i = 3;
237 int j = 5;
238 predicate_result const& res = EXPR_TYPE( 2*i-j > 1 ).evaluate();
239 BOOST_TEST( !res );
240 BOOST_TEST( res.message() == " [2 * 3 - 5 <= 1]" );
241 }
242
243 {
244 int j = 5;
245 predicate_result const& res = EXPR_TYPE( 2<<j < 30 ).evaluate();
246 BOOST_TEST( !res );
247 BOOST_TEST( res.message() == " [2 << 5 >= 30]" );
248 }
249
250 {
251 int i = 2;
252 int j = 5;
253 predicate_result const& res = EXPR_TYPE( i&j ).evaluate();
254 BOOST_TEST( !res );
255 BOOST_TEST( res.message() == " [2 & 5]" );
256 }
257
258 {
259 int i = 3;
260 int j = 5;
261 predicate_result const& res = EXPR_TYPE( i^j^6 ).evaluate();
262 BOOST_TEST( !res );
263 BOOST_TEST( res.message() == " [3 ^ 5 ^ 6]" );
264 }
265
266 // do not support
267 // EXPR_TYPE( 99/2 == 48 || 101/2 > 50 );
268 // EXPR_TYPE( a ? 100 < 50 : 25*2 == 50 );
269 // EXPR_TYPE( true,false );
270 }
271
272 //____________________________________________________________________________//
273
274 struct Testee {
275 static int s_copy_counter;
276
277 Testee() : m_value( false ) {}
278 Testee( Testee const& ) : m_value(false) { s_copy_counter++; }
279 Testee( Testee&& ) : m_value(false) {}
280 Testee( Testee const&& ) : m_value(false) {}
281
282 bool foo() { return m_value; }
283 operator bool() const { return m_value; }
284
285 friend std::ostream& operator<<( std::ostream& ostr, Testee const& ) { return ostr << "Testee"; }
286
287 bool m_value;
288 };
289
290 int Testee::s_copy_counter = 0;
291
292 Testee get_obj() { return Testee(); }
293 Testee const get_const_obj() { return Testee(); }
294
295 class NC : boost::noncopyable {
296 public:
297 NC() {}
298
299 bool operator==(NC const&) const { return false; }
300 friend std::ostream& operator<<(std::ostream& ostr, NC const&)
301 {
302 return ostr << "NC";
303 }
304 };
305
306 BOOST_AUTO_TEST_CASE( test_objects )
307 {
308 using namespace boost::test_tools;
309
310 int expected_copy_count = 0;
311
312 {
313 Testee obj;
314 Testee::s_copy_counter = 0;
315
316 predicate_result const& res = EXPR_TYPE( obj ).evaluate();
317 BOOST_TEST( !res );
318 BOOST_TEST( res.message() == " [(bool)Testee is false]" );
319 BOOST_TEST( Testee::s_copy_counter == expected_copy_count );
320 }
321
322 {
323 Testee const obj;
324 Testee::s_copy_counter = 0;
325
326 predicate_result const& res = EXPR_TYPE( obj ).evaluate();
327 BOOST_TEST( !res );
328 BOOST_TEST( res.message() == " [(bool)Testee is false]" );
329 BOOST_TEST( Testee::s_copy_counter == expected_copy_count );
330 }
331
332 {
333 Testee::s_copy_counter = 0;
334
335 predicate_result const& res = EXPR_TYPE( get_obj() ).evaluate();
336 BOOST_TEST( !res );
337 BOOST_TEST( res.message() == " [(bool)Testee is false]" );
338 BOOST_TEST( Testee::s_copy_counter == expected_copy_count );
339 }
340
341 {
342 Testee::s_copy_counter = 0;
343
344 predicate_result const& res = EXPR_TYPE( get_const_obj() ).evaluate();
345 BOOST_TEST( !res );
346 BOOST_TEST( res.message() == " [(bool)Testee is false]" );
347 BOOST_TEST( Testee::s_copy_counter == expected_copy_count );
348 }
349
350 {
351 Testee::s_copy_counter = 0;
352
353 Testee t1;
354 Testee t2;
355
356 predicate_result const& res = EXPR_TYPE( t1 != t2 ).evaluate();
357 BOOST_TEST( !res );
358 BOOST_TEST( res.message() == " [Testee == Testee]" );
359 BOOST_TEST( Testee::s_copy_counter == 0 );
360 }
361
362 {
363 NC nc1;
364 NC nc2;
365
366 predicate_result const& res = EXPR_TYPE( nc1 == nc2 ).evaluate();
367 BOOST_TEST( !res );
368 BOOST_TEST( res.message() == " [NC != NC]" );
369 }
370 }
371
372 //____________________________________________________________________________//
373
374 BOOST_AUTO_TEST_CASE( test_pointers )
375 {
376 using namespace boost::test_tools;
377
378 {
379 Testee* ptr = 0;
380
381 predicate_result const& res = EXPR_TYPE( ptr ).evaluate();
382 BOOST_TEST( !res );
383 }
384
385 {
386 Testee obj1;
387 Testee obj2;
388
389 predicate_result const& res = EXPR_TYPE( &obj1 == &obj2 ).evaluate();
390 BOOST_TEST( !res );
391 }
392
393 {
394 Testee obj;
395 Testee* ptr =&obj;
396
397 predicate_result const& res = EXPR_TYPE( *ptr ).evaluate();
398 BOOST_TEST( !res );
399 BOOST_TEST( res.message() == " [(bool)Testee is false]" );
400 }
401
402 {
403 Testee obj;
404 Testee* ptr =&obj;
405 bool Testee::* mem_ptr =&Testee::m_value;
406
407 predicate_result const& res = EXPR_TYPE( ptr->*mem_ptr ).evaluate();
408 BOOST_TEST( !res );
409 }
410
411 // do not support
412 // Testee obj;
413 // bool Testee::* mem_ptr =&Testee::m_value;
414 // EXPR_TYPE( obj.*mem_ptr );
415 }
416
417 //____________________________________________________________________________//
418
419 BOOST_AUTO_TEST_CASE( test_mutating_ops )
420 {
421 using namespace boost::test_tools;
422
423 {
424 int j = 5;
425
426 predicate_result const& res = EXPR_TYPE( j = 0 ).evaluate();
427 BOOST_TEST( !res );
428 BOOST_TEST( res.message() == " [(bool)0 is false]" );
429 BOOST_TEST( j == 0 );
430 }
431
432 {
433 int j = 5;
434
435 predicate_result const& res = EXPR_TYPE( j -= 5 ).evaluate();
436 BOOST_TEST( !res );
437 BOOST_TEST( res.message() == " [(bool)0 is false]" );
438 BOOST_TEST( j == 0 );
439 }
440
441 {
442 int j = 5;
443
444 predicate_result const& res = EXPR_TYPE( j *= 0 ).evaluate();
445 BOOST_TEST( !res );
446 BOOST_TEST( res.message() == " [(bool)0 is false]" );
447 BOOST_TEST( j == 0 );
448 }
449
450 {
451 int j = 5;
452
453 predicate_result const& res = EXPR_TYPE( j /= 10 ).evaluate();
454 BOOST_TEST( !res );
455 BOOST_TEST( res.message() == " [(bool)0 is false]" );
456 BOOST_TEST( j == 0 );
457 }
458
459 {
460 int j = 4;
461
462 predicate_result const& res = EXPR_TYPE( j %= 2 ).evaluate();
463 BOOST_TEST( !res );
464 BOOST_TEST( res.message() == " [(bool)0 is false]" );
465 BOOST_TEST( j == 0 );
466 }
467
468 {
469 int j = 5;
470
471 predicate_result const& res = EXPR_TYPE( j ^= j ).evaluate();
472 BOOST_TEST( !res );
473 BOOST_TEST( res.message() == " [(bool)0 is false]" );
474 BOOST_TEST( j == 0 );
475 }
476 }
477
478 // EOF
479