summaryrefslogtreecommitdiff
path: root/tests/Hash_Map_Manager_Test.cpp
blob: 196b1f0a12de295e5ddba42af0f53729120ec523 (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
// $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. No command line arguments
//      are needed to run this program.
//
// = AUTHOR
//    James Hu
//
// ============================================================================

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

#if defined (ACE_HAS_TEMPLATE_SPECIALIZATION)

#define HASH_STRING_ENTRY ACE_Hash_Map_Entry<char *, char *>
#define HASH_STRING_MAP ACE_Hash_Map_Manager<char *, char *, ACE_Null_Mutex>

#define MAP_STRING char *
#define ENTRY entry

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

HASH_STRING_ENTRY::~ACE_Hash_Map_Entry (void)
{
  ACE_DEBUG ((LM_DEBUG, "Freeing `%s' and `%s'\n", ext_id_, int_id_));
  ACE_OS::free (ext_id_);
  ACE_OS::free (int_id_);
}

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

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

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

#else

// Do this if we don't have template specialization

#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 MAP_STRING Dumb_String
#define ENTRY ((char *)entry)

Dumb_String::Dumb_String (char *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== (char 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;
}

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

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

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

#endif /* ACE_HAS_TEMPLATE_SPECIALIZATION */

static const int MAX_HASH = 256;

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

  // Scoping below so that result of destruction can be seen in the log.
  {
    HASH_STRING_MAP hash (MAX_HASH);

    hash.bind ("hello", "guten Tag");
    hash.bind ("goodbye", "auf wiedersehen");
    hash.bind ("funny", "lustig");

    MAP_STRING entry;

    if (hash.find ("hello", entry) == 0)
      ACE_DEBUG ((LM_DEBUG, "`%s' found `%s'\n", "hello", ENTRY));
    if (hash.find ("goodbye", entry) == 0)
      ACE_DEBUG ((LM_DEBUG, "`%s' found `%s'\n", "goodbye", ENTRY));
    if (hash.find ("funny", entry) == 0)
      ACE_DEBUG ((LM_DEBUG, "`%s' found `%s'\n", "funny", ENTRY));

    hash.unbind ("goodbye", entry);

    if (hash.find ("hello", entry) == 0)
      ACE_DEBUG ((LM_DEBUG, "`%s' found `%s'\n", "hello", ENTRY));
    if (hash.find ("goodbye", entry) == 0)
      ACE_DEBUG ((LM_DEBUG, "OOPS!  `%s' found `%s'\n", "goodbye", ENTRY));
    if (hash.find ("funny", entry) == 0)
      ACE_DEBUG ((LM_DEBUG, "`%s' found `%s'\n", "funny", ENTRY));
  }

  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>;
#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>
#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */