hash-map-tests.c: add a selftest involving int_hash
authorDavid Malcolm <dmalcolm@redhat.com>
Thu, 19 Dec 2019 15:01:49 +0000 (15:01 +0000)
committerDavid Malcolm <dmalcolm@gcc.gnu.org>
Thu, 19 Dec 2019 15:01:49 +0000 (15:01 +0000)
gcc/ChangeLog:
* hash-map-tests.c (selftest::test_map_of_int_to_strings): New
selftest.
(selftest::hash_map_tests_c_tests): Call it.

From-SVN: r279582

gcc/ChangeLog
gcc/hash-map-tests.c

index 4892f0385979b8011d3da0a5b4b79b2a7d42e2f7..299f3368d08b0343b34bd5bb07eea37fdf821474 100644 (file)
@@ -1,3 +1,9 @@
+2019-12-19  David Malcolm  <dmalcolm@redhat.com>
+
+       * hash-map-tests.c (selftest::test_map_of_int_to_strings): New
+       selftest.
+       (selftest::hash_map_tests_c_tests): Call it.
+
 2019-12-19  David Malcolm  <dmalcolm@redhat.com>
 
        * gimple-predict.h (gimple_predict_predictor): Make "gs" param
index a42eac21ab36b58331cba5278e2ea3c5402e8f3d..9a13a80442c81281170813288d6d55c9bbea97df 100644 (file)
@@ -103,6 +103,46 @@ test_map_of_strings_to_int ()
   ASSERT_EQ (1, string_map.elements ());
 }
 
+/* Construct a hash_map using int_hash and verify that
+   various operations work correctly.  */
+
+static void
+test_map_of_int_to_strings ()
+{
+  const int EMPTY = -1;
+  const int DELETED = -2;
+  typedef int_hash <int, EMPTY, DELETED> int_hash_t;
+  hash_map <int_hash_t, const char *> m;
+
+  const char *ostrich = "ostrich";
+  const char *elephant = "elephant";
+  const char *ant = "ant";
+  const char *spider = "spider";
+  const char *millipede = "Illacme plenipes";
+  const char *eric = "half a bee";
+
+  /* A fresh hash_map should be empty.  */
+  ASSERT_EQ (0, m.elements ());
+  ASSERT_EQ (NULL, m.get (2));
+
+  /* Populate the hash_map.  */
+  ASSERT_EQ (false, m.put (2, ostrich));
+  ASSERT_EQ (false, m.put (4, elephant));
+  ASSERT_EQ (false, m.put (6, ant));
+  ASSERT_EQ (false, m.put (8, spider));
+  ASSERT_EQ (false, m.put (750, millipede));
+  ASSERT_EQ (false, m.put (3, eric));
+
+  /* Verify that we can recover the stored values.  */
+  ASSERT_EQ (6, m.elements ());
+  ASSERT_EQ (*m.get (2), ostrich);
+  ASSERT_EQ (*m.get (4), elephant);
+  ASSERT_EQ (*m.get (6), ant);
+  ASSERT_EQ (*m.get (8), spider);
+  ASSERT_EQ (*m.get (750), millipede);
+  ASSERT_EQ (*m.get (3), eric);
+}
+
 typedef class hash_map_test_val_t
 {
 public:
@@ -244,6 +284,7 @@ void
 hash_map_tests_c_tests ()
 {
   test_map_of_strings_to_int ();
+  test_map_of_int_to_strings ();
   test_map_of_type_with_ctor_and_dtor ();
 }