summaryrefslogtreecommitdiff
path: root/tests/Map_Manager_Test.cpp
blob: bdd96a2e804a803aa83846c902b090b5d9d6edfd (plain)
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
// $Id$

// ============================================================================
//
// = LIBRARY
//    tests
//
// = FILENAME
//    Map_Manager_Test.cpp
//
// = DESCRIPTION
//     This is a simple test of the <ACE_Map_Manager> and
//     <ACE_Hash_Map_Manager> that illustrates how to use the forward
//     and reverse iterators.
//
// = AUTHOR
//    Irfan Pyarali and Douglas C. Schmidt
//
// ============================================================================

#include "test_config.h"

#if defined (ACE_HAS_TEMPLATE_SPECIALIZATION)

#include "ace/Map_Manager.h"
#include "ace/Hash_Map_Manager.h"
#include "ace/Profile_Timer.h"
#include "ace/Synch.h"

#if defined(__BORLANDC__) && __BORLANDC__ >= 0x0530
USELIB("..\ace\aced.lib");
//---------------------------------------------------------------------------
#endif /* defined(__BORLANDC__) && __BORLANDC__ >= 0x0530 */

typedef ACE_Null_Mutex MUTEX;
typedef size_t TYPE;

#if defined (ACE_HAS_TEMPLATE_SPECIALIZATION)

// We need this template specialization since TYPE is defined as a
// size_t, which doesn't have a hash () method defined on it.

u_long
ACE_Hash_Map_Manager<TYPE, TYPE, MUTEX>::hash (const TYPE& ext_id)
{
  return ext_id;
}

#endif /* ACE_HAS_TEMPLATE_SPECIALIZATION */

typedef ACE_Map_Manager <TYPE, TYPE, MUTEX> MAP_MANAGER;
typedef ACE_Map_Iterator <TYPE, TYPE, MUTEX> ITERATOR;
typedef ACE_Map_Reverse_Iterator <TYPE, TYPE, MUTEX> REVERSE_ITERATOR;
typedef ACE_Map_Entry <TYPE, TYPE> ENTRY;
typedef ACE_Hash_Map_Manager <TYPE, TYPE, MUTEX> HASH_MAP_MANAGER;
typedef ACE_Hash_Map_Iterator <TYPE, TYPE, MUTEX> HASH_ITERATOR;
typedef ACE_Hash_Map_Reverse_Iterator <TYPE, TYPE, MUTEX> HASH_REVERSE_ITERATOR;
typedef ACE_Hash_Map_Entry <TYPE, TYPE> HASH_ENTRY;

