diff --git a/test/Jamfile b/test/Jamfile index 5f41779..1cb27f8 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -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 ] + ; } diff --git a/test/optional_test.cpp b/test/optional_test.cpp index 9f33a50..2b1ca36 100644 --- a/test/optional_test.cpp +++ b/test/optional_test.cpp @@ -42,11 +42,11 @@ void assertion_failed (char const * expr, char const * func, char const * file, + string("\" at file \"") + string(file) + string("\" function \"") - + string(func) + + string(func) + string("\"") ; TRACE(msg); - + throw std::logic_error(msg); } @@ -54,6 +54,8 @@ void assertion_failed (char const * expr, char const * func, char const * file, using boost::optional ; +template 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 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 opt1(v1); optional 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 ) ; @@ -858,7 +824,7 @@ void test_with_class_type() test_no_throwing_swap( ARG(X) ); test_throwing_swap( ARG(X) ); test_relops( ARG(X) ) ; - BOOST_CHECK ( X::count == 0 ) ; + BOOST_CHECK ( X::count == 0 ) ; } int eat ( char ) { return 1 ; } @@ -874,7 +840,7 @@ template void test_no_implicit_conversions_impl( T const& ) { TRACE( std::endl << BOOST_CURRENT_FUNCTION ); - + optional def ; BOOST_CHECK ( eat(def) == 0 ) ; } @@ -909,15 +875,6 @@ void test_conversions() optional opt3 ; opt3 = opt2 ; BOOST_CHECK(*opt3 == d); - -#ifdef SHOW_COMPILATION_FAIL_5a - optional opt4(opt0); -#endif - -#ifdef SHOW_COMPILATION_FAIL_5b - optional opt5 ; - opt5 = opt0; -#endif } int test_main( int, char* [] ) diff --git a/test/optional_test_fail1.cpp b/test/optional_test_fail1.cpp new file mode 100644 index 0000000..408e6cb --- /dev/null +++ b/test/optional_test_fail1.cpp @@ -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 opt ; + boost::optional const copt ; + + *copt = opt ; // Cannot assign to "int const&" +} + + diff --git a/test/optional_test_fail2.cpp b/test/optional_test_fail2.cpp new file mode 100644 index 0000000..2ab17c6 --- /dev/null +++ b/test/optional_test_fail2.cpp @@ -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 opt(3) ; + opt = 4 ; // Cannot assign "int" to "optional" +} + + diff --git a/test/optional_test_fail3.cpp b/test/optional_test_fail3.cpp new file mode 100644 index 0000000..8dee9e1 --- /dev/null +++ b/test/optional_test_fail3.cpp @@ -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 opt = 3 ; // ERROR: Ctor is explicit. +} + + diff --git a/test/optional_test_fail4.cpp b/test/optional_test_fail4.cpp new file mode 100644 index 0000000..c242315 --- /dev/null +++ b/test/optional_test_fail4.cpp @@ -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 opt(1) ; + + // You can compare against 0 or against another optional<>, + // but not against another value + if ( opt == 1 ) ; +} + + diff --git a/test/optional_test_fail5a.cpp b/test/optional_test_fail5a.cpp new file mode 100644 index 0000000..771ef73 --- /dev/null +++ b/test/optional_test_fail5a.cpp @@ -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 + +#include "boost/optional.hpp" + +// +// THIS TEST SHOULD FAIL TO COMPILE +// +void test_no_unsupported_conversion() +{ + boost::optional opt1(1) ; + boost::optional< std::string > opt2( opt1 ) ; // Cannot convert from "int" to "std::string" +} + + diff --git a/test/optional_test_fail5b.cpp b/test/optional_test_fail5b.cpp new file mode 100644 index 0000000..1d00d2b --- /dev/null +++ b/test/optional_test_fail5b.cpp @@ -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 + +#include "boost/optional.hpp" + +// +// THIS TEST SHOULD FAIL TO COMPILE +// +void test_no_unsupported_conversion() +{ + boost::optional opt1(1) ; + boost::optional< std::string > opt2 ; + opt2 = opt1 ; // Cannot convert from "int" to "std::string" +} + +