]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/23_containers/unordered_map/operations/1.cc
1 // Copyright (C) 2021-2022 Free Software Foundation, Inc.
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)
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.
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/>.
18 // { dg-do run { target c++20 } }
20 #include <unordered_map>
22 #ifndef __cpp_lib_generic_unordered_lookup
23 # error "Feature-test macro for generic lookup missing in <unordered_map>"
24 #elif __cpp_lib_generic_unordered_lookup < 201811L
25 # error "Feature-test macro for generic lookup has wrong value in <unordered_map>"
28 #include <testsuite_hooks.h>
32 typedef void is_transparent
;
34 bool operator()(int i
, long l
) const { return i
== l
; }
35 bool operator()(long l
, int i
) const { return l
== i
; }
36 bool operator()(int i
, int j
) const { ++count
; return i
== j
; }
45 typedef void is_transparent
;
47 std::size_t operator()(int i
) const { ++count
; return i
; }
48 std::size_t operator()(long l
) const { return l
; }
55 using test_type
= std::unordered_map
<int, char, Hash
, Equal
>;
57 test_type x
{ { 1, '2' }, { 3, '4' } };
58 const test_type
& cx
= x
;
66 VERIFY( x
.contains(1L) );
69 VERIFY( it
!= x
.end() && it
->second
== '2' );
71 VERIFY( it
== x
.end() );
73 auto cit
= cx
.find(3L);
74 VERIFY( cit
!= cx
.end() && cit
->second
== '4' );
76 VERIFY( cit
== cx
.end() );
78 VERIFY( Hash::count
== 0 );
79 VERIFY( Equal::count
== 0 );
81 static_assert(std::is_same
<decltype(it
), test_type::iterator
>::value
,
82 "find returns iterator");
83 static_assert(std::is_same
<decltype(cit
), test_type::const_iterator
>::value
,
84 "const find returns const_iterator");
98 auto cn
= cx
.count(3L);
103 VERIFY( Hash::count
== 0 );
104 VERIFY( Equal::count
== 0 );
113 auto it
= x
.equal_range(1L);
114 VERIFY( it
.first
!= it
.second
&& it
.first
->second
== '2' );
115 it
= x
.equal_range(2L);
116 VERIFY( it
.first
== it
.second
&& it
.first
== x
.end() );
118 auto cit
= cx
.equal_range(1L);
119 VERIFY( cit
.first
!= cit
.second
&& cit
.first
->second
== '2' );
120 cit
= cx
.equal_range(2L);
121 VERIFY( cit
.first
== cit
.second
&& cit
.first
== cx
.end() );
123 VERIFY( Hash::count
== 0 );
124 VERIFY( Equal::count
== 0 );
126 using pair
= std::pair
<test_type::iterator
, test_type::iterator
>;
127 static_assert(std::is_same
<decltype(it
), pair
>::value
,
128 "equal_range returns pair<iterator, iterator>");
129 using cpair
= std::pair
<test_type::const_iterator
, test_type::const_iterator
>;
130 static_assert(std::is_same
<decltype(cit
), cpair
>::value
,
131 "const equal_range returns pair<const_iterator, const_iterator>");
139 bool operator()(int l
, int r
) const { return l
== r
; }
141 struct Partition
{ };
143 bool operator()(int l
, Partition
) const { return l
< 6; }
144 bool operator()(Partition
, int r
) const { return 3 < r
; }
146 using is_transparent
= void;
152 operator()(int x
) const
156 operator()(E::Partition
) const
159 using is_transparent
= void;
162 std::unordered_map
<int, int, H
, E
> m
{ {1,0}, {2,0}, {3,0}, {4, 0}, {5, 0} };
164 auto n
= m
.count(E::Partition
{});