[libcxx-commits] [libcxx] be4adb5 - [libcxx][test] Add tests for hash_function() and key_eq() in unordered containers

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Fri Sep 1 05:51:03 PDT 2023


Author: Ruslan Arutyunyan
Date: 2023-09-01T08:50:56-04:00
New Revision: be4adb5c2b77b66786617e101e6a845cc587b0a8

URL: https://github.com/llvm/llvm-project/commit/be4adb5c2b77b66786617e101e6a845cc587b0a8
DIFF: https://github.com/llvm/llvm-project/commit/be4adb5c2b77b66786617e101e6a845cc587b0a8.diff

LOG: [libcxx][test] Add tests for hash_function() and key_eq() in unordered containers

Add tests for `hasher hash_function() const` and `key_equal key_eq() const`
observers in unordered containers.

Differential Revision: https://reviews.llvm.org/D119703

Added: 
    libcxx/test/std/containers/unord/unord.map/hash_function.pass.cpp
    libcxx/test/std/containers/unord/unord.map/key_eq.pass.cpp
    libcxx/test/std/containers/unord/unord.multimap/hash_function.pass.cpp
    libcxx/test/std/containers/unord/unord.multimap/key_eq.pass.cpp
    libcxx/test/std/containers/unord/unord.multiset/hash_function.pass.cpp
    libcxx/test/std/containers/unord/unord.multiset/key_eq.pass.cpp
    libcxx/test/std/containers/unord/unord.set/hash_function.pass.cpp
    libcxx/test/std/containers/unord/unord.set/key_eq.pass.cpp

Modified: 
    

Removed: 
    


################################################################################
diff  --git a/libcxx/test/std/containers/unord/unord.map/hash_function.pass.cpp b/libcxx/test/std/containers/unord/unord.map/hash_function.pass.cpp
new file mode 100644
index 00000000000000..016d815353158c
--- /dev/null
+++ b/libcxx/test/std/containers/unord/unord.map/hash_function.pass.cpp
@@ -0,0 +1,32 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// <unordered_map>
+
+// template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>,
+//           class Alloc = allocator<pair<const Key, T>>>
+// class unordered_map
+
+// hasher hash_function() const;
+
+#include <unordered_map>
+#include <string>
+#include <cassert>
+
+int main(int, char**) {
+  typedef std::unordered_map<int, std::string> map_type;
+  map_type m;
+
+  std::pair<map_type::iterator, bool> p = m.insert(map_type::value_type(1, "abc"));
+
+  const map_type& cm = m;
+  assert(cm.hash_function()(p.first->first) == cm.hash_function()(1));
+  assert(cm.hash_function()(1) == cm.hash_function()(p.first->first));
+
+  return 0;
+}

diff  --git a/libcxx/test/std/containers/unord/unord.map/key_eq.pass.cpp b/libcxx/test/std/containers/unord/unord.map/key_eq.pass.cpp
new file mode 100644
index 00000000000000..beb19c84544b18
--- /dev/null
+++ b/libcxx/test/std/containers/unord/unord.map/key_eq.pass.cpp
@@ -0,0 +1,36 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// <unordered_map>
+
+// template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>,
+//           class Alloc = allocator<pair<const Key, T>>>
+// class unordered_map
+
+// hasher key_eq() const;
+
+#include <unordered_map>
+#include <string>
+#include <cassert>
+
+int main(int, char**) {
+  typedef std::unordered_map<int, std::string> map_type;
+
+  map_type m;
+  std::pair<map_type::iterator, bool> p1 = m.insert(map_type::value_type(1, "abc"));
+  std::pair<map_type::iterator, bool> p2 = m.insert(map_type::value_type(2, "abc"));
+
+  const map_type& cm = m;
+
+  assert(cm.key_eq()(p1.first->first, p1.first->first));
+  assert(cm.key_eq()(p2.first->first, p2.first->first));
+  assert(!cm.key_eq()(p1.first->first, p2.first->first));
+  assert(!cm.key_eq()(p2.first->first, p1.first->first));
+
+  return 0;
+}

diff  --git a/libcxx/test/std/containers/unord/unord.multimap/hash_function.pass.cpp b/libcxx/test/std/containers/unord/unord.multimap/hash_function.pass.cpp
new file mode 100644
index 00000000000000..d5631aeb52271c
--- /dev/null
+++ b/libcxx/test/std/containers/unord/unord.multimap/hash_function.pass.cpp
@@ -0,0 +1,33 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// <unordered_map>
+
+// template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>,
+//           class Alloc = allocator<pair<const Key, T>>>
+// class unordered_multimap
+
+// hasher hash_function() const;
+
+#include <unordered_map>
+#include <string>
+#include <cassert>
+
+int main(int, char**) {
+  typedef std::unordered_multimap<int, std::string> map_type;
+  map_type m;
+
+  map_type::iterator i1 = m.insert(map_type::value_type(1, "abc"));
+  map_type::iterator i2 = m.insert(map_type::value_type(1, "bcd"));
+
+  const map_type& cm = m;
+  assert(cm.hash_function()(i1->first) == cm.hash_function()(i2->first));
+  assert(cm.hash_function()(i2->first) == cm.hash_function()(i1->first));
+
+  return 0;
+}

diff  --git a/libcxx/test/std/containers/unord/unord.multimap/key_eq.pass.cpp b/libcxx/test/std/containers/unord/unord.multimap/key_eq.pass.cpp
new file mode 100644
index 00000000000000..87fa25c3919fbd
--- /dev/null
+++ b/libcxx/test/std/containers/unord/unord.multimap/key_eq.pass.cpp
@@ -0,0 +1,47 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// <unordered_map>
+
+// template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>,
+//           class Alloc = allocator<pair<const Key, T>>>
+// class unordered_multimap
+
+// hasher key_eq() const;
+
+#include <unordered_map>
+#include <string>
+#include <cassert>
+
+#include "test_macros.h"
+
+int main(int, char**) {
+  typedef std::unordered_multimap<int, std::string> map_type;
+
+  map_type m;
+  map_type::iterator i1 = m.insert(map_type::value_type(1, "abc"));
+  map_type::iterator i2 = m.insert(map_type::value_type(1, "bcd"));
+  map_type::iterator i3 = m.insert(map_type::value_type(2, "abc"));
+
+  const map_type& cm = m;
+
+  assert(cm.key_eq()(i1->first, i1->first));
+  assert(cm.key_eq()(i2->first, i2->first));
+  assert(cm.key_eq()(i3->first, i3->first));
+
+  assert(cm.key_eq()(i1->first, i2->first));
+  assert(cm.key_eq()(i2->first, i1->first));
+
+  assert(!cm.key_eq()(i1->first, i3->first));
+  assert(!cm.key_eq()(i3->first, i1->first));
+
+  assert(!cm.key_eq()(i2->first, i3->first));
+  assert(!cm.key_eq()(i3->first, i2->first));
+
+  return 0;
+}

diff  --git a/libcxx/test/std/containers/unord/unord.multiset/hash_function.pass.cpp b/libcxx/test/std/containers/unord/unord.multiset/hash_function.pass.cpp
new file mode 100644
index 00000000000000..17b8d006f49bcc
--- /dev/null
+++ b/libcxx/test/std/containers/unord/unord.multiset/hash_function.pass.cpp
@@ -0,0 +1,32 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// <unordered_set>
+
+// template <class Value, class Hash = hash<Value>, class Pred = equal_to<Value>,
+//           class Alloc = allocator<Value>>
+// class unordered_multiset
+
+// hasher hash_function() const;
+
+#include <unordered_set>
+#include <cassert>
+
+int main(int, char**) {
+  typedef std::unordered_multiset<int> set_type;
+  set_type s;
+
+  set_type::iterator i1 = s.insert(1);
+  set_type::iterator i2 = s.insert(1);
+
+  const set_type& cs = s;
+  assert(cs.hash_function()(*i1) == cs.hash_function()(*i2));
+  assert(cs.hash_function()(*i2) == cs.hash_function()(*i1));
+
+  return 0;
+}

