]>
Commit | Line | Data |
---|---|---|
7adcbafe | 1 | // Copyright (C) 2021-2022 Free Software Foundation, Inc. |
d2b1a684 FD |
2 | // |
3 | // This file is part of the GNU ISO C++ Library. This library is free | |
4 | // software; you can redistribute it and/or modify it under the | |
5 | // terms of the GNU General Public License as published by the | |
6 | // Free Software Foundation; either version 3, or (at your option) | |
7 | // any later version. | |
8 | ||
9 | // This library is distributed in the hope that it will be useful, | |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | // GNU General Public License for more details. | |
13 | ||
14 | // You should have received a copy of the GNU General Public License along | |
15 | // with this library; see the file COPYING3. If not see | |
16 | // <http://www.gnu.org/licenses/>. | |
17 | ||
18 | // { dg-do run { target c++20 } } | |
19 | ||
20 | #include <unordered_map> | |
21 | #include <testsuite_hooks.h> | |
22 | ||
23 | struct Equal | |
24 | { | |
25 | typedef void is_transparent; | |
26 | ||
27 | bool operator()(int i, long l) const { return i == l; } | |
28 | bool operator()(long l, int i) const { return l == i; } | |
29 | bool operator()(int i, int j) const { ++count; return i == j; } | |
30 | ||
31 | static int count; | |
32 | }; | |
33 | ||
34 | int Equal::count = 0; | |
35 | ||
36 | struct Hash | |
37 | { | |
38 | typedef void is_transparent; | |
39 | ||
40 | std::size_t operator()(int i) const { ++count; return i; } | |
41 | std::size_t operator()(long l) const { return l; } | |
42 | ||
43 | static int count; | |
44 | }; | |
45 | ||
46 | int Hash::count = 0; | |
47 | ||
48 | using test_type = std::unordered_multimap<int, char, Hash, Equal>; | |
49 | ||
50 | test_type x{ { 1, '2' }, { 3, '4' }, { 3, '5' } }; | |
51 | const test_type& cx = x; | |
52 | ||
53 | void | |
54 | test01() | |
55 | { | |
56 | Hash::count = 0; | |
57 | Equal::count = 0; | |
58 | ||
59 | VERIFY( x.contains(1L) ); | |
60 | ||
61 | auto it = x.find(1L); | |
62 | VERIFY( it != x.end() && it->second == '2' ); | |
63 | it = x.find(2L); | |
64 | VERIFY( it == x.end() ); | |
65 | ||
66 | auto cit = cx.find(3L); | |
67 | VERIFY( cit != cx.end() && cit->second == '5' ); | |
68 | cit = cx.find(2L); | |
69 | VERIFY( cit == cx.end() ); | |
70 | ||
71 | VERIFY( Hash::count == 0 ); | |
72 | VERIFY( Equal::count == 0 ); | |
73 | ||
74 | static_assert(std::is_same<decltype(it), test_type::iterator>::value, | |
75 | "find returns iterator"); | |
76 | static_assert(std::is_same<decltype(cit), test_type::const_iterator>::value, | |
77 | "const find returns const_iterator"); | |
78 | } | |
79 | ||
80 | void | |
81 | test02() | |
82 | { | |
83 | Hash::count = 0; | |
84 | Equal::count = 0; | |
85 | ||
86 | auto n = x.count(1L); | |
87 | VERIFY( n == 1 ); | |
88 | n = x.count(2L); | |
89 | VERIFY( n == 0 ); | |
90 | ||
91 | auto cn = cx.count(3L); | |
92 | VERIFY( cn == 2 ); | |
93 | cn = cx.count(2L); | |
94 | VERIFY( cn == 0 ); | |
95 | ||
96 | VERIFY( Hash::count == 0 ); | |
97 | VERIFY( Equal::count == 0 ); | |
98 | } | |
99 | ||
100 | void | |
101 | test03() | |
102 | { | |
103 | Hash::count = 0; | |
104 | Equal::count = 0; | |
105 | ||
106 | auto it = x.equal_range(1L); | |
107 | VERIFY( it.first != it.second && it.first->second == '2' ); | |
108 | it = x.equal_range(2L); | |
109 | VERIFY( it.first == it.second && it.first == x.end() ); | |
110 | ||
111 | auto cit = cx.equal_range(3L); | |
112 | VERIFY( cit.first != cit.second && cit.first->second == '5' ); | |
113 | VERIFY( std::distance(cit.first, cit.second) == 2 ); | |
114 | cit = cx.equal_range(2L); | |
115 | VERIFY( cit.first == cit.second && cit.first == cx.end() ); | |
116 | ||
117 | VERIFY( Hash::count == 0 ); | |
118 | VERIFY( Equal::count == 0 ); | |
119 | ||
120 | using pair = std::pair<test_type::iterator, test_type::iterator>; | |
121 | static_assert(std::is_same<decltype(it), pair>::value, | |
122 | "equal_range returns pair<iterator, iterator>"); | |
123 | using cpair = std::pair<test_type::const_iterator, test_type::const_iterator>; | |
124 | static_assert(std::is_same<decltype(cit), cpair>::value, | |
125 | "const equal_range returns pair<const_iterator, const_iterator>"); | |
126 | } | |
127 | ||
128 | ||
129 | int | |
130 | main() | |
131 | { | |
132 | test01(); | |
133 | test02(); | |
134 | test03(); | |
135 | } |