Skip to content

Commit

Permalink
* hash-map-tests.c (test_map_of_strings_to_int): Show how to use
Browse files Browse the repository at this point in the history
	string contents as hash_map keys.



git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@268121 138bc75d-0d04-0410-961f-82ee72b054a4
  • Loading branch information
plouj committed Jan 21, 2019
1 parent f6aa5c0 commit a8d12eb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
5 changes: 5 additions & 0 deletions gcc/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
2019-01-21 Michael Ploujnikov <michael.ploujnikov@oracle.com>

* hash-map-tests.c (test_map_of_strings_to_int): Show how to use
string contents as hash_map keys.

2019-01-21 Bernd Edlinger <bernd.edlinger@hotmail.de>

PR c/88928
Expand Down
20 changes: 20 additions & 0 deletions gcc/hash-map-tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down

0 comments on commit a8d12eb

Please sign in to comment.