2 // Copyright Antony Polukhin, 2012-2015.
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.hpp>
10 #include <boost/functional/hash.hpp>
11 #include <boost/lexical_cast.hpp>
13 #include <boost/core/lightweight_test.hpp>
14 #define BOOST_TEST_LE(x, y) BOOST_TEST(x <= y)
15 #define BOOST_TEST_GE(x, y) BOOST_TEST(x >= y)
18 namespace my_namespace1
{
23 namespace my_namespace2
{
28 void names_matches_type_id()
30 using namespace boost::typeindex
;
31 BOOST_TEST_EQ(type_id
<int>().pretty_name(), "int");
32 BOOST_TEST_EQ(type_id
<double>().pretty_name(), "double");
34 BOOST_TEST_EQ(type_id
<int>().name(), type_id
<int>().name());
35 BOOST_TEST_NE(type_id
<int>().name(), type_id
<double>().name());
36 BOOST_TEST_NE(type_id
<double>().name(), type_id
<int>().name());
37 BOOST_TEST_EQ(type_id
<double>().name(), type_id
<double>().name());
40 void default_construction()
42 using namespace boost::typeindex
;
44 BOOST_TEST_EQ(ti1
, ti2
);
45 BOOST_TEST_EQ(type_id
<void>(), ti1
);
47 BOOST_TEST_EQ(type_id
<void>().name(), ti1
.name());
48 BOOST_TEST_NE(type_id
<int>(), ti1
);
52 void copy_construction()
54 using namespace boost::typeindex
;
55 type_index ti1
, ti2
= type_id
<int>();
56 BOOST_TEST_NE(ti1
, ti2
);
58 BOOST_TEST_EQ(ti2
, ti1
);
60 const type_index
ti3(ti1
);
61 BOOST_TEST_EQ(ti3
, ti1
);
64 void comparators_type_id()
66 using namespace boost::typeindex
;
67 type_index t_int
= type_id
<int>();
68 type_index t_double
= type_id
<double>();
70 BOOST_TEST_EQ(t_int
, t_int
);
71 BOOST_TEST_LE(t_int
, t_int
);
72 BOOST_TEST_GE(t_int
, t_int
);
73 BOOST_TEST_NE(t_int
, t_double
);
75 BOOST_TEST_LE(t_double
, t_double
);
76 BOOST_TEST_GE(t_double
, t_double
);
77 BOOST_TEST_NE(t_double
, t_int
);
79 BOOST_TEST(t_double
< t_int
|| t_int
< t_double
);
80 BOOST_TEST(t_double
> t_int
|| t_int
> t_double
);
83 void hash_code_type_id()
85 using namespace boost::typeindex
;
86 std::size_t t_int1
= type_id
<int>().hash_code();
87 std::size_t t_double1
= type_id
<double>().hash_code();
89 std::size_t t_int2
= type_id
<int>().hash_code();
90 std::size_t t_double2
= type_id
<double>().hash_code();
92 BOOST_TEST_EQ(t_int1
, t_int2
);
93 BOOST_TEST_NE(t_int1
, t_double2
);
94 BOOST_TEST_LE(t_double1
, t_double2
);
99 template <class T1
, class T2
>
100 static void test_with_modofiers() {
101 using namespace boost::typeindex
;
103 type_index t1
= type_id_with_cvr
<T1
>();
104 type_index t2
= type_id_with_cvr
<T2
>();
106 BOOST_TEST_NE(t2
, t1
);
107 BOOST_TEST(t2
!= t1
.type_info());
108 BOOST_TEST(t2
.type_info() != t1
);
110 BOOST_TEST(t1
< t2
|| t2
< t1
);
111 BOOST_TEST(t1
> t2
|| t2
> t1
);
112 BOOST_TEST(t1
.type_info() < t2
|| t2
.type_info() < t1
);
113 BOOST_TEST(t1
.type_info() > t2
|| t2
.type_info() > t1
);
114 BOOST_TEST(t1
< t2
.type_info() || t2
< t1
.type_info());
115 BOOST_TEST(t1
> t2
.type_info() || t2
> t1
.type_info());
117 // Chaecking that comparison operators overloads compile
118 BOOST_TEST(t1
<= t2
|| t2
<= t1
);
119 BOOST_TEST(t1
>= t2
|| t2
>= t1
);
120 BOOST_TEST(t1
.type_info() <= t2
|| t2
.type_info() <= t1
);
121 BOOST_TEST(t1
.type_info() >= t2
|| t2
.type_info() >= t1
);
122 BOOST_TEST(t1
<= t2
.type_info() || t2
<= t1
.type_info());
123 BOOST_TEST(t1
>= t2
.type_info() || t2
>= t1
.type_info());
125 BOOST_TEST_EQ(t1
, type_id_with_cvr
<T1
>());
126 BOOST_TEST_EQ(t2
, type_id_with_cvr
<T2
>());
127 BOOST_TEST(t1
== type_id_with_cvr
<T1
>().type_info());
128 BOOST_TEST(t2
== type_id_with_cvr
<T2
>().type_info());
129 BOOST_TEST(t1
.type_info() == type_id_with_cvr
<T1
>());
130 BOOST_TEST(t2
.type_info() == type_id_with_cvr
<T2
>());
132 BOOST_TEST_EQ(t1
.hash_code(), type_id_with_cvr
<T1
>().hash_code());
133 BOOST_TEST_EQ(t2
.hash_code(), type_id_with_cvr
<T2
>().hash_code());
135 BOOST_TEST_NE(t1
.hash_code(), type_id_with_cvr
<T2
>().hash_code());
136 BOOST_TEST_NE(t2
.hash_code(), type_id_with_cvr
<T1
>().hash_code());
139 void type_id_storing_modifiers()
141 test_with_modofiers
<int, const int>();
142 test_with_modofiers
<int, const int&>();
143 test_with_modofiers
<int, int&>();
144 test_with_modofiers
<int, volatile int>();
145 test_with_modofiers
<int, volatile int&>();
146 test_with_modofiers
<int, const volatile int>();
147 test_with_modofiers
<int, const volatile int&>();
149 test_with_modofiers
<const int, int>();
150 test_with_modofiers
<const int, const int&>();
151 test_with_modofiers
<const int, int&>();
152 test_with_modofiers
<const int, volatile int>();
153 test_with_modofiers
<const int, volatile int&>();
154 test_with_modofiers
<const int, const volatile int>();
155 test_with_modofiers
<const int, const volatile int&>();
157 test_with_modofiers
<const int&, int>();
158 test_with_modofiers
<const int&, const int>();
159 test_with_modofiers
<const int&, int&>();
160 test_with_modofiers
<const int&, volatile int>();
161 test_with_modofiers
<const int&, volatile int&>();
162 test_with_modofiers
<const int&, const volatile int>();
163 test_with_modofiers
<const int&, const volatile int&>();
165 test_with_modofiers
<int&, const int>();
166 test_with_modofiers
<int&, const int&>();
167 test_with_modofiers
<int&, int>();
168 test_with_modofiers
<int&, volatile int>();
169 test_with_modofiers
<int&, volatile int&>();
170 test_with_modofiers
<int&, const volatile int>();
171 test_with_modofiers
<int&, const volatile int&>();
173 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
174 test_with_modofiers
<int&&, const int>();
175 test_with_modofiers
<int&&, const int&>();
176 test_with_modofiers
<int&&, const int&&>();
177 test_with_modofiers
<int&&, int>();
178 test_with_modofiers
<int&&, volatile int>();
179 test_with_modofiers
<int&&, volatile int&>();
180 test_with_modofiers
<int&&, volatile int&&>();
181 test_with_modofiers
<int&&, const volatile int>();
182 test_with_modofiers
<int&&, const volatile int&>();
183 test_with_modofiers
<int&&, const volatile int&&>();
188 static void test_storing_nonstoring_modifiers_templ() {
189 using namespace boost::typeindex
;
191 type_index t1
= type_id_with_cvr
<T
>();
192 type_index t2
= type_id
<T
>();
194 BOOST_TEST_EQ(t2
, t1
);
195 BOOST_TEST_EQ(t1
, t2
);
196 BOOST_TEST(t1
<= t2
);
197 BOOST_TEST(t1
>= t2
);
198 BOOST_TEST(t2
<= t1
);
199 BOOST_TEST(t2
>= t1
);
201 BOOST_TEST_EQ(t2
.pretty_name(), t1
.pretty_name());
204 void type_id_storing_modifiers_vs_nonstoring()
206 test_storing_nonstoring_modifiers_templ
<int>();
207 test_storing_nonstoring_modifiers_templ
<my_namespace1::my_class
>();
208 test_storing_nonstoring_modifiers_templ
<my_namespace2::my_class
>();
210 boost::typeindex::type_index t1
= boost::typeindex::type_id_with_cvr
<const int>();
211 boost::typeindex::type_index t2
= boost::typeindex::type_id
<int>();
212 BOOST_TEST_NE(t2
, t1
);
213 BOOST_TEST(t1
.pretty_name() == "const int" || t1
.pretty_name() == "int const");
216 void type_index_stream_operator_via_lexical_cast_testing()
218 using namespace boost::typeindex
;
220 std::string s_int2
= boost::lexical_cast
<std::string
>(type_id
<int>());
221 BOOST_TEST_EQ(s_int2
, "int");
223 std::string s_double2
= boost::lexical_cast
<std::string
>(type_id
<double>());
224 BOOST_TEST_EQ(s_double2
, "double");
227 void type_index_stripping_cvr_test()
229 using namespace boost::typeindex
;
231 BOOST_TEST_EQ(type_id
<int>(), type_id
<const int>());
232 BOOST_TEST_EQ(type_id
<int>(), type_id
<const volatile int>());
233 BOOST_TEST_EQ(type_id
<int>(), type_id
<const volatile int&>());
235 BOOST_TEST_EQ(type_id
<int>(), type_id
<int&>());
236 BOOST_TEST_EQ(type_id
<int>(), type_id
<volatile int>());
237 BOOST_TEST_EQ(type_id
<int>(), type_id
<volatile int&>());
240 BOOST_TEST_EQ(type_id
<double>(), type_id
<const double>());
241 BOOST_TEST_EQ(type_id
<double>(), type_id
<const volatile double>());
242 BOOST_TEST_EQ(type_id
<double>(), type_id
<const volatile double&>());
244 BOOST_TEST_EQ(type_id
<double>(), type_id
<double&>());
245 BOOST_TEST_EQ(type_id
<double>(), type_id
<volatile double>());
246 BOOST_TEST_EQ(type_id
<double>(), type_id
<volatile double&>());
250 void type_index_user_defined_class_test()
252 using namespace boost::typeindex
;
254 BOOST_TEST_EQ(type_id
<my_namespace1::my_class
>(), type_id
<my_namespace1::my_class
>());
255 BOOST_TEST_EQ(type_id
<my_namespace2::my_class
>(), type_id
<my_namespace2::my_class
>());
257 #ifndef BOOST_NO_RTTI
258 BOOST_TEST(type_id
<my_namespace1::my_class
>() == typeid(my_namespace1::my_class
));
259 BOOST_TEST(type_id
<my_namespace2::my_class
>() == typeid(my_namespace2::my_class
));
260 BOOST_TEST(typeid(my_namespace1::my_class
) == type_id
<my_namespace1::my_class
>());
261 BOOST_TEST(typeid(my_namespace2::my_class
) == type_id
<my_namespace2::my_class
>());
264 BOOST_TEST_NE(type_id
<my_namespace1::my_class
>(), type_id
<my_namespace2::my_class
>());
266 type_id
<my_namespace1::my_class
>().pretty_name().find("my_namespace1::my_class"),
276 BOOST_TYPE_INDEX_REGISTER_CLASS
281 BOOST_TYPE_INDEX_REGISTER_CLASS
285 BOOST_TYPE_INDEX_REGISTER_CLASS
288 void comparators_type_id_runtime()
297 #ifndef BOOST_NO_RTTI
298 BOOST_TEST(typeid(rc1
) == typeid(*pc1
));
299 BOOST_TEST(typeid(rb1
) == typeid(*pb1
));
301 BOOST_TEST(typeid(rc1
) != typeid(*pb1
));
302 BOOST_TEST(typeid(rb1
) != typeid(*pc1
));
304 BOOST_TEST(typeid(&rc1
) == typeid(pb1
));
305 BOOST_TEST(typeid(&rb1
) == typeid(pc1
));
307 BOOST_TEST(boost::typeindex::type_index(pc1
->boost_type_index_type_id_runtime_()).raw_name());
310 BOOST_TEST_EQ(boost::typeindex::type_id_runtime(rc1
), boost::typeindex::type_id_runtime(*pc1
));
311 BOOST_TEST_EQ(boost::typeindex::type_id
<C
>(), boost::typeindex::type_id_runtime(*pc1
));
312 BOOST_TEST_EQ(boost::typeindex::type_id_runtime(rb1
), boost::typeindex::type_id_runtime(*pb1
));
313 BOOST_TEST_EQ(boost::typeindex::type_id
<B
>(), boost::typeindex::type_id_runtime(*pb1
));
315 BOOST_TEST_NE(boost::typeindex::type_id_runtime(rc1
), boost::typeindex::type_id_runtime(*pb1
));
316 BOOST_TEST_NE(boost::typeindex::type_id_runtime(rb1
), boost::typeindex::type_id_runtime(*pc1
));
318 #ifndef BOOST_NO_RTTI
319 BOOST_TEST_EQ(boost::typeindex::type_id_runtime(&rc1
), boost::typeindex::type_id_runtime(pb1
));
320 BOOST_TEST_EQ(boost::typeindex::type_id_runtime(&rb1
), boost::typeindex::type_id_runtime(pc1
));
322 BOOST_TEST(boost::typeindex::type_id_runtime(rc1
) == typeid(*pc1
));
323 BOOST_TEST(boost::typeindex::type_id_runtime(rb1
) == typeid(*pb1
));
325 BOOST_TEST(boost::typeindex::type_id_runtime(rc1
) != typeid(*pb1
));
326 BOOST_TEST(boost::typeindex::type_id_runtime(rb1
) != typeid(*pc1
));
327 BOOST_TEST(boost::typeindex::type_id_runtime(&rc1
) == typeid(pb1
));
328 BOOST_TEST(boost::typeindex::type_id_runtime(&rb1
) == typeid(pc1
));
333 #ifndef BOOST_NO_RTTI
335 void comparators_type_id_vs_type_info()
337 using namespace boost::typeindex
;
338 type_index t_int
= type_id
<int>();
340 BOOST_TEST(t_int
== typeid(int));
341 BOOST_TEST(typeid(int) == t_int
);
342 BOOST_TEST(t_int
<= typeid(int));
343 BOOST_TEST(typeid(int) <= t_int
);
344 BOOST_TEST(t_int
>= typeid(int));
345 BOOST_TEST(typeid(int) >= t_int
);
347 type_index t_double
= type_id
<double>();
349 BOOST_TEST(t_double
== typeid(double));
350 BOOST_TEST(typeid(double) == t_double
);
351 BOOST_TEST(t_double
<= typeid(double));
352 BOOST_TEST(typeid(double) <= t_double
);
353 BOOST_TEST(t_double
>= typeid(double));
354 BOOST_TEST(typeid(double) >= t_double
);
356 if (t_double
< t_int
) {
357 BOOST_TEST(t_double
< typeid(int));
358 BOOST_TEST(typeid(double) < t_int
);
359 BOOST_TEST(typeid(int) > t_double
);
360 BOOST_TEST(t_int
> typeid(double));
363 BOOST_TEST(t_double
<= typeid(int));
364 BOOST_TEST(typeid(double) <= t_int
);
365 BOOST_TEST(typeid(int) >= t_double
);
366 BOOST_TEST(t_int
>= typeid(double));
368 BOOST_TEST(t_double
> typeid(int));
369 BOOST_TEST(typeid(double) > t_int
);
370 BOOST_TEST(typeid(int) < t_double
);
371 BOOST_TEST(t_int
< typeid(double));
374 BOOST_TEST(t_double
>= typeid(int));
375 BOOST_TEST(typeid(double) >= t_int
);
376 BOOST_TEST(typeid(int) <= t_double
);
377 BOOST_TEST(t_int
<= typeid(double));
382 #endif // BOOST_NO_RTTI
385 names_matches_type_id();
386 default_construction();
388 comparators_type_id();
391 type_id_storing_modifiers();
392 type_id_storing_modifiers_vs_nonstoring();
393 type_index_stream_operator_via_lexical_cast_testing();
394 type_index_stripping_cvr_test();
395 type_index_user_defined_class_test();
397 comparators_type_id_runtime();
398 #ifndef BOOST_NO_RTTI
399 comparators_type_id_vs_type_info();
402 return boost::report_errors();