static void
test_hash_map_manager (size_t table_size, size_t iterations)
{
  HASH_MAP_MANAGER map (table_size);
  TYPE i;
  TYPE j;
  ssize_t k;

  for (i = 0; i < iterations; i++)
    ACE_ASSERT (map.bind (i, i) != -1);

  {
    i = 0;

    for (HASH_ITERATOR iter = map.begin (), end = map.end ();
         iter != end;
         iter++, i++)
      {
        HASH_ENTRY &entry = *iter;
        ACE_DEBUG ((LM_DEBUG,
                    ASYS_TEXT ("(%d|%d|%d)"),
                    i,
                    entry.ext_id_,
                    entry.int_id_));
      }

    ACE_DEBUG ((LM_DEBUG, ASYS_TEXT ("\n")));
    ACE_ASSERT (i == iterations);
  }

  {
    HASH_ITERATOR iterator (map);

    HASH_ENTRY *entry = 0;

    for (entry = 0, i = 0;
         iterator.next (entry) != 0;
         iterator.advance (), i++)
      ACE_DEBUG ((LM_DEBUG,
                  ASYS_TEXT ("(%d|%d|%d)"),
                  i,
                  entry->ext_id_,
                  entry->int_id_));

    ACE_DEBUG ((LM_DEBUG, ASYS_TEXT ("\n")));
    ACE_ASSERT (i == iterations);
  }

  {
    k = iterations - 1;

    for (HASH_REVERSE_ITERATOR iter = map.rbegin (), rend = map.rend ();
         iter != rend;
         iter++, k--)
      {
        HASH_ENTRY &entry = *iter;
        ACE_DEBUG ((LM_DEBUG,
                    ASYS_TEXT ("(%d|%d|%d)"),
                    k,
                    entry.ext_id_,
                    entry.int_id_));
      }

    ACE_DEBUG ((LM_DEBUG, ASYS_TEXT ("\n")));
    ACE_ASSERT (k == -1);
  }

  {
    HASH_REVERSE_ITERATOR iterator (map);

    HASH_ENTRY *entry = 0;
    for (entry = 0, k = iterations - 1;
         iterator.next (entry) != 0;
         iterator.advance (), k--)
      ACE_DEBUG ((LM_DEBUG,
                  ASYS_TEXT ("(%d|%d|%d)"),
                  k,
                  entry->ext_id_,
                  entry->int_id_));
    ACE_DEBUG ((LM_DEBUG, ASYS_TEXT ("\n")));
    ACE_ASSERT (k == -1);
  }

  {
    i = 0;

    for (HASH_MAP_MANAGER::iterator iter = map.begin (), end = map.end ();
         iter != end;
         iter++, i++)
      {
        HASH_MAP_MANAGER::ENTRY &entry = *iter;
        ACE_DEBUG ((LM_DEBUG,
                    ASYS_TEXT ("(%d|%d|%d)"),
                    i,
                    entry.ext_id_,
                    entry.int_id_));
      }
    ACE_DEBUG ((LM_DEBUG, ASYS_TEXT ("\n")));
    ACE_ASSERT (i == iterations);
  }
  {
    HASH_MAP_MANAGER::ITERATOR iterator (map);

    HASH_MAP_MANAGER::ENTRY *entry = 0;
    for (entry = 0, i = 0;
         iterator.next (entry) != 0;
         iterator.advance (), i++)
      ACE_DEBUG ((LM_DEBUG,
                  ASYS_TEXT ("(%d|%d|%d)"),
                  i,
                  entry->ext_id_,
                  entry->int_id_));
    ACE_DEBUG ((LM_DEBUG, ASYS_TEXT ("\n")));
    ACE_ASSERT (i == iterations);
  }

  {
    k = iterations - 1;

    for (HASH_MAP_MANAGER::reverse_iterator iter = map.rbegin (), rend = map.rend ();
         iter != rend;
         iter++, k--)
      {
        HASH_MAP_MANAGER::ENTRY &entry = *iter;
        ACE_UNUSED_ARG (entry);
        ACE_DEBUG ((LM_DEBUG,
                    ASYS_TEXT ("(%d|%d|%d)"),
                    k,
                    entry.ext_id_,
                    entry.int_id_));
      }
    ACE_DEBUG ((LM_DEBUG, ASYS_TEXT ("\n")));
    ACE_ASSERT (k == -1);
  }
  {
    HASH_MAP_MANAGER::REVERSE_ITERATOR iterator (map);

    HASH_MAP_MANAGER::ENTRY *entry = 0;
    for (entry = 0, k = iterations - 1;
         iterator.next (entry) != 0;
         iterator.advance (), k--)
      ACE_DEBUG ((LM_DEBUG,
                  ASYS_TEXT ("(%d|%d|%d)"),
                  k,
                  entry->ext_id_,
                  entry->int_id_));
    ACE_DEBUG ((LM_DEBUG, ASYS_TEXT ("\n")));
    ACE_ASSERT (k == -1);
  }

  for (i = 0; i < iterations; i++)
    {
      ACE_ASSERT (map.find (i, j) != -1);
      ACE_ASSERT (i == j);
    }

  for (i = 0; i < iterations; i++)
    ACE_ASSERT (map.unbind (i) != -1);

  ACE_ASSERT (map.current_size () == 0);
}

