2 // Copyright Antony Polukhin, 2015-2016.
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
8 #include <boost/type_index/ctti_type_index.hpp>
10 #include <boost/functional/hash.hpp>
11 #include <boost/lexical_cast.hpp>
15 #include <boost/core/lightweight_test.hpp>
16 #define BOOST_TEST_LE(x, y) BOOST_TEST(x <= y)
17 #define BOOST_TEST_GE(x, y) BOOST_TEST(x >= y)
19 const char* hello1
= "Hello word";
20 const char* hello1_end
= hello1
+ sizeof("Hello word");
21 const char* hello2
= "Hello word, pal!";
22 const char* hello2_end
= hello2
+ sizeof("Hello word, pal!");
25 using boost::typeindex::detail::constexpr_strcmp
;
28 constexpr_strcmp(hello1
, hello1
) == 0
32 constexpr_strcmp(hello2
, hello2
) == 0
36 constexpr_strcmp(hello1
, hello2
) != 0
40 constexpr_strcmp(hello2
, hello1
) != 0
44 (constexpr_strcmp(hello2
, hello1
) < 0)
46 (std::strcmp(hello2
, hello1
) < 0)
50 (constexpr_strcmp(hello1
, hello2
) < 0)
52 (std::strcmp(hello1
, hello2
) < 0)
57 using boost::typeindex::detail::constexpr_search
;
59 constexpr_search(hello1
, hello1_end
, hello2
, hello2_end
) == std::search(hello1
, hello1_end
, hello2
, hello2_end
)
63 constexpr_search(hello2
, hello2_end
, hello1
, hello1_end
) == std::search(hello2
, hello2_end
, hello1
, hello1_end
)
66 const char* word
= "word";
67 const char* word_end
= word
+ sizeof("word") - 1;
69 constexpr_search(hello1
, hello1_end
, word
, word_end
) == std::search(hello1
, hello1_end
, word
, word_end
)
73 constexpr_search(hello2
, hello2_end
, word
, word_end
) == std::search(hello2
, hello2_end
, word
, word_end
)
77 template <class T
, std::size_t N
>
78 BOOST_CXX14_CONSTEXPR
bool in_namespace(const char (&ns
)[N
]) BOOST_NOEXCEPT
{
79 BOOST_CXX14_CONSTEXPR
const char* name
= boost::typeindex::ctti_type_index::type_id
<T
>().raw_name();
80 for (std::size_t i
= 0; i
< N
- 1; ++i
)
88 BOOST_CXX14_CONSTEXPR
bool is_boost_namespace() BOOST_NOEXCEPT
{
89 return in_namespace
<T
>("boost::") || in_namespace
<T
>("class boost::") || in_namespace
<T
>("struct boost::");
92 void constexpr_test() {
93 using namespace boost::typeindex
;
95 BOOST_CXX14_CONSTEXPR ctti_type_index t_int0
= ctti_type_index::type_id
<int>();
96 BOOST_CXX14_CONSTEXPR ctti_type_index t_short0
= ctti_type_index::type_id
<short>();
97 BOOST_CXX14_CONSTEXPR ctti_type_index t_int1
= ctti_type_index::type_id
<int>();
98 BOOST_CXX14_CONSTEXPR ctti_type_index t_short1
= ctti_type_index::type_id
<short>();
100 BOOST_CXX14_CONSTEXPR
bool same0
= (t_int0
== t_int1
);
103 BOOST_CXX14_CONSTEXPR
bool same1
= (t_short1
== t_short0
);
106 BOOST_CXX14_CONSTEXPR
bool same2
= (t_int1
== t_int1
);
109 BOOST_CXX14_CONSTEXPR
bool same3
= (t_short0
== t_short0
);
112 BOOST_CXX14_CONSTEXPR
bool same4
= !(t_short0
< t_short0
|| t_short0
> t_short0
);
115 BOOST_CXX14_CONSTEXPR
bool same5
= (t_short0
<= t_short0
&& t_short0
>= t_short0
);
119 BOOST_CXX14_CONSTEXPR
bool not_same0
= (t_int0
!= t_short1
);
120 BOOST_TEST(not_same0
);
122 BOOST_CXX14_CONSTEXPR
bool not_same1
= (t_int1
!= t_short0
);
123 BOOST_TEST(not_same1
);
125 BOOST_CXX14_CONSTEXPR
bool not_same2
= (t_int1
< t_short0
|| t_int1
> t_short0
);
126 BOOST_TEST(not_same2
);
129 BOOST_CXX14_CONSTEXPR
const char* int_name
= t_int0
.name();
130 BOOST_TEST(*int_name
!= '\0');
132 BOOST_CXX14_CONSTEXPR
const char* short_name
= t_short0
.name();
133 BOOST_TEST(*short_name
!= '\0');
135 BOOST_CXX14_CONSTEXPR
bool in_namespace
= is_boost_namespace
<ctti_type_index
>();
136 BOOST_TEST(in_namespace
);
138 BOOST_CXX14_CONSTEXPR
bool not_in_namespace
= !is_boost_namespace
<std::string
>();
139 BOOST_TEST(not_in_namespace
);
147 return boost::report_errors();