]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/type_index/test/type_index_constexpr_test.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / type_index / test / type_index_constexpr_test.cpp
1 //
2 // Copyright Antony Polukhin, 2015-2016.
3 //
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)
7
8 #include <boost/type_index/ctti_type_index.hpp>
9
10 #include <boost/functional/hash.hpp>
11 #include <boost/lexical_cast.hpp>
12 #include <algorithm>
13 #include <string>
14
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)
18
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!");
23
24 void strcmp_same() {
25 using boost::typeindex::detail::constexpr_strcmp;
26
27 BOOST_TEST(
28 constexpr_strcmp(hello1, hello1) == 0
29 );
30
31 BOOST_TEST(
32 constexpr_strcmp(hello2, hello2) == 0
33 );
34
35 BOOST_TEST(
36 constexpr_strcmp(hello1, hello2) != 0
37 );
38
39 BOOST_TEST(
40 constexpr_strcmp(hello2, hello1) != 0
41 );
42
43 BOOST_TEST(
44 (constexpr_strcmp(hello2, hello1) < 0)
45 ==
46 (std::strcmp(hello2, hello1) < 0)
47 );
48
49 BOOST_TEST(
50 (constexpr_strcmp(hello1, hello2) < 0)
51 ==
52 (std::strcmp(hello1, hello2) < 0)
53 );
54 }
55
56 void search_same() {
57 using boost::typeindex::detail::constexpr_search;
58 BOOST_TEST(
59 constexpr_search(hello1, hello1_end, hello2, hello2_end) == std::search(hello1, hello1_end, hello2, hello2_end)
60 );
61
62 BOOST_TEST(
63 constexpr_search(hello2, hello2_end, hello1, hello1_end) == std::search(hello2, hello2_end, hello1, hello1_end)
64 );
65
66 const char* word = "word";
67 const char* word_end = word + sizeof("word") - 1;
68 BOOST_TEST(
69 constexpr_search(hello1, hello1_end, word, word_end) == std::search(hello1, hello1_end, word, word_end)
70 );
71
72 BOOST_TEST(
73 constexpr_search(hello2, hello2_end, word, word_end) == std::search(hello2, hello2_end, word, word_end)
74 );
75 }
76
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)
81 if (name[i] != ns[i])
82 return false;
83
84 return true;
85 }
86
87 template <class T>
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::");
90 }
91
92 void constexpr_test() {
93 using namespace boost::typeindex;
94
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>();
99
100 BOOST_CXX14_CONSTEXPR bool same0 = (t_int0 == t_int1);
101 BOOST_TEST(same0);
102
103 BOOST_CXX14_CONSTEXPR bool same1 = (t_short1 == t_short0);
104 BOOST_TEST(same1);
105
106 BOOST_CXX14_CONSTEXPR bool same2 = (t_int1 == t_int1);
107 BOOST_TEST(same2);
108
109 BOOST_CXX14_CONSTEXPR bool same3 = (t_short0 == t_short0);
110 BOOST_TEST(same3);
111
112 BOOST_CXX14_CONSTEXPR bool same4 = !(t_short0 < t_short0 || t_short0 > t_short0);
113 BOOST_TEST(same4);
114
115 BOOST_CXX14_CONSTEXPR bool same5 = (t_short0 <= t_short0 && t_short0 >= t_short0);
116 BOOST_TEST(same5);
117
118
119 BOOST_CXX14_CONSTEXPR bool not_same0 = (t_int0 != t_short1);
120 BOOST_TEST(not_same0);
121
122 BOOST_CXX14_CONSTEXPR bool not_same1 = (t_int1 != t_short0);
123 BOOST_TEST(not_same1);
124
125 BOOST_CXX14_CONSTEXPR bool not_same2 = (t_int1 < t_short0 || t_int1 > t_short0);
126 BOOST_TEST(not_same2);
127
128
129 BOOST_CXX14_CONSTEXPR const char* int_name = t_int0.name();
130 BOOST_TEST(*int_name != '\0');
131
132 BOOST_CXX14_CONSTEXPR const char* short_name = t_short0.name();
133 BOOST_TEST(*short_name != '\0');
134
135 BOOST_CXX14_CONSTEXPR bool in_namespace = is_boost_namespace<ctti_type_index>();
136 BOOST_TEST(in_namespace);
137
138 BOOST_CXX14_CONSTEXPR bool not_in_namespace = !is_boost_namespace<std::string>();
139 BOOST_TEST(not_in_namespace);
140 }
141
142
143 int main() {
144 strcmp_same();
145 search_same();
146 constexpr_test();
147 return boost::report_errors();
148 }
149