File: pmr.pass.cpp

package info (click to toggle)
llvm-toolchain-21 1%3A21.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,235,796 kB
  • sloc: cpp: 7,617,614; ansic: 1,433,901; asm: 1,058,726; python: 252,096; f90: 94,671; objc: 70,753; lisp: 42,813; pascal: 18,401; sh: 10,032; ml: 5,111; perl: 4,720; awk: 3,523; makefile: 3,401; javascript: 2,272; xml: 892; fortran: 770
file content (326 lines) | stat: -rw-r--r-- 12,340 bytes parent folder | download | duplicates (4)
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
// UNSUPPORTED: availability-pmr-missing

// <flat_set>

// Test various constructors with pmr

#include <algorithm>
#include <cassert>
#include <deque>
#include <flat_set>
#include <functional>
#include <memory_resource>
#include <ranges>
#include <vector>
#include <string>

#include "test_iterators.h"
#include "test_macros.h"
#include "test_allocator.h"
#include "../../../test_compare.h"

void test() {
  {
    // flat_set(const Allocator& a);
    using M = std::flat_set<int, std::less<int>, std::pmr::vector<int>>;
    std::pmr::monotonic_buffer_resource mr;
    std::pmr::polymorphic_allocator<int> pa = &mr;
    auto m1                                 = M(pa);
    assert(m1.empty());
    assert(std::move(m1).extract().get_allocator() == pa);
    auto m2 = M(&mr);
    assert(m2.empty());
    assert(std::move(m2).extract().get_allocator() == pa);
  }
  {
    // flat_set(const key_compare& comp, const Alloc& a);
    using M = std::flat_set<int, std::function<bool(int, int)>, std::pmr::vector<int>>;
    std::pmr::monotonic_buffer_resource mr;
    std::pmr::vector<M> vm(&mr);
    vm.emplace_back(std::greater<int>());
    assert(vm[0] == M{});
    assert(vm[0].key_comp()(2, 1) == true);
    assert(vm[0].value_comp()(2, 1) == true);
    assert(std::move(vm[0]).extract().get_allocator().resource() == &mr);
  }
  {
    // flat_set(const key_container_type& key_cont, const mapped_container_type& mapped_cont,
    //          const Allocator& a);
    using M = std::flat_set<int, std::less<int>, std::pmr::vector<int>>;
    std::pmr::monotonic_buffer_resource mr;
    std::pmr::vector<M> vm(&mr);
    std::pmr::vector<int> ks = {1, 1, 1, 2, 2, 3, 2, 3, 3};
    assert(ks.get_allocator().resource() != &mr);
    vm.emplace_back(ks);
    assert(ks.size() == 9); // ks' value is unchanged, since it was an lvalue above
    assert((vm[0] == M{1, 2, 3}));
    assert(std::move(vm[0]).extract().get_allocator().resource() == &mr);
  }
  {
    // flat_set(const flat_set&, const allocator_type&);
    using C = test_less<int>;
    using M = std::flat_set<int, C, std::pmr::vector<int>>;
    std::pmr::monotonic_buffer_resource mr1;
    std::pmr::monotonic_buffer_resource mr2;
    M mo = M({1, 2, 3}, C(5), &mr1);
    M m  = {mo, &mr2}; // also test the implicitness of this constructor

    assert(m.key_comp() == C(5));
    auto keys = std::move(m).extract();
    assert((keys == std::pmr::vector<int>{1, 2, 3}));
    assert(keys.get_allocator().resource() == &mr2);

    // mo is unchanged
    assert(mo.key_comp() == C(5));
    auto keys2 = std::move(mo).extract();
    assert((keys2 == std::pmr::vector<int>{1, 2, 3}));
    assert(keys2.get_allocator().resource() == &mr1);
  }
  {
    // flat_set(const flat_set&, const allocator_type&);
    using M = std::flat_set<int, std::less<>, std::pmr::vector<int>>;
    std::pmr::vector<M> vs;
    M m = {1, 2, 3};
    vs.push_back(m);
    assert(vs[0] == m);
  }
  {
    // flat_set& operator=(const flat_set& m);
    // pmr allocator is not propagated
    using M = std::flat_set<int, std::less<>, std::pmr::deque<int>>;
    std::pmr::monotonic_buffer_resource mr1;
    std::pmr::monotonic_buffer_resource mr2;
    M mo = M({1, 2, 3}, &mr1);
    M m  = M({4, 5}, &mr2);
    m    = mo;
    assert((m == M{1, 2, 3}));
    assert(std::move(m).extract().get_allocator().resource() == &mr2);

    // mo is unchanged
    assert((mo == M{1, 2, 3}));
    assert(std::move(mo).extract().get_allocator().resource() == &mr1);
  }
  {
    // flat_set(const flat_set& m);
    using C = test_less<int>;
    std::pmr::monotonic_buffer_resource mr;
    using M = std::flat_set<int, C, std::pmr::vector<int>>;
    auto mo = M({1, 2, 3}, C(5), &mr);
    auto m  = mo;

    assert(m.key_comp() == C(5));
    assert((m == M{1, 2, 3}));
    auto ks = std::move(m).extract();
    assert(ks.get_allocator().resource() == std::pmr::get_default_resource());

    // mo is unchanged
    assert(mo.key_comp() == C(5));
    assert((mo == M{1, 2, 3}));
    auto kso = std::move(mo).extract();
    assert(kso.get_allocator().resource() == &mr);
  }
  {
    //  flat_set(initializer_list<value_type> il, const Alloc& a);
    using M = std::flat_set<int, std::less<int>, std::pmr::vector<int>>;
    std::pmr::monotonic_buffer_resource mr;
    std::pmr::vector<M> vm(&mr);
    std::initializer_list<M::value_type> il = {3, 1, 4, 1, 5};
    vm.emplace_back(il);
    assert((vm[0] == M{1, 3, 4, 5}));
    assert(std::move(vm[0]).extract().get_allocator().resource() == &mr);
  }
  {
    //  flat_set(initializer_list<value_type> il, const key_compare& comp, const Alloc& a);
    using C = test_less<int>;
    using M = std::flat_set<int, C, std::pmr::deque<int>>;
    std::pmr::monotonic_buffer_resource mr;
    std::pmr::vector<M> vm(&mr);
    std::initializer_list<M::value_type> il = {3, 1, 4, 1, 5};
    vm.emplace_back(il, C(5));
    assert((vm[0] == M{1, 3, 4, 5}));
    assert(std::move(vm[0]).extract().get_allocator().resource() == &mr);
    assert(vm[0].key_comp() == C(5));
  }
  {
    // flat_set(InputIterator first, InputIterator last, const Allocator& a);
    int ar[]       = {1, 1, 1, 2, 2, 3, 2, 3, 3};
    int expected[] = {1, 2, 3};
    {
      //  cpp17 iterator
      using M = std::flat_set<int, std::less<int>, std::pmr::vector<int>>;
      std::pmr::monotonic_buffer_resource mr;
      std::pmr::vector<M> vm(&mr);
      vm.emplace_back(cpp17_input_iterator<const int*>(ar), cpp17_input_iterator<const int*>(ar + 9));
      assert(std::ranges::equal(vm[0], expected));
      assert(std::move(vm[0]).extract().get_allocator().resource() == &mr);
    }
    {
      using M = std::flat_set<int, std::less<int>, std::pmr::vector<int>>;
      std::pmr::monotonic_buffer_resource mr;
      std::pmr::vector<M> vm(&mr);
      vm.emplace_back(ar, ar);
      assert(vm[0].empty());
      assert(std::move(vm[0]).extract().get_allocator().resource() == &mr);
    }
  }
  {
    // flat_set(flat_set&&, const allocator_type&);
    int expected[] = {1, 2, 3};
    using C        = test_less<int>;
    using M        = std::flat_set<int, C, std::pmr::vector<int>>;
    std::pmr::monotonic_buffer_resource mr1;
    std::pmr::monotonic_buffer_resource mr2;
    M mo = M({1, 3, 1, 2}, C(5), &mr1);
    M m  = {std::move(mo), &mr2}; // also test the implicitness of this constructor

    assert(m.key_comp() == C(5));
    assert(m.size() == 3);
    assert(std::equal(m.begin(), m.end(), expected, expected + 3));
    assert(std::move(m).extract().get_allocator().resource() == &mr2);

    // The original flat_set is moved-from.
    assert(std::is_sorted(mo.begin(), mo.end(), mo.value_comp()));
    assert(mo.key_comp() == C(5));
    assert(std::move(mo).extract().get_allocator().resource() == &mr1);
  }
  {
    // flat_set(flat_set&&, const allocator_type&);
    using M = std::flat_set<int, std::less<>, std::pmr::deque<int>>;
    std::pmr::vector<M> vs;
    M m = {1, 3, 1, 2};
    vs.push_back(std::move(m));
    assert((std::move(vs[0]).extract() == std::pmr::deque<int>{1, 2, 3}));
  }
  {
    // flat_set& operator=(flat_set&&);
    using M = std::flat_set<std::pmr::string, std::less<>, std::pmr::vector<std::pmr::string>>;
    std::pmr::monotonic_buffer_resource mr1;
    std::pmr::monotonic_buffer_resource mr2;
    M mo =
        M({"short", "very long string that definitely won't fit in the SSO buffer and therefore becomes empty on move"},
          &mr1);
    M m = M({"don't care"}, &mr2);
    m   = std::move(mo);
    assert(m.size() == 2);
    assert(std::is_sorted(m.begin(), m.end(), m.value_comp()));
    assert(m.begin()->get_allocator().resource() == &mr2);

    assert(std::is_sorted(mo.begin(), mo.end(), mo.value_comp()));
    mo.insert("foo");
    assert(mo.begin()->get_allocator().resource() == &mr1);
  }
  {
    //  flat_set(from_range_t, R&&, const Alloc&);
    int ar[]       = {1, 1, 1, 2, 2, 3, 2, 3, 3};
    int expected[] = {1, 2, 3};
    {
      // input_range
      using M    = std::flat_set<int, std::less<int>, std::pmr::vector<int>>;
      using Iter = cpp20_input_iterator<const int*>;
      using Sent = sentinel_wrapper<Iter>;
      using R    = std::ranges::subrange<Iter, Sent>;
      std::pmr::monotonic_buffer_resource mr;
      std::pmr::vector<M> vm(&mr);
      vm.emplace_back(std::from_range, R(Iter(ar), Sent(Iter(ar + 9))));
      assert(std::ranges::equal(vm[0], expected));
      assert(std::move(vm[0]).extract().get_allocator().resource() == &mr);
    }
    {
      using M = std::flat_set<int, std::less<int>, std::pmr::vector<int>>;
      using R = std::ranges::subrange<const int*>;
      std::pmr::monotonic_buffer_resource mr;
      std::pmr::vector<M> vm(&mr);
      vm.emplace_back(std::from_range, R(ar, ar));
      assert(vm[0].empty());
      assert(std::move(vm[0]).extract().get_allocator().resource() == &mr);
    }
  }
  {
    // flat_set(sorted_unique_t, const container_type& key_cont, const Alloc& a);
    using M = std::flat_set<int, std::less<int>, std::pmr::vector<int>>;
    std::pmr::monotonic_buffer_resource mr;
    std::pmr::vector<M> vm(&mr);
    std::pmr::vector<int> ks = {1, 2, 4, 10};
    vm.emplace_back(std::sorted_unique, ks);
    assert(!ks.empty()); // it was an lvalue above
    assert((vm[0] == M{1, 2, 4, 10}));
    assert(std::move(vm[0]).extract().get_allocator().resource() == &mr);
  }
  {
    // flat_set(sorted_unique_t, const container_type& key_cont,const Alloc& a);
    using M = std::flat_set<int, std::less<int>, std::pmr::vector<int>>;
    std::pmr::monotonic_buffer_resource mr;
    std::pmr::vector<M> vm(&mr);
    std::pmr::vector<int> ks({1, 2, 4, 10}, &mr);
    vm.emplace_back(std::sorted_unique, ks);
    assert((vm[0] == M{1, 2, 4, 10}));
    assert(std::move(vm[0]).extract().get_allocator().resource() == &mr);
  }
  {
    // flat_set(sorted_unique_t, initializer_list<value_type> il, const Alloc& a);
    // cpp_17
    using C = test_less<int>;
    using M = std::flat_set<int, C, std::pmr::vector<int>>;
    std::pmr::monotonic_buffer_resource mr;
    std::pmr::vector<M> vm(&mr);
    int ar[] = {1, 2, 4, 5};
    vm.emplace_back(
        std::sorted_unique, cpp17_input_iterator<const int*>(ar), cpp17_input_iterator<const int*>(ar + 4), C(3));
    assert((vm[0] == M{1, 2, 4, 5}));
    assert(vm[0].key_comp() == C(3));
    assert(std::move(vm[0]).extract().get_allocator().resource() == &mr);
  }
  {
    // flat_set(sorted_unique_t, initializer_list<value_type> il, const Alloc& a);
    using C = test_less<int>;
    using M = std::flat_set<int, C, std::pmr::vector<int>>;
    std::pmr::monotonic_buffer_resource mr;
    std::pmr::vector<M> vm(&mr);
    int ar[1] = {42};
    vm.emplace_back(std::sorted_unique, ar, ar, C(4));
    assert(vm[0] == M{});
    assert(vm[0].key_comp() == C(4));
    assert(std::move(vm[0]).extract().get_allocator().resource() == &mr);
  }
  {
    // flat_set(InputIterator first, InputIterator last, const Alloc& a);
    // cpp_17
    using C = test_less<int>;
    using M = std::flat_set<int, C, std::pmr::vector<int>>;
    std::pmr::monotonic_buffer_resource mr;
    std::pmr::vector<M> vm(&mr);
    int ar[] = {1, 2, 4, 5};
    vm.emplace_back(
        std::sorted_unique, cpp17_input_iterator<const int*>(ar), cpp17_input_iterator<const int*>(ar + 4), C(3));
    assert((vm[0] == M{1, 2, 4, 5}));
    assert(vm[0].key_comp() == C(3));
    assert(std::move(vm[0]).extract().get_allocator().resource() == &mr);
  }
  {
    // flat_set(InputIterator first, InputIterator last, const Alloc& a);
    using C = test_less<int>;
    using M = std::flat_set<int, C, std::pmr::vector<int>>;
    std::pmr::monotonic_buffer_resource mr;
    std::pmr::vector<M> vm(&mr);
    int ar[1] = {42};
    vm.emplace_back(std::sorted_unique, ar, ar, C(4));
    assert(vm[0] == M{});
    assert(vm[0].key_comp() == C(4));
    assert(std::move(vm[0]).extract().get_allocator().resource() == &mr);
  }
}

int main(int, char**) {
  test();

  return 0;
}