File: map_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 (422 lines) | stat: -rw-r--r-- 16,237 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2006. 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.
//
////////////////////////////////////////

#ifndef BOOST_INTERPROCESS_TEST_MAP_TEST_HEADER
#define BOOST_INTERPROCESS_TEST_MAP_TEST_HEADER

#include <boost/interprocess/detail/config_begin.hpp>
#include "check_equal_containers.hpp"
#include <map>
#include <functional>
#include <utility>
#include "print_container.hpp"
#include <boost/interprocess/detail/move_iterator.hpp>
#include <boost/interprocess/detail/utilities.hpp>
#include <boost/interprocess/detail/iterators.hpp>
#include <string>
#include "get_process_id_name.hpp"

template<class T1, class T2, class T3, class T4>
bool operator ==(std::pair<T1, T2> &p1, std::pair<T1, T2> &p2)
{
   return p1.first == p2.first && p1.second == p2.second;
}

namespace boost{
namespace interprocess{
namespace test{

template<class ManagedSharedMemory
        ,class MyShmMap
        ,class MyStdMap
        ,class MyShmMultiMap
        ,class MyStdMultiMap>
int map_test ()
{
   typedef typename MyShmMap::key_type    IntType;
   typedef std::pair<IntType, IntType>    IntPairType;
   typedef typename MyStdMap::value_type  StdPairType;
   const int memsize = 65536;
   const char *const shMemName = test::get_process_id_name();
   const int max = 100;

   try{
      //Create shared memory
      shared_memory_object::remove(shMemName);
      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
      MyShmMap *shmmap = 
         segment.template construct<MyShmMap>("MyShmMap")
            (std::less<IntType>(), segment.get_segment_manager());

      MyStdMap *stdmap = new MyStdMap;

      MyShmMultiMap *shmmultimap = 
         segment.template construct<MyShmMultiMap>("MyShmMultiMap")
            (std::less<IntType>(), segment.get_segment_manager());

      MyStdMultiMap *stdmultimap = new MyStdMultiMap;

      //Test construction from a range   
      {
         //This is really nasty, but we have no other simple choice
         IntPairType aux_vect[50];
         for(int i = 0; i < 50; ++i){
            new(&aux_vect[i])IntPairType(IntType(i/2), IntType(i/2));
         }

         typedef typename MyStdMap::value_type StdValueType;
         typedef typename MyStdMap::key_type StdKeyType;
         typedef typename MyStdMap::mapped_type StdMappedType;
         StdValueType aux_vect2[50];
         for(int i = 0; i < 50; ++i){
            new(&aux_vect2[i])StdValueType(StdKeyType(i/2), StdMappedType(i/2));
         }

         IntPairType aux_vect3[50];
         for(int i = 0; i < 50; ++i){
            new(&aux_vect3[i])IntPairType(IntType(i/2), IntType(i/2));
         }

         MyShmMap *shmmap2 = 
            segment.template construct<MyShmMap>("MyShmMap2")
               (detail::make_move_iterator(&aux_vect[0])
               , detail::make_move_iterator(aux_vect + 50)
               , std::less<IntType>(), segment.get_segment_manager());

         MyStdMap *stdmap2 = new MyStdMap(aux_vect2, aux_vect2 + 50);

         MyShmMultiMap *shmmultimap2 = 
            segment.template construct<MyShmMultiMap>("MyShmMultiMap2")
               (detail::make_move_iterator(&aux_vect3[0])
               , detail::make_move_iterator(aux_vect3 + 50)
               , std::less<IntType>(), segment.get_segment_manager());

         MyStdMultiMap *stdmultimap2 = new MyStdMultiMap(aux_vect2, aux_vect2 + 50);
         if(!CheckEqualContainers(shmmap2, stdmap2)) return 1;
         if(!CheckEqualContainers(shmmultimap2, stdmultimap2)) return 1;

         segment.destroy_ptr(shmmap2);
         segment.destroy_ptr(shmmultimap2);
         delete stdmap2;
         delete stdmultimap2;
      }

      int i, j;
      for(i = 0; i < max; ++i){
         shmmap->insert(move(IntPairType (move(IntType(i)), move(IntType(i)))));
         stdmap->insert(StdPairType(i, i));
         shmmultimap->insert(move(IntPairType(move(IntType(i)), move(IntType(i)))));
         stdmultimap->insert(StdPairType(i, i));
      }

      if(!CheckEqualPairContainers(shmmap, stdmap)) return 1;
      if(!CheckEqualPairContainers(shmmultimap, stdmultimap)) return 1;

      typename MyShmMap::iterator it;
      typename MyShmMap::const_iterator cit = it;

      shmmap->erase(shmmap->begin()++);
      stdmap->erase(stdmap->begin()++);
      shmmultimap->erase(shmmultimap->begin()++);
      stdmultimap->erase(stdmultimap->begin()++);
      if(!CheckEqualPairContainers(shmmap, stdmap)) return 1;
      if(!CheckEqualPairContainers(shmmultimap, stdmultimap)) return 1;

      shmmap->erase(shmmap->begin());
      stdmap->erase(stdmap->begin());
      shmmultimap->erase(shmmultimap->begin());
      stdmultimap->erase(stdmultimap->begin());
      if(!CheckEqualPairContainers(shmmap, stdmap)) return 1;
      if(!CheckEqualPairContainers(shmmultimap, stdmultimap)) return 1;

      //Swapping test
      std::less<IntType> lessfunc;
      MyShmMap tmpshmemap2 (lessfunc, segment.get_segment_manager());
      MyStdMap tmpstdmap2;
      MyShmMultiMap tmpshmemultimap2(lessfunc, segment.get_segment_manager());
      MyStdMultiMap tmpstdmultimap2;
      shmmap->swap(tmpshmemap2);
      stdmap->swap(tmpstdmap2);
      shmmultimap->swap(tmpshmemultimap2);
      stdmultimap->swap(tmpstdmultimap2);
      shmmap->swap(tmpshmemap2);
      stdmap->swap(tmpstdmap2);
      shmmultimap->swap(tmpshmemultimap2);
      stdmultimap->swap(tmpstdmultimap2);
      if(!CheckEqualPairContainers(shmmap, stdmap)) return 1;
      if(!CheckEqualPairContainers(shmmultimap, stdmultimap)) return 1;

      //Insertion from other container
      //Initialize values
      {
         //This is really nasty, but we have no other simple choice
         IntPairType aux_vect[50];
         for(int i = 0; i < 50; ++i){
            new(&aux_vect[i])IntPairType(IntType(-1), IntType(-1));
         }
         IntPairType aux_vect3[50];
         for(int i = 0; i < 50; ++i){
            new(&aux_vect3[i])IntPairType(IntType(-1), IntType(-1));
         }

         shmmap->insert(detail::make_move_iterator(&aux_vect[0]), detail::make_move_iterator(aux_vect + 50));
         StdPairType stdpairtype(-1, -1);
         constant_iterator<StdPairType> constant_beg(stdpairtype, 50), constant_end;
         stdmap->insert(constant_beg, constant_end);
         shmmultimap->insert(detail::make_move_iterator(&aux_vect3[0]), detail::make_move_iterator(aux_vect3 + 50));
         stdmultimap->insert(constant_beg, constant_end);
         if(!CheckEqualPairContainers(shmmap, stdmap)) return 1;
         if(!CheckEqualPairContainers(shmmultimap, stdmultimap)) return 1;

         for(int i = 0, j = static_cast<int>(shmmap->size()); i < j; ++i){
            shmmap->erase(IntType(i));
            stdmap->erase(i);
            shmmultimap->erase(IntType(i));
            stdmultimap->erase(i);
         }
         if(!CheckEqualPairContainers(shmmap, stdmap)) return 1;
         if(!CheckEqualPairContainers(shmmultimap, stdmultimap)) return 1;
      }
      {
         IntPairType aux_vect[50];
         for(int i = 0; i < 50; ++i){
            new(&aux_vect[i])IntPairType(IntType(-1), IntType(-1));
         }

         IntPairType aux_vect3[50];
         for(int i = 0; i < 50; ++i){
            new(&aux_vect3[i])IntPairType(IntType(-1), IntType(-1));
         }

         IntPairType aux_vect4[50];
         for(int i = 0; i < 50; ++i){
            new(&aux_vect4[i])IntPairType(IntType(-1), IntType(-1));
         }

         IntPairType aux_vect5[50];
         for(int i = 0; i < 50; ++i){
            new(&aux_vect5[i])IntPairType(IntType(-1), IntType(-1));
         }

         shmmap->insert(detail::make_move_iterator(&aux_vect[0]), detail::make_move_iterator(aux_vect + 50));
         shmmap->insert(detail::make_move_iterator(&aux_vect3[0]), detail::make_move_iterator(aux_vect3 + 50));
         StdPairType stdpairtype(-1, -1);
         constant_iterator<StdPairType> constant_beg(stdpairtype, 50), constant_end;
         stdmap->insert(constant_beg, constant_end);
         stdmap->insert(constant_beg, constant_end);
         shmmultimap->insert(detail::make_move_iterator(&aux_vect4[0]), detail::make_move_iterator(aux_vect4 + 50));
         shmmultimap->insert(detail::make_move_iterator(&aux_vect5[0]), detail::make_move_iterator(aux_vect5 + 50));
         stdmultimap->insert(constant_beg, constant_end);
         stdmultimap->insert(constant_beg, constant_end);
         if(!CheckEqualPairContainers(shmmap, stdmap)) return 1;
         if(!CheckEqualPairContainers(shmmultimap, stdmultimap)) return 1;

         shmmap->erase(shmmap->begin()->first);
         stdmap->erase(stdmap->begin()->first);
         shmmultimap->erase(shmmultimap->begin()->first);
         stdmultimap->erase(stdmultimap->begin()->first);
         if(!CheckEqualPairContainers(shmmap, stdmap)) return 1;
         if(!CheckEqualPairContainers(shmmultimap, stdmultimap)) return 1;
      }

      for(i = 0; i < max; ++i){
         shmmap->insert(move(IntPairType(move(IntType(i)), move(IntType(i)))));
         stdmap->insert(StdPairType(i, i));
         shmmultimap->insert(move(IntPairType(move(IntType(i)), move(IntType(i)))));
         stdmultimap->insert(StdPairType(i, i));
      }

      if(!CheckEqualPairContainers(shmmap, stdmap)) return 1;
      if(!CheckEqualPairContainers(shmmultimap, stdmultimap)) return 1;

      for(i = 0; i < max; ++i){
         shmmap->insert(shmmap->begin(), move(IntPairType(move(IntType(i)), move(IntType(i)))));
         stdmap->insert(stdmap->begin(), StdPairType(i, i));
         //PrintContainers(shmmap, stdmap);
         shmmultimap->insert(shmmultimap->begin(), move(IntPairType(move(IntType(i)), move(IntType(i)))));
         stdmultimap->insert(stdmultimap->begin(), StdPairType(i, i));
         //PrintContainers(shmmultimap, stdmultimap);
         if(!CheckEqualPairContainers(shmmap, stdmap))
            return 1;
         if(!CheckEqualPairContainers(shmmultimap, stdmultimap))
            return 1;

         shmmap->insert(shmmap->end(), move(IntPairType(move(IntType(i)), move(IntType(i)))));
         stdmap->insert(stdmap->end(), StdPairType(i, i));
         shmmultimap->insert(shmmultimap->end(), move(IntPairType(move(IntType(i)), move(IntType(i)))));
         stdmultimap->insert(stdmultimap->end(), StdPairType(i, i));
         if(!CheckEqualPairContainers(shmmap, stdmap))
            return 1;
         if(!CheckEqualPairContainers(shmmultimap, stdmultimap))
            return 1;

         shmmap->insert(shmmap->lower_bound(IntType(i)), move(IntPairType(move(IntType(i)), move(IntType(i)))));
         stdmap->insert(stdmap->lower_bound(i), StdPairType(i, i));
         //PrintContainers(shmmap, stdmap);
         shmmultimap->insert(shmmultimap->lower_bound(IntType(i)), move(IntPairType(move(IntType(i)), move(IntType(i)))));
         stdmultimap->insert(stdmultimap->lower_bound(i), StdPairType(i, i));
         //PrintContainers(shmmultimap, stdmultimap);
         if(!CheckEqualPairContainers(shmmap, stdmap))
            return 1;
         if(!CheckEqualPairContainers(shmmultimap, stdmultimap))
            return 1;
         shmmap->insert(shmmap->upper_bound(IntType(i)), move(IntPairType(move(IntType(i)), move(IntType(i)))));
         stdmap->insert(stdmap->upper_bound(i), StdPairType(i, i));
         //PrintContainers(shmmap, stdmap);
         shmmultimap->insert(shmmultimap->upper_bound(IntType(i)), move(IntPairType(move(IntType(i)), move(IntType(i)))));
         stdmultimap->insert(stdmultimap->upper_bound(i), StdPairType(i, i));
         //PrintContainers(shmmultimap, stdmultimap);
         if(!CheckEqualPairContainers(shmmap, stdmap))
            return 1;
         if(!CheckEqualPairContainers(shmmultimap, stdmultimap))
            return 1;
      }

      //Compare count with std containers
      for(i = 0; i < max; ++i){
         if(shmmap->count(IntType(i)) != stdmap->count(i)){
            return -1;
         }

         if(shmmultimap->count(IntType(i)) != stdmultimap->count(i)){
            return -1;
         }
      }

      //Now do count exercise
      shmmap->erase(shmmap->begin(), shmmap->end());
      shmmultimap->erase(shmmultimap->begin(), shmmultimap->end());
      shmmap->clear();
      shmmultimap->clear();

      for(j = 0; j < 3; ++j)
      for(i = 0; i < 100; ++i){
         shmmap->insert(move(IntPairType(move(IntType(i)), move(IntType(i)))));
         shmmultimap->insert(move(IntPairType(move(IntType(i)), move(IntType(i)))));
         if(shmmap->count(IntType(i)) != typename MyShmMultiMap::size_type(1))
            return 1;
         if(shmmultimap->count(IntType(i)) != typename MyShmMultiMap::size_type(j+1))
            return 1;
      }

      segment.template destroy<MyShmMap>("MyShmMap");
      delete stdmap;
      segment.destroy_ptr(shmmultimap);
      delete stdmultimap;

      segment.shrink_to_fit_indexes();

      if(!segment.all_memory_deallocated())
         return 1;
   }
   catch(...){
      shared_memory_object::remove(shMemName);
      throw;
   }
   shared_memory_object::remove(shMemName);
   return 0;
}

template<class ManagedSharedMemory
        ,class MyShmMap
        ,class MyStdMap
        ,class MyShmMultiMap
        ,class MyStdMultiMap>
int map_test_copyable ()
{
   typedef typename MyShmMap::key_type    IntType;
   typedef std::pair<IntType, IntType>    IntPairType;
   typedef typename MyStdMap::value_type  StdPairType;

   const int memsize = 65536;
   const char *const shMemName = test::get_process_id_name();
   const int max = 100;

   try{
   //Create shared memory
   shared_memory_object::remove(shMemName);
   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
   MyShmMap *shmmap = 
      segment.template construct<MyShmMap>("MyShmMap")
         (std::less<IntType>(), segment.get_segment_manager());

   MyStdMap *stdmap = new MyStdMap;

   MyShmMultiMap *shmmultimap = 
      segment.template construct<MyShmMultiMap>("MyShmMultiMap")
         (std::less<IntType>(), segment.get_segment_manager());

   MyStdMultiMap *stdmultimap = new MyStdMultiMap;

   int i;
   for(i = 0; i < max; ++i){
      shmmap->insert(move(IntPairType(move(IntType(i)), move(IntType(i)))));
      stdmap->insert(StdPairType(i, i));
      shmmultimap->insert(move(IntPairType(move(IntType(i)), move(IntType(i)))));
      stdmultimap->insert(StdPairType(i, i));
   }
   if(!CheckEqualContainers(shmmap, stdmap)) return 1;
   if(!CheckEqualContainers(shmmultimap, stdmultimap)) return 1;

      {
         //Now, test copy constructor
         MyShmMap shmmapcopy(*shmmap);
         MyStdMap stdmapcopy(*stdmap);
         MyShmMultiMap shmmmapcopy(*shmmultimap);
         MyStdMultiMap stdmmapcopy(*stdmultimap);

         if(!CheckEqualContainers(&shmmapcopy, &stdmapcopy))
            return 1;
         if(!CheckEqualContainers(&shmmmapcopy, &stdmmapcopy))
            return 1;

         //And now assignment
         shmmapcopy  = *shmmap;
         stdmapcopy  = *stdmap;
         shmmmapcopy = *shmmultimap;
         stdmmapcopy = *stdmultimap;
         
         if(!CheckEqualContainers(&shmmapcopy, &stdmapcopy))
            return 1;
         if(!CheckEqualContainers(&shmmmapcopy, &stdmmapcopy))
            return 1;
         segment.destroy_ptr(shmmap);
         segment.destroy_ptr(shmmultimap);
      }
      segment.shrink_to_fit_indexes();

      if(!segment.all_memory_deallocated())
         return 1;
   }
   catch(...){
      shared_memory_object::remove(shMemName);
      throw;
   }
   shared_memory_object::remove(shMemName);
   return 0;
}

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

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

#endif   //#ifndef BOOST_INTERPROCESS_TEST_MAP_TEST_HEADER