| 1 |
// Copyright (C) 2015-2025 Free Software Foundation, Inc. |
| 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++14 } } |
| 19 |
|
| 20 |
#include <set> |
| 21 |
#include <testsuite_hooks.h> |
| 22 |
|
| 23 |
struct Cmp |
| 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 Cmp::count = 0; |
| 35 |
|
| 36 |
using test_type = std::set<int, Cmp>; |
| 37 |
|
| 38 |
test_type x{ 1, 3, 5 }; |
| 39 |
const test_type& cx = x; |
| 40 |
|
| 41 |
void |
| 42 |
test01() |
| 43 |
{ |
| 44 |
Cmp::count = 0; |
| 45 |
|
| 46 |
auto it = x.find(1L); |
| 47 |
VERIFY( it != x.end() && *it == 1 ); |
| 48 |
it = x.find(2L); |
| 49 |
VERIFY( it == x.end() ); |
| 50 |
|
| 51 |
auto cit = cx.find(3L); |
| 52 |
VERIFY( cit != cx.end() && *cit == 3 ); |
| 53 |
cit = cx.find(2L); |
| 54 |
VERIFY( cit == cx.end() ); |
| 55 |
|
| 56 |
VERIFY( Cmp::count == 0 ); |
| 57 |
|
| 58 |
static_assert(std::is_same<decltype(it), test_type::iterator>::value, |
| 59 |
"find returns iterator"); |
| 60 |
static_assert(std::is_same<decltype(cit), test_type::const_iterator>::value, |
| 61 |
"const find returns const_iterator"); |
| 62 |
} |
| 63 |
|
| 64 |
void |
| 65 |
test02() |
| 66 |
{ |
| 67 |
Cmp::count = 0; |
| 68 |
|
| 69 |
auto n = x.count(1L); |
| 70 |
VERIFY( n == 1 ); |
| 71 |
n = x.count(2L); |
| 72 |
VERIFY( n == 0 ); |
| 73 |
|
| 74 |
auto cn = cx.count(3L); |
| 75 |
VERIFY( cn == 1 ); |
| 76 |
cn = cx.count(2L); |
| 77 |
VERIFY( cn == 0 ); |
| 78 |
|
| 79 |
VERIFY( Cmp::count == 0 ); |
| 80 |
} |
| 81 |
|
| 82 |
void |
| 83 |
test03() |
| 84 |
{ |
| 85 |
Cmp::count = 0; |
| 86 |
|
| 87 |
auto it = x.lower_bound(1L); |
| 88 |
VERIFY( it != x.end() && *it == 1 ); |
| 89 |
it = x.lower_bound(2L); |
| 90 |
VERIFY( it != x.end() && *it == 3 ); |
| 91 |
|
| 92 |
auto cit = cx.lower_bound(1L); |
| 93 |
VERIFY( cit != cx.end() && *cit == 1 ); |
| 94 |
cit = cx.lower_bound(2L); |
| 95 |
VERIFY( cit != cx.end() && *cit == 3 ); |
| 96 |
|
| 97 |
VERIFY( Cmp::count == 0 ); |
| 98 |
|
| 99 |
static_assert(std::is_same<decltype(it), test_type::iterator>::value, |
| 100 |
"lower_bound returns iterator"); |
| 101 |
static_assert(std::is_same<decltype(cit), test_type::const_iterator>::value, |
| 102 |
"const lower_bound returns const_iterator"); |
| 103 |
} |
| 104 |
|
| 105 |
void |
| 106 |
test04() |
| 107 |
{ |
| 108 |
Cmp::count = 0; |
| 109 |
|
| 110 |
auto it = x.upper_bound(1L); |
| 111 |
VERIFY( it != x.end() && *it == 3 ); |
| 112 |
it = x.upper_bound(5L); |
| 113 |
VERIFY( it == x.end() ); |
| 114 |
|
| 115 |
auto cit = cx.upper_bound(1L); |
| 116 |
VERIFY( cit != cx.end() && *cit == 3 ); |
| 117 |
cit = cx.upper_bound(5L); |
| 118 |
VERIFY( cit == cx.end() ); |
| 119 |
|
| 120 |
VERIFY( Cmp::count == 0 ); |
| 121 |
|
| 122 |
static_assert(std::is_same<decltype(it), test_type::iterator>::value, |
| 123 |
"upper_bound returns iterator"); |
| 124 |
static_assert(std::is_same<decltype(cit), test_type::const_iterator>::value, |
| 125 |
"const upper_bound returns const_iterator"); |
| 126 |
} |
| 127 |
|
| 128 |
void |
| 129 |
test05() |
| 130 |
{ |
| 131 |
Cmp::count = 0; |
| 132 |
|
| 133 |
auto it = x.equal_range(1L); |
| 134 |
VERIFY( it.first != it.second && *it.first == 1 ); |
| 135 |
it = x.equal_range(2L); |
| 136 |
VERIFY( it.first == it.second && it.first != x.end() ); |
| 137 |
|
| 138 |
auto cit = cx.equal_range(1L); |
| 139 |
VERIFY( cit.first != cit.second && *cit.first == 1 ); |
| 140 |
cit = cx.equal_range(2L); |
| 141 |
VERIFY( cit.first == cit.second && cit.first != cx.end() ); |
| 142 |
|
| 143 |
VERIFY( Cmp::count == 0 ); |
| 144 |
|
| 145 |
using pair = std::pair<test_type::iterator, test_type::iterator>; |
| 146 |
static_assert(std::is_same<decltype(it), pair>::value, |
| 147 |
"equal_range returns pair<iterator, iterator>"); |
| 148 |
using cpair = std::pair<test_type::const_iterator, test_type::const_iterator>; |
| 149 |
static_assert(std::is_same<decltype(cit), cpair>::value, |
| 150 |
"const equal_range returns pair<const_iterator, const_iterator>"); |
| 151 |
} |
| 152 |
|
| 153 |
void |
| 154 |
test06() |
| 155 |
{ |
| 156 |
// https://gcc.gnu.org/ml/libstdc++/2015-01/msg00176.html |
| 157 |
// Verify the new function template overloads do not cause problems |
| 158 |
// when the comparison function is not transparent. |
| 159 |
struct I |
| 160 |
{ |
| 161 |
int i; |
| 162 |
operator int() const { return i; } |
| 163 |
}; |
| 164 |
|
| 165 |
std::set<int> s; |
| 166 |
I i = { }; |
| 167 |
s.find(i); |
| 168 |
} |
| 169 |
|
| 170 |
void |
| 171 |
test07() |
| 172 |
{ |
| 173 |
// PR libstdc++/78273 |
| 174 |
|
| 175 |
struct C { |
| 176 |
bool operator()(int l, int r) const { return l < r; } |
| 177 |
|
| 178 |
struct Partition { }; |
| 179 |
|
| 180 |
bool operator()(int l, Partition) const { return l < 2; } |
| 181 |
bool operator()(Partition, int r) const { return 4 < r; } |
| 182 |
|
| 183 |
using is_transparent = void; |
| 184 |
}; |
| 185 |
|
| 186 |
std::set<int, C> s{ 1, 2, 3, 4, 5 }; |
| 187 |
|
| 188 |
auto n = s.count(C::Partition{}); |
| 189 |
VERIFY( n == 3 ); |
| 190 |
} |
| 191 |
|
| 192 |
int |
| 193 |
main() |
| 194 |
{ |
| 195 |
test01(); |
| 196 |
test02(); |
| 197 |
test03(); |
| 198 |
test04(); |
| 199 |
test05(); |
| 200 |
test06(); |
| 201 |
test07(); |
| 202 |
} |