summaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite/20_util/scoped_allocator/2.cc
blob: 384361d255c722b9872aaf9977fa6dae1d8163cc (plain)
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// { dg-options "-std=gnu++11" }

// Copyright (C) 2012-2015 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/>.

#include <memory>
#include <scoped_allocator>
#include <vector>
#include <testsuite_hooks.h>
#include <testsuite_allocator.h>

// 20.12.4 Scoped allocator adaptor members [allocator.adaptor.members]
//
// Test piecewise construction of std::pair by scoped_allocator_adaptor

using __gnu_test::uneq_allocator;
using std::scoped_allocator_adaptor;

// a DefaultConstructible and CopyConstructible type
struct def
{
  def() : id(999) { }

  int id;
};

// a CopyConstructible and non-DefaultConstructible type
struct copyable
{
  copyable(int id) : id(id) { }

  // not constructed with an allocator so nothing to test
  bool verify() const { return true; }

  int id;
};

// a MoveConstructible and non-DefaultConstructible type
struct move_only
{
  move_only(int id) : id(id) { }
  move_only(move_only&&) = default;

  // not constructed with an allocator so nothing to test
  bool verify() const { return true; }

  int id;
};

// a type for which std::uses_allocator is true
struct uses_alloc_post
{
  typedef uneq_allocator<uses_alloc_post> allocator_type;

  uses_alloc_post(const allocator_type& alloc)
  : allocator_personality(alloc.get_personality()), id(999)
  { }

  uses_alloc_post(copyable arg, const allocator_type& alloc)
  : allocator_personality(alloc.get_personality()), id(arg.id)
  { }

  uses_alloc_post(move_only arg, const allocator_type& alloc)
  : allocator_personality(alloc.get_personality()), id(arg.id)
  { }

  // allocator-extended copy ctor
  uses_alloc_post(const uses_alloc_post& other, const allocator_type& alloc)
  : allocator_personality(alloc.get_personality()), id(other.id)
  { }

  // verify we were constructed with right allocator
  bool verify() const { return allocator_personality == id; }

  int allocator_personality;
  int id;
};

// a type for which std::uses_allocator is true
struct uses_alloc_pre : uses_alloc_post
{
  typedef uneq_allocator<uses_alloc_pre> allocator_type;

  uses_alloc_pre(std::allocator_arg_t, const allocator_type& alloc)
  : uses_alloc_post(alloc)
  { }

  uses_alloc_pre(std::allocator_arg_t, const allocator_type& alloc,
                 copyable arg)
  : uses_alloc_post(arg, alloc)
  { }

  // allocator-extended copy ctor
  uses_alloc_pre(std::allocator_arg_t, const allocator_type& alloc,
                 const uses_alloc_pre& other)
  : uses_alloc_post(other, alloc)
  { }

  uses_alloc_pre(std::allocator_arg_t, const allocator_type& alloc,
                 move_only arg)
  : uses_alloc_post(std::move(arg), alloc)
  { }
};

template<typename A, typename B>
  void
  test_def()
  {
    bool test __attribute((unused)) = false;

    typedef std::pair<A, B> test_type;
    typedef uneq_allocator<test_type> alloc_type;
    typedef scoped_allocator_adaptor<alloc_type, alloc_type> alloc_adaptor;

    int inner_id = 2;
    alloc_adaptor a(-1, alloc_type(inner_id)); // outer=-1, inner=2

    // all pair members that can be constructed with an allocator
    // should be constructed with the inner allocator, with personality==2

    auto p = a.allocate(1);

    // construct(pair<T1, T2>* p, piecewise_construct_t, tuple<...>, tuple<...>)
    std::tuple<> t;
    a.construct(p, std::piecewise_construct, t, t);
    VERIFY( p->first.id == 999 );
    VERIFY( p->second.id == 999 );
    a.destroy(p);

    // construct(pair<T1, T2>* __p)
    a.construct(p);
    VERIFY( p->first.id == 999 );
    VERIFY( p->second.id == 999 );
    auto pp = *p;
    a.destroy(p);

    // construct(pair<T1, T2>* p, const pair<U, V>& x)
    a.construct(p, pp);
    VERIFY( p->first.id == 999 );
    VERIFY( p->second.id == 999 );
    a.destroy(p);

    // construct(pair<T1, T2>* p, pair<U, V>&& x)
    a.construct(p, std::move(pp));
    VERIFY( p->first.id == 999 );
    VERIFY( p->second.id == 999 );
    a.destroy(p);

    a.deallocate(p, 1);
  }