static void
test_map_manager (size_t table_size, size_t iterations)
{
  MAP_MANAGER map (table_size);
  TYPE i;
  TYPE j;
  ssize_t k;

  for (i = 0; i < iterations; i++)
    ACE_ASSERT (map.bind (i, i) != -1);

  {
    i = 0;
    for (ITERATOR iter = map.begin (), end = map.end ();
         iter != end;
         i++, iter ++)
      {
        ENTRY &entry = *iter;
        ACE_DEBUG ((LM_DEBUG,
                    ASYS_TEXT ("(%d|%d|%d)"),
                    i,
                    entry.ext_id_,
                    entry.int_id_));
      }
    ACE_DEBUG ((LM_DEBUG, ASYS_TEXT ("\n")));
    ACE_ASSERT (i == iterations);
  }
  {
    ITERATOR iterator (map);
    ENTRY *entry = 0;
    for (entry = 0, i = 0;
         iterator.next (entry) != 0;
         iterator.advance (), i++)
      ACE_DEBUG ((LM_DEBUG,
                  ASYS_TEXT ("(%d|%d|%d)"),
                  i,
                  entry->ext_id_,
                  entry->int_id_));
    ACE_DEBUG ((LM_DEBUG, ASYS_TEXT ("\n")));
    ACE_ASSERT (i == iterations);
  }

  {
    ENTRY entry;
    k = iterations - 1;
    for (REVERSE_ITERATOR iter = map.rbegin (), rend = map.rend ();
         iter != rend;
         k--, iter ++)
      {
        entry = *iter;
        ACE_DEBUG ((LM_DEBUG, ASYS_TEXT ("%u "), entry.int_id_));
      }
    ACE_DEBUG ((LM_DEBUG, ASYS_TEXT ("\n")));
    ACE_ASSERT (k == -1);
  }
  {
    REVERSE_ITERATOR iterator (map);

    ENTRY *entry = 0;
    for (entry = 0, k = iterations - 1;
         iterator.next (entry) != 0;
         iterator.advance (), k--)
      ACE_DEBUG ((LM_DEBUG,
                  ASYS_TEXT ("(%d|%d|%d)"),
                  k,
                  entry->ext_id_,
                  entry->int_id_));
    ACE_DEBUG ((LM_DEBUG, ASYS_TEXT ("\n")));
    ACE_ASSERT (k == -1);
  }
  {
    MAP_MANAGER::ENTRY entry;
    i = 0;
    for (MAP_MANAGER::iterator iter = map.begin (), end = map.end ();
         iter != end;
         i++, iter ++)
      {
        entry = *iter;
        ACE_DEBUG ((LM_DEBUG, ASYS_TEXT ("%u "), entry.int_id_));
      }
    ACE_DEBUG ((LM_DEBUG, ASYS_TEXT ("\n")));
    ACE_ASSERT (i == iterations);
  }
  {
    MAP_MANAGER::ITERATOR iterator (map);

    MAP_MANAGER::ENTRY *entry = 0;
    for (entry = 0, i = 0;
         iterator.next (entry) != 0;
         iterator.advance (), i++)
      ACE_DEBUG ((LM_DEBUG,
                  ASYS_TEXT ("(%d|%d|%d)"),
                  i,
                  entry->ext_id_,
                  entry->int_id_));
    ACE_DEBUG ((LM_DEBUG, ASYS_TEXT ("\n")));
    ACE_ASSERT (i == iterations);
  }
  {
    ENTRY entry;
    k = iterations - 1;

    for (MAP_MANAGER::reverse_iterator iter = map.rbegin (), rend = map.rend ();
         iter != rend;
         k--, iter ++)
      {
        entry = *iter;
        ACE_DEBUG ((LM_DEBUG, ASYS_TEXT ("%u "), entry.int_id_));
      }
    ACE_DEBUG ((LM_DEBUG, ASYS_TEXT ("\n")));
    ACE_ASSERT (k == -1);
  }

  {
    MAP_MANAGER::REVERSE_ITERATOR iterator (map);

    MAP_MANAGER::ENTRY *entry = 0;
    for (entry = 0, k = iterations - 1;
         iterator.next (entry) != 0;
         iterator.advance (), k--)
      ACE_DEBUG ((LM_DEBUG,
                  ASYS_TEXT ("(%d|%d|%d)"),
                  k,
                  entry->ext_id_,
                  entry->int_id_));
    ACE_DEBUG ((LM_DEBUG, ASYS_TEXT ("\n")));
    ACE_ASSERT (k == -1);
  }

  for (i = 0; i < iterations; i++)
    {
      ACE_ASSERT (map.find (i, j) != -1);
      ACE_ASSERT (i == j);
    }

  for (i = 0; i < iterations; i++)
    ACE_ASSERT (map.unbind (i) != -1);

  ACE_ASSERT (map.current_size () == 0);
}

