PageRenderTime 40ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/boost_1_57_0/libs/any/test/any_test.cpp

http://github.com/MisterTea/HyperNEAT
C++ | 370 lines | 278 code | 68 blank | 24 comment | 12 complexity | 8a622c6476fa699da64ab1fefe5572ea MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0, GPL-3.0, GPL-2.0
  1. // what: unit tests for variant type boost::any
  2. // who: contributed by Kevlin Henney
  3. // when: July 2001, 2013, 2014
  4. // where: tested with BCC 5.5, MSVC 6.0, and g++ 2.95
  5. #include <cstdlib>
  6. #include <string>
  7. #include <vector>
  8. #include <utility>
  9. #include "boost/any.hpp"
  10. #include "test.hpp"
  11. namespace any_tests
  12. {
  13. typedef test<const char *, void (*)()> test_case;
  14. typedef const test_case * test_case_iterator;
  15. extern const test_case_iterator begin, end;
  16. }
  17. int main()
  18. {
  19. using namespace any_tests;
  20. tester<test_case_iterator> test_suite(begin, end);
  21. return test_suite() ? EXIT_SUCCESS : EXIT_FAILURE;
  22. }
  23. namespace any_tests // test suite
  24. {
  25. void test_default_ctor();
  26. void test_converting_ctor();
  27. void test_copy_ctor();
  28. void test_copy_assign();
  29. void test_converting_assign();
  30. void test_bad_cast();
  31. void test_swap();
  32. void test_null_copying();
  33. void test_cast_to_reference();
  34. void test_with_array();
  35. void test_with_func();
  36. void test_clear();
  37. void test_vectors();
  38. const test_case test_cases[] =
  39. {
  40. { "default construction", test_default_ctor },
  41. { "single argument construction", test_converting_ctor },
  42. { "copy construction", test_copy_ctor },
  43. { "copy assignment operator", test_copy_assign },
  44. { "converting assignment operator", test_converting_assign },
  45. { "failed custom keyword cast", test_bad_cast },
  46. { "swap member function", test_swap },
  47. { "copying operations on a null", test_null_copying },
  48. { "cast to reference types", test_cast_to_reference },
  49. { "storing an array inside", test_with_array },
  50. { "implicit cast of returned value",test_with_func },
  51. { "clear() methods", test_clear },
  52. { "testing with vectors", test_vectors }
  53. };
  54. const test_case_iterator begin = test_cases;
  55. const test_case_iterator end =
  56. test_cases + (sizeof test_cases / sizeof *test_cases);
  57. struct copy_counter
  58. {
  59. public:
  60. copy_counter() {}
  61. copy_counter(const copy_counter&) { ++count; }
  62. copy_counter& operator=(const copy_counter&) { ++count; return *this; }
  63. static int get_count() { return count; }
  64. private:
  65. static int count;
  66. };
  67. int copy_counter::count = 0;
  68. }
  69. namespace any_tests // test definitions
  70. {
  71. using namespace boost;
  72. void test_default_ctor()
  73. {
  74. const any value;
  75. check_true(value.empty(), "empty");
  76. check_null(any_cast<int>(&value), "any_cast<int>");
  77. check_equal(value.type(), boost::typeindex::type_id<void>(), "type");
  78. }
  79. void test_converting_ctor()
  80. {
  81. std::string text = "test message";
  82. any value = text;
  83. check_false(value.empty(), "empty");
  84. check_equal(value.type(), boost::typeindex::type_id<std::string>(), "type");
  85. check_null(any_cast<int>(&value), "any_cast<int>");
  86. check_non_null(any_cast<std::string>(&value), "any_cast<std::string>");
  87. check_equal(
  88. any_cast<std::string>(value), text,
  89. "comparing cast copy against original text");
  90. check_unequal(
  91. any_cast<std::string>(&value), &text,
  92. "comparing address in copy against original text");
  93. }
  94. void test_copy_ctor()
  95. {
  96. std::string text = "test message";
  97. any original = text, copy = original;
  98. check_false(copy.empty(), "empty");
  99. check_equal(boost::typeindex::type_index(original.type()), copy.type(), "type");
  100. check_equal(
  101. any_cast<std::string>(original), any_cast<std::string>(copy),
  102. "comparing cast copy against original");
  103. check_equal(
  104. text, any_cast<std::string>(copy),
  105. "comparing cast copy against original text");
  106. check_unequal(
  107. any_cast<std::string>(&original),
  108. any_cast<std::string>(&copy),
  109. "comparing address in copy against original");
  110. }
  111. void test_copy_assign()
  112. {
  113. std::string text = "test message";
  114. any original = text, copy;
  115. any * assign_result = &(copy = original);
  116. check_false(copy.empty(), "empty");
  117. check_equal(boost::typeindex::type_index(original.type()), copy.type(), "type");
  118. check_equal(
  119. any_cast<std::string>(original), any_cast<std::string>(copy),
  120. "comparing cast copy against cast original");
  121. check_equal(
  122. text, any_cast<std::string>(copy),
  123. "comparing cast copy against original text");
  124. check_unequal(
  125. any_cast<std::string>(&original),
  126. any_cast<std::string>(&copy),
  127. "comparing address in copy against original");
  128. check_equal(assign_result, &copy, "address of assignment result");
  129. }
  130. void test_converting_assign()
  131. {
  132. std::string text = "test message";
  133. any value;
  134. any * assign_result = &(value = text);
  135. check_false(value.empty(), "type");
  136. check_equal(value.type(), boost::typeindex::type_id<std::string>(), "type");
  137. check_null(any_cast<int>(&value), "any_cast<int>");
  138. check_non_null(any_cast<std::string>(&value), "any_cast<std::string>");
  139. check_equal(
  140. any_cast<std::string>(value), text,
  141. "comparing cast copy against original text");
  142. check_unequal(
  143. any_cast<std::string>(&value),
  144. &text,
  145. "comparing address in copy against original text");
  146. check_equal(assign_result, &value, "address of assignment result");
  147. }
  148. void test_bad_cast()
  149. {
  150. std::string text = "test message";
  151. any value = text;
  152. TEST_CHECK_THROW(
  153. any_cast<const char *>(value),
  154. bad_any_cast,
  155. "any_cast to incorrect type");
  156. }
  157. void test_swap()
  158. {
  159. std::string text = "test message";
  160. any original = text, swapped;
  161. std::string * original_ptr = any_cast<std::string>(&original);
  162. any * swap_result = &original.swap(swapped);
  163. check_true(original.empty(), "empty on original");
  164. check_false(swapped.empty(), "empty on swapped");
  165. check_equal(swapped.type(), boost::typeindex::type_id<std::string>(), "type");
  166. check_equal(
  167. text, any_cast<std::string>(swapped),
  168. "comparing swapped copy against original text");
  169. check_non_null(original_ptr, "address in pre-swapped original");
  170. check_equal(
  171. original_ptr,
  172. any_cast<std::string>(&swapped),
  173. "comparing address in swapped against original");
  174. check_equal(swap_result, &original, "address of swap result");
  175. any copy1 = copy_counter();
  176. any copy2 = copy_counter();
  177. int count = copy_counter::get_count();
  178. swap(copy1, copy2);
  179. check_equal(count, copy_counter::get_count(), "checking that free swap doesn't make any copies.");
  180. }
  181. void test_null_copying()
  182. {
  183. const any null;
  184. any copied = null, assigned;
  185. assigned = null;
  186. check_true(null.empty(), "empty on null");
  187. check_true(copied.empty(), "empty on copied");
  188. check_true(assigned.empty(), "empty on copied");
  189. }
  190. void test_cast_to_reference()
  191. {
  192. any a(137);
  193. const any b(a);
  194. int & ra = any_cast<int &>(a);
  195. int const & ra_c = any_cast<int const &>(a);
  196. int volatile & ra_v = any_cast<int volatile &>(a);
  197. int const volatile & ra_cv = any_cast<int const volatile&>(a);
  198. check_true(
  199. &ra == &ra_c && &ra == &ra_v && &ra == &ra_cv,
  200. "cv references to same obj");
  201. int const & rb_c = any_cast<int const &>(b);
  202. int const volatile & rb_cv = any_cast<int const volatile &>(b);
  203. check_true(&rb_c == &rb_cv, "cv references to copied const obj");
  204. check_true(&ra != &rb_c, "copies hold different objects");
  205. ++ra;
  206. int incremented = any_cast<int>(a);
  207. check_true(incremented == 138, "increment by reference changes value");
  208. TEST_CHECK_THROW(
  209. any_cast<char &>(a),
  210. bad_any_cast,
  211. "any_cast to incorrect reference type");
  212. TEST_CHECK_THROW(
  213. any_cast<const char &>(b),
  214. bad_any_cast,
  215. "any_cast to incorrect const reference type");
  216. }
  217. void test_with_array()
  218. {
  219. any value1("Char array");
  220. any value2;
  221. value2 = "Char array";
  222. check_false(value1.empty(), "type");
  223. check_false(value2.empty(), "type");
  224. check_equal(value1.type(), boost::typeindex::type_id<const char*>(), "type");
  225. check_equal(value2.type(), boost::typeindex::type_id<const char*>(), "type");
  226. check_non_null(any_cast<const char*>(&value1), "any_cast<const char*>");
  227. check_non_null(any_cast<const char*>(&value2), "any_cast<const char*>");
  228. }
  229. const std::string& returning_string1()
  230. {
  231. static const std::string ret("foo");
  232. return ret;
  233. }
  234. std::string returning_string2()
  235. {
  236. static const std::string ret("foo");
  237. return ret;
  238. }
  239. void test_with_func()
  240. {
  241. std::string s;
  242. s = any_cast<std::string>(returning_string1());
  243. s = any_cast<const std::string&>(returning_string1());
  244. s = any_cast<std::string>(returning_string2());
  245. s = any_cast<const std::string&>(returning_string2());
  246. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  247. #if !defined(__INTEL_COMPILER) && !defined(__ICL) && (!defined(_MSC_VER) || _MSC_VER != 1600)
  248. // Intel compiler thinks that it must choose the `any_cast(const any&)` function
  249. // instead of the `any_cast(const any&&)`.
  250. // Bug was not reported because of missing premier support account + annoying
  251. // registrations requirements.
  252. // MSVC-10 had a bug:
  253. //
  254. // any.hpp(291) : error C2440: 'return' : cannot convert.
  255. // Conversion loses qualifiers
  256. // any_test.cpp(304) : see reference to function template instantiation
  257. //
  258. // This issue was fixed in MSVC-11.
  259. s = any_cast<std::string&&>(returning_string1());
  260. #endif
  261. s = any_cast<std::string&&>(returning_string2());
  262. #endif
  263. }
  264. void test_clear()
  265. {
  266. std::string text = "test message";
  267. any value = text;
  268. check_false(value.empty(), "empty");
  269. value.clear();
  270. check_true(value.empty(), "non-empty after clear");
  271. value.clear();
  272. check_true(value.empty(), "non-empty after second clear");
  273. value = text;
  274. check_false(value.empty(), "empty");
  275. value.clear();
  276. check_true(value.empty(), "non-empty after clear");
  277. }
  278. // Following tests cover the case from #9462
  279. // https://svn.boost.org/trac/boost/ticket/9462
  280. boost::any makeVec()
  281. {
  282. return std::vector<int>(100 /*size*/, 7 /*value*/);
  283. }
  284. void test_vectors()
  285. {
  286. const std::vector<int>& vec = boost::any_cast<std::vector<int> >(makeVec());
  287. check_equal(vec.size(), 100u, "size of vector extracted from boost::any");
  288. check_equal(vec.back(), 7, "back value of vector extracted from boost::any");
  289. check_equal(vec.front(), 7, "front value of vector extracted from boost::any");
  290. std::vector<int> vec1 = boost::any_cast<std::vector<int> >(makeVec());
  291. check_equal(vec1.size(), 100u, "size of second vector extracted from boost::any");
  292. check_equal(vec1.back(), 7, "back value of second vector extracted from boost::any");
  293. check_equal(vec1.front(), 7, "front value of second vector extracted from boost::any");
  294. }
  295. }
  296. // Copyright Kevlin Henney, 2000, 2001. All rights reserved.
  297. // Copyright Antony Polukhin, 2013-2014.
  298. //
  299. // Distributed under the Boost Software License, Version 1.0. (See
  300. // accompanying file LICENSE_1_0.txt or copy at
  301. // http://www.boost.org/LICENSE_1_0.txt)
  302. //