any_test_rv.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. // Unit test for boost::any.
  2. //
  3. // See http://www.boost.org for most recent version, including documentation.
  4. //
  5. // Copyright Antony Polukhin, 2013-2019.
  6. //
  7. // Distributed under the Boost
  8. // Software License, Version 1.0. (See accompanying file
  9. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt).
  10. #include <cstdlib>
  11. #include <string>
  12. #include <utility>
  13. #include <boost/any.hpp>
  14. #include "test.hpp"
  15. #include <boost/move/move.hpp>
  16. #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
  17. int main()
  18. {
  19. return EXIT_SUCCESS;
  20. }
  21. #else
  22. namespace any_tests
  23. {
  24. typedef test<const char *, void (*)()> test_case;
  25. typedef const test_case * test_case_iterator;
  26. extern const test_case_iterator begin, end;
  27. }
  28. int main()
  29. {
  30. using namespace any_tests;
  31. tester<test_case_iterator> test_suite(begin, end);
  32. return test_suite() ? EXIT_SUCCESS : EXIT_FAILURE;
  33. }
  34. namespace any_tests // test suite
  35. {
  36. void test_move_construction();
  37. void test_move_assignment();
  38. void test_copy_construction();
  39. void test_copy_assignment();
  40. void test_move_construction_from_value();
  41. void test_move_assignment_from_value();
  42. void test_copy_construction_from_value();
  43. void test_copy_assignment_from_value();
  44. void test_construction_from_const_any_rv();
  45. void test_cast_to_rv();
  46. const test_case test_cases[] =
  47. {
  48. { "move construction of any", test_move_construction },
  49. { "move assignment of any", test_move_assignment },
  50. { "copy construction of any", test_copy_construction },
  51. { "copy assignment of any", test_copy_assignment },
  52. { "move construction from value", test_move_construction_from_value },
  53. { "move assignment from value", test_move_assignment_from_value },
  54. { "copy construction from value", test_copy_construction_from_value },
  55. { "copy assignment from value", test_copy_assignment_from_value },
  56. { "constructing from const any&&", test_construction_from_const_any_rv },
  57. { "casting to rvalue reference", test_cast_to_rv }
  58. };
  59. const test_case_iterator begin = test_cases;
  60. const test_case_iterator end =
  61. test_cases + (sizeof test_cases / sizeof *test_cases);
  62. class move_copy_conting_class {
  63. public:
  64. static unsigned int moves_count;
  65. static unsigned int copy_count;
  66. move_copy_conting_class(){}
  67. move_copy_conting_class(move_copy_conting_class&& /*param*/) {
  68. ++ moves_count;
  69. }
  70. move_copy_conting_class& operator=(move_copy_conting_class&& /*param*/) {
  71. ++ moves_count;
  72. return *this;
  73. }
  74. move_copy_conting_class(const move_copy_conting_class&) {
  75. ++ copy_count;
  76. }
  77. move_copy_conting_class& operator=(const move_copy_conting_class& /*param*/) {
  78. ++ copy_count;
  79. return *this;
  80. }
  81. };
  82. unsigned int move_copy_conting_class::moves_count = 0;
  83. unsigned int move_copy_conting_class::copy_count = 0;
  84. }
  85. namespace any_tests // test definitions
  86. {
  87. using namespace boost;
  88. void test_move_construction()
  89. {
  90. any value0 = move_copy_conting_class();
  91. move_copy_conting_class::copy_count = 0;
  92. move_copy_conting_class::moves_count = 0;
  93. any value(boost::move(value0));
  94. check(value0.empty(), "moved away value is empty");
  95. check_false(value.empty(), "empty");
  96. check_equal(value.type(), typeindex::type_id<move_copy_conting_class>(), "type");
  97. check_non_null(any_cast<move_copy_conting_class>(&value), "any_cast<move_copy_conting_class>");
  98. check_equal(
  99. move_copy_conting_class::copy_count, 0u,
  100. "checking copy counts");
  101. check_equal(
  102. move_copy_conting_class::moves_count, 0u,
  103. "checking move counts");
  104. }
  105. void test_move_assignment()
  106. {
  107. any value0 = move_copy_conting_class();
  108. any value = move_copy_conting_class();
  109. move_copy_conting_class::copy_count = 0;
  110. move_copy_conting_class::moves_count = 0;
  111. value = boost::move(value0);
  112. check(value0.empty(), "moved away is empty");
  113. check_false(value.empty(), "empty");
  114. check_equal(value.type(), typeindex::type_id<move_copy_conting_class>(), "type");
  115. check_non_null(any_cast<move_copy_conting_class>(&value), "any_cast<move_copy_conting_class>");
  116. check_equal(
  117. move_copy_conting_class::copy_count, 0u,
  118. "checking copy counts");
  119. check_equal(
  120. move_copy_conting_class::moves_count, 0u,
  121. "checking move counts");
  122. }
  123. void test_copy_construction()
  124. {
  125. any value0 = move_copy_conting_class();
  126. move_copy_conting_class::copy_count = 0;
  127. move_copy_conting_class::moves_count = 0;
  128. any value(value0);
  129. check_false(value0.empty(), "copied value is not empty");
  130. check_false(value.empty(), "empty");
  131. check_equal(value.type(), typeindex::type_id<move_copy_conting_class>(), "type");
  132. check_non_null(any_cast<move_copy_conting_class>(&value), "any_cast<move_copy_conting_class>");
  133. check_equal(
  134. move_copy_conting_class::copy_count, 1u,
  135. "checking copy counts");
  136. check_equal(
  137. move_copy_conting_class::moves_count, 0u,
  138. "checking move counts");
  139. }
  140. void test_copy_assignment()
  141. {
  142. any value0 = move_copy_conting_class();
  143. any value = move_copy_conting_class();
  144. move_copy_conting_class::copy_count = 0;
  145. move_copy_conting_class::moves_count = 0;
  146. value = value0;
  147. check_false(value0.empty(), "copied value is not empty");
  148. check_false(value.empty(), "empty");
  149. check_equal(value.type(), typeindex::type_id<move_copy_conting_class>(), "type");
  150. check_non_null(any_cast<move_copy_conting_class>(&value), "any_cast<move_copy_conting_class>");
  151. check_equal(
  152. move_copy_conting_class::copy_count, 1u,
  153. "checking copy counts");
  154. check_equal(
  155. move_copy_conting_class::moves_count, 0u,
  156. "checking move counts");
  157. }
  158. void test_move_construction_from_value()
  159. {
  160. move_copy_conting_class value0;
  161. move_copy_conting_class::copy_count = 0;
  162. move_copy_conting_class::moves_count = 0;
  163. #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
  164. any value(boost::move(value0));
  165. #else
  166. any value(value0);
  167. #endif
  168. check_false(value.empty(), "empty");
  169. check_equal(value.type(), typeindex::type_id<move_copy_conting_class>(), "type");
  170. check_non_null(any_cast<move_copy_conting_class>(&value), "any_cast<move_copy_conting_class>");
  171. #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
  172. check_equal(
  173. move_copy_conting_class::copy_count, 0u,
  174. "checking copy counts");
  175. check_equal(
  176. move_copy_conting_class::moves_count, 1u,
  177. "checking move counts");
  178. #endif
  179. }
  180. void test_move_assignment_from_value()
  181. {
  182. move_copy_conting_class value0;
  183. any value;
  184. move_copy_conting_class::copy_count = 0;
  185. move_copy_conting_class::moves_count = 0;
  186. #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
  187. value = boost::move(value0);
  188. #else
  189. value = value0;
  190. #endif
  191. check_false(value.empty(), "empty");
  192. check_equal(value.type(), typeindex::type_id<move_copy_conting_class>(), "type");
  193. check_non_null(any_cast<move_copy_conting_class>(&value), "any_cast<move_copy_conting_class>");
  194. #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
  195. check_equal(
  196. move_copy_conting_class::copy_count, 0u,
  197. "checking copy counts");
  198. check_equal(
  199. move_copy_conting_class::moves_count, 1u,
  200. "checking move counts");
  201. #endif
  202. }
  203. void test_copy_construction_from_value()
  204. {
  205. move_copy_conting_class value0;
  206. move_copy_conting_class::copy_count = 0;
  207. move_copy_conting_class::moves_count = 0;
  208. any value(value0);
  209. check_false(value.empty(), "empty");
  210. check_equal(value.type(), typeindex::type_id<move_copy_conting_class>(), "type");
  211. check_non_null(any_cast<move_copy_conting_class>(&value), "any_cast<move_copy_conting_class>");
  212. check_equal(
  213. move_copy_conting_class::copy_count, 1u,
  214. "checking copy counts");
  215. check_equal(
  216. move_copy_conting_class::moves_count, 0u,
  217. "checking move counts");
  218. }
  219. void test_copy_assignment_from_value()
  220. {
  221. move_copy_conting_class value0;
  222. any value;
  223. move_copy_conting_class::copy_count = 0;
  224. move_copy_conting_class::moves_count = 0;
  225. value = value0;
  226. check_false(value.empty(), "empty");
  227. check_equal(value.type(), typeindex::type_id<move_copy_conting_class>(), "type");
  228. check_non_null(any_cast<move_copy_conting_class>(&value), "any_cast<move_copy_conting_class>");
  229. check_equal(
  230. move_copy_conting_class::copy_count, 1u,
  231. "checking copy counts");
  232. check_equal(
  233. move_copy_conting_class::moves_count, 0u,
  234. "checking move counts");
  235. }
  236. const any helper_method() {
  237. return true;
  238. }
  239. const bool helper_method1() {
  240. return true;
  241. }
  242. void test_construction_from_const_any_rv()
  243. {
  244. any values[] = {helper_method(), helper_method1() };
  245. (void)values;
  246. }
  247. void test_cast_to_rv()
  248. {
  249. move_copy_conting_class value0;
  250. any value;
  251. value = value0;
  252. move_copy_conting_class::copy_count = 0;
  253. move_copy_conting_class::moves_count = 0;
  254. move_copy_conting_class value1 = any_cast<move_copy_conting_class&&>(value);
  255. check_equal(
  256. move_copy_conting_class::copy_count, 0u,
  257. "checking copy counts");
  258. check_equal(
  259. move_copy_conting_class::moves_count, 1u,
  260. "checking move counts");
  261. (void)value1;
  262. /* Following code shall fail to compile
  263. const any cvalue = value0;
  264. move_copy_conting_class::copy_count = 0;
  265. move_copy_conting_class::moves_count = 0;
  266. move_copy_conting_class value2 = any_cast<move_copy_conting_class&&>(cvalue);
  267. check_equal(
  268. move_copy_conting_class::copy_count, 1u,
  269. "checking copy counts");
  270. check_equal(
  271. move_copy_conting_class::moves_count, 0u,
  272. "checking move counts");
  273. (void)value2;
  274. */
  275. }
  276. }
  277. #endif