2
0

fcstack.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. // Copyright (c) 2006-2018 Maxim Khizhinsky
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #include <cds_test/ext_gtest.h>
  6. #include <cds/container/fcstack.h>
  7. #include <vector>
  8. #include <list>
  9. namespace {
  10. class FCStack : public ::testing::Test
  11. {
  12. protected:
  13. template <class Stack>
  14. void test()
  15. {
  16. typedef typename Stack::value_type value_type;
  17. Stack stack;
  18. value_type v;
  19. ASSERT_TRUE( stack.empty());
  20. ASSERT_EQ( stack.size(), 0u );
  21. ASSERT_TRUE( stack.push( 1 ));
  22. ASSERT_TRUE( !stack.empty());
  23. ASSERT_EQ( stack.size(), 1u );
  24. ASSERT_TRUE( stack.push( 2 ));
  25. ASSERT_TRUE( !stack.empty());
  26. ASSERT_EQ( stack.size(), 2u );
  27. ASSERT_TRUE( stack.push( 3 ));
  28. ASSERT_TRUE( !stack.empty());
  29. ASSERT_EQ( stack.size(), 3u );
  30. ASSERT_TRUE( stack.pop( v ));
  31. EXPECT_EQ( v, value_type( 3 ));
  32. ASSERT_TRUE( !stack.empty());
  33. ASSERT_EQ( stack.size(), 2u );
  34. ASSERT_TRUE( stack.pop( v ));
  35. EXPECT_EQ( v, value_type( 2 ));
  36. ASSERT_TRUE( !stack.empty());
  37. ASSERT_EQ( stack.size(), 1u );
  38. ASSERT_TRUE( stack.pop( v ));
  39. EXPECT_EQ( v, value_type( 1 ));
  40. ASSERT_TRUE( stack.empty());
  41. ASSERT_EQ( stack.size(), 0u );
  42. v = 1000;
  43. ASSERT_TRUE( !stack.pop( v ));
  44. EXPECT_EQ( v, value_type( 1000 ));
  45. ASSERT_TRUE( stack.empty());
  46. ASSERT_EQ( stack.size(), 0u );
  47. ASSERT_TRUE( stack.push( 10 ));
  48. ASSERT_TRUE( stack.push( 20 ));
  49. ASSERT_TRUE( stack.push( 30 ));
  50. ASSERT_TRUE( !stack.empty());
  51. ASSERT_EQ( stack.size(), 3u );
  52. while ( stack.pop( v ));
  53. ASSERT_TRUE( stack.empty());
  54. ASSERT_EQ( stack.size(), 0u );
  55. }
  56. };
  57. TEST_F( FCStack, default_stack )
  58. {
  59. typedef cds::container::FCStack< unsigned int > stack_type;
  60. test<stack_type>();
  61. }
  62. TEST_F( FCStack, deque_based )
  63. {
  64. typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::deque<unsigned int>>> stack_type;
  65. test<stack_type>();
  66. }
  67. TEST_F( FCStack, deque_empty_wait_strategy )
  68. {
  69. struct stack_traits: public
  70. cds::container::fcstack::make_traits <
  71. cds::opt::wait_strategy<cds::algo::flat_combining::wait_strategy::empty>
  72. > ::type
  73. {};
  74. typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::deque<unsigned int>>, stack_traits > stack_type;
  75. test<stack_type>();
  76. }
  77. TEST_F( FCStack, deque_single_mutex_single_condvar )
  78. {
  79. struct stack_traits: public
  80. cds::container::fcstack::make_traits <
  81. cds::opt::wait_strategy<cds::algo::flat_combining::wait_strategy::single_mutex_single_condvar<>>
  82. > ::type
  83. {};
  84. typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::deque<unsigned int>>, stack_traits > stack_type;
  85. test<stack_type>();
  86. }
  87. TEST_F( FCStack, deque_single_mutex_multi_condvar )
  88. {
  89. struct stack_traits: public
  90. cds::container::fcstack::make_traits <
  91. cds::opt::wait_strategy<cds::algo::flat_combining::wait_strategy::single_mutex_multi_condvar<>>
  92. > ::type
  93. {};
  94. typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::deque<unsigned int>>, stack_traits > stack_type;
  95. test<stack_type>();
  96. }
  97. TEST_F( FCStack, deque_multi_mutex_multi_condvar )
  98. {
  99. struct stack_traits: public
  100. cds::container::fcstack::make_traits <
  101. cds::opt::wait_strategy<cds::algo::flat_combining::wait_strategy::multi_mutex_multi_condvar<>>
  102. > ::type
  103. {};
  104. typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::deque<unsigned int>>, stack_traits > stack_type;
  105. test<stack_type>();
  106. }
  107. TEST_F( FCStack, deque_single_mutex_single_condvar_2ms )
  108. {
  109. struct stack_traits: public
  110. cds::container::fcstack::make_traits <
  111. cds::opt::wait_strategy<cds::algo::flat_combining::wait_strategy::single_mutex_single_condvar<2>>
  112. > ::type
  113. {};
  114. typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::deque<unsigned int>>, stack_traits > stack_type;
  115. test<stack_type>();
  116. }
  117. TEST_F( FCStack, deque_single_mutex_multi_condvar_2ms )
  118. {
  119. struct stack_traits: public
  120. cds::container::fcstack::make_traits <
  121. cds::opt::wait_strategy<cds::algo::flat_combining::wait_strategy::single_mutex_multi_condvar<2>>
  122. > ::type
  123. {};
  124. typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::deque<unsigned int>>, stack_traits > stack_type;
  125. test<stack_type>();
  126. }
  127. TEST_F( FCStack, deque_multi_mutex_multi_condvar_3ms )
  128. {
  129. struct stack_traits: public
  130. cds::container::fcstack::make_traits <
  131. cds::opt::wait_strategy<cds::algo::flat_combining::wait_strategy::multi_mutex_multi_condvar<3>>
  132. > ::type
  133. {};
  134. typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::deque<unsigned int>>, stack_traits > stack_type;
  135. test<stack_type>();
  136. }
  137. TEST_F( FCStack, deque_elimination )
  138. {
  139. struct stack_traits : public
  140. cds::container::fcstack::make_traits <
  141. cds::opt::enable_elimination < true >
  142. > ::type
  143. {};
  144. typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::deque<unsigned int>>, stack_traits > stack_type;
  145. test<stack_type>();
  146. }
  147. TEST_F( FCStack, vector_based )
  148. {
  149. typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::vector<unsigned int>>> stack_type;
  150. test<stack_type>();
  151. }
  152. TEST_F( FCStack, vector_empty_wait_strategy )
  153. {
  154. typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::vector<unsigned int>>,
  155. cds::container::fcstack::make_traits<
  156. cds::opt::wait_strategy< cds::algo::flat_combining::wait_strategy::empty >
  157. >::type
  158. > stack_type;
  159. test<stack_type>();
  160. }
  161. TEST_F( FCStack, vector_multi_mutex_multi_condvar )
  162. {
  163. typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::vector<unsigned int>>,
  164. cds::container::fcstack::make_traits<
  165. cds::opt::wait_strategy< cds::algo::flat_combining::wait_strategy::multi_mutex_multi_condvar<>>
  166. >::type
  167. > stack_type;
  168. test<stack_type>();
  169. }
  170. TEST_F( FCStack, vector_single_mutex_multi_condvar )
  171. {
  172. typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::vector<unsigned int>>,
  173. cds::container::fcstack::make_traits<
  174. cds::opt::wait_strategy< cds::algo::flat_combining::wait_strategy::single_mutex_multi_condvar<>>
  175. >::type
  176. > stack_type;
  177. test<stack_type>();
  178. }
  179. TEST_F( FCStack, vector_single_mutex_single_condvar )
  180. {
  181. typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::vector<unsigned int>>,
  182. cds::container::fcstack::make_traits<
  183. cds::opt::wait_strategy< cds::algo::flat_combining::wait_strategy::single_mutex_single_condvar<>>
  184. >::type
  185. > stack_type;
  186. test<stack_type>();
  187. }
  188. TEST_F( FCStack, vector_elimination )
  189. {
  190. typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::vector<unsigned int>>,
  191. cds::container::fcstack::make_traits<
  192. cds::opt::enable_elimination< true >
  193. >::type
  194. > stack_type;
  195. test<stack_type>();
  196. }
  197. TEST_F( FCStack, list_based )
  198. {
  199. typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::list<unsigned int>>> stack_type;
  200. test<stack_type>();
  201. }
  202. TEST_F( FCStack, list_empty_wait_strategy )
  203. {
  204. typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::list<unsigned int>>,
  205. cds::container::fcstack::make_traits<
  206. cds::opt::wait_strategy< cds::algo::flat_combining::wait_strategy::empty >
  207. >::type
  208. > stack_type;
  209. test<stack_type>();
  210. }
  211. TEST_F( FCStack, list_single_mutex_single_condvar )
  212. {
  213. typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::list<unsigned int>>,
  214. cds::container::fcstack::make_traits<
  215. cds::opt::wait_strategy< cds::algo::flat_combining::wait_strategy::single_mutex_single_condvar<>>
  216. >::type
  217. > stack_type;
  218. test<stack_type>();
  219. }
  220. TEST_F( FCStack, list_single_mutex_multi_condvar )
  221. {
  222. typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::list<unsigned int>>,
  223. cds::container::fcstack::make_traits<
  224. cds::opt::wait_strategy< cds::algo::flat_combining::wait_strategy::single_mutex_multi_condvar<>>
  225. >::type
  226. > stack_type;
  227. test<stack_type>();
  228. }
  229. TEST_F( FCStack, list_multi_mutex_multi_condvar )
  230. {
  231. typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::list<unsigned int>>,
  232. cds::container::fcstack::make_traits<
  233. cds::opt::wait_strategy< cds::algo::flat_combining::wait_strategy::multi_mutex_multi_condvar<>>
  234. >::type
  235. > stack_type;
  236. test<stack_type>();
  237. }
  238. TEST_F( FCStack, list_elimination )
  239. {
  240. typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::list<unsigned int>>,
  241. cds::container::fcstack::make_traits<
  242. cds::opt::enable_elimination< true >
  243. , cds::opt::wait_strategy< cds::algo::flat_combining::wait_strategy::multi_mutex_multi_condvar<2>>
  244. >::type
  245. > stack_type;
  246. test<stack_type>();
  247. }
  248. } // namespace