diff  --git a/libcxx/test/std/containers/unord/unord.multiset/key_eq.pass.cpp b/libcxx/test/std/containers/unord/unord.multiset/key_eq.pass.cpp
new file mode 100644
index 00000000000000..c2ff7bc7c281ee
--- /dev/null
+++ b/libcxx/test/std/containers/unord/unord.multiset/key_eq.pass.cpp
@@ -0,0 +1,44 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// <unordered_set>
+
+// template <class Value, class Hash = hash<Value>, class Pred = equal_to<Value>,
+//           class Alloc = allocator<Value>>
+// class unordered_multiset
+
+// key_equal key_eq() const;
+
+#include <unordered_set>
+#include <cassert>
+
+int main(int, char**) {
+  typedef std::unordered_multiset<int> set_type;
+  set_type s;
+
+  set_type::iterator i1 = s.insert(1);
+  set_type::iterator i2 = s.insert(1);
+  set_type::iterator i3 = s.insert(2);
+
+  const set_type& cs = s;
+
+  assert(cs.key_eq()(*i1, *i1));
+  assert(cs.key_eq()(*i2, *i2));
+  assert(cs.key_eq()(*i3, *i3));
+
+  assert(cs.key_eq()(*i1, *i2));
+  assert(cs.key_eq()(*i2, *i1));
+
+  assert(!cs.key_eq()(*i1, *i3));
+  assert(!cs.key_eq()(*i3, *i1));
+
+  assert(!cs.key_eq()(*i2, *i3));
+  assert(!cs.key_eq()(*i3, *i2));
+
+  return 0;
+}

diff  --git a/libcxx/test/std/containers/unord/unord.set/hash_function.pass.cpp b/libcxx/test/std/containers/unord/unord.set/hash_function.pass.cpp
new file mode 100644
index 00000000000000..ea403572a06a80
--- /dev/null
+++ b/libcxx/test/std/containers/unord/unord.set/hash_function.pass.cpp
@@ -0,0 +1,31 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// <unordered_set>
+
+// template <class Value, class Hash = hash<Value>, class Pred = equal_to<Value>,
+//           class Alloc = allocator<Value>>
+// class unordered_set
+
+// hasher hash_function() const;
+
+#include <unordered_set>
+#include <cassert>
+
+int main(int, char**) {
+  typedef std::unordered_set<int> set_type;
+  set_type s;
+
+  std::pair<set_type::iterator, bool> p = s.insert(1);
+
+  const set_type& cs = s;
+  assert(cs.hash_function()(*p.first) == cs.hash_function()(1));
+  assert(cs.hash_function()(1) == cs.hash_function()(*p.first));
+
+  return 0;
+}

diff  --git a/libcxx/test/std/containers/unord/unord.set/key_eq.pass.cpp b/libcxx/test/std/containers/unord/unord.set/key_eq.pass.cpp
new file mode 100644
index 00000000000000..c66dc83aedd9a1
--- /dev/null
+++ b/libcxx/test/std/containers/unord/unord.set/key_eq.pass.cpp
@@ -0,0 +1,37 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// <unordered_set>
+
+// template <class Value, class Hash = hash<Value>, class Pred = equal_to<Value>,
+//           class Alloc = allocator<Value>>
+// class unordered_set
+
+// key_equal key_eq() const;
+
+#include <unordered_set>
+#include <cassert>
+
+int main(int, char**) {
+  typedef std::unordered_set<int> set_type;
+
+  set_type s;
+
+  std::pair<set_type::iterator, bool> p1 = s.insert(1);
+  std::pair<set_type::iterator, bool> p2 = s.insert(2);
+
+  const set_type& cs = s;
+
+  assert(cs.key_eq()(*p1.first, *p1.first));
+  assert(cs.key_eq()(*p2.first, *p2.first));
+
+  assert(!cs.key_eq()(*p1.first, *p2.first));
+  assert(!cs.key_eq()(*p2.first, *p1.first));
+
+  return 0;
+}


        


More information about the libcxx-commits mailing list