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