summaryrefslogtreecommitdiff
path: root/tests/Hash_Map_Manager_Test.cpp
blob: b7aea0c075a92fd61fa7a546452eb637cffe981d (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
// $Id$

// ============================================================================
//
// = LIBRARY
//    tests
//
// = FILENAME
//    Hash_Map_Manager_Test.cpp
//
// = DESCRIPTION
//      This test illustrates the use of <ACE_Hash_Map_Manager> to
//      maintain a hash table using strings.  In addition, it also
//      illustrates how the <ACE_Static_Allocator> works in
//      conjunction with the <ACE_Hash_Map_Manager>.
//
// = AUTHOR
//    James Hu and Douglas C. Schmidt
//
// ============================================================================

#include "test_config.h"
#include "ace/Hash_Map_Manager.h"
#include "ace/Malloc_T.h"
#include "ace/SString.h"
#include "ace/Synch.h"

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

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

#if defined (ACE_HAS_TEMPLATE_SPECIALIZATION)

#define HASH_STRING_ENTRY ACE_Hash_Map_Entry<ASYS_TCHAR *, ASYS_TCHAR *>
#define HASH_STRING_MAP ACE_Hash_Map_Manager<ASYS_TCHAR *, ASYS_TCHAR *, ACE_Null_Mutex>
#define HASH_STRING_ITER ACE_Hash_Map_Iterator<ASYS_TCHAR *, ASYS_TCHAR *, ACE_Null_Mutex>
#define HASH_STRING_REVERSE_ITER ACE_Hash_Map_Reverse_Iterator<ASYS_TCHAR *, ASYS_TCHAR *, ACE_Null_Mutex>

#define MAP_STRING ASYS_TCHAR *
#define ENTRY entry

HASH_STRING_ENTRY::ACE_Hash_Map_Entry (ASYS_TCHAR *const &ext_id,
                                       ASYS_TCHAR *const &int_id,
                                       HASH_STRING_ENTRY *next,
                                       HASH_STRING_ENTRY *prev)
  : ext_id_ (ACE_OS::strdup (ext_id)),
    int_id_ (ACE_OS::strdup (int_id)),
    next_ (next),
    prev_ (prev)
{
  ACE_DEBUG ((LM_DEBUG,
              ASYS_TEXT ("Creating `%s' and `%s'\n"),
              ext_id_,
              int_id_));
}

HASH_STRING_ENTRY::ACE_Hash_Map_Entry (HASH_STRING_ENTRY *next,
                                       HASH_STRING_ENTRY *prev)
  : ext_id_ (0),
    int_id_ (0),
    next_ (next),
    prev_ (prev)
{
}

HASH_STRING_ENTRY::~ACE_Hash_Map_Entry (void)
{
  ASYS_TCHAR *key = ext_id_;
  ASYS_TCHAR *value = int_id_;

  if (key != 0 && value != 0)
    ACE_DEBUG ((LM_DEBUG,
                ASYS_TEXT ("Freeing `%s' and `%s'\n"),
                key,
                value));
  ACE_OS::free (key);
  ACE_OS::free (value);
}

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

long unsigned int
HASH_STRING_MAP::hash (ASYS_TCHAR *const &ext_id)
{
  return ACE::hash_pjw (ext_id);
}

int
HASH_STRING_MAP::equal (ASYS_TCHAR *const &id1, ASYS_TCHAR *const &id2)
{
  return ACE_OS::strcmp (id1, id2) == 0;
}

#else

// Do this if we don't have template specialization.  It's not as efficient

#include "Hash_Map_Manager_Test.h"      // Dumb_String is in here

#define HASH_STRING_ENTRY ACE_Hash_Map_Entry<Dumb_String, Dumb_String>
#define HASH_STRING_MAP \
        ACE_Hash_Map_Manager<Dumb_String, Dumb_String, ACE_Null_Mutex>
#define HASH_STRING_ITER \
        ACE_Hash_Map_Iterator<Dumb_String, Dumb_String, ACE_Null_Mutex>
#define HASH_STRING_REVERSE_ITER \
        ACE_Hash_Map_Reverse_Iterator<Dumb_String, Dumb_String, ACE_Null_Mutex>

#define MAP_STRING Dumb_String
#define ENTRY ((ASYS_TCHAR *) entry)

Dumb_String::Dumb_String (ASYS_TCHAR *s)
  : s_ (s ? ACE_OS::strdup (s) : s),
    copy_ (s ? *(new int (1)) : junk_),
    junk_ (1)
{
}

Dumb_String::Dumb_String (const Dumb_String &ds)
  : s_ (ds.s_),
    copy_ (ds.copy_),
    junk_ (1)
{
  copy_++;
}

Dumb_String::~Dumb_String (void)
{
  if (--copy_ == 0)
    {
      ACE_OS::free (s_);
      if (&copy_ != &junk_)
        delete &copy_;
    }
}

u_long
Dumb_String::hash (void) const
{
  return ACE::hash_pjw (s_);
}

int
Dumb_String::operator== (ASYS_TCHAR const * s) const
{
  return ACE_OS::strcmp (s_, s) == 0;
}

int
Dumb_String::operator== (const Dumb_String &ds) const
{
  return ACE_OS::strcmp (s_, ds.s_) == 0;
}

ASYS_TCHAR *
Dumb_String::operator= (const Dumb_String &ds)
{
  this->Dumb_String::~Dumb_String ();
  new (this) Dumb_String (ds);
  return s_;
}

Dumb_String::operator ASYS_TCHAR * (void) const
{
  return s_;
}

// Note that in this version, you will not get the Creating and
// Freeing diagnostic messages.

#endif /* ACE_HAS_TEMPLATE_SPECIALIZATION */

struct String_Table
{
  ASYS_TCHAR *key_;
  ASYS_TCHAR *value_;
};

static String_Table string_table[] =
{
  { 
    ASYS_TEXT ("hello"),
    ASYS_TEXT ("guten Tag")
  },
  { 
    ASYS_TEXT ("goodbye"),
    ASYS_TEXT ("auf wiedersehen")
  },
  { 
    ASYS_TEXT ("funny"),
    ASYS_TEXT ("lustig")
  },
  { 
    0,
    0
  }
};

static const size_t STRING_TABLE_SIZE = 3;
static const size_t MAX_HASH = 6;

// @@ The following requires too much internal implementation
// information about the <ACE_Hash_Map_Manager>.  We need to figure
// out how to simplify this.
static const size_t String_Table_size = sizeof (HASH_STRING_ENTRY) * (STRING_TABLE_SIZE + MAX_HASH);
static ACE_Static_Allocator<String_Table_size> alloc;

static int
run_test (void)
{
  alloc.dump ();

  HASH_STRING_MAP hash (MAX_HASH, &alloc);

  size_t i;

  for (i = 0; string_table[i].key_ != 0; i++)
    if (hash.bind (string_table[i].key_,
                   string_table[i].value_) == -1)
      ACE_ERROR_RETURN ((LM_ERROR,
                         ASYS_TEXT ("%p failed for %s \n"),
                         ASYS_TEXT ("bind"),
                         string_table[i].key_), -1);

  MAP_STRING entry;

  for (i = 0; string_table[i].key_ != 0; i++)
    if (hash.find (string_table[i].key_,
                   entry) == 0)
      ACE_DEBUG ((LM_DEBUG,
                  ASYS_TEXT ("`%s' found `%s'\n"),
                  string_table[i].key_,
                  ENTRY));
    else
      ACE_ERROR_RETURN ((LM_ERROR,
                         ASYS_TEXT ("`%s' not found\n"),
                         string_table[i].key_),
                        -1);

  // Let's test the iterator while we are at it.
  {
    HASH_STRING_ENTRY *entry;
    size_t i = 0;

    for (HASH_STRING_ITER hash_iter (hash);
         hash_iter.next (entry) != 0;
         hash_iter.advance ())
      {
        ACE_DEBUG ((LM_DEBUG,
                    ASYS_TEXT ("iterating (%d): [%s, %s]\n"),
                    i,
                    (ASYS_TCHAR *) entry->ext_id_,
                    (ASYS_TCHAR *) entry->int_id_));
        i++;
      }
  }

  hash.unbind (string_table[2].key_, entry);

  for (i = 0; string_table[i].key_ != 0; i++)
    if (hash.find (string_table[i].key_,
                   entry) == 0)
      ACE_DEBUG ((LM_DEBUG,
                  ASYS_TEXT ("`%s' found `%s'\n"),
                  string_table[i].key_,
                  ENTRY));
    else if (i != 2)
      ACE_ERROR_RETURN ((LM_ERROR,
                         ASYS_TEXT ("`%s' not found\n"),
                         string_table[i].key_),
                        -1);

  // Let's test the iterator backwards.
  {
    HASH_STRING_ENTRY *entry;
    size_t i = 0;

    for (HASH_STRING_REVERSE_ITER hash_iter (hash);
         hash_iter.next (entry) != 0;
         hash_iter.advance ())
      {
        ACE_DEBUG ((LM_DEBUG,
                    ASYS_TEXT ("iterating (%d): [%s, %s]\n"),
                    i,
                    (ASYS_TCHAR *) entry->ext_id_,
                    (ASYS_TCHAR *) entry->int_id_));
        i++;
      }
  }

  alloc.dump ();
  return 0;
}

int
main (int, ASYS_TCHAR *[])
{
  ACE_START_TEST (ASYS_TEXT ("Hash_Map_Manager_Test"));

  run_test ();

  ACE_END_TEST;

  return 0;
}

#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
template class ACE_Hash_Map_Entry<MAP_STRING, MAP_STRING>;
template class ACE_Hash_Map_Manager<MAP_STRING, MAP_STRING, ACE_Null_Mutex>;
template class ACE_Hash_Map_Iterator<MAP_STRING, MAP_STRING, ACE_Null_Mutex>;
template class ACE_Hash_Map_Iterator_Base<MAP_STRING, MAP_STRING, ACE_Null_Mutex>;
template class ACE_Hash_Map_Reverse_Iterator<MAP_STRING, MAP_STRING, ACE_Null_Mutex>;
template class ACE_Static_Allocator<String_Table_size>;
#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
#pragma instantiate ACE_Hash_Map_Entry<MAP_STRING, MAP_STRING>
#pragma instantiate ACE_Hash_Map_Manager<MAP_STRING, MAP_STRING, ACE_Null_Mutex>
#pragma instantiate ACE_Hash_Map_Iterator<MAP_STRING, MAP_STRING, ACE_Null_Mutex>
#pragma instantiate ACE_Hash_Map_Iterator_Base<MAP_STRING, MAP_STRING, ACE_Null_Mutex>
#pragma instantiate ACE_Hash_Map_Reverse_Iterator<MAP_STRING, MAP_STRING, ACE_Null_Mutex>
#pragma instantiate ACE_Static_Allocator<String_Table_size>
#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */