Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r69452 - sandbox/enums/libs/enums/test
From: vicente.botet_at_[hidden]
Date: 2011-03-01 18:45:31


Author: viboes
Date: 2011-03-01 18:45:27 EST (Tue, 01 Mar 2011)
New Revision: 69452
URL: http://svn.boost.org/trac/boost/changeset/69452

Log:
Enums: Add comparaison + desactivate default test + add some test on enum_range
Added:
   sandbox/enums/libs/enums/test/comparaision.cpp (contents, props changed)
Removed:
   sandbox/enums/libs/enums/test/comparaision_fails.cpp
Text files modified:
   sandbox/enums/libs/enums/test/EnumClass.cpp | 68 ++++++++++++++++++--------
   sandbox/enums/libs/enums/test/EnumClass.hpp | 98 ++++++++++++++++++++++++++++++++++-----
   sandbox/enums/libs/enums/test/Jamfile.v2 | 6 +-
   sandbox/enums/libs/enums/test/defaults_to_enum_default.cpp | 5 +
   4 files changed, 137 insertions(+), 40 deletions(-)

Modified: sandbox/enums/libs/enums/test/EnumClass.cpp
==============================================================================
--- sandbox/enums/libs/enums/test/EnumClass.cpp (original)
+++ sandbox/enums/libs/enums/test/EnumClass.cpp 2011-03-01 18:45:27 EST (Tue, 01 Mar 2011)
@@ -17,15 +17,24 @@
 #include "./EnumClass.hpp"
 #include "./f.hpp"
 #include <boost/detail/lightweight_test.hpp>
+#include <boost/enums/enum_range.hpp>
+#include <boost/range/algorithm/for_each.hpp>
 
 #define RUN_TIME
 #define COMPILE_TIME
 
+void p(EnumClass )
+{
+
+}
+
+
 int main() {
 
   using namespace boost;
   using namespace boost::enums;
 
+ std::cout << __LINE__ << std::endl;
 
   { // The wrapper can be constructed from a valid const char* repressentation
     EnumClass e = convert_to<EnumClass>("Enum2");
@@ -49,6 +58,7 @@
     BOOST_TEST(e==EnumClass::Enum2);
     BOOST_TEST(strcmp(c_str(e),"EnumClass::Enum2")==0);
   }
+ std::cout << __LINE__ << std::endl;
 #ifdef COMPILE_TIME2
   { // Construction of the wrapper from const char * compile fails
     std::string str;
@@ -64,23 +74,26 @@
     BOOST_TEST(strcmp(c_str(e), "EnumClass::CHASSE")==0);
   }
 #endif
-
- //~ { // Explicit conversion from valid int works
- //~ EnumClass e(convert_to<EnumClass>(u’\1);
- //~ BOOST_TEST(e==EnumClass::Enum1);
- //~ }
- //~ { // Explicit conversion from invalid int works also !!!
- //~ EnumClass e(convert_to<EnumClass>(u’\3);
- //~ // ... fail
- //~ }
- //~ { // Construction of the wrapper with a valid int works
- //~ EnumClass e(convert_to<EnumClass>(u’\2);
- //~ BOOST_TEST(e==EnumClass::Enum2);
- //~ }
- //~ { // Construction of the wrapper with an invalid ints results in run-time error
- //~ EnumClass e(convert_to<EnumClass>(u’\3);
- //~ //BOOST_TEST(e==3);
- //~ }
+ std::cout << __LINE__ << std::endl;
+ { // Explicit conversion from valid int works
+ EnumClass e(convert_to<EnumClass>((unsigned char)(4)));
+ BOOST_TEST(e==EnumClass::Enum1);
+ }
+ std::cout << __LINE__ << std::endl;
+ { // Explicit conversion from invalid int results in run-time error (undefined behavior)
+// EnumClass e(convert_to<EnumClass>((unsigned char)(6)));
+// BOOST_TEST(get_value(e)==(unsigned char)(6));
+ }
+ std::cout << __LINE__ << std::endl;
+ { // Construction of the wrapper with a valid int works
+ EnumClass e(convert_to<EnumClass>((unsigned char)(5)));
+ BOOST_TEST(e==EnumClass::Enum2);
+ }
+ std::cout << __LINE__ << std::endl; { // Construction of the wrapper with an invalid ints results in run-time error (undefined behavior)
+// EnumClass e(convert_to<EnumClass>((unsigned char)(6)));
+// BOOST_TEST((unsigned char)(enums::enum_type<EnumClass>::type(6))==(unsigned char)(6));
+// BOOST_TEST(get_value(e)==(unsigned char)(6));
+ }
 #if !defined(CTOR) && (BOOST_ENUMS_USE_CONSTRUCTORS==0)
   { // The wrapper can be used as member of a union as it is the case of the underlying enum (When constructors are not defined).
     union U {
@@ -91,19 +104,30 @@
     u.e = EnumClass::Enum1;
   }
 #endif
- { // The wrapper can be used in switch through the function get only :(
- //~ EnumClass e = default_value<EnumClass>();
- EnumClass e ;
+ std::cout << __LINE__ << std::endl;
+ { // The wrapper can be used in switch through the function get only :(
+ EnumClass e = EnumClass::Default;
+ std::cout << __LINE__ << std::endl; //EnumClass e ;
+ enums::enum_type<EnumClass>::type c=get_value(e);
+ std::cout << __LINE__ << std::endl;
+ std::cout << int(c) << std::endl;
     switch (get_value(e)) { // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
       case EnumClass::Enum1:
       case EnumClass::Enum2:
       case EnumClass::Default:
- std::cout << e << std::endl;
+ std::cout << __LINE__ << std::endl;
+ std::cout << e << std::endl;
         break;
       default:
- std::cout << e << ":"<< get_value(e) << std::endl;
+ //std::cout << e << ":"<< get_value(e) << std::endl;
         ;
     }
   }
+ std::cout << __LINE__ << std::endl;
+ {
+ for_each(enum_range<EnumClass>(),p);
+ enum_range<EnumClass> er;
+ //EnumClass e = *boost::begin(er);
+ }
   return boost::report_errors();
 }

Modified: sandbox/enums/libs/enums/test/EnumClass.hpp
==============================================================================
--- sandbox/enums/libs/enums/test/EnumClass.hpp (original)
+++ sandbox/enums/libs/enums/test/EnumClass.hpp 2011-03-01 18:45:27 EST (Tue, 01 Mar 2011)
@@ -18,7 +18,13 @@
 #include <boost/enums/enum_type.hpp>
 #include <boost/enums/default_value.hpp>
 #include <boost/enums/get_value.hpp>
+#include <boost/enums/pos.hpp>
+#include <boost/enums/first.hpp>
+#include <boost/enums/last.hpp>
+#include <boost/enums/size.hpp>
+#include <boost/enums/enum_traits.hpp>
 #include <boost/conversion/convert_to.hpp>
+#include <boost/enums/enum_traiter.hpp>
 #include <boost/enums/emulation.hpp>
 #include <cassert>
 #include <cstring>
@@ -30,7 +36,7 @@
 //~ #if defined(BOOST_ENUM_CLASS_START)
 #if 1
   BOOST_ENUM_CLASS_START(EnumClass, unsigned char) {
- Default = 0,
+ Default = 3,
     Enum1,
     Enum2
   } BOOST_ENUM_CLASS_CONS_END(EnumClass, unsigned char)
@@ -39,7 +45,7 @@
   #if !defined(BOOST_NO_SCOPED_ENUMS)
 enum class EnumClass : unsigned char
 {
- Default = 0,
+ Default = 3,
   Enum1,
   Enum2
 }
@@ -60,7 +66,7 @@
   //! nested C++98 enumeration class
   enum type
   {
- Default = 0,
+ Default = 3,
     Enum1,
     Enum2
   };
@@ -96,7 +102,7 @@
 
 #ifdef CTOR
   //! default constructor :: Default
- EnumClass() : Val()
+ EnumClass() : Val(type())
   {
   }
 
@@ -143,11 +149,8 @@
 
 
 #endif // !defined(BOOST_NO_SCOPED_ENUMS)
-#endif // defined(BOOST_ENUM_CLASS_START)
-
 //! conversion from the underlying int.
 inline EnumClass convert_to(boost::enums::underlying_type<EnumClass>::type v
-//~ inline EnumClass convert_to(unsigned int v
   , boost::dummy::type_tag<EnumClass> const&
 )
 {
@@ -158,7 +161,7 @@
 #endif
 }
 
-//! conversion from the underlying int.
+//! conversion from the native enum type.
 inline EnumClass convert_to(boost::enums::enum_type<EnumClass>::type v
   , boost::dummy::type_tag<EnumClass> const&
 )
@@ -169,8 +172,7 @@
   return EnumClass(v);
 #endif
 }
-
-
+#endif // defined(BOOST_ENUM_CLASS_START)
 
 //! conversion from c-string.
 inline EnumClass convert_to(const char* str
@@ -180,7 +182,7 @@
     if (strcmp(str, "Default") ==0) { return boost::convert_to<EnumClass>(EnumClass::Default); }
     if (strcmp(str, "Enum1") ==0) { return boost::convert_to<EnumClass>(EnumClass::Enum1); }
     if (strcmp(str, "Enum2") ==0) { return boost::convert_to<EnumClass>(EnumClass::Enum2); }
- assert(false && "invalid string for ArqType");
+ assert(false && "invalid string for EnumClass");
 }
 
 //! conversion from std::string.
@@ -191,7 +193,7 @@
     return boost::convert_to<EnumClass>(str.c_str());
 }
 
