aboutsummaryrefslogtreecommitdiff
path: root/gcc/hash-map-tests.c
diff options
context:
space:
mode:
authorMichael Ploujnikov <michael.ploujnikov@oracle.com>2019-01-21 19:04:04 +0000
committerMichael Ploujnikov <plouj@gcc.gnu.org>2019-01-21 19:04:04 +0000
commit3b1f091c4b6881cd60f6f37c31b0054e04d137cc (patch)
tree0dae35d7f9650ce70726e092cb7b4e781f5a1406 /gcc/hash-map-tests.c
parent9011fa06de952258f7e514bd37c5265a73d15781 (diff)
downloadgcc-3b1f091c4b6881cd60f6f37c31b0054e04d137cc.zip
gcc-3b1f091c4b6881cd60f6f37c31b0054e04d137cc.tar.gz
gcc-3b1f091c4b6881cd60f6f37c31b0054e04d137cc.tar.bz2
hash-map-tests.c (test_map_of_strings_to_int): Show how to use string contents as hash_map keys.
* hash-map-tests.c (test_map_of_strings_to_int): Show how to use string contents as hash_map keys. From-SVN: r268121
Diffstat (limited to 'gcc/hash-map-tests.c')
-rw-r--r--gcc/hash-map-tests.c20
1 files changed, 20 insertions, 0 deletions
diff --git a/gcc/hash-map-tests.c b/gcc/hash-map-tests.c
index 7b7a1c8..2137464 100644
--- a/gcc/hash-map-tests.c
+++ b/gcc/hash-map-tests.c
@@ -77,6 +77,26 @@ test_map_of_strings_to_int ()
m.remove (eric);
ASSERT_EQ (5, m.elements ());
ASSERT_EQ (NULL, m.get (eric));
+
+ /* A plain char * key is hashed based on its value (address), rather
+ than the string it points to. */
+ char *another_ant = static_cast <char *> (xcalloc (4, 1));
+ another_ant[0] = 'a';
+ another_ant[1] = 'n';
+ another_ant[2] = 't';
+ another_ant[3] = 0;
+ ASSERT_NE (ant, another_ant);
+ unsigned prev_size = m.elements ();
+ ASSERT_EQ (false, m.put (another_ant, 7));
+ ASSERT_EQ (prev_size + 1, m.elements ());
+
+ /* Need to use string_hash or nofree_string_hash key types to hash
+ based on the string contents. */
+ hash_map <nofree_string_hash, int> string_map;
+ ASSERT_EQ (false, string_map.put (ant, 1));
+ ASSERT_EQ (1, string_map.elements ());
+ ASSERT_EQ (true, string_map.put (another_ant, 5));
+ ASSERT_EQ (1, string_map.elements ());
}
/* Run all of the selftests within this file. */