summaryrefslogtreecommitdiff
path: root/trunk/ACE/tests/Hash_Multi_Map_Manager_Test.cpp
blob: 2e883a7115264ae3393f06200a074a48831b3149 (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
// $Id$

// ============================================================================
//
// = LIBRARY
//    tests
//
// = FILENAME
//    Hash_Multi_Map_Manager_Test.cpp
//
// = DESCRIPTION
//      This test illustrates the use of <ACE_Hash_Multi_Map_Manager> to
//      maintain a hash table using strings.
//
// = AUTHOR
//    Shanshan Jiang <shanshan.jiang@vanderbilt.edu>
//
// ============================================================================

#include "test_config.h"
#include "ace/Hash_Multi_Map_Manager_T.h"
#include "ace/Null_Mutex.h"

ACE_RCSID(tests, Hash_Multi_Map_Manager_Test, "$Id$")

typedef ACE_Hash_Multi_Map_Manager<const ACE_TCHAR *,
                                   const ACE_TCHAR *,
                                   ACE_Hash<const ACE_TCHAR *>,
                                   ACE_Equal_To<const ACE_TCHAR *>,
                                   ACE_Null_Mutex> HASH_STRING_MAP;

typedef HASH_STRING_MAP::ENTRY HASH_STRING_ENTRY;
typedef HASH_STRING_ENTRY::VALUE_SET HASH_STRING_VALUE_SET;
typedef HASH_STRING_ENTRY::VALUE_SET_ITERATOR HASH_STRING_VALUE_SET_ITERATOR;
typedef HASH_STRING_MAP::ITERATOR HASH_STRING_ITERATOR;
typedef HASH_STRING_MAP::CONST_ITERATOR HASH_STRING_CONST_ITERATOR;
typedef HASH_STRING_MAP::REVERSE_ITERATOR HASH_STRING_REVERSE_ITERATOR;

static const ACE_TCHAR *color_sorts[] = {ACE_TEXT ("warm"), ACE_TEXT ("cold"), ACE_TEXT ("neutral"), 0};
static const ACE_TCHAR *warm_colors[] = {ACE_TEXT ("red"), ACE_TEXT ("yellow"), ACE_TEXT ("orange"), 0};
static const ACE_TCHAR *cold_colors[] = {ACE_TEXT ("blue"), ACE_TEXT ("cyan"), 0};
static const ACE_TCHAR *neutral_colors[] = {ACE_TEXT ("green"), ACE_TEXT ("purple"), 0};

static int
run_test (void)
{
  HASH_STRING_MAP colors;

  size_t i;

  // Check the <bind> operation.
  for (i = 0; warm_colors[i] != 0; i++)
    {
      if (colors.bind (color_sorts[0],
                       warm_colors[i]) == -1)
        ACE_ERROR_RETURN ((LM_ERROR,
                          ACE_TEXT ("bind failed for %s in %s\n"),
                          warm_colors[i], 
                          color_sorts[0]),
                          -1);
    }

  HASH_STRING_ENTRY *entry = 0;
  for (i = 0; cold_colors[i] != 0; i++)
    {
      if (colors.bind (color_sorts[1],
                       cold_colors[i],
                       entry) == -1)
        ACE_ERROR_RETURN ((LM_ERROR,
                          ACE_TEXT ("bind failed for %s in %s\n"),
                          cold_colors[i], 
                          color_sorts[1]),
                          -1);
    }
  
  HASH_STRING_VALUE_SET value_set;
  entry = 0;
  for (i = 0; neutral_colors[i] != 0; i++)
    value_set.insert (neutral_colors[i]);
  if (colors.bind (color_sorts[2],
                   value_set,
                   entry) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT ("bind failed for %s\n"),
                       color_sorts[2]),
                       -1);

  // Check the <trybind> operation.
  value_set.reset ();
  if (colors.trybind (color_sorts[2],
                      value_set) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT ("bind failed for %s\n"),
                       color_sorts[2]),
                       -1);

  value_set.reset ();
  entry = 0;
  if (colors.trybind (color_sorts[2],
                      value_set,
                      entry) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT ("bind failed for %s\n"),
                       color_sorts[2]),
                       -1);

  value_set.reset ();
  entry = 0;
  if (colors.trybind (color_sorts[2],
                      value_set,
                      entry) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT ("bind failed for %s\n"),
                       color_sorts[2]),
                       -1);

  // Check the <rebind> operation.
  value_set.reset ();
  value_set.insert (neutral_colors[0]);
  if (colors.rebind (color_sorts[2],
                     value_set) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT ("bind failed for %s\n"),
                       color_sorts[2]),
                       -1);

  value_set.reset ();
  value_set.insert (neutral_colors[1]);
  entry = 0;
  if (colors.rebind (color_sorts[2],
                     value_set,
                     entry) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT ("bind failed for %s\n"),
                       color_sorts[2]),
                       -1);

  value_set.reset ();
  value_set.insert (neutral_colors[0]);
  HASH_STRING_VALUE_SET old_value_set;
  if (colors.rebind (color_sorts[2],
                     value_set,
                     old_value_set) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT ("bind failed for %s\n"),
                       color_sorts[2]),
                       -1);

  value_set.reset ();
  value_set.insert (neutral_colors[1]);
  old_value_set.reset ();
  entry = 0;
  if (colors.rebind (color_sorts[2],
                     value_set,
                     old_value_set,
                     entry) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT ("bind failed for %s\n"),
                       color_sorts[2]),
                       -1);

  value_set.reset ();
  value_set.insert (neutral_colors[0]);
  const ACE_TCHAR *old_key;
  old_value_set.reset ();
  if (colors.rebind (color_sorts[2],
                     value_set,
                     old_key,
                     old_value_set) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT ("bind failed for %s\n"),
                       color_sorts[2]),
                       -1);

  value_set.reset ();
  for (i = 0; neutral_colors[i] != 0; i++)
    value_set.insert (neutral_colors[i]);
  old_key = 0;
  old_value_set.reset ();
  entry = 0;
  if (colors.rebind (color_sorts[2],
                     value_set,
                     old_key,
                     old_value_set,
                     entry) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT ("bind failed for %s\n"),
                       color_sorts[2]),
                       -1);

  // Check the <find> operation.
  for (i = 0; color_sorts[i] != 0; i++)
    if (colors.find (color_sorts[i]) == 0)
      ACE_DEBUG ((LM_DEBUG,
                  ACE_TEXT ("%s found\n"),
                  color_sorts[i]));
    else
      ACE_ERROR_RETURN ((LM_ERROR,
                         ACE_TEXT ("%s not found\n"),
                         color_sorts[i]),
                        -1);

  entry = 0;
  for (i = 0; color_sorts[i] != 0; i++)
    if (colors.find (color_sorts[i],
                     entry) == 0)
      ACE_DEBUG ((LM_DEBUG,
                  ACE_TEXT ("%s found\n"),
                  color_sorts[i]));
    else
      ACE_ERROR_RETURN ((LM_ERROR,
                         ACE_TEXT ("%s not found\n"),
                         color_sorts[i]),
                        -1);

  value_set.reset ();
  for (i = 0; color_sorts[i] != 0; i++)
    if (colors.find (color_sorts[i],
                     value_set) == 0)
      ACE_DEBUG ((LM_DEBUG,
                  ACE_TEXT ("%s found\n"),
                  color_sorts[i]));
    else
      ACE_ERROR_RETURN ((LM_ERROR,
                         ACE_TEXT ("%s not found\n"),
                         color_sorts[i]),
                        -1);

  for (i = 0; warm_colors[i] != 0; i++)
    if (colors.find (color_sorts[0],
                     warm_colors[i]) == 0)
      ACE_DEBUG ((LM_DEBUG,
                  ACE_TEXT ("%s color %s found\n"),
                  color_sorts[0],
                  warm_colors[i]));
    else
      ACE_ERROR_RETURN ((LM_ERROR,
                         ACE_TEXT ("%s color %s not found\n"),
                         color_sorts[0],
                         warm_colors[i]),
                        -1);

  // Check the iterator.
  i = 0;
  entry = 0;
  for (HASH_STRING_ITERATOR hash_iter (colors);
       hash_iter.next (entry) != 0;
       hash_iter.advance ())
    {
      ACE_DEBUG ((LM_DEBUG,
                  ACE_TEXT ("iterating (%d): %s:\n"),
                  i,
                  entry->ext_id_));
      i++;

      for (HASH_STRING_VALUE_SET_ITERATOR iter (entry->int_id_set_);
           iter != entry->int_id_set_.end ();
           iter++)
        {
          ACE_DEBUG ((LM_DEBUG,
                      ACE_TEXT ("%s  \n"),
                      *iter));
        }
    }
  
  // Check the unbind operation.
  if (colors.unbind (color_sorts[0]) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT ("unbind failed for %s\n"),
                       color_sorts[1]),
                      -1);
  for (i = 0; warm_colors[i] != 0; i++)
    {
      if (colors.bind (color_sorts[0],
                       warm_colors[i]) == -1)
        ACE_ERROR_RETURN ((LM_ERROR,
                          ACE_TEXT ("bind failed for %s in %s\n"),
                          warm_colors[i], 
                          color_sorts[0]),
                          -1);
    }

  value_set.reset ();
  if (colors.unbind (color_sorts[0], value_set) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT ("unbind failed for %s\n"),
                       color_sorts[1]),
                      -1);
  for (i = 0; warm_colors[i] != 0; i++)
    {
      if (colors.bind (color_sorts[0],
                       warm_colors[i]) == -1)
        ACE_ERROR_RETURN ((LM_ERROR,
                          ACE_TEXT ("bind failed for %s in %s\n"),
                          warm_colors[i], 
                          color_sorts[0]),
                          -1);
    }

  for (i = 0; warm_colors[i] != 0; i++)
    {
      if (colors.unbind (color_sorts[0],
                         warm_colors[i]) == -1)
        ACE_ERROR_RETURN ((LM_ERROR,
                           ACE_TEXT ("unbind failed for %s in %s\n"),
                           warm_colors[i], 
                           color_sorts[0]),
                          -1);
    }

  // Check the reverse iterator.
  i = 0;
  entry = 0;
  for (HASH_STRING_REVERSE_ITERATOR hash_iter (colors);
       hash_iter.next (entry) != 0;
       hash_iter.advance ())
    {
      ACE_DEBUG ((LM_DEBUG,
                  ACE_TEXT ("reverse iterating (%d): %s\n"),
                  i,
                  entry->ext_id_));
      i++;

      for (HASH_STRING_VALUE_SET_ITERATOR iter (entry->int_id_set_);
           iter != entry->int_id_set_.end ();
           iter++)
        {
          ACE_DEBUG ((LM_DEBUG,
                      ACE_TEXT ("%s  \n"),
                      *iter));
        }
    }

  entry = 0;
  colors.find (color_sorts[1], entry);
  if (colors.unbind (entry) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT ("unbind failed for %s\n"),
                       color_sorts[1]),
                      -1);

  // Check the const iterator.
  i = 0;
  entry = 0;
  for (HASH_STRING_CONST_ITERATOR hash_iter (colors);
       hash_iter.next (entry) != 0;
       hash_iter.advance ())
    {
      ACE_DEBUG ((LM_DEBUG,
                  ACE_TEXT ("const iterating (%d): %s\n"),
                  i,
                  entry->ext_id_));
      i++;

      for (HASH_STRING_VALUE_SET_ITERATOR iter (entry->int_id_set_);
           iter != entry->int_id_set_.end ();
           iter++)
        {
          ACE_DEBUG ((LM_DEBUG,
                      ACE_TEXT ("%s  \n"),
                      *iter));
        }
    }

  // Check the unbind_all operation.
  if (colors.unbind_all () != 0)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT ("unbind_all failed\n")),
                       -1);

  return 0;
}

int
run_main (int, ACE_TCHAR *[])
{
  ACE_START_TEST (ACE_TEXT ("Hash_Multi_Map_Manager_Test"));

  run_test ();

  ACE_END_TEST;

  return 0;
}