1 | // Copyright (C) 2003, Fernando Luis Cacciola Carballal. |
---|
2 | // |
---|
3 | // Use, modification, and distribution is subject to the Boost Software |
---|
4 | // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
---|
5 | // http://www.boost.org/LICENSE_1_0.txt) |
---|
6 | // |
---|
7 | // See http://www.boost.org/lib/optional for documentation. |
---|
8 | // |
---|
9 | // You are welcome to contact the author at: |
---|
10 | // fernando_cacciola@hotmail.com |
---|
11 | // |
---|
12 | #include<iostream> |
---|
13 | #include<stdexcept> |
---|
14 | #include<string> |
---|
15 | |
---|
16 | #define BOOST_ENABLE_ASSERT_HANDLER |
---|
17 | |
---|
18 | #include "boost/optional.hpp" |
---|
19 | |
---|
20 | #ifdef __BORLANDC__ |
---|
21 | #pragma hdrstop |
---|
22 | #endif |
---|
23 | |
---|
24 | #include "boost/none.hpp" |
---|
25 | |
---|
26 | #include "boost/test/minimal.hpp" |
---|
27 | |
---|
28 | #include "optional_test_common.cpp" |
---|
29 | |
---|
30 | void test_implicit_construction ( optional<double> opt, double v, double z ) |
---|
31 | { |
---|
32 | check_value(opt,v,z); |
---|
33 | } |
---|
34 | |
---|
35 | void test_implicit_construction ( optional<X> opt, X const& v, X const& z ) |
---|
36 | { |
---|
37 | check_value(opt,v,z); |
---|
38 | } |
---|
39 | |
---|
40 | void test_default_implicit_construction ( double, optional<double> opt ) |
---|
41 | { |
---|
42 | BOOST_CHECK(!opt); |
---|
43 | } |
---|
44 | |
---|
45 | void test_default_implicit_construction ( X const&, optional<X> opt ) |
---|
46 | { |
---|
47 | BOOST_CHECK(!opt); |
---|
48 | } |
---|
49 | |
---|
50 | // |
---|
51 | // Basic test. |
---|
52 | // Check ordinary functionality: |
---|
53 | // Initialization, assignment, comparison and value-accessing. |
---|
54 | // |
---|
55 | template<class T> |
---|
56 | void test_basics( T const* ) |
---|
57 | { |
---|
58 | TRACE( std::endl << BOOST_CURRENT_FUNCTION ); |
---|
59 | |
---|
60 | T z(0); |
---|
61 | |
---|
62 | T a(1); |
---|
63 | |
---|
64 | // Default construction. |
---|
65 | // 'def' state is Uninitialized. |
---|
66 | // T::T() is not called (and it is not even defined) |
---|
67 | optional<T> def ; |
---|
68 | check_uninitialized(def); |
---|
69 | |
---|
70 | // Implicit construction |
---|
71 | // The first parameter is implicitely converted to optional<T>(a); |
---|
72 | test_implicit_construction(a,a,z); |
---|
73 | |
---|
74 | // Direct initialization. |
---|
75 | // 'oa' state is Initialized with 'a' |
---|
76 | // T::T( T const& x ) is used. |
---|
77 | set_pending_copy( ARG(T) ) ; |
---|
78 | optional<T> oa ( a ) ; |
---|
79 | check_is_not_pending_copy( ARG(T) ); |
---|
80 | check_initialized(oa); |
---|
81 | check_value(oa,a,z); |
---|
82 | |
---|
83 | T b(2); |
---|
84 | |
---|
85 | optional<T> ob ; |
---|
86 | |
---|
87 | // Value-Assignment upon Uninitialized optional. |
---|
88 | // T::T( T const& x ) is used. |
---|
89 | set_pending_copy( ARG(T) ) ; |
---|
90 | ob = a ; |
---|
91 | check_is_not_pending_copy( ARG(T) ) ; |
---|
92 | check_initialized(ob); |
---|
93 | check_value(ob,a,z); |
---|
94 | |
---|
95 | // Value-Assignment upon Initialized optional. |
---|
96 | // T::operator=( T const& x ) is used |
---|
97 | set_pending_assign( ARG(T) ) ; |
---|
98 | set_pending_copy ( ARG(T) ) ; |
---|
99 | set_pending_dtor ( ARG(T) ) ; |
---|
100 | ob = b ; |
---|
101 | check_is_not_pending_assign( ARG(T) ) ; |
---|
102 | check_is_pending_copy ( ARG(T) ) ; |
---|
103 | check_is_pending_dtor ( ARG(T) ) ; |
---|
104 | check_initialized(ob); |
---|
105 | check_value(ob,b,z); |
---|
106 | |
---|
107 | // Assignment initialization. |
---|
108 | // T::T ( T const& x ) is used to copy new value. |
---|
109 | set_pending_copy( ARG(T) ) ; |
---|
110 | optional<T> const oa2 ( oa ) ; |
---|
111 | check_is_not_pending_copy( ARG(T) ) ; |
---|
112 | check_initialized_const(oa2); |
---|
113 | check_value_const(oa2,a,z); |
---|
114 | |
---|
115 | // Assignment |
---|
116 | // T::operator= ( T const& x ) is used to copy new value. |
---|
117 | set_pending_assign( ARG(T) ) ; |
---|
118 | set_pending_copy ( ARG(T) ) ; |
---|
119 | set_pending_dtor ( ARG(T) ) ; |
---|
120 | oa = ob ; |
---|
121 | check_is_not_pending_assign( ARG(T) ) ; |
---|
122 | check_is_pending_copy ( ARG(T) ) ; |
---|
123 | check_is_pending_dtor ( ARG(T) ) ; |
---|
124 | check_initialized(oa); |
---|
125 | check_value(oa,b,z); |
---|
126 | |
---|
127 | // Uninitializing Assignment upon Initialized Optional |
---|
128 | // T::~T() is used to destroy previous value in oa. |
---|
129 | set_pending_dtor( ARG(T) ) ; |
---|
130 | set_pending_copy( ARG(T) ) ; |
---|
131 | oa = def ; |
---|
132 | check_is_not_pending_dtor( ARG(T) ) ; |
---|
133 | check_is_pending_copy ( ARG(T) ) ; |
---|
134 | check_uninitialized(oa); |
---|
135 | |
---|
136 | // Uninitializing Assignment upon Uninitialized Optional |
---|
137 | // (Dtor is not called this time) |
---|
138 | set_pending_dtor( ARG(T) ) ; |
---|
139 | set_pending_copy( ARG(T) ) ; |
---|
140 | oa = def ; |
---|
141 | check_is_pending_dtor( ARG(T) ) ; |
---|
142 | check_is_pending_copy( ARG(T) ) ; |
---|
143 | check_uninitialized(oa); |
---|
144 | |
---|
145 | // Deinitialization of Initialized Optional |
---|
146 | // T::~T() is used to destroy previous value in ob. |
---|
147 | set_pending_dtor( ARG(T) ) ; |
---|
148 | ob.reset(); |
---|
149 | check_is_not_pending_dtor( ARG(T) ) ; |
---|
150 | check_uninitialized(ob); |
---|
151 | |
---|
152 | // Deinitialization of Uninitialized Optional |
---|
153 | // (Dtor is not called this time) |
---|
154 | set_pending_dtor( ARG(T) ) ; |
---|
155 | ob.reset(); |
---|
156 | check_is_pending_dtor( ARG(T) ) ; |
---|
157 | check_uninitialized(ob); |
---|
158 | } |
---|
159 | |
---|
160 | // |
---|
161 | // Test Direct Value Manipulation |
---|
162 | // |
---|
163 | template<class T> |
---|
164 | void test_direct_value_manip( T const* ) |
---|
165 | { |
---|
166 | TRACE( std::endl << BOOST_CURRENT_FUNCTION ); |
---|
167 | |
---|
168 | T x(3); |
---|
169 | |
---|
170 | optional<T> const c_opt0(x) ; |
---|
171 | optional<T> opt0(x); |
---|
172 | |
---|
173 | BOOST_CHECK( c_opt0.get().V() == x.V() ) ; |
---|
174 | BOOST_CHECK( opt0.get().V() == x.V() ) ; |
---|
175 | |
---|
176 | BOOST_CHECK( c_opt0->V() == x.V() ) ; |
---|
177 | BOOST_CHECK( opt0->V() == x.V() ) ; |
---|
178 | |
---|
179 | BOOST_CHECK( (*c_opt0).V() == x.V() ) ; |
---|
180 | BOOST_CHECK( (* opt0).V() == x.V() ) ; |
---|
181 | |
---|
182 | T y(4); |
---|
183 | opt0 = y ; |
---|
184 | BOOST_CHECK( get(opt0).V() == y.V() ) ; |
---|
185 | } |
---|
186 | |
---|
187 | // |
---|
188 | // Test Uninitialized access assert |
---|
189 | // |
---|
190 | template<class T> |
---|
191 | void test_uninitialized_access( T const* ) |
---|
192 | { |
---|
193 | TRACE( std::endl << BOOST_CURRENT_FUNCTION ); |
---|
194 | |
---|
195 | optional<T> def ; |
---|
196 | |
---|
197 | bool passed = false ; |
---|
198 | try |
---|
199 | { |
---|
200 | // This should throw because 'def' is uninitialized |
---|
201 | T const& n = def.get() ; |
---|
202 | unused_variable(n); |
---|
203 | passed = true ; |
---|
204 | } |
---|
205 | catch (...) {} |
---|
206 | BOOST_CHECK(!passed); |
---|
207 | |
---|
208 | passed = false ; |
---|
209 | try |
---|
210 | { |
---|
211 | // This should throw because 'def' is uninitialized |
---|
212 | T const& n = *def ; |
---|
213 | unused_variable(n); |
---|
214 | passed = true ; |
---|
215 | } |
---|
216 | catch (...) {} |
---|
217 | BOOST_CHECK(!passed); |
---|
218 | |
---|
219 | passed = false ; |
---|
220 | try |
---|
221 | { |
---|
222 | T v(5) ; |
---|
223 | unused_variable(v); |
---|
224 | // This should throw because 'def' is uninitialized |
---|
225 | *def = v ; |
---|
226 | passed = true ; |
---|
227 | } |
---|
228 | catch (...) {} |
---|
229 | BOOST_CHECK(!passed); |
---|
230 | |
---|
231 | passed = false ; |
---|
232 | try |
---|
233 | { |
---|
234 | // This should throw because 'def' is uninitialized |
---|
235 | T v = *(def.operator->()) ; |
---|
236 | unused_variable(v); |
---|
237 | passed = true ; |
---|
238 | } |
---|
239 | catch (...) {} |
---|
240 | BOOST_CHECK(!passed); |
---|
241 | } |
---|
242 | |
---|
243 | #if BOOST_WORKAROUND( BOOST_INTEL_CXX_VERSION, <= 700) // Intel C++ 7.0 |
---|
244 | void prevent_buggy_optimization( bool v ) {} |
---|
245 | #endif |
---|
246 | |
---|
247 | // |
---|
248 | // Test Direct Initialization of optional for a T with throwing copy-ctor. |
---|
249 | // |
---|
250 | template<class T> |
---|
251 | void test_throwing_direct_init( T const* ) |
---|
252 | { |
---|
253 | TRACE( std::endl << BOOST_CURRENT_FUNCTION ); |
---|
254 | |
---|
255 | T a(6); |
---|
256 | |
---|
257 | int count = get_instance_count( ARG(T) ) ; |
---|
258 | |
---|
259 | set_throw_on_copy( ARG(T) ) ; |
---|
260 | |
---|
261 | bool passed = false ; |
---|
262 | try |
---|
263 | { |
---|
264 | // This should: |
---|
265 | // Attempt to copy construct 'a' and throw. |
---|
266 | // 'opt' won't be constructed. |
---|
267 | set_pending_copy( ARG(T) ) ; |
---|
268 | |
---|
269 | #if BOOST_WORKAROUND( BOOST_INTEL_CXX_VERSION, <= 700) // Intel C++ 7.0 |
---|
270 | // Intel C++ 7.0 specific: |
---|
271 | // For some reason, when "check_is_not_pending_copy", |
---|
272 | // after the exception block is reached, |
---|
273 | // X::pending_copy==true even though X's copy ctor set it to false. |
---|
274 | // I guessed there is some sort of optimization bug, |
---|
275 | // and it seems to be the since the following additional line just |
---|
276 | // solves the problem (!?) |
---|
277 | prevent_buggy_optimization(X::pending_copy); |
---|
278 | #endif |
---|
279 | |
---|
280 | optional<T> opt(a) ; |
---|
281 | passed = true ; |
---|
282 | } |
---|
283 | catch ( ... ){} |
---|
284 | |
---|
285 | BOOST_CHECK(!passed); |
---|
286 | check_is_not_pending_copy( ARG(T) ); |
---|
287 | check_instance_count(count, ARG(T) ); |
---|
288 | |
---|
289 | reset_throw_on_copy( ARG(T) ) ; |
---|
290 | |
---|
291 | } |
---|
292 | |
---|
293 | // |
---|
294 | // Test Value Assignment to an Uninitialized optional for a T with a throwing copy-ctor |
---|
295 | // |
---|
296 | template<class T> |
---|
297 | void test_throwing_val_assign_on_uninitialized( T const* ) |
---|
298 | { |
---|
299 | TRACE( std::endl << BOOST_CURRENT_FUNCTION ); |
---|
300 | |
---|
301 | T a(7); |
---|
302 | |
---|
303 | int count = get_instance_count( ARG(T) ) ; |
---|
304 | |
---|
305 | set_throw_on_copy( ARG(T) ) ; |
---|
306 | |
---|
307 | optional<T> opt ; |
---|
308 | |
---|
309 | bool passed = false ; |
---|
310 | try |
---|
311 | { |
---|
312 | // This should: |
---|
313 | // Attempt to copy construct 'a' and throw. |
---|
314 | // opt should be left uninitialized. |
---|
315 | set_pending_copy( ARG(T) ) ; |
---|
316 | opt.reset( a ); |
---|
317 | passed = true ; |
---|
318 | } |
---|
319 | catch ( ... ) {} |
---|
320 | |
---|
321 | BOOST_CHECK(!passed); |
---|
322 | |
---|
323 | check_is_not_pending_copy( ARG(T) ); |
---|
324 | check_instance_count(count, ARG(T) ); |
---|
325 | check_uninitialized(opt); |
---|
326 | |
---|
327 | reset_throw_on_copy( ARG(T) ) ; |
---|
328 | } |
---|
329 | |
---|
330 | // |
---|
331 | // Test Value Reset on an Initialized optional for a T with a throwing copy-ctor |
---|
332 | // |
---|
333 | template<class T> |
---|
334 | void test_throwing_val_assign_on_initialized( T const* ) |
---|
335 | { |
---|
336 | TRACE( std::endl << BOOST_CURRENT_FUNCTION ); |
---|
337 | |
---|
338 | T z(0); |
---|
339 | T a(8); |
---|
340 | T b(9); |
---|
341 | T x(-1); |
---|
342 | |
---|
343 | int count = get_instance_count( ARG(T) ) ; |
---|
344 | |
---|
345 | optional<T> opt ( b ) ; |
---|
346 | ++ count ; |
---|
347 | |
---|
348 | check_instance_count(count, ARG(T) ); |
---|
349 | |
---|
350 | check_value(opt,b,z); |
---|
351 | |
---|
352 | set_throw_on_assign( ARG(T) ) ; |
---|
353 | |
---|
354 | bool passed = false ; |
---|
355 | try |
---|
356 | { |
---|
357 | // This should: |
---|
358 | // Attempt to assign 'a' and throw. |
---|
359 | // opt is kept initialized but its value not neccesarily fully assigned |
---|
360 | // (in this test, incompletely assigned is flaged with the value -1 being set) |
---|
361 | set_pending_assign( ARG(T) ) ; |
---|
362 | opt.reset ( a ) ; |
---|
363 | passed = true ; |
---|
364 | } |
---|
365 | catch ( ... ) {} |
---|
366 | |
---|
367 | BOOST_CHECK(!passed); |
---|
368 | |
---|
369 | check_is_not_pending_assign( ARG(T) ); |
---|
370 | check_instance_count(count, ARG(T) ); |
---|
371 | check_initialized(opt); |
---|
372 | check_value(opt,x,z); |
---|
373 | |
---|
374 | reset_throw_on_assign ( ARG(T) ) ; |
---|
375 | } |
---|
376 | |
---|
377 | // |
---|
378 | // Test Copy Initialization from an Initialized optional for a T with a throwing copy-ctor |
---|
379 | // |
---|
380 | template<class T> |
---|
381 | void test_throwing_copy_initialization( T const* ) |
---|
382 | { |
---|
383 | TRACE( std::endl << BOOST_CURRENT_FUNCTION ); |
---|
384 | |
---|
385 | T z(0); |
---|
386 | T a(10); |
---|
387 | |
---|
388 | optional<T> opt (a); |
---|
389 | |
---|
390 | int count = get_instance_count( ARG(T) ) ; |
---|
391 | |
---|
392 | set_throw_on_copy( ARG(T) ) ; |
---|
393 | |
---|
394 | bool passed = false ; |
---|
395 | try |
---|
396 | { |
---|
397 | // This should: |
---|
398 | // Attempt to copy construct 'opt' and throw. |
---|
399 | // opt1 won't be constructed. |
---|
400 | set_pending_copy( ARG(T) ) ; |
---|
401 | optional<T> opt1 = opt ; |
---|
402 | passed = true ; |
---|
403 | } |
---|
404 | catch ( ... ) {} |
---|
405 | |
---|
406 | BOOST_CHECK(!passed); |
---|
407 | |
---|
408 | check_is_not_pending_copy( ARG(T) ); |
---|
409 | check_instance_count(count, ARG(T) ); |
---|
410 | |
---|
411 | // Nothing should have happened to the source optional. |
---|
412 | check_initialized(opt); |
---|
413 | check_value(opt,a,z); |
---|
414 | |
---|
415 | reset_throw_on_copy( ARG(T) ) ; |
---|
416 | } |
---|
417 | |
---|
418 | // |
---|
419 | // Test Assignment to an Uninitialized optional from an Initialized optional |
---|
420 | // for a T with a throwing copy-ctor |
---|
421 | // |
---|
422 | template<class T> |
---|
423 | void test_throwing_assign_to_uninitialized( T const* ) |
---|
424 | { |
---|
425 | TRACE( std::endl << BOOST_CURRENT_FUNCTION ); |
---|
426 | |
---|
427 | T z(0); |
---|
428 | T a(11); |
---|
429 | |
---|
430 | optional<T> opt0 ; |
---|
431 | optional<T> opt1(a) ; |
---|
432 | |
---|
433 | int count = get_instance_count( ARG(T) ) ; |
---|
434 | |
---|
435 | set_throw_on_copy( ARG(T) ) ; |
---|
436 | |
---|
437 | bool passed = false ; |
---|
438 | try |
---|
439 | { |
---|
440 | // This should: |
---|
441 | // Attempt to copy construct 'opt1.value()' into opt0 and throw. |
---|
442 | // opt0 should be left uninitialized. |
---|
443 | set_pending_copy( ARG(T) ) ; |
---|
444 | opt0 = opt1 ; |
---|
445 | passed = true ; |
---|
446 | } |
---|
447 | catch ( ... ) {} |
---|
448 | |
---|
449 | BOOST_CHECK(!passed); |
---|
450 | |
---|
451 | check_is_not_pending_copy( ARG(T) ); |
---|
452 | check_instance_count(count, ARG(T) ); |
---|
453 | check_uninitialized(opt0); |
---|
454 | |
---|
455 | reset_throw_on_copy( ARG(T) ) ; |
---|
456 | } |
---|
457 | |
---|
458 | // |
---|
459 | // Test Assignment to an Initialized optional from an Initialized optional |
---|
460 | // for a T with a throwing copy-ctor |
---|
461 | // |
---|
462 | template<class T> |
---|
463 | void test_throwing_assign_to_initialized( T const* ) |
---|
464 | { |
---|
465 | TRACE( std::endl << BOOST_CURRENT_FUNCTION ); |
---|
466 | |
---|
467 | T z(0); |
---|
468 | T a(12); |
---|
469 | T b(13); |
---|
470 | T x(-1); |
---|
471 | |
---|
472 | optional<T> opt0(a) ; |
---|
473 | optional<T> opt1(b) ; |
---|
474 | |
---|
475 | int count = get_instance_count( ARG(T) ) ; |
---|
476 | |
---|
477 | set_throw_on_assign( ARG(T) ) ; |
---|
478 | |
---|
479 | bool passed = false ; |
---|
480 | try |
---|
481 | { |
---|
482 | // This should: |
---|
483 | // Attempt to copy construct 'opt1.value()' into opt0 and throw. |
---|
484 | // opt0 is kept initialized but its value not neccesarily fully assigned |
---|
485 | // (in this test, incompletely assigned is flaged with the value -1 being set) |
---|
486 | set_pending_assign( ARG(T) ) ; |
---|
487 | opt0 = opt1 ; |
---|
488 | passed = true ; |
---|
489 | } |
---|
490 | catch ( ... ) {} |
---|
491 | |
---|
492 | BOOST_CHECK(!passed); |
---|
493 | |
---|
494 | // opt0 was left uninitialized |
---|
495 | check_is_not_pending_assign( ARG(T) ); |
---|
496 | check_instance_count(count, ARG(T) ); |
---|
497 | check_initialized(opt0); |
---|
498 | check_value(opt0,x,z); |
---|
499 | |
---|
500 | reset_throw_on_assign( ARG(T) ) ; |
---|
501 | } |
---|
502 | |
---|
503 | // |
---|
504 | // Test swap in a no-throwing case |
---|
505 | // |
---|
506 | template<class T> |
---|
507 | void test_no_throwing_swap( T const* ) |
---|
508 | { |
---|
509 | TRACE( std::endl << BOOST_CURRENT_FUNCTION ); |
---|
510 | |
---|
511 | T z(0); |
---|
512 | T a(14); |
---|
513 | T b(15); |
---|
514 | |
---|
515 | optional<T> def0 ; |
---|
516 | optional<T> def1 ; |
---|
517 | optional<T> opt0(a) ; |
---|
518 | optional<T> opt1(b) ; |
---|
519 | |
---|
520 | int count = get_instance_count( ARG(T) ) ; |
---|
521 | |
---|
522 | swap(def0,def1); |
---|
523 | check_uninitialized(def0); |
---|
524 | check_uninitialized(def1); |
---|
525 | |
---|
526 | swap(def0,opt0); |
---|
527 | check_uninitialized(opt0); |
---|
528 | check_initialized(def0); |
---|
529 | check_value(def0,a,z); |
---|
530 | |
---|
531 | // restore def0 and opt0 |
---|
532 | swap(def0,opt0); |
---|
533 | |
---|
534 | swap(opt0,opt1); |
---|
535 | check_instance_count(count, ARG(T) ); |
---|
536 | check_initialized(opt0); |
---|
537 | check_initialized(opt1); |
---|
538 | check_value(opt0,b,z); |
---|
539 | check_value(opt1,a,z); |
---|
540 | } |
---|
541 | |
---|
542 | // |
---|
543 | // Test swap in a throwing case |
---|
544 | // |
---|
545 | template<class T> |
---|
546 | void test_throwing_swap( T const* ) |
---|
547 | { |
---|
548 | TRACE( std::endl << BOOST_CURRENT_FUNCTION ); |
---|
549 | |
---|
550 | T a(16); |
---|
551 | T b(17); |
---|
552 | T x(-1); |
---|
553 | |
---|
554 | optional<T> opt0(a) ; |
---|
555 | optional<T> opt1(b) ; |
---|
556 | |
---|
557 | set_throw_on_assign( ARG(T) ) ; |
---|
558 | |
---|
559 | // |
---|
560 | // Case 1: Both Initialized. |
---|
561 | // |
---|
562 | bool passed = false ; |
---|
563 | try |
---|
564 | { |
---|
565 | // This should attempt to swap optionals and fail at swap(X&,X&). |
---|
566 | swap(opt0,opt1); |
---|
567 | |
---|
568 | passed = true ; |
---|
569 | } |
---|
570 | catch ( ... ) {} |
---|
571 | |
---|
572 | BOOST_CHECK(!passed); |
---|
573 | |
---|
574 | // optional's swap doesn't affect the initialized states of the arguments. Therefore, |
---|
575 | // the following must hold: |
---|
576 | check_initialized(opt0); |
---|
577 | check_initialized(opt1); |
---|
578 | check_value(opt0,x,a); |
---|
579 | check_value(opt1,b,x); |
---|
580 | |
---|
581 | |
---|
582 | // |
---|
583 | // Case 2: Only one Initialized. |
---|
584 | // |
---|
585 | reset_throw_on_assign( ARG(T) ) ; |
---|
586 | |
---|
587 | opt0.reset(); |
---|
588 | opt1.reset(a); |
---|
589 | |
---|
590 | set_throw_on_copy( ARG(T) ) ; |
---|
591 | |
---|
592 | passed = false ; |
---|
593 | try |
---|
594 | { |
---|
595 | // This should attempt to swap optionals and fail at opt0.reset(*opt1) |
---|
596 | // Both opt0 and op1 are left unchanged (unswaped) |
---|
597 | swap(opt0,opt1); |
---|
598 | |
---|
599 | passed = true ; |
---|
600 | } |
---|
601 | catch ( ... ) {} |
---|
602 | |
---|
603 | BOOST_CHECK(!passed); |
---|
604 | |
---|
605 | check_uninitialized(opt0); |
---|
606 | check_initialized(opt1); |
---|
607 | check_value(opt1,a,x); |
---|
608 | |
---|
609 | reset_throw_on_copy( ARG(T) ) ; |
---|
610 | } |
---|
611 | |
---|
612 | // |
---|
613 | // This verifies relational operators. |
---|
614 | // |
---|
615 | template<class T> |
---|
616 | void test_relops( T const* ) |
---|
617 | { |
---|
618 | TRACE( std::endl << BOOST_CURRENT_FUNCTION ); |
---|
619 | |
---|
620 | T v0(18); |
---|
621 | T v1(19); |
---|
622 | T v2(19); |
---|
623 | |
---|
624 | optional<T> def0 ; |
---|
625 | optional<T> def1 ; |
---|
626 | optional<T> opt0(v0); |
---|
627 | optional<T> opt1(v1); |
---|
628 | optional<T> opt2(v2); |
---|
629 | |
---|
630 | // Check identity |
---|
631 | BOOST_CHECK ( def0 == def0 ) ; |
---|
632 | BOOST_CHECK ( opt0 == opt0 ) ; |
---|
633 | BOOST_CHECK ( !(def0 != def0) ) ; |
---|
634 | BOOST_CHECK ( !(opt0 != opt0) ) ; |
---|
635 | |
---|
636 | // Check when both are uininitalized. |
---|
637 | BOOST_CHECK ( def0 == def1 ) ; // both uninitialized compare equal |
---|
638 | BOOST_CHECK ( !(def0 < def1) ) ; // uninitialized is never less than uninitialized |
---|
639 | BOOST_CHECK ( !(def0 > def1) ) ; // uninitialized is never greater than uninitialized |
---|
640 | BOOST_CHECK ( !(def0 != def1) ) ; |
---|
641 | BOOST_CHECK ( def0 <= def1 ) ; |
---|
642 | BOOST_CHECK ( def0 >= def1 ) ; |
---|
643 | |
---|
644 | // Check when only lhs is uninitialized. |
---|
645 | BOOST_CHECK ( def0 != opt0 ) ; // uninitialized is never equal to initialized |
---|
646 | BOOST_CHECK ( !(def0 == opt0) ) ; |
---|
647 | BOOST_CHECK ( def0 < opt0 ) ; // uninitialized is always less than initialized |
---|
648 | BOOST_CHECK ( !(def0 > opt0) ) ; |
---|
649 | BOOST_CHECK ( def0 <= opt0 ) ; |
---|
650 | BOOST_CHECK ( !(def0 >= opt0) ) ; |
---|
651 | |
---|
652 | // Check when only rhs is uninitialized. |
---|
653 | BOOST_CHECK ( opt0 != def0 ) ; // initialized is never equal to uninitialized |
---|
654 | BOOST_CHECK ( !(opt0 == def0) ) ; |
---|
655 | BOOST_CHECK ( !(opt0 < def0) ) ; // initialized is never less than uninitialized |
---|
656 | BOOST_CHECK ( opt0 > def0 ) ; |
---|
657 | BOOST_CHECK ( !(opt0 <= def0) ) ; |
---|
658 | BOOST_CHECK ( opt0 >= opt0 ) ; |
---|
659 | |
---|
660 | // If both are initialized, values are compared |
---|
661 | BOOST_CHECK ( opt0 != opt1 ) ; |
---|
662 | BOOST_CHECK ( opt1 == opt2 ) ; |
---|
663 | BOOST_CHECK ( opt0 < opt1 ) ; |
---|
664 | BOOST_CHECK ( opt1 > opt0 ) ; |
---|
665 | BOOST_CHECK ( opt1 <= opt2 ) ; |
---|
666 | BOOST_CHECK ( opt1 >= opt0 ) ; |
---|
667 | } |
---|
668 | |
---|
669 | template<class T> |
---|
670 | void test_none( T const* ) |
---|
671 | { |
---|
672 | TRACE( std::endl << BOOST_CURRENT_FUNCTION ); |
---|
673 | |
---|
674 | using boost::none ; |
---|
675 | |
---|
676 | optional<T> def0 ; |
---|
677 | optional<T> def1(none) ; |
---|
678 | optional<T> non_def( T(1234) ) ; |
---|
679 | |
---|
680 | BOOST_CHECK ( def0 == none ) ; |
---|
681 | BOOST_CHECK ( non_def != none ) ; |
---|
682 | BOOST_CHECK ( !def1 ) ; |
---|
683 | |
---|
684 | non_def = none ; |
---|
685 | BOOST_CHECK ( !non_def ) ; |
---|
686 | |
---|
687 | test_default_implicit_construction(T(1),none); |
---|
688 | } |
---|
689 | |
---|
690 | void test_with_builtin_types() |
---|
691 | { |
---|
692 | TRACE( std::endl << BOOST_CURRENT_FUNCTION ); |
---|
693 | |
---|
694 | test_basics( ARG(double) ); |
---|
695 | test_uninitialized_access( ARG(double) ); |
---|
696 | test_no_throwing_swap( ARG(double) ); |
---|
697 | test_relops( ARG(double) ) ; |
---|
698 | test_none( ARG(double) ) ; |
---|
699 | } |
---|
700 | |
---|
701 | void test_with_class_type() |
---|
702 | { |
---|
703 | TRACE( std::endl << BOOST_CURRENT_FUNCTION ); |
---|
704 | |
---|
705 | test_basics( ARG(X) ); |
---|
706 | test_direct_value_manip( ARG(X) ); |
---|
707 | test_uninitialized_access( ARG(X) ); |
---|
708 | test_throwing_direct_init( ARG(X) ); |
---|
709 | test_throwing_val_assign_on_uninitialized( ARG(X) ); |
---|
710 | test_throwing_val_assign_on_initialized( ARG(X) ); |
---|
711 | test_throwing_copy_initialization( ARG(X) ); |
---|
712 | test_throwing_assign_to_uninitialized( ARG(X) ); |
---|
713 | test_throwing_assign_to_initialized( ARG(X) ); |
---|
714 | test_no_throwing_swap( ARG(X) ); |
---|
715 | test_throwing_swap( ARG(X) ); |
---|
716 | test_relops( ARG(X) ) ; |
---|
717 | test_none( ARG(X) ) ; |
---|
718 | BOOST_CHECK ( X::count == 0 ) ; |
---|
719 | } |
---|
720 | |
---|
721 | int eat ( bool ) { return 1 ; } |
---|
722 | int eat ( char ) { return 1 ; } |
---|
723 | int eat ( int ) { return 1 ; } |
---|
724 | int eat ( void const* ) { return 1 ; } |
---|
725 | |
---|
726 | template<class T> int eat ( T ) { return 0 ; } |
---|
727 | |
---|
728 | // |
---|
729 | // This verifies that operator safe_bool() behaves properly. |
---|
730 | // |
---|
731 | template<class T> |
---|
732 | void test_no_implicit_conversions_impl( T const& ) |
---|
733 | { |
---|
734 | TRACE( std::endl << BOOST_CURRENT_FUNCTION ); |
---|
735 | |
---|
736 | optional<T> def ; |
---|
737 | BOOST_CHECK ( eat(def) == 0 ) ; |
---|
738 | } |
---|
739 | |
---|
740 | void test_no_implicit_conversions() |
---|
741 | { |
---|
742 | TRACE( std::endl << BOOST_CURRENT_FUNCTION ); |
---|
743 | |
---|
744 | bool b = false ; |
---|
745 | char c = 0 ; |
---|
746 | int i = 0 ; |
---|
747 | void const* p = 0 ; |
---|
748 | |
---|
749 | test_no_implicit_conversions_impl(b); |
---|
750 | test_no_implicit_conversions_impl(c); |
---|
751 | test_no_implicit_conversions_impl(i); |
---|
752 | test_no_implicit_conversions_impl(p); |
---|
753 | } |
---|
754 | |
---|
755 | struct A {} ; |
---|
756 | void test_conversions1() |
---|
757 | { |
---|
758 | TRACE( std::endl << BOOST_CURRENT_FUNCTION ); |
---|
759 | |
---|
760 | #ifndef BOOST_OPTIONAL_NO_CONVERTING_COPY_CTOR |
---|
761 | char c = 20 ; |
---|
762 | optional<char> opt0(c); |
---|
763 | optional<int> opt1(opt0); |
---|
764 | BOOST_CHECK(*opt1 == static_cast<int>(c)); |
---|
765 | #endif |
---|
766 | |
---|
767 | #ifndef BOOST_OPTIONAL_NO_CONVERTING_ASSIGNMENT |
---|
768 | float f = 21.22f ; |
---|
769 | double d = f ; |
---|
770 | optional<float> opt2(f) ; |
---|
771 | optional<double> opt3 ; |
---|
772 | opt3 = opt2 ; |
---|
773 | BOOST_CHECK(*opt3 == d); |
---|
774 | #endif |
---|
775 | } |
---|
776 | |
---|
777 | void test_conversions2() |
---|
778 | { |
---|
779 | TRACE( std::endl << BOOST_CURRENT_FUNCTION ); |
---|
780 | |
---|
781 | char c = 20 ; |
---|
782 | optional<int> opt(c); |
---|
783 | BOOST_CHECK( get(opt) == static_cast<int>(c)); |
---|
784 | |
---|
785 | float f = 21.22f ; |
---|
786 | optional<double> opt1; |
---|
787 | opt1 = f ; |
---|
788 | BOOST_CHECK(*get(&opt1) == static_cast<double>(f)); |
---|
789 | } |
---|
790 | |
---|
791 | int test_main( int, char* [] ) |
---|
792 | { |
---|
793 | try |
---|
794 | { |
---|
795 | test_with_class_type(); |
---|
796 | test_with_builtin_types(); |
---|
797 | test_no_implicit_conversions(); |
---|
798 | test_conversions1(); |
---|
799 | test_conversions2(); |
---|
800 | } |
---|
801 | catch ( ... ) |
---|
802 | { |
---|
803 | BOOST_ERROR("Unexpected Exception caught!"); |
---|
804 | } |
---|
805 | |
---|
806 | return 0; |
---|
807 | } |
---|
808 | |
---|
809 | |
---|