1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
// Copyright (C) 2021-2022 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
// { dg-do run { target c++20 } }
#include <unordered_set>
#ifndef __cpp_lib_generic_unordered_lookup
# error "Feature-test macro for generic lookup missing in <unordered_set>"
#elif __cpp_lib_generic_unordered_lookup < 201811L
# error "Feature-test macro for generic lookup has wrong value in <unordered_set>"
#endif
#include <testsuite_hooks.h>
struct Equal
{
typedef void is_transparent;
bool operator()(int i, long l) const { return i == l; }
bool operator()(long l, int i) const { return l == i; }
bool operator()(int i, int j) const { ++count; return i == j; }
static int count;
};
int Equal::count = 0;
struct Hash
{
typedef void is_transparent;
std::size_t operator()(int i) const { ++count; return i; }
std::size_t operator()(long l) const { return l; }
static int count;
};
int Hash::count = 0;
using test_type = std::unordered_set<int, Hash, Equal>;
test_type x{ 1, 3, 5 };
const test_type& cx = x;
void
test01()
{
Hash::count = 0;
Equal::count = 0;
VERIFY( x.contains(1L) );
auto it = x.find(1L);
VERIFY( it != x.end() && *it == 1 );
it = x.find(2L);
VERIFY( it == x.end() );
auto cit = cx.find(3L);
VERIFY( cit != cx.end() && *cit == 3 );
cit = cx.find(2L);
VERIFY( cit == cx.end() );
VERIFY( Hash::count == 0 );
VERIFY( Equal::count == 0 );
static_assert(std::is_same<decltype(it), test_type::iterator>::value,
"find returns iterator");
static_assert(std::is_same<decltype(cit), test_type::const_iterator>::value,
"const find returns const_iterator");
}
void
test02()
{
Hash::count = 0;
Equal::count = 0;
auto n = x.count(1L);
VERIFY( n == 1 );
n = x.count(2L);
VERIFY( n == 0 );
auto cn = cx.count(3L);
VERIFY( cn == 1 );
cn = cx.count(2L);
VERIFY( cn == 0 );
VERIFY( Hash::count == 0 );
VERIFY( Equal::count == 0 );
}
void
test03()
{
Hash::count = 0;
Equal::count = 0;
auto it = x.equal_range(1L);
VERIFY( it.first != it.second && *it.first == 1 );
it = x.equal_range(2L);
VERIFY( it.first == it.second && it.first == x.end() );
auto cit = cx.equal_range(1L);
VERIFY( cit.first != cit.second && *cit.first == 1 );
cit = cx.equal_range(2L);
VERIFY( cit.first == cit.second && cit.first == cx.end() );
VERIFY( Hash::count == 0 );
VERIFY( Equal::count == 0 );
using pair = std::pair<test_type::iterator, test_type::iterator>;
static_assert(std::is_same<decltype(it), pair>::value,
"equal_range returns pair<iterator, iterator>");
using cpair = std::pair<test_type::const_iterator, test_type::const_iterator>;
static_assert(std::is_same<decltype(cit), cpair>::value,
"const equal_range returns pair<const_iterator, const_iterator>");
}
void
test04()
{
// https://gcc.gnu.org/ml/libstdc++/2015-01/msg00176.html
// Verify the new function template overloads do not cause problems
// when the comparison function is not transparent.
struct I
{
int i;
operator int() const { return i; }
};
std::unordered_set<int> s;
I i = { };
s.find(i);
}
void
test05()
{
struct E
{
bool operator()(int l, int r) const { return l == r; }
struct Partition { };
bool operator()(int l, Partition) const { return l < 6; }
bool operator()(Partition, int r) const { return r > 3; }
using is_transparent = void;
};
struct H
{
size_t
operator()(int x) const
{ return (size_t)(x / 10); }
size_t
operator()(E::Partition) const
{ return 0; }
using is_transparent = void;
};
std::unordered_set<int, H, E> s{ 1, 2, 3, 4, 5 };
auto n = s.count(E::Partition{});
VERIFY( n == 2 );
}
int
main()
{
test01();
test02();
test03();
test04();
test05();
}
|