static void
run_test (void (*ptf) (size_t, size_t),
          size_t table_size,
          size_t iterations,
          const ASYS_TCHAR *test_name)
{
  ACE_Profile_Timer timer;
  timer.start ();

  (*ptf) (table_size, iterations);

  timer.stop ();

  ACE_Profile_Timer::ACE_Elapsed_Time et;

  timer.elapsed_time (et);

  ACE_DEBUG ((LM_DEBUG,
              ASYS_TEXT ("time to test a map of size %d for %d iterations using %s\n"),
              table_size,
              iterations,
              test_name));

  ACE_DEBUG ((LM_DEBUG,
              ASYS_TEXT ("real time = %f secs, user time = %f secs, system time = %f secs\n"),
              et.real_time,
              et.user_time,
              et.system_time));

  ACE_DEBUG ((LM_DEBUG,
              ASYS_TEXT ("time per call = %f usecs\n"),
              (et.real_time / double (iterations)) * 1000000));
}

int
main (int argc, ASYS_TCHAR *argv[])
{
  ACE_START_TEST (ASYS_TEXT ("Map_Manager_Test"));
  ACE_LOG_MSG->clr_flags (ACE_Log_Msg::VERBOSE_LITE);

  size_t table_size = ACE_MAX_ITERATIONS / 2;
  size_t iterations = ACE_MAX_ITERATIONS;

  if (argc > 1)
    table_size = ACE_OS::atoi (argv[1]);

  if (argc > 2)
    iterations = ACE_OS::atoi (argv[2]);

  // Test the <ACE_Hash_Map_Manager>.
  run_test (&test_hash_map_manager,
            table_size,
            iterations,
            ASYS_TEXT ("Hash_Map_Manager"));

  // Test the <ACE_Map_Manager>.
  run_test (&test_map_manager,
            table_size,
            iterations,
            ASYS_TEXT ("Map_Manager"));

  ACE_LOG_MSG->set_flags (ACE_Log_Msg::VERBOSE_LITE);
  ACE_END_TEST;
  return 0;
}

#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
template class ACE_Hash_Map_Manager<TYPE, TYPE, MUTEX>;
template class ACE_Hash_Map_Iterator_Base<TYPE, TYPE, MUTEX>;
template class ACE_Hash_Map_Iterator<TYPE, TYPE, MUTEX>;
template class ACE_Hash_Map_Reverse_Iterator<TYPE, TYPE, MUTEX>;
template class ACE_Hash_Map_Entry<TYPE, TYPE>;
template class ACE_Map_Manager<TYPE, TYPE, MUTEX>;
template class ACE_Map_Iterator_Base<TYPE, TYPE, MUTEX>;
template class ACE_Map_Iterator<TYPE, TYPE, MUTEX>;
template class ACE_Map_Reverse_Iterator<TYPE, TYPE, MUTEX>;
template class ACE_Map_Entry<TYPE, TYPE>;
#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
#pragma instantiate ACE_Hash_Map_Manager<TYPE, TYPE, MUTEX>
#pragma instantiate ACE_Hash_Map_Iterator_Base<TYPE, TYPE, MUTEX>
#pragma instantiate ACE_Hash_Map_Iterator<TYPE, TYPE, MUTEX>
#pragma instantiate ACE_Hash_Map_Reverse_Iterator<TYPE, TYPE, MUTEX>
#pragma instantiate ACE_Hash_Map_Entry<TYPE, TYPE>
#pragma instantiate ACE_Map_Manager<TYPE, TYPE, MUTEX>
#pragma instantiate ACE_Map_Iterator_Base<TYPE, TYPE, MUTEX>
#pragma instantiate ACE_Map_Iterator<TYPE, TYPE, MUTEX>
#pragma instantiate ACE_Map_Reverse_Iterator<TYPE, TYPE, MUTEX>
#pragma instantiate ACE_Map_Entry<TYPE, TYPE>
#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */

#else  /* ACE_HAS_TEMPLATE_SPECIALIZATION */

int
main (int argc, ASYS_TCHAR *argv[])
{
  ACE_START_TEST (ASYS_TEXT ("Map_Manager_Test"));

  ACE_UNUSED_ARG (argc);
  ACE_UNUSED_ARG (argv);
  ACE_ERROR ((LM_ERROR, ASYS_TEXT ("Template specializations not supported on this platform\n")));

  ACE_END_TEST;
  return 0;
}

#endif /* ACE_HAS_TEMPLATE_SPECIALIZATION */