Skip to content

Commit be4adb5

Browse files
rarutyunldionne
authored andcommitted
[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
1 parent a68d72a commit be4adb5

File tree

8 files changed

+292
-0
lines changed

8 files changed

+292
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// <unordered_map>
10+
11+
// template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>,
12+
// class Alloc = allocator<pair<const Key, T>>>
13+
// class unordered_map
14+
15+
// hasher hash_function() const;
16+
17+
#include <unordered_map>
18+
#include <string>
19+
#include <cassert>
20+
21+
int main(int, char**) {
22+
typedef std::unordered_map<int, std::string> map_type;
23+
map_type m;
24+
25+
std::pair<map_type::iterator, bool> p = m.insert(map_type::value_type(1, "abc"));
26+
27+
const map_type& cm = m;
28+
assert(cm.hash_function()(p.first->first) == cm.hash_function()(1));
29+
assert(cm.hash_function()(1) == cm.hash_function()(p.first->first));
30+
31+
return 0;
32+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// <unordered_map>
10+
11+
// template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>,
12+
// class Alloc = allocator<pair<const Key, T>>>
13+
// class unordered_map
14+
15+
// hasher key_eq() const;
16+
17+
#include <unordered_map>
18+
#include <string>
19+
#include <cassert>
20+
21+
int main(int, char**) {
22+
typedef std::unordered_map<int, std::string> map_type;
23+
24+
map_type m;
25+
std::pair<map_type::iterator, bool> p1 = m.insert(map_type::value_type(1, "abc"));
26+
std::pair<map_type::iterator, bool> p2 = m.insert(map_type::value_type(2, "abc"));
27+
28+
const map_type& cm = m;
29+
30+
assert(cm.key_eq()(p1.first->first, p1.first->first));
31+
assert(cm.key_eq()(p2.first->first, p2.first->first));
32+
assert(!cm.key_eq()(p1.first->first, p2.first->first));
33+
assert(!cm.key_eq()(p2.first->first, p1.first->first));
34+
35+
return 0;
36+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// <unordered_map>
10+
11+
// template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>,
12+
// class Alloc = allocator<pair<const Key, T>>>
13+
// class unordered_multimap
14+
15+
// hasher hash_function() const;
16+
17+
#include <unordered_map>
18+
#include <string>
19+
#include <cassert>
20+
21+
int main(int, char**) {
22+
typedef std::unordered_multimap<int, std::string> map_type;
23+
map_type m;
24+
25+
map_type::iterator i1 = m.insert(map_type::value_type(1, "abc"));
26+
map_type::iterator i2 = m.insert(map_type::value_type(1, "bcd"));
27+
28+
const map_type& cm = m;
29+
assert(cm.hash_function()(i1->first) == cm.hash_function()(i2->first));
30+
assert(cm.hash_function()(i2->first) == cm.hash_function()(i1->first));
31+
32+
return 0;
33+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// <unordered_map>
10+
11+
// template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>,
12+
// class Alloc = allocator<pair<const Key, T>>>
13+
// class unordered_multimap
14+
15+
// hasher key_eq() const;
16+
17+
#include <unordered_map>
18+
#include <string>
19+
#include <cassert>
20+
21+
#include "test_macros.h"
22+
23+
int main(int, char**) {
24+
typedef std::unordered_multimap<int, std::string> map_type;
25+
26+
map_type m;
27+
map_type::iterator i1 = m.insert(map_type::value_type(1, "abc"));
28+
map_type::iterator i2 = m.insert(map_type::value_type(1, "bcd"));
29+
map_type::iterator i3 = m.insert(map_type::value_type(2, "abc"));
30+
31+
const map_type& cm = m;
32+
33+
assert(cm.key_eq()(i1->first, i1->first));
34+
assert(cm.key_eq()(i2->first, i2->first));
35+
assert(cm.key_eq()(i3->first, i3->first));
36+
37+
assert(cm.key_eq()(i1->first, i2->first));
38+
assert(cm.key_eq()(i2->first, i1->first));
39+
40+
assert(!cm.key_eq()(i1->first, i3->first));
41+
assert(!cm.key_eq()(i3->first, i1->first));
42+
43+
assert(!cm.key_eq()(i2->first, i3->first));
44+
assert(!cm.key_eq()(i3->first, i2->first));
45+
46+
return 0;
47+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// <unordered_set>
10+
11+
// template <class Value, class Hash = hash<Value>, class Pred = equal_to<Value>,
12+
// class Alloc = allocator<Value>>
13+
// class unordered_multiset
14+
15+
// hasher hash_function() const;
16+
17+
#include <unordered_set>
18+
#include <cassert>
19+
20+
int main(int, char**) {
21+
typedef std::unordered_multiset<int> set_type;
22+
set_type s;
23+
24+
set_type::iterator i1 = s.insert(1);
25+
set_type::iterator i2 = s.insert(1);
26+
27+
const set_type& cs = s;
28+
assert(cs.hash_function()(*i1) == cs.hash_function()(*i2));
29+
assert(cs.hash_function()(*i2) == cs.hash_function()(*i1));
30+
31+
return 0;
32+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// <unordered_set>
10+
11+
// template <class Value, class Hash = hash<Value>, class Pred = equal_to<Value>,
12+
// class Alloc = allocator<Value>>
13+
// class unordered_multiset
14+
15+
// key_equal key_eq() const;
16+
17+
#include <unordered_set>
18+
#include <cassert>
19+
20+
int main(int, char**) {
21+
typedef std::unordered_multiset<int> set_type;
22+
set_type s;
23+
24+
set_type::iterator i1 = s.insert(1);
25+
set_type::iterator i2 = s.insert(1);
26+
set_type::iterator i3 = s.insert(2);
27+
28+
const set_type& cs = s;
29+
30+
assert(cs.key_eq()(*i1, *i1));
31+
assert(cs.key_eq()(*i2, *i2));
32+
assert(cs.key_eq()(*i3, *i3));
33+
34+
assert(cs.key_eq()(*i1, *i2));
35+
assert(cs.key_eq()(*i2, *i1));
36+
37+
assert(!cs.key_eq()(*i1, *i3));
38+
assert(!cs.key_eq()(*i3, *i1));
39+
40+
assert(!cs.key_eq()(*i2, *i3));
41+
assert(!cs.key_eq()(*i3, *i2));
42+
43+
return 0;
44+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// <unordered_set>
10+
11+
// template <class Value, class Hash = hash<Value>, class Pred = equal_to<Value>,
12+
// class Alloc = allocator<Value>>
13+
// class unordered_set
14+
15+
// hasher hash_function() const;
16+
17+
#include <unordered_set>
18+
#include <cassert>
19+
20+
int main(int, char**) {
21+
typedef std::unordered_set<int> set_type;
22+
set_type s;
23+
24+
std::pair<set_type::iterator, bool> p = s.insert(1);
25+
26+
const set_type& cs = s;
27+
assert(cs.hash_function()(*p.first) == cs.hash_function()(1));
28+
assert(cs.hash_function()(1) == cs.hash_function()(*p.first));
29+
30+
return 0;
31+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// <unordered_set>
10+
11+
// template <class Value, class Hash = hash<Value>, class Pred = equal_to<Value>,
12+
// class Alloc = allocator<Value>>
13+
// class unordered_set
14+
15+
// key_equal key_eq() const;
16+
17+
#include <unordered_set>
18+
#include <cassert>
19+
20+
int main(int, char**) {
21+
typedef std::unordered_set<int> set_type;
22+
23+
set_type s;
24+
25+
std::pair<set_type::iterator, bool> p1 = s.insert(1);
26+
std::pair<set_type::iterator, bool> p2 = s.insert(2);
27+
28+
const set_type& cs = s;
29+
30+
assert(cs.key_eq()(*p1.first, *p1.first));
31+
assert(cs.key_eq()(*p2.first, *p2.first));
32+
33+
assert(!cs.key_eq()(*p1.first, *p2.first));
34+
assert(!cs.key_eq()(*p2.first, *p1.first));
35+
36+
return 0;
37+
}

0 commit comments

Comments
 (0)