node_handle_test.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2016-2016. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/container for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. #include <boost/core/lightweight_test.hpp>
  11. #include <boost/static_assert.hpp>
  12. #include <boost/container/node_handle.hpp>
  13. #include <boost/container/new_allocator.hpp>
  14. #include <boost/move/utility_core.hpp>
  15. #include <boost/move/adl_move_swap.hpp>
  16. #include <boost/container/detail/pair_key_mapped_of_value.hpp>
  17. using namespace ::boost::container;
  18. enum EAllocState
  19. {
  20. DefaultConstructed,
  21. MoveConstructed,
  22. MoveAssigned,
  23. CopyConstructed,
  24. CopyAssigned,
  25. Swapped,
  26. Destructed
  27. };
  28. template<class Node>
  29. class trace_allocator
  30. : public new_allocator<Node>
  31. {
  32. BOOST_COPYABLE_AND_MOVABLE(trace_allocator)
  33. typedef new_allocator<Node> base_t;
  34. public:
  35. struct propagate_on_container_move_assignment
  36. {
  37. static const bool value = true;
  38. };
  39. struct propagate_on_container_swap
  40. {
  41. static const bool value = true;
  42. };
  43. //!Obtains an new_allocator that allocates
  44. //!objects of type T2
  45. template<class T2>
  46. struct rebind
  47. {
  48. typedef trace_allocator<T2> other;
  49. };
  50. explicit trace_allocator(unsigned value = 999)
  51. : m_state(DefaultConstructed), m_value(value)
  52. {
  53. ++count;
  54. }
  55. trace_allocator(BOOST_RV_REF(trace_allocator) other)
  56. : base_t(boost::move(BOOST_MOVE_BASE(base_t, other))), m_state(MoveConstructed), m_value(other.m_value)
  57. {
  58. ++count;
  59. }
  60. trace_allocator(const trace_allocator &other)
  61. : base_t(other), m_state(CopyConstructed), m_value(other.m_value)
  62. {
  63. ++count;
  64. }
  65. trace_allocator & operator=(BOOST_RV_REF(trace_allocator) other)
  66. {
  67. m_value = other.m_value;
  68. m_state = MoveAssigned;
  69. return *this;
  70. }
  71. template<class OtherNode>
  72. trace_allocator(const trace_allocator<OtherNode> &other)
  73. : m_state(CopyConstructed), m_value(other.m_value)
  74. {
  75. ++count;
  76. }
  77. template<class OtherNode>
  78. trace_allocator & operator=(BOOST_COPY_ASSIGN_REF(trace_allocator<OtherNode>) other)
  79. {
  80. m_value = other.m_value;
  81. m_state = CopyAssigned;
  82. return *this;
  83. }
  84. ~trace_allocator()
  85. {
  86. m_value = 0u-1u;
  87. m_state = Destructed;
  88. --count;
  89. }
  90. void swap(trace_allocator &other)
  91. {
  92. boost::adl_move_swap(m_value, other.m_value);
  93. m_state = other.m_state = Swapped;
  94. }
  95. friend void swap(trace_allocator &left, trace_allocator &right)
  96. {
  97. left.swap(right);
  98. }
  99. EAllocState m_state;
  100. unsigned m_value;
  101. static unsigned int count;
  102. static void reset_count()
  103. { count = 0; }
  104. };
  105. template<class Node>
  106. unsigned int trace_allocator<Node>::count = 0;
  107. template<class T>
  108. struct node
  109. {
  110. typedef T value_type;
  111. value_type value;
  112. value_type &get_data() { return value; }
  113. const value_type &get_data() const { return value; }
  114. node()
  115. {
  116. ++count;
  117. }
  118. ~node()
  119. {
  120. --count;
  121. }
  122. static unsigned int count;
  123. static void reset_count()
  124. { count = 0; }
  125. };
  126. template<class T1, class T2>
  127. struct value
  128. {
  129. T1 first;
  130. T2 second;
  131. };
  132. template<class T>
  133. unsigned int node<T>::count = 0;
  134. //Common types
  135. typedef value<int, unsigned> test_pair;
  136. typedef pair_key_mapped_of_value<int, unsigned> key_mapped_t;
  137. typedef node<test_pair> node_t;
  138. typedef trace_allocator< node_t > node_alloc_t;
  139. typedef node_handle<node_alloc_t, void> node_handle_set_t;
  140. typedef node_handle<node_alloc_t, key_mapped_t> node_handle_map_t;
  141. typedef allocator_traits<node_alloc_t>::portable_rebind_alloc<test_pair>::type value_allocator_type;
  142. void test_types()
  143. {
  144. //set
  145. BOOST_STATIC_ASSERT(( dtl::is_same<node_handle_set_t::value_type, test_pair>::value ));
  146. BOOST_STATIC_ASSERT(( dtl::is_same<node_handle_set_t::key_type, test_pair>::value ));
  147. BOOST_STATIC_ASSERT(( dtl::is_same<node_handle_set_t::mapped_type, test_pair>::value ));
  148. BOOST_STATIC_ASSERT(( dtl::is_same<node_handle_set_t::allocator_type, value_allocator_type>::value ));
  149. //map
  150. BOOST_STATIC_ASSERT(( dtl::is_same<node_handle_map_t::value_type, test_pair>::value ));
  151. BOOST_STATIC_ASSERT(( dtl::is_same<node_handle_map_t::key_type, int>::value ));
  152. BOOST_STATIC_ASSERT(( dtl::is_same<node_handle_map_t::mapped_type, unsigned>::value ));
  153. BOOST_STATIC_ASSERT(( dtl::is_same<node_handle_map_t::allocator_type, value_allocator_type>::value ));
  154. }
  155. void test_default_constructor()
  156. {
  157. node_alloc_t::reset_count();
  158. {
  159. node_handle_set_t nh;
  160. BOOST_TEST(node_alloc_t::count == 0);
  161. }
  162. BOOST_TEST(node_alloc_t::count == 0);
  163. }
  164. void test_arg_constructor()
  165. {
  166. //With non-null pointer
  167. node_alloc_t::reset_count();
  168. node_t::reset_count();
  169. {
  170. const node_alloc_t al;
  171. BOOST_TEST(node_alloc_t::count == 1);
  172. {
  173. node_handle_set_t nh(new node_t, al);
  174. BOOST_TEST(node_t::count == 1);
  175. BOOST_TEST(node_alloc_t::count == 2);
  176. }
  177. BOOST_TEST(node_alloc_t::count == 1);
  178. }
  179. BOOST_TEST(node_t::count == 0);
  180. BOOST_TEST(node_alloc_t::count == 0);
  181. //With null pointer
  182. node_alloc_t::reset_count();
  183. node_t::reset_count();
  184. {
  185. const node_alloc_t al;
  186. BOOST_TEST(node_alloc_t::count == 1);
  187. {
  188. node_handle_set_t nh(0, al);
  189. BOOST_TEST(node_t::count == 0);
  190. BOOST_TEST(node_alloc_t::count == 1);
  191. }
  192. BOOST_TEST(node_alloc_t::count == 1);
  193. BOOST_TEST(node_t::count == 0);
  194. }
  195. BOOST_TEST(node_alloc_t::count == 0);
  196. }
  197. void test_move_constructor()
  198. {
  199. //With non-null pointer
  200. node_alloc_t::reset_count();
  201. node_t::reset_count();
  202. {
  203. const node_alloc_t al;
  204. BOOST_TEST(node_alloc_t::count == 1);
  205. {
  206. node_t *const from_ptr = new node_t;
  207. node_handle_set_t nh(from_ptr, al);
  208. BOOST_TEST(node_t::count == 1);
  209. BOOST_TEST(node_alloc_t::count == 2);
  210. {
  211. node_handle_set_t nh2(boost::move(nh));
  212. BOOST_TEST(nh.empty());
  213. BOOST_TEST(!nh2.empty());
  214. BOOST_TEST(nh2.get() == from_ptr);
  215. BOOST_TEST(nh2.node_alloc().m_state == MoveConstructed);
  216. BOOST_TEST(node_t::count == 1);
  217. BOOST_TEST(node_alloc_t::count == 2);
  218. }
  219. BOOST_TEST(node_t::count == 0);
  220. BOOST_TEST(node_alloc_t::count == 1);
  221. }
  222. BOOST_TEST(node_alloc_t::count == 1);
  223. }
  224. BOOST_TEST(node_t::count == 0);
  225. BOOST_TEST(node_alloc_t::count == 0);
  226. //With null pointer
  227. node_alloc_t::reset_count();
  228. node_t::reset_count();
  229. {
  230. const node_alloc_t al;
  231. BOOST_TEST(node_alloc_t::count == 1);
  232. {
  233. node_handle_set_t nh;
  234. {
  235. node_handle_set_t nh2(boost::move(nh));
  236. BOOST_TEST(nh.empty());
  237. BOOST_TEST(nh2.empty());
  238. BOOST_TEST(node_alloc_t::count == 1);
  239. }
  240. BOOST_TEST(node_t::count == 0);
  241. BOOST_TEST(node_alloc_t::count == 1);
  242. }
  243. BOOST_TEST(node_alloc_t::count == 1);
  244. }
  245. BOOST_TEST(node_t::count == 0);
  246. BOOST_TEST(node_alloc_t::count == 0);
  247. }
  248. void test_related_constructor()
  249. {
  250. //With non-null pointer
  251. node_alloc_t::reset_count();
  252. node_t::reset_count();
  253. {
  254. const node_alloc_t al;
  255. BOOST_TEST(node_alloc_t::count == 1);
  256. {
  257. node_t *const from_ptr = new node_t;
  258. node_handle_map_t nh(from_ptr, al);
  259. BOOST_TEST(node_t::count == 1);
  260. BOOST_TEST(node_alloc_t::count == 2);
  261. {
  262. node_handle_set_t nh2(boost::move(nh));
  263. BOOST_TEST(nh.empty());
  264. BOOST_TEST(!nh2.empty());
  265. BOOST_TEST(nh2.get() == from_ptr);
  266. BOOST_TEST(nh2.node_alloc().m_state == MoveConstructed);
  267. BOOST_TEST(node_t::count == 1);
  268. BOOST_TEST(node_alloc_t::count == 2);
  269. }
  270. BOOST_TEST(node_t::count == 0);
  271. BOOST_TEST(node_alloc_t::count == 1);
  272. }
  273. BOOST_TEST(node_alloc_t::count == 1);
  274. }
  275. BOOST_TEST(node_t::count == 0);
  276. BOOST_TEST(node_alloc_t::count == 0);
  277. //With null pointer
  278. node_alloc_t::reset_count();
  279. node_t::reset_count();
  280. {
  281. const node_alloc_t al;
  282. BOOST_TEST(node_alloc_t::count == 1);
  283. {
  284. node_handle_set_t nh;
  285. {
  286. node_handle_map_t nh2(boost::move(nh));
  287. BOOST_TEST(nh.empty());
  288. BOOST_TEST(nh2.empty());
  289. BOOST_TEST(node_alloc_t::count == 1);
  290. }
  291. BOOST_TEST(node_t::count == 0);
  292. BOOST_TEST(node_alloc_t::count == 1);
  293. }
  294. BOOST_TEST(node_alloc_t::count == 1);
  295. }
  296. BOOST_TEST(node_t::count == 0);
  297. BOOST_TEST(node_alloc_t::count == 0);
  298. }
  299. void test_move_assignment()
  300. {
  301. //empty = full
  302. {
  303. node_alloc_t::reset_count();
  304. node_t::reset_count();
  305. node_t *const from_ptr = new node_t;
  306. node_handle_set_t nh_from(from_ptr, node_alloc_t());
  307. BOOST_TEST(node_t::count == 1);
  308. BOOST_TEST(node_alloc_t::count == 1);
  309. node_handle_set_t nh_to;
  310. BOOST_TEST(nh_to.empty());
  311. BOOST_TEST(node_t::count == 1);
  312. BOOST_TEST(node_alloc_t::count == 1);
  313. nh_to = boost::move(nh_from);
  314. BOOST_TEST(nh_from.empty());
  315. BOOST_TEST(!nh_to.empty());
  316. BOOST_TEST(nh_to.get() == from_ptr);
  317. BOOST_TEST(nh_to.node_alloc().m_state == MoveConstructed);
  318. BOOST_TEST(node_t::count == 1);
  319. BOOST_TEST(node_alloc_t::count == 1);
  320. }
  321. //empty = empty
  322. {
  323. node_alloc_t::reset_count();
  324. node_t::reset_count();
  325. node_handle_set_t nh_from;
  326. BOOST_TEST(nh_from.empty());
  327. BOOST_TEST(node_t::count == 0);
  328. BOOST_TEST(node_alloc_t::count == 0);
  329. node_handle_set_t nh_to;
  330. BOOST_TEST(nh_to.empty());
  331. BOOST_TEST(node_t::count == 0);
  332. BOOST_TEST(node_alloc_t::count == 0);
  333. nh_to = boost::move(nh_from);
  334. BOOST_TEST(nh_from.empty());
  335. BOOST_TEST(nh_to.empty());
  336. BOOST_TEST(node_t::count == 0);
  337. BOOST_TEST(node_alloc_t::count == 0);
  338. }
  339. //full = empty
  340. {
  341. node_alloc_t::reset_count();
  342. node_t::reset_count();
  343. node_handle_set_t nh_from;
  344. BOOST_TEST(nh_from.empty());
  345. BOOST_TEST(node_t::count == 0);
  346. BOOST_TEST(node_alloc_t::count == 0);
  347. node_handle_set_t nh_to(new node_t, node_alloc_t());
  348. BOOST_TEST(node_t::count == 1);
  349. BOOST_TEST(node_alloc_t::count == 1);
  350. nh_to = boost::move(nh_from);
  351. BOOST_TEST(nh_from.empty());
  352. BOOST_TEST(nh_to.empty());
  353. BOOST_TEST(node_t::count == 0);
  354. BOOST_TEST(node_alloc_t::count == 0);
  355. }
  356. //full = full
  357. {
  358. node_alloc_t::reset_count();
  359. node_t::reset_count();
  360. node_t *const from_ptr = new node_t;
  361. node_handle_set_t nh_from(from_ptr, node_alloc_t());
  362. BOOST_TEST(node_t::count == 1);
  363. BOOST_TEST(node_alloc_t::count == 1);
  364. node_handle_set_t nh_to(new node_t, node_alloc_t());
  365. BOOST_TEST(node_t::count == 2);
  366. BOOST_TEST(node_alloc_t::count == 2);
  367. nh_to = boost::move(nh_from);
  368. BOOST_TEST(nh_from.empty());
  369. BOOST_TEST(!nh_to.empty());
  370. BOOST_TEST(nh_to.get() == from_ptr);
  371. BOOST_TEST(nh_to.node_alloc().m_state == MoveAssigned);
  372. BOOST_TEST(node_t::count == 1);
  373. BOOST_TEST(node_alloc_t::count == 1);
  374. }
  375. }
  376. void test_value_key_mapped()
  377. {
  378. //value()
  379. {
  380. node_t *from_ptr = new node_t;
  381. const node_handle_set_t nh_from(from_ptr, node_alloc_t());
  382. from_ptr->value.first = -99;
  383. from_ptr->value.second = 99;
  384. BOOST_TEST(nh_from.value().first == -99);
  385. BOOST_TEST(nh_from.value().second == 99);
  386. }
  387. //key()/mapped()
  388. {
  389. node_t *from_ptr = new node_t;
  390. const node_handle_map_t nh_from(from_ptr, node_alloc_t());
  391. from_ptr->value.first = -98;
  392. from_ptr->value.second = 98;
  393. BOOST_TEST(nh_from.key() == -98);
  394. BOOST_TEST(nh_from.mapped() == 98);
  395. }
  396. }
  397. void test_get_allocator()
  398. {
  399. const node_handle_set_t nh(new node_t, node_alloc_t(888));
  400. allocator_traits<node_alloc_t>::portable_rebind_alloc<test_pair>::type a = nh.get_allocator();
  401. BOOST_TEST(a.m_value == 888);
  402. }
  403. void test_bool_conversion_empty()
  404. {
  405. const node_handle_set_t nh(new node_t, node_alloc_t(777));
  406. const node_handle_set_t nh_null;
  407. BOOST_TEST(nh && !nh_null);
  408. BOOST_TEST(!(!nh || nh_null));
  409. BOOST_TEST(!nh.empty() && nh_null.empty());
  410. BOOST_TEST(!(nh.empty() || !nh_null.empty()));
  411. }
  412. void test_swap()
  413. {
  414. //empty.swap(full)
  415. {
  416. node_alloc_t::reset_count();
  417. node_t::reset_count();
  418. node_t *const from_ptr = new node_t;
  419. node_handle_set_t nh_from(from_ptr, node_alloc_t());
  420. BOOST_TEST(node_t::count == 1);
  421. BOOST_TEST(node_alloc_t::count == 1);
  422. node_handle_set_t nh_to;
  423. BOOST_TEST(nh_to.empty());
  424. BOOST_TEST(node_t::count == 1);
  425. BOOST_TEST(node_alloc_t::count == 1);
  426. nh_to.swap(nh_from);
  427. BOOST_TEST(nh_from.empty());
  428. BOOST_TEST(!nh_to.empty());
  429. BOOST_TEST(nh_to.get() == from_ptr);
  430. BOOST_TEST(nh_to.node_alloc().m_state == MoveConstructed);
  431. BOOST_TEST(node_t::count == 1);
  432. BOOST_TEST(node_alloc_t::count == 1);
  433. }
  434. //empty.swap(empty)
  435. {
  436. node_alloc_t::reset_count();
  437. node_t::reset_count();
  438. node_handle_set_t nh_from;
  439. BOOST_TEST(nh_from.empty());
  440. BOOST_TEST(node_t::count == 0);
  441. BOOST_TEST(node_alloc_t::count == 0);
  442. node_handle_set_t nh_to;
  443. BOOST_TEST(nh_to.empty());
  444. BOOST_TEST(node_t::count == 0);
  445. BOOST_TEST(node_alloc_t::count == 0);
  446. nh_to.swap(nh_from);
  447. BOOST_TEST(nh_from.empty());
  448. BOOST_TEST(nh_to.empty());
  449. BOOST_TEST(node_t::count == 0);
  450. BOOST_TEST(node_alloc_t::count == 0);
  451. }
  452. //full.swap(empty)
  453. {
  454. node_alloc_t::reset_count();
  455. node_t::reset_count();
  456. node_handle_set_t nh_from;
  457. BOOST_TEST(nh_from.empty());
  458. BOOST_TEST(node_t::count == 0);
  459. BOOST_TEST(node_alloc_t::count == 0);
  460. node_t *const to_ptr = new node_t;
  461. node_handle_set_t nh_to(to_ptr, node_alloc_t());
  462. BOOST_TEST(node_t::count == 1);
  463. BOOST_TEST(node_alloc_t::count == 1);
  464. nh_to.swap(nh_from);
  465. BOOST_TEST(!nh_from.empty());
  466. BOOST_TEST(nh_from.node_alloc().m_state == MoveConstructed);
  467. BOOST_TEST(nh_from.get() == to_ptr);
  468. BOOST_TEST(nh_to.empty());
  469. BOOST_TEST(node_t::count == 1);
  470. BOOST_TEST(node_alloc_t::count == 1);
  471. }
  472. //full.swap(full)
  473. {
  474. node_alloc_t::reset_count();
  475. node_t::reset_count();
  476. node_t *const from_ptr = new node_t;
  477. node_handle_set_t nh_from(from_ptr, node_alloc_t());
  478. BOOST_TEST(node_t::count == 1);
  479. BOOST_TEST(node_alloc_t::count == 1);
  480. node_t *const to_ptr = new node_t;
  481. node_handle_set_t nh_to(to_ptr, node_alloc_t());
  482. BOOST_TEST(node_t::count == 2);
  483. BOOST_TEST(node_alloc_t::count == 2);
  484. nh_to.swap(nh_from);
  485. BOOST_TEST(!nh_from.empty());
  486. BOOST_TEST(nh_from.get() == to_ptr);
  487. BOOST_TEST(nh_from.node_alloc().m_state == Swapped);
  488. BOOST_TEST(!nh_to.empty());
  489. BOOST_TEST(nh_to.get() == from_ptr);
  490. BOOST_TEST(nh_to.node_alloc().m_state == Swapped);
  491. BOOST_TEST(node_t::count == 2);
  492. BOOST_TEST(node_alloc_t::count == 2);
  493. }
  494. }
  495. void test_get_release()
  496. {
  497. //get()
  498. {
  499. node_alloc_t::reset_count();
  500. node_t::reset_count();
  501. node_t *const ptr = new node_t;
  502. const node_handle_set_t nh(ptr, node_alloc_t());
  503. BOOST_TEST(node_t::count == 1);
  504. BOOST_TEST(node_alloc_t::count == 1);
  505. BOOST_TEST(nh.get() == ptr);
  506. BOOST_TEST(!nh.empty());
  507. BOOST_TEST(node_t::count == 1);
  508. BOOST_TEST(node_alloc_t::count == 1);
  509. }
  510. BOOST_TEST(node_t::count == 0);
  511. BOOST_TEST(node_alloc_t::count == 0);
  512. //release()
  513. {
  514. node_alloc_t::reset_count();
  515. node_t::reset_count();
  516. node_t *const ptr = new node_t;
  517. node_handle_set_t nh(ptr, node_alloc_t());
  518. BOOST_TEST(node_t::count == 1);
  519. BOOST_TEST(node_alloc_t::count == 1);
  520. BOOST_TEST(nh.release() == ptr);
  521. BOOST_TEST(nh.empty());
  522. BOOST_TEST(node_t::count == 1);
  523. BOOST_TEST(node_alloc_t::count == 0);
  524. delete ptr;
  525. }
  526. BOOST_TEST(node_t::count == 0);
  527. }
  528. int main()
  529. {
  530. test_types();
  531. test_default_constructor();
  532. test_arg_constructor();
  533. test_move_constructor();
  534. test_related_constructor();
  535. test_move_assignment();
  536. test_value_key_mapped();
  537. test_get_allocator();
  538. test_bool_conversion_empty();
  539. test_swap();
  540. test_get_release();
  541. return ::boost::report_errors();
  542. }