template<typename A, typename B>
  void
  test_copying()
  {
    bool test __attribute((unused)) = false;

    typedef std::pair<A, B> test_type;
    typedef uneq_allocator<test_type> alloc_type;
    typedef scoped_allocator_adaptor<alloc_type, alloc_type> alloc_adaptor;

    int inner_id = 2;
    alloc_adaptor a(-1, alloc_type(inner_id)); // outer=-1, inner=2

    // all pair members that can be constructed with an allocator
    // should be constructed with the inner allocator, with personality==2

    auto p = a.allocate(1);

    // construct(pair<T1, T2>* p, piecewise_construct_t, tuple<...>, tuple<...>)
    auto t = std::make_tuple(copyable(inner_id));
    a.construct(p, std::piecewise_construct, t, t);
    VERIFY( p->first.verify() );
    VERIFY( p->second.verify() );
    a.destroy(p);

    // construct(pair<T1, T2>* __p)
    // cannot test this overload using non-DefaultConstructible types

    // construct(pair<T1, T2>* p, U&& x, V&& y)
    copyable c(inner_id);
    a.construct(p, c, c);
    VERIFY( p->first.verify() );
    VERIFY( p->second.verify() );
    auto pp = *p;
    a.destroy(p);

    // construct(pair<T1, T2>* p, const pair<U, V>& x)
    a.construct(p, pp);
    VERIFY( p->first.verify() );
    VERIFY( p->second.verify() );
    a.destroy(p);

    // construct(pair<T1, T2>* p, pair<U, V>&& x)
    a.construct(p, std::move(pp));
    VERIFY( p->first.verify() );
    VERIFY( p->second.verify() );
    a.destroy(p);

    a.deallocate(p, 1);
  }

template<typename A, typename B>
  void
  test_moving()
  {
    bool test __attribute((unused)) = false;

    typedef std::pair<A, B> test_type;
    typedef uneq_allocator<test_type> alloc_type;
    typedef scoped_allocator_adaptor<alloc_type, alloc_type> alloc_adaptor;

    int inner_id = 2;
    alloc_adaptor a(-1, alloc_type(inner_id)); // outer=-1, inner=2

    // all pair members that can be constructed with an allocator
    // should be constructed with the inner allocator, with personality==2

    auto p = a.allocate(1);

    // construct(pair<T1, T2>* p, piecewise_construct_t, tuple<...>, tuple<...>)
    a.construct(p, std::piecewise_construct,
                std::make_tuple(move_only(inner_id)),
                std::make_tuple(move_only(inner_id)));
    VERIFY( p->first.verify() );
    VERIFY( p->second.verify() );
    a.destroy(p);

    // construct(pair<T1, T2>* __p)
    // cannot test this overload using non-DefaultConstructible types

    // construct(pair<T1, T2>* p, U&& x, V&& y)
    a.construct(p, move_only(inner_id), move_only(inner_id));
    VERIFY( p->first.verify() );
    VERIFY( p->second.verify() );
    a.destroy(p);

    // construct(pair<T1, T2>* p, const pair<U, V>& x)
    // cannot test this overload using move-only types

    // construct(pair<T1, T2>* p, pair<U, V>&& x)
    a.construct(p, std::make_pair(move_only(inner_id), move_only(inner_id)));
    VERIFY( p->first.verify() );
    VERIFY( p->second.verify() );
    a.destroy(p);

    a.deallocate(p, 1);
  }

void test01()
{
  test_def<def, def>();
  test_def<def, uses_alloc_pre>();
  test_def<def, uses_alloc_post>();
  test_def<uses_alloc_pre, def>();
  test_def<uses_alloc_pre, uses_alloc_pre>();
  test_def<uses_alloc_pre, uses_alloc_post>();
  test_def<uses_alloc_post, def>();
  test_def<uses_alloc_post, uses_alloc_pre>();
  test_def<uses_alloc_post, uses_alloc_post>();
}

void test02()
{
  test_copying<copyable, copyable>();
  test_copying<copyable, uses_alloc_pre>();
  test_copying<copyable, uses_alloc_post>();
  test_copying<uses_alloc_pre, copyable>();
  test_copying<uses_alloc_pre, uses_alloc_pre>();
  test_copying<uses_alloc_pre, uses_alloc_post>();
  test_copying<uses_alloc_post, copyable>();
  test_copying<uses_alloc_post, uses_alloc_pre>();
  test_copying<uses_alloc_post, uses_alloc_post>();
}

void test03()
{
  test_moving<move_only, move_only>();
  test_moving<move_only, uses_alloc_pre>();
  test_moving<move_only, uses_alloc_post>();
  test_moving<uses_alloc_pre, move_only>();
  test_moving<uses_alloc_pre, uses_alloc_pre>();
  test_moving<uses_alloc_pre, uses_alloc_post>();
  test_moving<uses_alloc_post, move_only>();
  test_moving<uses_alloc_post, uses_alloc_pre>();
  test_moving<uses_alloc_post, uses_alloc_post>();
}

int main()
{
  test01();
  test02();
  test03();
}