mirror of
https://github.com/boostorg/optional.git
synced 2025-07-14 12:56:35 +02:00
compile-file cases added to the test suite
[SVN r17029]
This commit is contained in:
@ -15,5 +15,11 @@ DEPENDS all : test ;
|
||||
|
||||
test-suite optional :
|
||||
[ run libs/optional/test/optional_test.cpp ]
|
||||
[ compile-fail libs/optional/test/optional_test_fail1.cpp ]
|
||||
[ compile-fail libs/optional/test/optional_test_fail2.cpp ]
|
||||
[ compile-fail libs/optional/test/optional_test_fail3.cpp ]
|
||||
[ compile-fail libs/optional/test/optional_test_fail4.cpp ]
|
||||
[ compile-fail libs/optional/test/optional_test_fail5a.cpp ]
|
||||
[ compile-fail libs/optional/test/optional_test_fail5b.cpp ]
|
||||
;
|
||||
}
|
||||
|
@ -54,6 +54,8 @@ void assertion_failed (char const * expr, char const * func, char const * file,
|
||||
|
||||
using boost::optional ;
|
||||
|
||||
template<class T> inline void unused_variable ( T ) {}
|
||||
|
||||
#ifdef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP
|
||||
using boost::swap ;
|
||||
using boost::get_pointer ;
|
||||
@ -61,14 +63,6 @@ using boost::get_pointer ;
|
||||
|
||||
#define ARG(T) (static_cast< T const* >(0))
|
||||
|
||||
//#define SHOW_COMPILATION_FAIL_1
|
||||
//#define SHOW_COMPILATION_FAIL_2
|
||||
//#define SHOW_COMPILATION_FAIL_3
|
||||
//#define SHOW_COMPILATION_FAIL_4a
|
||||
//#define SHOW_COMPILATION_FAIL_4b
|
||||
//#define SHOW_COMPILATION_FAIL_5a
|
||||
//#define SHOW_COMPILATION_FAIL_5b
|
||||
|
||||
//
|
||||
// Helper class used to verify the lifetime managment of the values held by optional
|
||||
//
|
||||
@ -344,25 +338,6 @@ void test_basics( T const* )
|
||||
ob.reset();
|
||||
check_is_pending_dtor( ARG(T) ) ;
|
||||
check_uninitialized(ob);
|
||||
|
||||
#ifdef SHOW_COMPILATION_FAIL_1
|
||||
// This is illegal since 'oa2' is const.
|
||||
*oa2 = oa ;
|
||||
#endif
|
||||
|
||||
#ifdef SHOW_COMPILATION_FAIL_2
|
||||
T c(3);
|
||||
// Direct Value Assignment is not allowed.
|
||||
// Use operator*() instead.
|
||||
oa = c ;
|
||||
#endif
|
||||
|
||||
#ifdef SHOW_COMPILATION_FAIL_3
|
||||
T d(4);
|
||||
// Direct Value Construction is explicit.
|
||||
optional<T> oc = d ;
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
//
|
||||
@ -404,9 +379,9 @@ void test_uninitialized_access( T const* )
|
||||
bool passed = false ;
|
||||
try
|
||||
{
|
||||
// This should throw becasue 'def' is uninitialized
|
||||
// This should throw because 'def' is uninitialized
|
||||
T const& n = *def ;
|
||||
(n);
|
||||
unused_variable(n);
|
||||
passed = true ;
|
||||
}
|
||||
catch (...) {}
|
||||
@ -415,8 +390,9 @@ void test_uninitialized_access( T const* )
|
||||
passed = false ;
|
||||
try
|
||||
{
|
||||
T v(1) ; (v);
|
||||
// This should throw becasue 'def' is uninitialized
|
||||
T v(1) ;
|
||||
unused_variable(v);
|
||||
// This should throw because 'def' is uninitialized
|
||||
*def = v ;
|
||||
passed = true ;
|
||||
}
|
||||
@ -426,9 +402,9 @@ void test_uninitialized_access( T const* )
|
||||
passed = false ;
|
||||
try
|
||||
{
|
||||
// This should throw becasue 'def' is uninitialized
|
||||
// This should throw because 'def' is uninitialized
|
||||
T v = *(def.operator->()) ;
|
||||
(v);
|
||||
unused_variable(v);
|
||||
passed = true ;
|
||||
}
|
||||
catch (...) {}
|
||||
@ -803,16 +779,6 @@ void test_relops( T const* )
|
||||
optional<T> opt1(v1);
|
||||
optional<T> opt2(v2);
|
||||
|
||||
#ifdef SHOW_COMPILATION_FAIL_4a
|
||||
// You can compare against 0 or against another optional<>,
|
||||
// but not against another value
|
||||
if ( def0 == 1 ) ;
|
||||
#endif
|
||||
|
||||
#ifdef SHOW_COMPILATION_FAIL_4b
|
||||
if ( def0 != 1 ) ;
|
||||
#endif
|
||||
|
||||
// Check identity
|
||||
BOOST_CHECK ( def0 == def0 ) ;
|
||||
BOOST_CHECK ( opt0 == opt0 ) ;
|
||||
@ -909,15 +875,6 @@ void test_conversions()
|
||||
optional<double> opt3 ;
|
||||
opt3 = opt2 ;
|
||||
BOOST_CHECK(*opt3 == d);
|
||||
|
||||
#ifdef SHOW_COMPILATION_FAIL_5a
|
||||
optional<A> opt4(opt0);
|
||||
#endif
|
||||
|
||||
#ifdef SHOW_COMPILATION_FAIL_5b
|
||||
optional<A> opt5 ;
|
||||
opt5 = opt0;
|
||||
#endif
|
||||
}
|
||||
|
||||
int test_main( int, char* [] )
|
||||
|
28
test/optional_test_fail1.cpp
Normal file
28
test/optional_test_fail1.cpp
Normal file
@ -0,0 +1,28 @@
|
||||
// (C) 2003, Fernando Luis Cacciola Carballal.
|
||||
//
|
||||
// This material is provided "as is", with absolutely no warranty expressed
|
||||
// or implied. Any use is at your own risk.
|
||||
//
|
||||
// Permission to use or copy this software for any purpose is hereby granted
|
||||
// without fee, provided the above notices are retained on all copies.
|
||||
// Permission to modify the code and to distribute modified code is granted,
|
||||
// provided the above notices are retained, and a notice that the code was
|
||||
// modified is included with the above copyright notice.
|
||||
//
|
||||
// You are welcome to contact the author at:
|
||||
// fernando_cacciola@hotmail.com
|
||||
//
|
||||
#include "boost/optional.hpp"
|
||||
|
||||
//
|
||||
// THIS TEST SHOULD FAIL TO COMPILE
|
||||
//
|
||||
void test_deep_constantness()
|
||||
{
|
||||
boost::optional<int> opt ;
|
||||
boost::optional<int> const copt ;
|
||||
|
||||
*copt = opt ; // Cannot assign to "int const&"
|
||||
}
|
||||
|
||||
|
26
test/optional_test_fail2.cpp
Normal file
26
test/optional_test_fail2.cpp
Normal file
@ -0,0 +1,26 @@
|
||||
// (C) 2003, Fernando Luis Cacciola Carballal.
|
||||
//
|
||||
// This material is provided "as is", with absolutely no warranty expressed
|
||||
// or implied. Any use is at your own risk.
|
||||
//
|
||||
// Permission to use or copy this software for any purpose is hereby granted
|
||||
// without fee, provided the above notices are retained on all copies.
|
||||
// Permission to modify the code and to distribute modified code is granted,
|
||||
// provided the above notices are retained, and a notice that the code was
|
||||
// modified is included with the above copyright notice.
|
||||
//
|
||||
// You are welcome to contact the author at:
|
||||
// fernando_cacciola@hotmail.com
|
||||
//
|
||||
#include "boost/optional.hpp"
|
||||
|
||||
//
|
||||
// THIS TEST SHOULD FAIL TO COMPILE
|
||||
//
|
||||
void test_no_direct_value_assignment()
|
||||
{
|
||||
boost::optional<int> opt(3) ;
|
||||
opt = 4 ; // Cannot assign "int" to "optional<int>"
|
||||
}
|
||||
|
||||
|
25
test/optional_test_fail3.cpp
Normal file
25
test/optional_test_fail3.cpp
Normal file
@ -0,0 +1,25 @@
|
||||
// (C) 2003, Fernando Luis Cacciola Carballal.
|
||||
//
|
||||
// This material is provided "as is", with absolutely no warranty expressed
|
||||
// or implied. Any use is at your own risk.
|
||||
//
|
||||
// Permission to use or copy this software for any purpose is hereby granted
|
||||
// without fee, provided the above notices are retained on all copies.
|
||||
// Permission to modify the code and to distribute modified code is granted,
|
||||
// provided the above notices are retained, and a notice that the code was
|
||||
// modified is included with the above copyright notice.
|
||||
//
|
||||
// You are welcome to contact the author at:
|
||||
// fernando_cacciola@hotmail.com
|
||||
//
|
||||
#include "boost/optional.hpp"
|
||||
|
||||
//
|
||||
// THIS TEST SHOULD FAIL TO COMPILE
|
||||
//
|
||||
void test_explicit_constructor()
|
||||
{
|
||||
boost::optional<int> opt = 3 ; // ERROR: Ctor is explicit.
|
||||
}
|
||||
|
||||
|
29
test/optional_test_fail4.cpp
Normal file
29
test/optional_test_fail4.cpp
Normal file
@ -0,0 +1,29 @@
|
||||
// (C) 2003, Fernando Luis Cacciola Carballal.
|
||||
//
|
||||
// This material is provided "as is", with absolutely no warranty expressed
|
||||
// or implied. Any use is at your own risk.
|
||||
//
|
||||
// Permission to use or copy this software for any purpose is hereby granted
|
||||
// without fee, provided the above notices are retained on all copies.
|
||||
// Permission to modify the code and to distribute modified code is granted,
|
||||
// provided the above notices are retained, and a notice that the code was
|
||||
// modified is included with the above copyright notice.
|
||||
//
|
||||
// You are welcome to contact the author at:
|
||||
// fernando_cacciola@hotmail.com
|
||||
//
|
||||
#include "boost/optional.hpp"
|
||||
|
||||
//
|
||||
// THIS TEST SHOULD FAIL TO COMPILE
|
||||
//
|
||||
void test_no_implicit_conversion()
|
||||
{
|
||||
boost::optional<int> opt(1) ;
|
||||
|
||||
// You can compare against 0 or against another optional<>,
|
||||
// but not against another value
|
||||
if ( opt == 1 ) ;
|
||||
}
|
||||
|
||||
|
28
test/optional_test_fail5a.cpp
Normal file
28
test/optional_test_fail5a.cpp
Normal file
@ -0,0 +1,28 @@
|
||||
// (C) 2003, Fernando Luis Cacciola Carballal.
|
||||
//
|
||||
// This material is provided "as is", with absolutely no warranty expressed
|
||||
// or implied. Any use is at your own risk.
|
||||
//
|
||||
// Permission to use or copy this software for any purpose is hereby granted
|
||||
// without fee, provided the above notices are retained on all copies.
|
||||
// Permission to modify the code and to distribute modified code is granted,
|
||||
// provided the above notices are retained, and a notice that the code was
|
||||
// modified is included with the above copyright notice.
|
||||
//
|
||||
// You are welcome to contact the author at:
|
||||
// fernando_cacciola@hotmail.com
|
||||
//
|
||||
#include<string>
|
||||
|
||||
#include "boost/optional.hpp"
|
||||
|
||||
//
|
||||
// THIS TEST SHOULD FAIL TO COMPILE
|
||||
//
|
||||
void test_no_unsupported_conversion()
|
||||
{
|
||||
boost::optional<int> opt1(1) ;
|
||||
boost::optional< std::string > opt2( opt1 ) ; // Cannot convert from "int" to "std::string"
|
||||
}
|
||||
|
||||
|
29
test/optional_test_fail5b.cpp
Normal file
29
test/optional_test_fail5b.cpp
Normal file
@ -0,0 +1,29 @@
|
||||
// (C) 2003, Fernando Luis Cacciola Carballal.
|
||||
//
|
||||
// This material is provided "as is", with absolutely no warranty expressed
|
||||
// or implied. Any use is at your own risk.
|
||||
//
|
||||
// Permission to use or copy this software for any purpose is hereby granted
|
||||
// without fee, provided the above notices are retained on all copies.
|
||||
// Permission to modify the code and to distribute modified code is granted,
|
||||
// provided the above notices are retained, and a notice that the code was
|
||||
// modified is included with the above copyright notice.
|
||||
//
|
||||
// You are welcome to contact the author at:
|
||||
// fernando_cacciola@hotmail.com
|
||||
//
|
||||
#include<string>
|
||||
|
||||
#include "boost/optional.hpp"
|
||||
|
||||
//
|
||||
// THIS TEST SHOULD FAIL TO COMPILE
|
||||
//
|
||||
void test_no_unsupported_conversion()
|
||||
{
|
||||
boost::optional<int> opt1(1) ;
|
||||
boost::optional< std::string > opt2 ;
|
||||
opt2 = opt1 ; // Cannot convert from "int" to "std::string"
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user