| 1 |
// { dg-do run { target c++11 } } |
| 2 |
|
| 3 |
// Copyright (C) 2012-2025 Free Software Foundation, Inc. |
| 4 |
// |
| 5 |
// This file is part of the GNU ISO C++ Library. This library is free |
| 6 |
// software; you can redistribute it and/or modify it under the |
| 7 |
// terms of the GNU General Public License as published by the |
| 8 |
// Free Software Foundation; either version 3, or (at your option) |
| 9 |
// any later version. |
| 10 |
|
| 11 |
// This library is distributed in the hope that it will be useful, |
| 12 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
// GNU General Public License for more details. |
| 15 |
|
| 16 |
// You should have received a copy of the GNU General Public License along |
| 17 |
// with this library; see the file COPYING3. If not see |
| 18 |
// <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
#include <memory> |
| 21 |
#include <scoped_allocator> |
| 22 |
#include <vector> |
| 23 |
#include <testsuite_hooks.h> |
| 24 |
#include <testsuite_allocator.h> |
| 25 |
|
| 26 |
// 20.12.4 Scoped allocator adaptor members [allocator.adaptor.members] |
| 27 |
// |
| 28 |
// Test piecewise construction of std::pair by scoped_allocator_adaptor |
| 29 |
|
| 30 |
using __gnu_test::uneq_allocator; |
| 31 |
using std::scoped_allocator_adaptor; |
| 32 |
|
| 33 |
// a DefaultConstructible and CopyConstructible type |
| 34 |
struct def |
| 35 |
{ |
| 36 |
def() : id(999) { } |
| 37 |
|
| 38 |
int id; |
| 39 |
}; |
| 40 |
|
| 41 |
// a CopyConstructible and non-DefaultConstructible type |
| 42 |
struct copyable |
| 43 |
{ |
| 44 |
copyable(int id) : id(id) { } |
| 45 |
|
| 46 |
// not constructed with an allocator so nothing to test |
| 47 |
bool verify() const { return true; } |
| 48 |
|
| 49 |
int id; |
| 50 |
}; |
| 51 |
|
| 52 |
// a MoveConstructible and non-DefaultConstructible type |
| 53 |
struct move_only |
| 54 |
{ |
| 55 |
move_only(int id) : id(id) { } |
| 56 |
move_only(move_only&&) = default; |
| 57 |
|
| 58 |
// not constructed with an allocator so nothing to test |
| 59 |
bool verify() const { return true; } |
| 60 |
|
| 61 |
int id; |
| 62 |
}; |
| 63 |
|
| 64 |
// a type for which std::uses_allocator is true |
| 65 |
struct uses_alloc_post |
| 66 |
{ |
| 67 |
typedef uneq_allocator<uses_alloc_post> allocator_type; |
| 68 |
|
| 69 |
uses_alloc_post(const allocator_type& alloc) |
| 70 |
: allocator_personality(alloc.get_personality()), id(999) |
| 71 |
{ } |
| 72 |
|
| 73 |
uses_alloc_post(copyable arg, const allocator_type& alloc) |
| 74 |
: allocator_personality(alloc.get_personality()), id(arg.id) |
| 75 |
{ } |
| 76 |
|
| 77 |
uses_alloc_post(move_only arg, const allocator_type& alloc) |
| 78 |
: allocator_personality(alloc.get_personality()), id(arg.id) |
| 79 |
{ } |
| 80 |
|
| 81 |
// allocator-extended copy ctor |
| 82 |
uses_alloc_post(const uses_alloc_post& other, const allocator_type& alloc) |
| 83 |
: allocator_personality(alloc.get_personality()), id(other.id) |
| 84 |
{ } |
| 85 |
|
| 86 |
// verify we were constructed with right allocator |
| 87 |
bool verify() const { return allocator_personality == id; } |
| 88 |
|
| 89 |
int allocator_personality; |
| 90 |
int id; |
| 91 |
}; |
| 92 |
|
| 93 |
// a type for which std::uses_allocator is true |
| 94 |
struct uses_alloc_pre : uses_alloc_post |
| 95 |
{ |
| 96 |
typedef uneq_allocator<uses_alloc_pre> allocator_type; |
| 97 |
|
| 98 |
uses_alloc_pre(std::allocator_arg_t, const allocator_type& alloc) |
| 99 |
: uses_alloc_post(alloc) |
| 100 |
{ } |
| 101 |
|
| 102 |
uses_alloc_pre(std::allocator_arg_t, const allocator_type& alloc, |
| 103 |
copyable arg) |
| 104 |
: uses_alloc_post(arg, alloc) |
| 105 |
{ } |
| 106 |
|
| 107 |
// allocator-extended copy ctor |
| 108 |
uses_alloc_pre(std::allocator_arg_t, const allocator_type& alloc, |
| 109 |
const uses_alloc_pre& other) |
| 110 |
: uses_alloc_post(other, alloc) |
| 111 |
{ } |
| 112 |
|
| 113 |
uses_alloc_pre(std::allocator_arg_t, const allocator_type& alloc, |
| 114 |
move_only arg) |
| 115 |
: uses_alloc_post(std::move(arg), alloc) |
| 116 |
{ } |
| 117 |
}; |
| 118 |
|
| 119 |
template<typename A, typename B> |
| 120 |
void |
| 121 |
test_def() |
| 122 |
{ |
| 123 |
typedef std::pair<A, B> test_type; |
| 124 |
typedef uneq_allocator<test_type> alloc_type; |
| 125 |
typedef scoped_allocator_adaptor<alloc_type, alloc_type> alloc_adaptor; |
| 126 |
|
| 127 |
int inner_id = 2; |
| 128 |
alloc_adaptor a(-1, alloc_type(inner_id)); // outer=-1, inner=2 |
| 129 |
|
| 130 |
// all pair members that can be constructed with an allocator |
| 131 |
// should be constructed with the inner allocator, with personality==2 |
| 132 |
|
| 133 |
auto p = a.allocate(1); |
| 134 |
|
| 135 |
// construct(pair<T1, T2>* p, piecewise_construct_t, tuple<...>, tuple<...>) |
| 136 |
std::tuple<> t; |
| 137 |
a.construct(p, std::piecewise_construct, t, t); |
| 138 |
VERIFY( p->first.id == 999 ); |
| 139 |
VERIFY( p->second.id == 999 ); |
| 140 |
a.destroy(p); |
| 141 |
|
| 142 |
// construct(pair<T1, T2>* __p) |
| 143 |
a.construct(p); |
| 144 |
VERIFY( p->first.id == 999 ); |
| 145 |
VERIFY( p->second.id == 999 ); |
| 146 |
auto pp = *p; |
| 147 |
a.destroy(p); |
| 148 |
|
| 149 |
// construct(pair<T1, T2>* p, const pair<U, V>& x) |
| 150 |
a.construct(p, pp); |
| 151 |
VERIFY( p->first.id == 999 ); |
| 152 |
VERIFY( p->second.id == 999 ); |
| 153 |
a.destroy(p); |
| 154 |
|
| 155 |
// construct(pair<T1, T2>* p, pair<U, V>&& x) |
| 156 |
a.construct(p, std::move(pp)); |
| 157 |
VERIFY( p->first.id == 999 ); |
| 158 |
VERIFY( p->second.id == 999 ); |
| 159 |
a.destroy(p); |
| 160 |
|
| 161 |
a.deallocate(p, 1); |
| 162 |
} |
| 163 |
|
| 164 |
template<typename A, typename B> |
| 165 |
void |
| 166 |
test_copying() |
| 167 |
{ |
| 168 |
typedef std::pair<A, B> test_type; |
| 169 |
typedef uneq_allocator<test_type> alloc_type; |
| 170 |
typedef scoped_allocator_adaptor<alloc_type, alloc_type> alloc_adaptor; |
| 171 |
|
| 172 |
int inner_id = 2; |
| 173 |
alloc_adaptor a(-1, alloc_type(inner_id)); // outer=-1, inner=2 |
| 174 |
|
| 175 |
// all pair members that can be constructed with an allocator |
| 176 |
// should be constructed with the inner allocator, with personality==2 |
| 177 |
|
| 178 |
auto p = a.allocate(1); |
| 179 |
|
| 180 |
// construct(pair<T1, T2>* p, piecewise_construct_t, tuple<...>, tuple<...>) |
| 181 |
auto t = std::make_tuple(copyable(inner_id)); |
| 182 |
a.construct(p, std::piecewise_construct, t, t); |
| 183 |
VERIFY( p->first.verify() ); |
| 184 |
VERIFY( p->second.verify() ); |
| 185 |
a.destroy(p); |
| 186 |
|
| 187 |
// construct(pair<T1, T2>* __p) |
| 188 |
// cannot test this overload using non-DefaultConstructible types |
| 189 |
|
| 190 |
// construct(pair<T1, T2>* p, U&& x, V&& y) |
| 191 |
copyable c(inner_id); |
| 192 |
a.construct(p, c, c); |
| 193 |
VERIFY( p->first.verify() ); |
| 194 |
VERIFY( p->second.verify() ); |
| 195 |
auto pp = *p; |
| 196 |
a.destroy(p); |
| 197 |
|
| 198 |
// construct(pair<T1, T2>* p, const pair<U, V>& x) |
| 199 |
a.construct(p, pp); |
| 200 |
VERIFY( p->first.verify() ); |
| 201 |
VERIFY( p->second.verify() ); |
| 202 |
a.destroy(p); |
| 203 |
|
| 204 |
// construct(pair<T1, T2>* p, pair<U, V>&& x) |
| 205 |
a.construct(p, std::move(pp)); |
| 206 |
VERIFY( p->first.verify() ); |
| 207 |
VERIFY( p->second.verify() ); |
| 208 |
a.destroy(p); |
| 209 |
|
| 210 |
a.deallocate(p, 1); |
| 211 |
} |
| 212 |
|
| 213 |
template<typename A, typename B> |
| 214 |
void |
| 215 |
test_moving() |
| 216 |
{ |
| 217 |
typedef std::pair<A, B> test_type; |
| 218 |
typedef uneq_allocator<test_type> alloc_type; |
| 219 |
typedef scoped_allocator_adaptor<alloc_type, alloc_type> alloc_adaptor; |
| 220 |
|
| 221 |
int inner_id = 2; |
| 222 |
alloc_adaptor a(-1, alloc_type(inner_id)); // outer=-1, inner=2 |
| 223 |
|
| 224 |
// all pair members that can be constructed with an allocator |
| 225 |
// should be constructed with the inner allocator, with personality==2 |
| 226 |
|
| 227 |
auto p = a.allocate(1); |
| 228 |
|
| 229 |
// construct(pair<T1, T2>* p, piecewise_construct_t, tuple<...>, tuple<...>) |
| 230 |
a.construct(p, std::piecewise_construct, |
| 231 |
std::make_tuple(move_only(inner_id)), |
| 232 |
std::make_tuple(move_only(inner_id))); |
| 233 |
VERIFY( p->first.verify() ); |
| 234 |
VERIFY( p->second.verify() ); |
| 235 |
a.destroy(p); |
| 236 |
|
| 237 |
// construct(pair<T1, T2>* __p) |
| 238 |
// cannot test this overload using non-DefaultConstructible types |
| 239 |
|
| 240 |
// construct(pair<T1, T2>* p, U&& x, V&& y) |
| 241 |
a.construct(p, move_only(inner_id), move_only(inner_id)); |
| 242 |
VERIFY( p->first.verify() ); |
| 243 |
VERIFY( p->second.verify() ); |
| 244 |
a.destroy(p); |
| 245 |
|
| 246 |
// construct(pair<T1, T2>* p, const pair<U, V>& x) |
| 247 |
// cannot test this overload using move-only types |
| 248 |
|
| 249 |
// construct(pair<T1, T2>* p, pair<U, V>&& x) |
| 250 |
a.construct(p, std::make_pair(move_only(inner_id), move_only(inner_id))); |
| 251 |
VERIFY( p->first.verify() ); |
| 252 |
VERIFY( p->second.verify() ); |
| 253 |
a.destroy(p); |
| 254 |
|
| 255 |
a.deallocate(p, 1); |
| 256 |
} |
| 257 |
|
| 258 |
void test01() |
| 259 |
{ |
| 260 |
test_def<def, def>(); |
| 261 |
test_def<def, uses_alloc_pre>(); |
| 262 |
test_def<def, uses_alloc_post>(); |
| 263 |
test_def<uses_alloc_pre, def>(); |
| 264 |
test_def<uses_alloc_pre, uses_alloc_pre>(); |
| 265 |
test_def<uses_alloc_pre, uses_alloc_post>(); |
| 266 |
test_def<uses_alloc_post, def>(); |
| 267 |
test_def<uses_alloc_post, uses_alloc_pre>(); |
| 268 |
test_def<uses_alloc_post, uses_alloc_post>(); |
| 269 |
} |
| 270 |
|
| 271 |
void test02() |
| 272 |
{ |
| 273 |
test_copying<copyable, copyable>(); |
| 274 |
test_copying<copyable, uses_alloc_pre>(); |
| 275 |
test_copying<copyable, uses_alloc_post>(); |
| 276 |
test_copying<uses_alloc_pre, copyable>(); |
| 277 |
test_copying<uses_alloc_pre, uses_alloc_pre>(); |
| 278 |
test_copying<uses_alloc_pre, uses_alloc_post>(); |
| 279 |
test_copying<uses_alloc_post, copyable>(); |
| 280 |
test_copying<uses_alloc_post, uses_alloc_pre>(); |
| 281 |
test_copying<uses_alloc_post, uses_alloc_post>(); |
| 282 |
} |
| 283 |
|
| 284 |
void test03() |
| 285 |
{ |
| 286 |
test_moving<move_only, move_only>(); |
| 287 |
test_moving<move_only, uses_alloc_pre>(); |
| 288 |
test_moving<move_only, uses_alloc_post>(); |
| 289 |
test_moving<uses_alloc_pre, move_only>(); |
| 290 |
test_moving<uses_alloc_pre, uses_alloc_pre>(); |
| 291 |
test_moving<uses_alloc_pre, uses_alloc_post>(); |
| 292 |
test_moving<uses_alloc_post, move_only>(); |
| 293 |
test_moving<uses_alloc_post, uses_alloc_pre>(); |
| 294 |
test_moving<uses_alloc_post, uses_alloc_post>(); |
| 295 |
} |
| 296 |
|
| 297 |
int main() |
| 298 |
{ |
| 299 |
test01(); |
| 300 |
test02(); |
| 301 |
test03(); |
| 302 |
} |