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
469
470
471
472
473
474
475
476
477
|
//=============================================================================
/**
* @file Array_Map_Test.cpp
*
* Regression test for ACE_Array_Map.
*
* @author Ossama Othman
*/
//=============================================================================
#include "test_config.h"
#include "ace/SString.h"
#include "ace/Array_Map.h"
#include <algorithm>
//#include <map> /* For STL portability testing. */
static char const letters[] =
{ 'A', 'C', 'E', ' ', 'r', 'u', 'l', 'e', 'z', '!' };
static ACE_TString const words[] =
{
ACE_TEXT ("alpha"), // A
ACE_TEXT ("charlie"), // C
ACE_TEXT ("echo"), // E
ACE_TEXT (" "), //
ACE_TEXT ("romeo"), // r
ACE_TEXT ("uniform"), // u
ACE_TEXT ("lima"), // l
ACE_TEXT ("echo"), // e
ACE_TEXT ("zulu"), // z
ACE_TEXT ("!") // !
};
static size_t const letters_len = sizeof (letters) / sizeof (letters[0]);
static size_t const words_len = sizeof (words) / sizeof (words[0]);
// --------------------------------------------------------------
bool
insertion_removal_test ()
{
// Instantiate the map.
typedef ACE_Array_Map<char, ACE_TString> Map;
Map phonetic[2];
ACE_TEST_ASSERT (phonetic[0] == phonetic[1]); // Sanity check.
static size_t const phonetic_len =
sizeof (phonetic) / sizeof (phonetic[0]);
unsigned int count = 1;
// Test map insertion.
for (Map * m = phonetic;
m != phonetic + phonetic_len;
++m, ++count)
{
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("------------- Populating map %u -------------\n"),
count));
ACE_TString const * word = words;
for (char const * i = letters;
i != letters + letters_len;
++i, ++word)
{
std::pair<Map::iterator, bool> const result =
m->insert (std::make_pair (*i, *word));
if (result.second)
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("Inserted \"%c | %s\"\n"),
*i,
word->c_str ()));
else
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("Insertion of \"%c | %s\" failed.\n"),
*i,
word->c_str ()),
false);
}
ACE_TEST_ASSERT (m->size () == letters_len);
}
// Test equality of identically populated maps.
if (!(phonetic[0] == phonetic[1]))
{
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("Equality comparison of two identical ")
ACE_TEXT ("maps failed.\n")),
false);
}
else if (phonetic[0] < phonetic[1])
{
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("Relational comparison of two identical ")
ACE_TEXT ("maps incorrectly passed.\n")),
false);
}
ACE_TEST_ASSERT (!phonetic[0].is_empty ()); // Sanity check.
Map foo (phonetic[0]); // Copy construction
Map bar = foo; // Assignment
if (!(foo == bar))
{
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("Second equality comparison of two ")
ACE_TEXT ("identical maps failed.\n")),
false);
}
Map::value_type values[letters_len];
size_t const values_len = sizeof (values) / sizeof (values[0]);
ACE_TString const * word = words;
char const * letter = letters;
for (Map::value_type * v = values;
v != values + values_len;
++v, ++letter, ++word)
{
*v = std::make_pair (*letter, *word);
}
Map A (values, values + values_len);
if (!(A == bar))
{
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("Third equality comparison of two ")
ACE_TEXT ("identical maps failed.\n")),
false);
}
Map B;
B.insert (values, values + values_len);
if (!(A == B))
{
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("Fourth equality comparison of two ")
ACE_TEXT ("identical maps failed.\n")),
false);
}
// ==== Removal tests ====
// Remove two elements from map.
Map::iterator letter_A = A.find ('A');
ACE_TEST_ASSERT (letter_A == A.begin ()); // Should be first map element.
ACE_TEST_ASSERT (A.count ('A') == 1); // Should only be one letter 'A'.
A.erase (letter_A);
if (A.find ('A') != A.end ()
|| A.count ('A') != 0)
{
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("Letter '%c' not removed from map\n"),
(*letter_A).first),
false);
}
static char const z = 'z';
if (A.erase (z) != 1
|| A.count (z) != 0
|| A.find (z) != A.end ())
{
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("Letter '%c' not removed from map\n"),
z),
false);
}
// Remove range of letters from map.
static size_t const removed_len = 3;
Map::iterator const first = B.begin () + 2;
Map::iterator const last = first + removed_len;
ACE_TEST_ASSERT (static_cast<Map::size_type> (last - first) < B.size ());
ACE_TEST_ASSERT (last < B.end ());
Map::value_type removed[removed_len];
size_t rcount = 0;
for (Map::iterator x = first; x != last; ++x)
{
removed[rcount++] = *x;
}
B.erase (first, last);
for (size_t s = 0; s < removed_len ; ++s)
{
Map::key_type const key = removed[s].first;
if (B.count (key) != 0
|| B.find (key) != B.end ())
{
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("Letter '%c' from range of letters ")
ACE_TEXT ("not removed from map\n"),
key),
false);
}
}
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("Insertion/removal test passed.\n")));
return true;
}
// --------------------------------------------------------------
bool
index_operator_test ()
{
// Instantiate the map.
typedef ACE_Array_Map<char, ACE_TString> Map;
Map phonetic;
ACE_TEST_ASSERT (phonetic.size () == 0 && phonetic.is_empty ());
ACE_TEST_ASSERT (phonetic.max_size () > 1);
// Run the same test twice, clearing the contents of the map between
// the iterations. The goal is to verify that the constant time
// clear() method performs as advertised.
for (unsigned int count = 1; count < 3; ++count)
{
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("----- Index operator test ")
ACE_TEXT ("iteration %u -----\n"),
count));
// Test map insertion through the index operator.
ACE_TString const * word = words;
for (char const * i = letters;
i != letters + letters_len;
++i, ++word)
{
phonetic[*i] = *word;
}
ACE_TEST_ASSERT (phonetic.size () == letters_len);
typedef Map::const_iterator const_iterator;
// Access the elements that were inserted into the map.
char const * letter = letters;
word = words;
const_iterator const last = phonetic.end ();
for (const_iterator n = phonetic.begin ();
n != last;
++n, ++letter, ++word)
{
if ((*n).first != *letter || (*n).second != *word)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("Key/Datum mismatch:\n")
ACE_TEXT (" key \"%c\" should be \"%c\"\n")
ACE_TEXT (" datum \"%s\" should be \"%s\"\n"),
(*n).first,
*letter,
(*n).second.c_str (),
word->c_str ()),
false);
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("\t%c\t%s\n"),
(*n).first,
(*n).second.c_str ()));
}
// Now run the same test in reverse.
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("... in reverse ...\n")));
typedef Map::const_reverse_iterator const_reverse_iterator;
letter = letters + letters_len - 1;
word = words + words_len - 1;
// Work around compiler / STL implementations that cannot
// handle implicit conversions from iterator to const_iterator
// (e.g. due to missing template constructor.)
//
// We don't strictly need a const Map for this test but having
// one allows us to exercise const iterators.
Map const & const_phonetic = phonetic;
const_reverse_iterator const rlast = const_phonetic.rend ();
for (const_reverse_iterator r = const_phonetic.rbegin ();
!(r == rlast); // Sun C++ Forte doesn't support operator!=
++r, --letter, --word)
{
if ((*r).first != *letter || (*r).second != *word)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("Key/Datum mismatch:\n")
ACE_TEXT (" key \"%c\" should be \"%c\"\n")
ACE_TEXT (" datum \"%s\" should be \"%s\"\n"),
(*r).first,
*letter,
(*r).second.c_str (),
word->c_str ()),
false);
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("\t%c\t%s\n"),
(*r).first,
(*r).second.c_str ()));
}
// The size should not have changed.
ACE_TEST_ASSERT (phonetic.size () == letters_len);
// Empty the map of its contents wholesale.
phonetic.clear ();
ACE_TEST_ASSERT (phonetic.size () == 0 && phonetic.is_empty ());
}
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("Index operator test passed.\n")));
return true;
}
// --------------------------------------------------------------
class RefCounted
{
public:
RefCounted ()
: refcount_ (0)
{
}
RefCounted (unsigned int * count)
: refcount_ (count)
{
}
~RefCounted ()
{
if (this->refcount_)
--(*this->refcount_);
}
RefCounted (RefCounted const & r)
: refcount_ (r.refcount_ptr ())
{
if (this->refcount_)
++(*this->refcount_);
}
RefCounted &
operator= (RefCounted const & r)
{
RefCounted tmp (r);
std::swap (this->refcount_, tmp.refcount_);
return *this;
}
unsigned int *
refcount_ptr () const
{
return this->refcount_;
}
unsigned int
refcount () const
{
return *this->refcount_;
}
private:
unsigned int * refcount_;
};
// --------
bool
reference_count_test ()
{
typedef ACE_Array_Map<ACE_TString, RefCounted> Map;
static Map::size_type const CAPACITY = 30;
unsigned int ref_count = 1;
RefCounted counted (&ref_count);
ACE_TEST_ASSERT (counted.refcount () == 1);
{
Map map (CAPACITY); // Preallocate storage for a number of
// elements even if they are not used to test
// some internals.
map[ACE_TEXT("One")] = counted;
ACE_TEST_ASSERT (counted.refcount () == 2);
std::pair<Map::iterator, bool> result;
{
// Enter a new scope block to assure destruction of temporaries
// on systems like Solaris / Sun C++.
result = map.insert (std::make_pair (ACE_TString (ACE_TEXT ("Two")),
counted));
ACE_TEST_ASSERT (result.second);
}
ACE_TEST_ASSERT (counted.refcount () == 3);
{
// Enter a new scope block to assure destruction of temporaries
// on systems like Solaris / Sun C++.
result = map.insert (std::make_pair (ACE_TString (ACE_TEXT ("Three")),
counted));
ACE_TEST_ASSERT (result.second);
}
ACE_TEST_ASSERT (counted.refcount () == 4);
Map::size_type const erased = map.erase (ACE_TEXT ("One"));
ACE_TEST_ASSERT (erased == 1);
ACE_TEST_ASSERT (counted.refcount () == 3);
}
// Map instance no longer contains any references to the "counted"
// object so the reference count should be back to one.
ACE_TEST_ASSERT (counted.refcount () == 1);
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("Reference count test passed.\n")));
return true;
}
// --------------------------------------------------------------
int
run_main (int, ACE_TCHAR *[])
{
ACE_START_TEST (ACE_TEXT ("Array_Map_Test"));
ACE_TEST_ASSERT (::letters_len == ::words_len);
bool const success =
::insertion_removal_test ()
&& ::index_operator_test ()
&& ::reference_count_test ();
ACE_END_TEST;
return (success ? 0 : -1);
}
|