-//! explicit conversion to c-string.
+//!explicit conversion to c-string.
 inline const char* c_str(EnumClass e)
 {
     switch (boost::enums::get_value(e))
@@ -204,10 +206,80 @@
     }
 }
 
+namespace boost {
+ namespace enums {
+ namespace meta {
+
+ template <>
+ struct size<EnumClass>
+ {
+ static const int value = 3;
+ };
+ template <>
+ struct pos<EnumClass, EnumClass::Default>
+ {
+ static const int value = 0;
+ };
+ template <>
+ struct pos<EnumClass, EnumClass::Enum1>
+ {
+ static const int value = 1;
+ };
+ template <>
+ struct pos<EnumClass, EnumClass::Enum2>
+ {
+ static const int value = 2;
+ };
+
+ template <>
+ struct val<EnumClass, 0>
+ {
+ static const boost::enums::enum_type<EnumClass>::type value = EnumClass::Default;
+ };
+ template <>
+ struct val<EnumClass, 1>
+ {
+ static const boost::enums::enum_type<EnumClass>::type value = EnumClass::Enum1;
+ };
+ template <>
+ struct val<EnumClass, 2>
+ {
+ static const boost::enums::enum_type<EnumClass>::type value = EnumClass::Enum2;
+ };
+ } // namespace meta
+ template <>
+ struct enum_traits<EnumClass> : enum_traiter<EnumClass>
+ {
+ static int pos(EnumClass e)
+ {
+ switch (boost::enums::get_value(e))
+ {
+ case EnumClass::Default: return 0;
+ case EnumClass::Enum1: return 1;
+ case EnumClass::Enum2: return 2;
+ default: return -1;
+ }
+ }
+ static EnumClass val(int p)
+ {
+ switch (p)
+ {
+ case 0: return boost::convert_to<EnumClass>(EnumClass::Default);
+ case 1: return boost::convert_to<EnumClass>(EnumClass::Enum1);
+ case 2: return boost::convert_to<EnumClass>(EnumClass::Enum2);
+ default: throw "bad_parameter";
+ }
+ }
+ };
+ }
+}
+
+
+
 //! OSTRREAM overloading
 template <typename OSTREAM>
 inline OSTREAM& operator <<(OSTREAM& os, EnumClass v) {
- os << c_str(v);
+ os << int(boost::enums::get_value(v));
   return os;
 }
 

