list_test.hpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. ////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2006-2012. 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/interprocess for documentation.
  8. //
  9. ////////////////////////////////////////
  10. #ifndef BOOST_INTERPROCESS_TEST_LIST_TEST_HEADER
  11. #define BOOST_INTERPROCESS_TEST_LIST_TEST_HEADER
  12. #include <boost/interprocess/detail/config_begin.hpp>
  13. #include "check_equal_containers.hpp"
  14. #include <memory>
  15. #include <list>
  16. #include <vector>
  17. #include <functional>
  18. #include "print_container.hpp"
  19. #include <boost/move/utility_core.hpp>
  20. #include <string>
  21. #include "get_process_id_name.hpp"
  22. namespace boost{
  23. namespace interprocess{
  24. namespace test{
  25. template<bool DoublyLinked>
  26. struct push_data_function
  27. {
  28. template<class MyShmList, class MyStdList>
  29. static int execute(int max, MyShmList *shmlist, MyStdList *stdlist)
  30. {
  31. typedef typename MyShmList::value_type IntType;
  32. for(int i = 0; i < max; ++i){
  33. IntType move_me(i);
  34. shmlist->push_back(boost::move(move_me));
  35. stdlist->push_back(i);
  36. shmlist->push_back(IntType(i));
  37. stdlist->push_back(int(i));
  38. }
  39. if(!CheckEqualContainers(shmlist, stdlist))
  40. return 1;
  41. return 0;
  42. }
  43. };
  44. template<>
  45. struct push_data_function<false>
  46. {
  47. template<class MyShmList, class MyStdList>
  48. static int execute(int max, MyShmList *shmlist, MyStdList *stdlist)
  49. {
  50. typedef typename MyShmList::value_type IntType;
  51. for(int i = 0; i < max; ++i){
  52. IntType move_me(i);
  53. shmlist->push_front(boost::move(move_me));
  54. stdlist->push_front(i);
  55. shmlist->push_front(IntType(i));
  56. stdlist->push_front(int(i));
  57. }
  58. if(!CheckEqualContainers(shmlist, stdlist))
  59. return 1;
  60. return 0;
  61. }
  62. };
  63. template<bool DoublyLinked>
  64. struct pop_back_function
  65. {
  66. template<class MyStdList, class MyShmList>
  67. static int execute(MyShmList *shmlist, MyStdList *stdlist)
  68. {
  69. shmlist->pop_back();
  70. stdlist->pop_back();
  71. if(!CheckEqualContainers(shmlist, stdlist))
  72. return 1;
  73. return 0;
  74. }
  75. };
  76. template<>
  77. struct pop_back_function<false>
  78. {
  79. template<class MyStdList, class MyShmList>
  80. static int execute(MyShmList *shmlist, MyStdList *stdlist)
  81. {
  82. (void)shmlist; (void)stdlist;
  83. return 0;
  84. }
  85. };
  86. template<class ManagedSharedMemory
  87. ,class MyShmList
  88. ,bool DoublyLinked>
  89. int list_test (bool copied_allocators_equal = true)
  90. {
  91. typedef std::list<int> MyStdList;
  92. typedef typename MyShmList::value_type IntType;
  93. const int memsize = 65536;
  94. const char *const shMemName = test::get_process_id_name();
  95. const int max = 100;
  96. typedef push_data_function<DoublyLinked> push_data_t;
  97. try{
  98. //Named new capable shared mem allocator
  99. //Create shared memory
  100. shared_memory_object::remove(shMemName);
  101. ManagedSharedMemory segment(create_only, shMemName, memsize);
  102. segment.reserve_named_objects(100);
  103. //Shared memory allocator must be always be initialized
  104. //since it has no default constructor
  105. MyShmList *shmlist = segment.template construct<MyShmList>("MyList")
  106. (segment.get_segment_manager());
  107. MyStdList *stdlist = new MyStdList;
  108. if(push_data_t::execute(max/2, shmlist, stdlist)){
  109. return 1;
  110. }
  111. shmlist->erase(shmlist->begin()++);
  112. stdlist->erase(stdlist->begin()++);
  113. if(!CheckEqualContainers(shmlist, stdlist)) return 1;
  114. if(pop_back_function<DoublyLinked>::execute(shmlist, stdlist)){
  115. return 1;
  116. }
  117. shmlist->pop_front();
  118. stdlist->pop_front();
  119. if(!CheckEqualContainers(shmlist, stdlist)) return 1;
  120. {
  121. IntType aux_vect[50];
  122. for(int i = 0; i < 50; ++i){
  123. IntType move_me(-1);
  124. aux_vect[i] = boost::move(move_me);
  125. }
  126. int aux_vect2[50];
  127. for(int i = 0; i < 50; ++i){
  128. aux_vect2[i] = -1;
  129. }
  130. shmlist->assign(::boost::make_move_iterator(&aux_vect[0])
  131. ,::boost::make_move_iterator(&aux_vect[50]));
  132. stdlist->assign(&aux_vect2[0], &aux_vect2[50]);
  133. if(!CheckEqualContainers(shmlist, stdlist)) return 1;
  134. }
  135. if(copied_allocators_equal){
  136. shmlist->sort();
  137. stdlist->sort();
  138. if(!CheckEqualContainers(shmlist, stdlist)) return 1;
  139. }
  140. shmlist->reverse();
  141. stdlist->reverse();
  142. if(!CheckEqualContainers(shmlist, stdlist)) return 1;
  143. shmlist->reverse();
  144. stdlist->reverse();
  145. if(!CheckEqualContainers(shmlist, stdlist)) return 1;
  146. {
  147. IntType aux_vect[50];
  148. for(int i = 0; i < 50; ++i){
  149. IntType move_me(-1);
  150. aux_vect[i] = boost::move(move_me);
  151. }
  152. int aux_vect2[50];
  153. for(int i = 0; i < 50; ++i){
  154. aux_vect2[i] = -1;
  155. }
  156. shmlist->insert(shmlist->begin()
  157. ,::boost::make_move_iterator(&aux_vect[0])
  158. ,::boost::make_move_iterator(&aux_vect[50]));
  159. stdlist->insert(stdlist->begin(), &aux_vect2[0], &aux_vect2[50]);
  160. }
  161. shmlist->unique();
  162. stdlist->unique();
  163. if(!CheckEqualContainers(shmlist, stdlist))
  164. return 1;
  165. if(copied_allocators_equal){
  166. shmlist->sort(std::greater<IntType>());
  167. stdlist->sort(std::greater<int>());
  168. if(!CheckEqualContainers(shmlist, stdlist))
  169. return 1;
  170. }
  171. shmlist->resize(25);
  172. stdlist->resize(25);
  173. shmlist->resize(50);
  174. stdlist->resize(50);
  175. shmlist->resize(0);
  176. stdlist->resize(0);
  177. if(!CheckEqualContainers(shmlist, stdlist))
  178. return 1;
  179. if(push_data_t::execute(max/2, shmlist, stdlist)){
  180. return 1;
  181. }
  182. {
  183. MyShmList othershmlist(shmlist->get_allocator());
  184. MyStdList otherstdlist;
  185. int listsize = (int)shmlist->size();
  186. if(push_data_t::execute(listsize/2, shmlist, stdlist)){
  187. return 1;
  188. }
  189. if(copied_allocators_equal){
  190. shmlist->splice(shmlist->begin(), othershmlist);
  191. stdlist->splice(stdlist->begin(), otherstdlist);
  192. if(!CheckEqualContainers(shmlist, stdlist))
  193. return 1;
  194. }
  195. listsize = (int)shmlist->size();
  196. if(push_data_t::execute(listsize/2, shmlist, stdlist)){
  197. return 1;
  198. }
  199. if(push_data_t::execute(listsize/2, &othershmlist, &otherstdlist)){
  200. return 1;
  201. }
  202. if(copied_allocators_equal){
  203. shmlist->sort(std::greater<IntType>());
  204. stdlist->sort(std::greater<int>());
  205. if(!CheckEqualContainers(shmlist, stdlist))
  206. return 1;
  207. othershmlist.sort(std::greater<IntType>());
  208. otherstdlist.sort(std::greater<int>());
  209. if(!CheckEqualContainers(&othershmlist, &otherstdlist))
  210. return 1;
  211. shmlist->merge(othershmlist, std::greater<IntType>());
  212. stdlist->merge(otherstdlist, std::greater<int>());
  213. if(!CheckEqualContainers(shmlist, stdlist))
  214. return 1;
  215. }
  216. for(int i = 0; i < max; ++i){
  217. shmlist->insert(shmlist->begin(), IntType(i));
  218. stdlist->insert(stdlist->begin(), int(i));
  219. }
  220. if(!CheckEqualContainers(shmlist, stdlist))
  221. return 1;
  222. }
  223. segment.template destroy<MyShmList>("MyList");
  224. delete stdlist;
  225. segment.shrink_to_fit_indexes();
  226. if(!segment.all_memory_deallocated())
  227. return 1;
  228. }
  229. catch(...){
  230. shared_memory_object::remove(shMemName);
  231. throw;
  232. }
  233. shared_memory_object::remove(shMemName);
  234. return 0;
  235. }
  236. } //namespace test{
  237. } //namespace interprocess{
  238. } //namespace boost{
  239. #include <boost/interprocess/detail/config_end.hpp>
  240. #endif