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