Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r72307 - sandbox/conversion/libs/conversion_ext/test
From: vicente.botet_at_[hidden]
Date: 2011-05-31 10:12:13


Author: viboes
Date: 2011-05-31 10:12:12 EDT (Tue, 31 May 2011)
New Revision: 72307
URL: http://svn.boost.org/trac/boost/changeset/72307

Log:
Conversion:Added more teste on Optional
Text files modified:
   sandbox/conversion/libs/conversion_ext/test/builtins.cpp | 1
   sandbox/conversion/libs/conversion_ext/test/optional.cpp | 108 ++++++++++++++++++++++++++++++++++-----
   2 files changed, 93 insertions(+), 16 deletions(-)

Modified: sandbox/conversion/libs/conversion_ext/test/builtins.cpp
==============================================================================
--- sandbox/conversion/libs/conversion_ext/test/builtins.cpp (original)
+++ sandbox/conversion/libs/conversion_ext/test/builtins.cpp 2011-05-31 10:12:12 EDT (Tue, 31 May 2011)
@@ -17,6 +17,7 @@
 #include <boost/static_assert.hpp>
 
 #include <boost/conversion/try_convert_to.hpp>
+#include <boost/conversion/try_assign_to.hpp>
 
 #include <boost/conversion/fp/convert_to.hpp>
 #include <boost/phoenix/core/argument.hpp>

Modified: sandbox/conversion/libs/conversion_ext/test/optional.cpp
==============================================================================
--- sandbox/conversion/libs/conversion_ext/test/optional.cpp (original)
+++ sandbox/conversion/libs/conversion_ext/test/optional.cpp 2011-05-31 10:12:12 EDT (Tue, 31 May 2011)
@@ -14,14 +14,20 @@
 #include <iostream>
 #include <boost/detail/lightweight_test.hpp>
 #include <boost/conversion/try_convert_to.hpp>
+#include <boost/conversion/try_assign_to.hpp>
 
 
 struct A1{};
 struct B1{};
+struct C1{};
 A1 convert_to(const B1&, boost::conversion::dummy::type_tag<A1> const&)
 {
   return A1();
 }
+A1 convert_to(const C1&, boost::conversion::dummy::type_tag<A1> const&)
+{
+ throw 1;
+}
 
 A1& assign_to(A1& to, const B1&)
 {
@@ -31,37 +37,107 @@
 
 void explicit_convert_to()
 {
- B1 b1;
- boost::optional<B1> b;
- boost::optional<A1> a1(boost::conversion::convert_to<boost::optional<A1> >(b));
- boost::optional<A1> a3;
- a3=boost::conversion::convert_to<boost::optional<A1> >(b);
- boost::optional<A1> a2(boost::conversion::convert_to<boost::optional<A1> >(boost::optional<B1>(b1)));
+ {
+ B1 b1;
+ boost::optional<B1> b=b1;
+ boost::optional<A1> a(boost::conversion::convert_to<boost::optional<A1> >(b));
+ BOOST_TEST(a);
+ }
+
+ {
+ B1 b1;
+ boost::optional<A1> a(boost::conversion::convert_to<boost::optional<A1> >(boost::optional<B1>(b1)));
+ BOOST_TEST(a);
+ }
+
+ { // target is not assigned when the source is not initialized
+ boost::optional<B1> b;
+ boost::optional<A1> a;
+ a=boost::conversion::convert_to<boost::optional<A1> >(b);
+ BOOST_TEST(!a);
+ }
+
+ { // target is not assigned when the source is not initialized
+ boost::optional<C1> c;
+ boost::optional<A1> a;
+ a=boost::conversion::convert_to<boost::optional<A1> >(c);
+ BOOST_TEST(!a);
+ }
+
+ { // conversion of optional throws if conversion of elements throw
+ C1 c1;
+ boost::optional<C1> c=c1;
+ boost::optional<A1> a;
+ try {
+ a=boost::conversion::convert_to<boost::optional<A1> >(c);
+ BOOST_TEST(false);
+ } catch (...) { }
+ }
+
   
 }
 
 void explicit_assign_to()
 {
- B1 b1;
- boost::optional<A1> a;
- boost::optional<B1> b;
- assign_to(a,b);
- boost::conversion::assign_to(a, boost::optional<B1>(b1));
+ { // target is assigned when the source is initialized
+ B1 b1;
+ boost::optional<A1> a;
+ BOOST_TEST(!a);
+ boost::conversion::assign_to(a, boost::optional<B1>(b1));
+ BOOST_TEST(a);
+ }
+ { // assign_to can be used when found by ADL
+ B1 b1;
+ boost::optional<A1> a;
+ boost::optional<B1> b=b1;
+ assign_to(a,b);
+ BOOST_TEST(a);
+ }
 }
 
 void explicit_try_convert_to()
 {
- B1 b1;
- boost::optional<A1> a3;
- a3=boost::conversion::convert_to<boost::optional<A1> >(b1);
- a3=boost::conversion::try_convert_to<A1 >(b1);
-
+ { // when the target is optional and the source isn't it, the target is assigned
+ B1 b1;
+ boost::optional<A1> a;
+ a=boost::conversion::convert_to<boost::optional<A1> >(b1);
+ BOOST_TEST(a);
+ }
+ { // try_convert_to return an optional initialized when the conversion succeeds
+ B1 b1;
+ boost::optional<A1> a;
+ a=boost::conversion::try_convert_to<A1 >(b1);
+ BOOST_TEST(a);
+ }
+ { // try_convert_to return an optional uninitialized when the conversion fails
+ C1 c1;
+ boost::optional<A1> a;
+ a=boost::conversion::try_convert_to<A1 >(c1);
+ BOOST_TEST(!a);
+ }
+
+}
+void explicit_try_assign_to()
+{
+ {
+ A1 a1;
+ B1 b1;
+ bool res = boost::conversion::try_assign_to<A1 >(a1,b1);
+ BOOST_TEST(res);
+ }
+ {
+ A1 a1;
+ C1 c1;
+ bool res = boost::conversion::try_assign_to<A1 >(a1,c1);
+ BOOST_TEST(!res);
+ }
 }
 int main()
 {
   explicit_convert_to();
   explicit_assign_to();
   explicit_try_convert_to();
+ explicit_try_assign_to();
   return boost::report_errors();
 }
 


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk