1 ////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2006. 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)
7 // See http://www.boost.org/libs/interprocess for documentation.
9 ////////////////////////////////////////
11 #ifndef BOOST_INTERPROCESS_TEST_LIST_TEST_HEADER
12 #define BOOST_INTERPROCESS_TEST_LIST_TEST_HEADER
14 #include <boost/interprocess/detail/config_begin.hpp>
15 #include "check_equal_containers.hpp"
20 #include "print_container.hpp"
21 #include <boost/interprocess/detail/move.hpp>
23 #include "get_process_id_name.hpp"
26 namespace interprocess{
29 template<bool DoublyLinked>
30 struct push_data_function
32 template<class MyShmList, class MyStdList>
33 static int execute(int max, MyShmList *shmlist, MyStdList *stdlist)
35 typedef typename MyShmList::value_type IntType;
36 for(int i = 0; i < max; ++i){
38 shmlist->push_back(boost::move(move_me));
39 stdlist->push_back(i);
40 shmlist->push_back(IntType(i));
41 stdlist->push_back(int(i));
43 if(!CheckEqualContainers(shmlist, stdlist))
50 struct push_data_function<false>
52 template<class MyShmList, class MyStdList>
53 static int execute(int max, MyShmList *shmlist, MyStdList *stdlist)
55 typedef typename MyShmList::value_type IntType;
56 for(int i = 0; i < max; ++i){
58 shmlist->push_front(boost::move(move_me));
59 stdlist->push_front(i);
60 shmlist->push_front(IntType(i));
61 stdlist->push_front(int(i));
63 if(!CheckEqualContainers(shmlist, stdlist))
69 template<bool DoublyLinked>
70 struct pop_back_function
72 template<class MyStdList, class MyShmList>
73 static int execute(MyShmList *shmlist, MyStdList *stdlist)
77 if(!CheckEqualContainers(shmlist, stdlist))
84 struct pop_back_function<false>
86 template<class MyStdList, class MyShmList>
87 static int execute(MyShmList *shmlist, MyStdList *stdlist)
89 (void)shmlist; (void)stdlist;
94 template<class ManagedSharedMemory
97 int list_test (bool copied_allocators_equal = true)
99 typedef std::list<int> MyStdList;
100 typedef typename MyShmList::value_type IntType;
101 const int memsize = 65536;
102 const char *const shMemName = test::get_process_id_name();
104 typedef push_data_function<DoublyLinked> push_data_t;
107 //Named new capable shared mem allocator
108 //Create shared memory
109 shared_memory_object::remove(shMemName);
110 ManagedSharedMemory segment(create_only, shMemName, memsize);
112 segment.reserve_named_objects(100);
114 //Shared memory allocator must be always be initialized
115 //since it has no default constructor
116 MyShmList *shmlist = segment.template construct<MyShmList>("MyList")
117 (segment.get_segment_manager());
120 MyStdList *stdlist = new MyStdList;
122 if(push_data_t::execute(max/2, shmlist, stdlist)){
126 shmlist->erase(shmlist->begin()++);
127 stdlist->erase(stdlist->begin()++);
128 if(!CheckEqualContainers(shmlist, stdlist)) return 1;
130 if(pop_back_function<DoublyLinked>::execute(shmlist, stdlist)){
134 shmlist->pop_front();
135 stdlist->pop_front();
136 if(!CheckEqualContainers(shmlist, stdlist)) return 1;
139 IntType aux_vect[50];
140 for(int i = 0; i < 50; ++i){
142 aux_vect[i] = boost::move(move_me);
145 for(int i = 0; i < 50; ++i){
148 shmlist->assign(::boost::make_move_iterator(&aux_vect[0])
149 ,::boost::make_move_iterator(&aux_vect[50]));
150 stdlist->assign(&aux_vect2[0], &aux_vect2[50]);
151 if(!CheckEqualContainers(shmlist, stdlist)) return 1;
154 if(copied_allocators_equal){
157 if(!CheckEqualContainers(shmlist, stdlist)) return 1;
162 if(!CheckEqualContainers(shmlist, stdlist)) return 1;
166 if(!CheckEqualContainers(shmlist, stdlist)) return 1;
169 IntType aux_vect[50];
170 for(int i = 0; i < 50; ++i){
172 aux_vect[i] = boost::move(move_me);
175 for(int i = 0; i < 50; ++i){
178 shmlist->insert(shmlist->begin()
179 ,::boost::make_move_iterator(&aux_vect[0])
180 ,::boost::make_move_iterator(&aux_vect[50]));
181 stdlist->insert(stdlist->begin(), &aux_vect2[0], &aux_vect2[50]);
186 if(!CheckEqualContainers(shmlist, stdlist))
189 if(copied_allocators_equal){
190 shmlist->sort(std::greater<IntType>());
191 stdlist->sort(std::greater<int>());
192 if(!CheckEqualContainers(shmlist, stdlist))
202 if(!CheckEqualContainers(shmlist, stdlist))
205 if(push_data_t::execute(max/2, shmlist, stdlist)){
209 MyShmList othershmlist(shmlist->get_allocator());
210 MyStdList otherstdlist;
212 int listsize = (int)shmlist->size();
214 if(push_data_t::execute(listsize/2, shmlist, stdlist)){
218 if(copied_allocators_equal){
219 shmlist->splice(shmlist->begin(), othershmlist);
220 stdlist->splice(stdlist->begin(), otherstdlist);
221 if(!CheckEqualContainers(shmlist, stdlist))
225 listsize = (int)shmlist->size();
227 if(push_data_t::execute(listsize/2, shmlist, stdlist)){
231 if(push_data_t::execute(listsize/2, &othershmlist, &otherstdlist)){
235 if(copied_allocators_equal){
236 shmlist->sort(std::greater<IntType>());
237 stdlist->sort(std::greater<int>());
238 if(!CheckEqualContainers(shmlist, stdlist))
241 othershmlist.sort(std::greater<IntType>());
242 otherstdlist.sort(std::greater<int>());
243 if(!CheckEqualContainers(&othershmlist, &otherstdlist))
246 shmlist->merge(othershmlist, std::greater<IntType>());
247 stdlist->merge(otherstdlist, std::greater<int>());
248 if(!CheckEqualContainers(shmlist, stdlist))
252 for(int i = 0; i < max; ++i){
253 shmlist->insert(shmlist->begin(), IntType(i));
254 stdlist->insert(stdlist->begin(), int(i));
256 if(!CheckEqualContainers(shmlist, stdlist))
260 segment.template destroy<MyShmList>("MyList");
262 segment.shrink_to_fit_indexes();
264 if(!segment.all_memory_deallocated())
268 shared_memory_object::remove(shMemName);
271 shared_memory_object::remove(shMemName);
276 } //namespace interprocess{
279 #include <boost/interprocess/detail/config_end.hpp>