Skip to content

Commit

Permalink
[libcxx][test] Add tests for hash_function() and key_eq() in unordere…
Browse files Browse the repository at this point in the history
…d 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
  • Loading branch information
rarutyun authored and ldionne committed Sep 1, 2023
1 parent a68d72a commit be4adb5
Show file tree
Hide file tree
Showing 8 changed files with 292 additions and 0 deletions.
32 changes: 32 additions & 0 deletions 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;
}
36 changes: 36 additions & 0 deletions 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;
}
@@ -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;
}
47 changes: 47 additions & 0 deletions 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;
}
@@ -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;
}
44 changes: 44 additions & 0 deletions 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;
}
31 changes: 31 additions & 0 deletions 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;
}
37 changes: 37 additions & 0 deletions 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;
}

0 comments on commit be4adb5

Please sign in to comment.