polymorphic_cast_test.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. //
  2. // Test boost::polymorphic_cast, boost::polymorphic_downcast and
  3. // boost::polymorphic_pointer_cast, boost::polymorphic_pointer_downcast
  4. //
  5. // Copyright 1999 Beman Dawes
  6. // Copyright 1999 Dave Abrahams
  7. // Copyright 2014 Peter Dimov
  8. // Copyright 2014 Boris Rasin, Antony Polukhin
  9. //
  10. // Distributed under the Boost Software License, Version 1.0.
  11. //
  12. // See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt
  13. //
  14. #define BOOST_ENABLE_ASSERT_HANDLER
  15. #include <boost/polymorphic_cast.hpp>
  16. #include <boost/polymorphic_pointer_cast.hpp>
  17. #include <boost/smart_ptr/shared_ptr.hpp>
  18. #include <boost/smart_ptr/intrusive_ptr.hpp>
  19. #include <boost/smart_ptr/intrusive_ref_counter.hpp>
  20. #include <boost/core/lightweight_test.hpp>
  21. #include <string>
  22. #include <memory>
  23. static bool expect_assertion = false;
  24. static int assertion_failed_count = 0;
  25. //assertion handler throws it to exit like assert, but to be able to catch it and stop
  26. //usage: BOOST_TEST_THROWS( function_with_assert(), expected_assertion );
  27. struct expected_assertion {};
  28. // BOOST_ASSERT custom handler
  29. void boost::assertion_failed( char const * expr, char const * function, char const * file, long line )
  30. {
  31. if( expect_assertion )
  32. {
  33. ++assertion_failed_count;
  34. throw expected_assertion();
  35. }
  36. else
  37. {
  38. BOOST_ERROR( "unexpected assertion" );
  39. BOOST_LIGHTWEIGHT_TEST_OSTREAM
  40. << file << "(" << line << "): assertion '" << expr << "' failed in function '"
  41. << function << "'" << std::endl;
  42. }
  43. }
  44. //
  45. struct Base : boost::intrusive_ref_counter<Base>
  46. {
  47. virtual ~Base() {}
  48. virtual std::string kind() { return "Base"; }
  49. };
  50. struct Base2
  51. {
  52. virtual ~Base2() {}
  53. virtual std::string kind2() { return "Base2"; }
  54. };
  55. struct Derived : public Base, Base2
  56. {
  57. virtual std::string kind() { return "Derived"; }
  58. };
  59. static void test_polymorphic_cast()
  60. {
  61. Base * base = new Derived;
  62. Derived * derived;
  63. try
  64. {
  65. derived = boost::polymorphic_cast<Derived*>( base );
  66. BOOST_TEST( derived != 0 );
  67. if( derived != 0 )
  68. {
  69. BOOST_TEST_EQ( derived->kind(), "Derived" );
  70. }
  71. }
  72. catch( std::bad_cast const& )
  73. {
  74. BOOST_ERROR( "boost::polymorphic_cast<Derived*>( base ) threw std::bad_cast" );
  75. }
  76. Base2 * base2;
  77. try
  78. {
  79. base2 = boost::polymorphic_cast<Base2*>( base ); // crosscast
  80. BOOST_TEST( base2 != 0 );
  81. if( base2 != 0 )
  82. {
  83. BOOST_TEST_EQ( base2->kind2(), "Base2" );
  84. }
  85. }
  86. catch( std::bad_cast const& )
  87. {
  88. BOOST_ERROR( "boost::polymorphic_cast<Base2*>( base ) threw std::bad_cast" );
  89. }
  90. delete base;
  91. }
  92. static void test_polymorphic_pointer_cast()
  93. {
  94. Base * base = new Derived;
  95. Derived * derived;
  96. try
  97. {
  98. derived = boost::polymorphic_pointer_cast<Derived>( base );
  99. BOOST_TEST( derived != 0 );
  100. if( derived != 0 )
  101. {
  102. BOOST_TEST_EQ( derived->kind(), "Derived" );
  103. }
  104. }
  105. catch( std::bad_cast const& )
  106. {
  107. BOOST_ERROR( "boost::polymorphic_pointer_cast<Derived>( base ) threw std::bad_cast" );
  108. }
  109. Base2 * base2;
  110. try
  111. {
  112. base2 = boost::polymorphic_pointer_cast<Base2>( base ); // crosscast
  113. BOOST_TEST( base2 != 0 );
  114. if( base2 != 0 )
  115. {
  116. BOOST_TEST_EQ( base2->kind2(), "Base2" );
  117. }
  118. }
  119. catch( std::bad_cast const& )
  120. {
  121. BOOST_ERROR( "boost::polymorphic_pointer_cast<Base2>( base ) threw std::bad_cast" );
  122. }
  123. boost::shared_ptr<Base> sp_base( base );
  124. boost::shared_ptr<Base2> sp_base2;
  125. try
  126. {
  127. sp_base2 = boost::polymorphic_pointer_cast<Base2>( sp_base ); // crosscast
  128. BOOST_TEST( sp_base2 != 0 );
  129. if( sp_base2 != 0 )
  130. {
  131. BOOST_TEST_EQ( sp_base2->kind2(), "Base2" );
  132. }
  133. }
  134. catch( std::bad_cast const& )
  135. {
  136. BOOST_ERROR( "boost::polymorphic_pointer_cast<Base2>( sp_base ) threw std::bad_cast" );
  137. }
  138. // we do not `delete base;` because sahred_ptr is holding base
  139. }
  140. static void test_polymorphic_downcast()
  141. {
  142. Base * base = new Derived;
  143. Derived * derived = boost::polymorphic_downcast<Derived*>( base );
  144. BOOST_TEST( derived != 0 );
  145. if( derived != 0 )
  146. {
  147. BOOST_TEST_EQ( derived->kind(), "Derived" );
  148. }
  149. // polymorphic_downcast can't do crosscasts
  150. delete base;
  151. }
  152. static void test_polymorphic_pointer_downcast_builtin()
  153. {
  154. Base * base = new Derived;
  155. Derived * derived = boost::polymorphic_pointer_downcast<Derived>( base );
  156. BOOST_TEST( derived != 0 );
  157. if( derived != 0 )
  158. {
  159. BOOST_TEST_EQ( derived->kind(), "Derived" );
  160. }
  161. // polymorphic_pointer_downcast can't do crosscasts
  162. delete base;
  163. }
  164. static void test_polymorphic_pointer_downcast_boost_shared()
  165. {
  166. boost::shared_ptr<Base> base (new Derived);
  167. boost::shared_ptr<Derived> derived = boost::polymorphic_pointer_downcast<Derived>( base );
  168. BOOST_TEST( derived != 0 );
  169. if( derived != 0 )
  170. {
  171. BOOST_TEST_EQ( derived->kind(), "Derived" );
  172. }
  173. }
  174. static void test_polymorphic_pointer_downcast_intrusive()
  175. {
  176. boost::intrusive_ptr<Base> base (new Derived);
  177. boost::intrusive_ptr<Derived> derived = boost::polymorphic_pointer_downcast<Derived>( base );
  178. BOOST_TEST( derived != 0 );
  179. if( derived != 0 )
  180. {
  181. BOOST_TEST_EQ( derived->kind(), "Derived" );
  182. }
  183. }
  184. #ifndef BOOST_NO_CXX11_SMART_PTR
  185. static void test_polymorphic_pointer_downcast_std_shared()
  186. {
  187. std::shared_ptr<Base> base (new Derived);
  188. std::shared_ptr<Derived> derived = boost::polymorphic_pointer_downcast<Derived>( base );
  189. BOOST_TEST( derived != 0 );
  190. if( derived != 0 )
  191. {
  192. BOOST_TEST_EQ( derived->kind(), "Derived" );
  193. }
  194. }
  195. #endif
  196. static void test_polymorphic_cast_fail()
  197. {
  198. Base * base = new Base;
  199. BOOST_TEST_THROWS( boost::polymorphic_cast<Derived*>( base ), std::bad_cast );
  200. delete base;
  201. }
  202. static void test_polymorphic_pointer_cast_fail()
  203. {
  204. Base * base = new Base;
  205. BOOST_TEST_THROWS( boost::polymorphic_pointer_cast<Derived>( base ), std::bad_cast );
  206. delete base;
  207. BOOST_TEST_THROWS( boost::polymorphic_pointer_cast<Derived>( boost::shared_ptr<Base>(new Base) ), std::bad_cast );
  208. #ifndef BOOST_NO_CXX11_SMART_PTR
  209. BOOST_TEST_THROWS( boost::polymorphic_pointer_cast<Derived>( std::shared_ptr<Base>(new Base) ), std::bad_cast );
  210. #endif
  211. BOOST_TEST_THROWS( boost::polymorphic_pointer_cast<Derived>( boost::intrusive_ptr<Base>(new Base) ), std::bad_cast );
  212. }
  213. static void test_polymorphic_downcast_fail()
  214. {
  215. Base * base = new Base;
  216. int old_count = assertion_failed_count;
  217. expect_assertion = true;
  218. BOOST_TEST_THROWS( boost::polymorphic_downcast<Derived*>( base ), expected_assertion ); // should assert
  219. BOOST_TEST_EQ( assertion_failed_count, old_count + 1 );
  220. expect_assertion = false;
  221. delete base;
  222. }
  223. static void test_polymorphic_pointer_downcast_builtin_fail()
  224. {
  225. Base * base = new Base;
  226. int old_count = assertion_failed_count;
  227. expect_assertion = true;
  228. BOOST_TEST_THROWS( boost::polymorphic_pointer_downcast<Derived>( base ), expected_assertion ); // should assert
  229. BOOST_TEST_EQ( assertion_failed_count, old_count + 1 );
  230. expect_assertion = false;
  231. delete base;
  232. }
  233. static void test_polymorphic_pointer_downcast_boost_shared_fail()
  234. {
  235. boost::shared_ptr<Base> base (new Base);
  236. int old_count = assertion_failed_count;
  237. expect_assertion = true;
  238. BOOST_TEST_THROWS( boost::polymorphic_pointer_downcast<Derived>( base ), expected_assertion ); // should assert
  239. BOOST_TEST_EQ( assertion_failed_count, old_count + 1 );
  240. expect_assertion = false;
  241. }
  242. #ifndef BOOST_NO_CXX11_SMART_PTR
  243. static void test_polymorphic_pointer_downcast_std_shared_fail()
  244. {
  245. std::shared_ptr<Base> base (new Base);
  246. int old_count = assertion_failed_count;
  247. expect_assertion = true;
  248. BOOST_TEST_THROWS( boost::polymorphic_pointer_downcast<Derived>( base ), expected_assertion ); // should assert
  249. BOOST_TEST_EQ( assertion_failed_count, old_count + 1 );
  250. expect_assertion = false;
  251. }
  252. #endif
  253. static void test_polymorphic_pointer_downcast_intrusive_fail()
  254. {
  255. boost::intrusive_ptr<Base> base (new Base);
  256. int old_count = assertion_failed_count;
  257. expect_assertion = true;
  258. BOOST_TEST_THROWS( boost::polymorphic_pointer_downcast<Derived>( base ), expected_assertion); // should assert
  259. BOOST_TEST_EQ( assertion_failed_count, old_count + 1 );
  260. expect_assertion = false;
  261. }
  262. int main()
  263. {
  264. test_polymorphic_cast();
  265. test_polymorphic_pointer_cast();
  266. test_polymorphic_downcast();
  267. test_polymorphic_pointer_downcast_builtin();
  268. test_polymorphic_pointer_downcast_boost_shared();
  269. test_polymorphic_pointer_downcast_intrusive();
  270. test_polymorphic_cast_fail();
  271. test_polymorphic_pointer_cast_fail();
  272. test_polymorphic_downcast_fail();
  273. test_polymorphic_pointer_downcast_builtin_fail();
  274. test_polymorphic_pointer_downcast_boost_shared_fail();
  275. test_polymorphic_pointer_downcast_intrusive_fail();
  276. #ifndef BOOST_NO_CXX11_SMART_PTR
  277. test_polymorphic_pointer_downcast_std_shared();
  278. test_polymorphic_pointer_downcast_std_shared_fail();
  279. #endif
  280. return boost::report_errors();
  281. }