diff options
author | schmidt <douglascraigschmidt@users.noreply.github.com> | 1998-04-08 21:58:51 +0000 |
---|---|---|
committer | schmidt <douglascraigschmidt@users.noreply.github.com> | 1998-04-08 21:58:51 +0000 |
commit | 331b41f32e1071730d8fb949e4316eb8db2e2f30 (patch) | |
tree | 0cb6e6d290c14152a8c3a5da8476990b2d26f40e /tests/Hash_Map_Manager_Test.cpp | |
parent | 64b3b4027e1242b0266ca56b15ecbca213838829 (diff) | |
download | ATCD-331b41f32e1071730d8fb949e4316eb8db2e2f30.tar.gz |
*** empty log message ***
Diffstat (limited to 'tests/Hash_Map_Manager_Test.cpp')
-rw-r--r-- | tests/Hash_Map_Manager_Test.cpp | 166 |
1 files changed, 111 insertions, 55 deletions
diff --git a/tests/Hash_Map_Manager_Test.cpp b/tests/Hash_Map_Manager_Test.cpp index 91ee29e109d..55f1e486009 100644 --- a/tests/Hash_Map_Manager_Test.cpp +++ b/tests/Hash_Map_Manager_Test.cpp @@ -10,16 +10,18 @@ // // = 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. +// 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 +// 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" @@ -95,7 +97,7 @@ HASH_STRING_MAP::equal (char *const &id1, char *const &id2) ACE_Hash_Map_Reverse_Iterator<Dumb_String, Dumb_String, ACE_Null_Mutex> #define MAP_STRING Dumb_String -#define ENTRY ((char *)entry) +#define ENTRY ((char *) entry) Dumb_String::Dumb_String (char *s) : s_ (s ? ACE_OS::strdup (s) : s), @@ -158,67 +160,121 @@ Dumb_String::operator char * (void) const #endif /* ACE_HAS_TEMPLATE_SPECIALIZATION */ -static const int MAX_HASH = 256; +struct String_Table +{ + char *key_; + char *value_; +}; -int -main (int, char *[]) +static String_Table string_table[] = { - ACE_START_TEST ("Hash_Map_Manager_Test"); + { "hello", + "guten Tag" + }, + { "goodbye", + "auf wiedersehen" + }, + { "funny", + "lustig" + }, + { 0, 0 } +}; - // Scoping below so that result of destruction can be seen in the log. - { - HASH_STRING_MAP hash (MAX_HASH); +static const int MAX_HASH = 256; - hash.bind ("hello", "guten Tag"); - hash.bind ("goodbye", "auf wiedersehen"); - hash.bind ("funny", "lustig"); +// @@ 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 POOL_SIZE = + sizeof (HASH_STRING_ENTRY) * 3 // Number of items in <string_table>. + + sizeof (HASH_STRING_ENTRY) * MAX_HASH; // Size of the Hash_Map_Manager table - MAP_STRING entry; +static ACE_Static_Allocator<POOL_SIZE> allocator; - 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)); +static int +run_test (void) +{ + allocator.dump (); + + HASH_STRING_MAP hash (MAX_HASH, &allocator); + + 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, + "%p failed for %s \n", + "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, + "`%s' found `%s'\n", + string_table[i].key_, + ENTRY)); + else + ACE_ERROR_RETURN ((LM_ERROR, + "`%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 (), i++) + ACE_DEBUG ((LM_DEBUG, "iterating (%d): [%s, %s]\n", + i, + (char *) entry->ext_id_, + (char *) entry->int_id_)); + } - // 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 (), i++) - ACE_DEBUG ((LM_DEBUG, "iterating (%d): [%s, %s]\n", - i, - (char *) entry->ext_id_, - (char *) entry->int_id_)); - } + 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, + "`%s' found `%s'\n", + string_table[i].key_, + ENTRY)); + else if (i != 2) + ACE_ERROR_RETURN ((LM_ERROR, + "`%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 (), i++) + ACE_DEBUG ((LM_DEBUG, "iterating (%d): [%s, %s]\n", + i, + (char *) entry->ext_id_, + (char *) entry->int_id_)); + } - hash.unbind ("goodbye", entry); + allocator.dump (); +} - 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)); +int +main (int, char *[]) +{ + ACE_START_TEST ("Hash_Map_Manager_Test"); - // 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 (), i++) - ACE_DEBUG ((LM_DEBUG, "iterating (%d): [%s, %s]\n", - i, - (char *) entry->ext_id_, - (char *) entry->int_id_)); - } - } + run_test (); ACE_END_TEST; |