2 * Copyright Andrey Semashev 2014.
3 * Distributed under the Boost Software License, Version 1.0.
4 * (See accompanying file LICENSE_1_0.txt or copy at
5 * http://www.boost.org/LICENSE_1_0.txt)
8 * \file scoped_enum.cpp
9 * \author Andrey Semashev
12 * \brief This test checks that scoped enum emulation works similar to C++11 scoped enums.
15 #include <boost/core/scoped_enum.hpp>
16 #include <boost/core/lightweight_test.hpp>
18 BOOST_SCOPED_ENUM_DECLARE_BEGIN(namespace_enum1)
24 BOOST_SCOPED_ENUM_DECLARE_END(namespace_enum1)
26 BOOST_SCOPED_ENUM_UT_DECLARE_BEGIN(namespace_enum2, unsigned char)
28 // Checks that enum value names do not clash
33 BOOST_SCOPED_ENUM_DECLARE_END(namespace_enum2)
37 // Checks that declarations are valid in class scope
38 BOOST_SCOPED_ENUM_DECLARE_BEGIN(color)
44 BOOST_SCOPED_ENUM_DECLARE_END(color)
48 explicit my_struct(color col) : m_color(col)
52 color get_color() const
58 void check_operators()
60 namespace_enum1 enum1 = namespace_enum1::value0;
61 BOOST_TEST(enum1 == namespace_enum1::value0);
62 BOOST_TEST(enum1 != namespace_enum1::value1);
63 BOOST_TEST(enum1 != namespace_enum1::value2);
65 enum1 = namespace_enum1::value1;
66 BOOST_TEST(enum1 != namespace_enum1::value0);
67 BOOST_TEST(enum1 == namespace_enum1::value1);
68 BOOST_TEST(enum1 != namespace_enum1::value2);
70 BOOST_TEST(!(enum1 < namespace_enum1::value0));
71 BOOST_TEST(!(enum1 <= namespace_enum1::value0));
72 BOOST_TEST(enum1 >= namespace_enum1::value0);
73 BOOST_TEST(enum1 > namespace_enum1::value0);
75 BOOST_TEST(!(enum1 < namespace_enum1::value1));
76 BOOST_TEST(enum1 <= namespace_enum1::value1);
77 BOOST_TEST(enum1 >= namespace_enum1::value1);
78 BOOST_TEST(!(enum1 > namespace_enum1::value1));
80 namespace_enum1 enum2 = namespace_enum1::value0;
81 BOOST_TEST(enum1 != enum2);
84 BOOST_TEST(enum1 == enum2);
87 void check_argument_passing()
89 my_struct str(my_struct::color::green);
90 BOOST_TEST(str.get_color() == my_struct::color::green);
93 void check_switch_case()
95 my_struct str(my_struct::color::blue);
97 switch (boost::native_value(str.get_color()))
99 case my_struct::color::blue:
102 BOOST_ERROR("Unexpected color value in switch/case");
106 template< typename T >
109 enum _ { value = 0 };
113 struct my_trait< BOOST_SCOPED_ENUM_NATIVE(namespace_enum2) >
115 enum _ { value = 1 };
118 template< typename T >
119 void native_type_helper(T)
121 BOOST_TEST(my_trait< T >::value != 0);
124 void check_native_type()
126 BOOST_TEST(my_trait< int >::value == 0);
127 BOOST_TEST(my_trait< BOOST_SCOPED_ENUM_NATIVE(namespace_enum2) >::value != 0);
128 BOOST_TEST(my_trait< boost::native_type< namespace_enum2 >::type >::value != 0);
130 namespace_enum2 enum1 = namespace_enum2::value0;
131 native_type_helper(boost::native_value(enum1));
134 void check_underlying_cast()
136 namespace_enum2 enum1 = namespace_enum2::value1;
137 BOOST_TEST(boost::underlying_cast< unsigned char >(enum1) == 20);
140 void check_underlying_type()
142 // The real check for the type is in the underlying_type trait test.
143 namespace_enum2 enum1 = namespace_enum2::value1;
144 BOOST_TEST(sizeof(enum1) == sizeof(unsigned char));
147 int main(int, char*[])
150 check_argument_passing();
153 check_underlying_cast();
154 check_underlying_type();
156 return boost::report_errors();