Modified: sandbox/enums/libs/enums/test/Jamfile.v2
==============================================================================
--- sandbox/enums/libs/enums/test/Jamfile.v2 (original)
+++ sandbox/enums/libs/enums/test/Jamfile.v2 2011-03-01 18:45:27 EST (Tue, 01 Mar 2011)
@@ -1,6 +1,6 @@
 # Boost Enums Library test Jamfile
 
-# Copyright Vicente Botet 2010-2011
+# Copyright Vicente Botet 2011
 
 # Distributed under the Boost Software License, Version 1.0.
 # See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt
@@ -41,12 +41,12 @@
    test-suite "examples"
         :
         [ compile-fail construction_from_int_fails.cpp ]
- [ compile-fail comparaision_fails.cpp ]
+ [ run comparaision.cpp ]
         [ compile-fail implicit_conversion_to_ut_fails.cpp ]
         [ compile-fail implicit_conversion_to_bool_fails.cpp ]
 
         [ compile has_same_size_as_ut.cpp ]
- [ run defaults_to_enum_default.cpp ]
+ #[ run defaults_to_enum_default.cpp ]
         [ run constructible_from_enum.cpp ]
         [ run equal_comparable.cpp ]
         [ run not_equal_comparable.cpp ]

Added: sandbox/enums/libs/enums/test/comparaision.cpp
==============================================================================
--- (empty file)
+++ sandbox/enums/libs/enums/test/comparaision.cpp 2011-03-01 18:45:27 EST (Tue, 01 Mar 2011)
@@ -0,0 +1,31 @@
+//////////////////////////////////////////////////////////////////////////////
+//
+// (C) Copyright Vicente J. Botet Escriba 2011.
+// Distributed under 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/libs/enums for documentation.
+//
+//////////////////////////////////////////////////////////////////////////////
+
+#include "./EnumClass.hpp"
+#include <boost/detail/lightweight_test.hpp>
+
+int main() {
+
+ { // The wrapper can be compared
+ EnumClass e0;
+ e0= EnumClass::Default;
+ BOOST_TEST(e0 < EnumClass::Enum2);
+ }
+ { // The wrapper can be compared
+ EnumClass e0;
+ e0= EnumClass::Default;
+ EnumClass e1;
+ e1= EnumClass::Enum2;
+ BOOST_TEST(e0 < e1);
+ }
+ return boost::report_errors();
+}

Deleted: sandbox/enums/libs/enums/test/comparaision_fails.cpp
==============================================================================
--- sandbox/enums/libs/enums/test/comparaision_fails.cpp 2011-03-01 18:45:27 EST (Tue, 01 Mar 2011)
+++ (empty file)
@@ -1,30 +0,0 @@
-//////////////////////////////////////////////////////////////////////////////
-//
-// (C) Copyright Vicente J. Botet Escriba 2011.
-// Distributed under 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/libs/enums for documentation.
-//
-//////////////////////////////////////////////////////////////////////////////
-
-#include "./EnumClass.hpp"
-
-void fail() {
-
- { // The wrapper can be compared
- EnumClass e0;
- e0= EnumClass::Default;
- BOOST_TEST(e0 < EnumClass::Enum2);
- }
- { // The wrapper can be compared
- EnumClass e0;
- e0= EnumClass::Default;
- EnumClass e1;
- e1= EnumClass::Enum2;
- BOOST_TEST(e0 < e1);
- }
-
-}

Modified: sandbox/enums/libs/enums/test/defaults_to_enum_default.cpp
==============================================================================
--- sandbox/enums/libs/enums/test/defaults_to_enum_default.cpp (original)
+++ sandbox/enums/libs/enums/test/defaults_to_enum_default.cpp 2011-03-01 18:45:27 EST (Tue, 01 Mar 2011)
@@ -17,7 +17,8 @@
   using namespace boost::enums;
 
   { // defaults to the enum default
- EnumClass e =EnumClass() ;
+ BOOST_TEST(EnumClass::type()==EnumClass::Default);
+ EnumClass e = EnumClass() ;
     BOOST_TEST(e==EnumClass::Default);
   }
   { // defaults to the enum default
@@ -25,5 +26,5 @@
     BOOST_TEST(e==EnumClass::Default);
   }
   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