File: vector_test.hpp

package info (click to toggle)
boost1.35 1.35.0-5
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 203,856 kB
  • ctags: 337,867
  • sloc: cpp: 938,683; xml: 56,847; ansic: 41,589; python: 18,999; sh: 11,566; makefile: 664; perl: 494; yacc: 456; asm: 353; csh: 6
file content (219 lines) | stat: -rw-r--r-- 7,721 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2004-2007. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/libs/interprocess for documentation.
//
//////////////////////////////////////////////////////////////////////////////

#include <boost/interprocess/detail/config_begin.hpp>
#include <algorithm>
#include <memory>
#include <vector>
#include <iostream>
#include <functional>

#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/indexes/flat_map_index.hpp>
#include <boost/interprocess/exceptions.hpp>
#include <boost/interprocess/detail/move_iterator.hpp>
#include <boost/interprocess/detail/move.hpp>
#include "print_container.hpp"
#include "check_equal_containers.hpp"
#include "movable_int.hpp"
#include <string>
#include "get_process_id_name.hpp"

namespace boost{
namespace interprocess{
namespace test{

template<class V1, class V2>
bool copyable_only(V1 *, V2 *, detail::false_type)
{
   return true;
}

//Function to check if both sets are equal
template<class V1, class V2>
bool copyable_only(V1 *shmvector, V2 *stdvector, detail::true_type)
{
   typedef typename V1::value_type IntType;
   std::size_t size = shmvector->size();
   stdvector->insert(stdvector->end(), 50, 1);
   shmvector->insert(shmvector->end(), 50, 1);
   if(!test::CheckEqualContainers(shmvector, stdvector)) return false;

   {
   IntType move_me(1);
   stdvector->insert(stdvector->begin()+size/2, 50, 1);
   shmvector->insert(shmvector->begin()+size/2, 50, move(move_me));
   if(!test::CheckEqualContainers(shmvector, stdvector)) return false;
   }
   {
   IntType move_me(2);
   shmvector->assign(shmvector->size()/2, move(move_me));
   stdvector->assign(stdvector->size()/2, 2);
   if(!test::CheckEqualContainers(shmvector, stdvector)) return false;
   }
   {
   IntType move_me(3);
   shmvector->assign(shmvector->size()*3-1, move(move_me));
   stdvector->assign(stdvector->size()*3-1, 3);
   if(!test::CheckEqualContainers(shmvector, stdvector)) return false;
   }
   return true;
}

template<class ManagedSharedMemory
        ,class MyShmVector>
int vector_test()
{
   typedef std::vector<int>                     MyStdVector;
   typedef typename MyShmVector::value_type     IntType;

   std::string process_name;
   test::get_process_id_name(process_name);

   const int Memsize = 65536;
   const char *const shMemName = process_name.c_str();
   const int max = 100;

   {
      //Compare several shared memory vector operations with std::vector
      //Create shared memory
      shared_memory_object::remove(shMemName);
      try{
         ManagedSharedMemory segment(create_only, shMemName, Memsize);

         segment.reserve_named_objects(100);

         //Shared memory allocator must be always be initialized
         //since it has no default constructor
         MyShmVector *shmvector = segment.template construct<MyShmVector>("MyShmVector")
                                 (segment.get_segment_manager());
         MyStdVector *stdvector = new MyStdVector;

         shmvector->resize(100);
         stdvector->resize(100);
         if(!test::CheckEqualContainers(shmvector, stdvector)) return 1;         

         shmvector->resize(200);
         stdvector->resize(200);
         if(!test::CheckEqualContainers(shmvector, stdvector)) return 1;         

         shmvector->resize(0);
         stdvector->resize(0);
         if(!test::CheckEqualContainers(shmvector, stdvector)) return 1;         

         for(int i = 0; i < max; ++i){
            IntType new_int(i);
            shmvector->insert(shmvector->end(), move(new_int));
            stdvector->insert(stdvector->end(), i);
         }
         if(!test::CheckEqualContainers(shmvector, stdvector)) return 1;

         typename MyShmVector::iterator shmit(shmvector->begin());
         typename MyStdVector::iterator stdit(stdvector->begin());
         typename MyShmVector::const_iterator cshmit = shmit;
         ++shmit; ++stdit;
         shmvector->erase(shmit);
         stdvector->erase(stdit);
         if(!test::CheckEqualContainers(shmvector, stdvector)) return 1;

         shmvector->erase(shmvector->begin());
         stdvector->erase(stdvector->begin());
         if(!test::CheckEqualContainers(shmvector, stdvector)) return 1;

         {
            //Initialize values
            IntType aux_vect[50];
            for(int i = 0; i < 50; ++i){
               IntType new_int(-1);
               aux_vect[i] = move(new_int);
            }
            int aux_vect2[50];
            for(int i = 0; i < 50; ++i){
               aux_vect2[i] = -1;
            }

            shmvector->insert(shmvector->end()
                              ,detail::make_move_iterator(&aux_vect[0])
                              ,detail::make_move_iterator(aux_vect + 50));
            stdvector->insert(stdvector->end(), aux_vect2, aux_vect2 + 50);
            if(!test::CheckEqualContainers(shmvector, stdvector)) return 1;

            for(int i = 0, j = static_cast<int>(shmvector->size()); i < j; ++i){
               shmvector->erase(shmvector->begin());
               stdvector->erase(stdvector->begin());
            }
            if(!test::CheckEqualContainers(shmvector, stdvector)) return 1;
         }
         {
            IntType aux_vect[50];
            for(int i = 0; i < 50; ++i){
               IntType new_int(-1);
               aux_vect[i] = move(new_int);
            }
            int aux_vect2[50];
            for(int i = 0; i < 50; ++i){
               aux_vect2[i] = -1;
            }
            shmvector->insert(shmvector->begin()
                              ,detail::make_move_iterator(&aux_vect[0])
                              ,detail::make_move_iterator(aux_vect + 50));
            stdvector->insert(stdvector->begin(), aux_vect2, aux_vect2 + 50);
            if(!test::CheckEqualContainers(shmvector, stdvector)) return 1;
         }

         shmvector->reserve(shmvector->size()*2);
         stdvector->reserve(stdvector->size()*2);
         if(!test::CheckEqualContainers(shmvector, stdvector)) return 1;

         IntType push_back_this(1);
         shmvector->push_back(move(push_back_this));
         stdvector->push_back(int(1));
         if(!test::CheckEqualContainers(shmvector, stdvector)) return 1;

         if(!copyable_only(shmvector, stdvector
                        ,detail::bool_<!is_movable<IntType>::value>())){
            return 1;
         }

         shmvector->erase(shmvector->begin());
         stdvector->erase(stdvector->begin());
         if(!test::CheckEqualContainers(shmvector, stdvector)) return 1;

         for(int i = 0; i < max; ++i){
            IntType insert_this(i);
            shmvector->insert(shmvector->begin(), move(insert_this));
            stdvector->insert(stdvector->begin(), i);
         }
         if(!test::CheckEqualContainers(shmvector, stdvector)) return 1;

         delete stdvector;
         segment.template destroy<MyShmVector>("MyShmVector");
         segment.shrink_to_fit_indexes();

         if(!segment.all_memory_deallocated())
            return 1;
      }
      catch(std::exception &ex){
         shared_memory_object::remove(shMemName);
         std::cout << ex.what() << std::endl;
         return 1;
      }
   }
   shared_memory_object::remove(shMemName);
   std::cout << std::endl << "Test OK!" << std::endl;
   return 0;
}

}  //namespace test{
}  //namespace interprocess{
}  //namespace boost{

#include <boost/interprocess/detail/config_end.hpp>