File: insert_range_maps_sets.h

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 1,998,492 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (415 lines) | stat: -rw-r--r-- 14,486 bytes parent folder | download | duplicates (8)
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

#ifndef SUPPORT_INSERT_RANGE_MAPS_SETS_H
#define SUPPORT_INSERT_RANGE_MAPS_SETS_H

#include <algorithm>
#include <array>
#include <cassert>
#include <concepts>
#include <ranges>
#include <type_traits>
#include <vector>

#include "MoveOnly.h"
#include "almost_satisfies_types.h"
#include "count_new.h"
#include "exception_safety_helpers.h"
#include "insert_range_helpers.h"
#include "min_allocator.h"
#include "test_allocator.h"
#include "test_compare.h"
#include "test_hash.h"
#include "test_iterators.h"
#include "test_macros.h"
#include "type_algorithms.h"

template <class Container, class Range>
concept HasInsertRange = requires (Container& c, Range&& range) {
  c.insert_range(range);
};

template <template <class...> class Container, class T, class U>
constexpr bool test_set_constraints_insert_range() {
  // Input range with the same value type.
  static_assert(HasInsertRange<Container<T>, InputRange<T>>);
  // Input range with a convertible value type.
  static_assert(HasInsertRange<Container<T>, InputRange<U>>);
  // Input range with a non-convertible value type.
  static_assert(!HasInsertRange<Container<T>, InputRange<Empty>>);
  // Not an input range.
  static_assert(!HasInsertRange<Container<T>, InputRangeNotDerivedFrom>);
  static_assert(!HasInsertRange<Container<T>, InputRangeNotIndirectlyReadable>);
  static_assert(!HasInsertRange<Container<T>, InputRangeNotInputOrOutputIterator>);

  return true;
}

template <template <class...> class Container, class K, class V, class K2, class V2>
constexpr bool test_map_constraints_insert_range() {
  using ValueType = std::pair<const K, V>;

  // Input range with the same value type.
  static_assert(HasInsertRange<Container<K, V>, InputRange<ValueType>>);
  // Input range with a convertible value type.
  static_assert(HasInsertRange<Container<K, V>, InputRange<std::pair<const K2, V2>>>);
  // Input range with a non-convertible value type.
  static_assert(!HasInsertRange<Container<K, V>, InputRange<std::pair<const K, Empty>>>);
  static_assert(!HasInsertRange<Container<K, V>, InputRange<std::pair<const Empty, V>>>);
  // Not an input range.
  static_assert(!HasInsertRange<Container<K, V>, InputRangeNotDerivedFromGeneric<ValueType>>);

  return true;
}

template <class T>
struct TestCaseMapSet {
  Buffer<T> initial;
  Buffer<T> input;
  Buffer<T> expected;
  Buffer<T> expected_multi;
};

// Empty container.

template <class T>
TestCaseMapSet<T> constexpr EmptyContainer_EmptyRange {
  .initial = {}, .input = {}, .expected = {}
};

template <class T>
TestCaseMapSet<T> constexpr EmptyContainer_OneElementRange {
  .initial = {}, .input = {1}, .expected = {1}
};
template <class K, class V> TestCaseMapSet<std::pair<K, V>>
constexpr EmptyContainer_OneElementRange<std::pair<K, V>> {
  .initial = {}, .input = {{1, 'a'}}, .expected = {{1, 'a'}}
};

template <class T>
TestCaseMapSet<T> constexpr EmptyContainer_RangeNoDuplicates {
  .initial = {}, .input = {5, 1, 3, 8, 6}, .expected = {5, 1, 3, 8, 6}
};
template <class K, class V> TestCaseMapSet<std::pair<K, V>>
constexpr EmptyContainer_RangeNoDuplicates<std::pair<K, V>> {
  .initial = {}, .input = {{5, 'a'}, {1, 'e'}, {3, 'i'}, {8, 'o'}, {6, 'u'}},
  .expected = {{5, 'a'}, {1, 'e'}, {3, 'i'}, {8, 'o'}, {6, 'u'}}
};

template <class T>
TestCaseMapSet<T> constexpr EmptyContainer_RangeWithDuplicates {
  .initial = {},
  .input = {5, 1, 1, 3, 5, 8, 5, 6, 10},
  .expected = {5, 1, 3, 8, 6, 10},
  .expected_multi = {5, 1, 1, 3, 5, 8, 5, 6, 10}
};
template <class K, class V> TestCaseMapSet<std::pair<K, V>>
constexpr EmptyContainer_RangeWithDuplicates<std::pair<K, V>> {
  .initial = {},
  .input = {{5, 'a'}, {1, 'a'}, {1, 'b'}, {3, 'a'}, {5, 'b'}, {8, 'a'}, {5, 'c'}, {6, 'a'}, {10, 'b'}},
  .expected = {{5, 'a'}, {1, 'a'}, {3, 'a'}, {8, 'a'}, {6, 'a'}, {10, 'b'}},
  .expected_multi = {{5, 'a'}, {1, 'a'}, {1, 'b'}, {3, 'a'}, {5, 'b'}, {8, 'a'}, {5, 'c'}, {6, 'a'}, {10, 'b'}}
};

// One-element container.

template <class T>
TestCaseMapSet<T> constexpr OneElementContainer_EmptyRange {
  .initial = {10}, .input = {}, .expected = {10}
};
template <class K, class V> TestCaseMapSet<std::pair<K, V>>
constexpr OneElementContainer_EmptyRange<std::pair<K, V>> {
  .initial = {{10, 'A'}}, .input = {}, .expected = {{10, 'A'}}
};

template <class T>
TestCaseMapSet<T> constexpr OneElementContainer_OneElementRange {
  .initial = {10}, .input = {1}, .expected = {1, 10}
};
template <class K, class V> TestCaseMapSet<std::pair<K, V>>
constexpr OneElementContainer_OneElementRange<std::pair<K, V>> {
  .initial = {{10, 'A'}}, .input = {{1, 'a'}}, .expected = {{1, 'a'}, {10, 'A'}}
};

template <class T>
TestCaseMapSet<T> constexpr OneElementContainer_RangeNoDuplicates {
  .initial = {10}, .input = {5, 1, 3, 8, 6}, .expected = {5, 1, 3, 8, 6, 10}
};
template <class K, class V> TestCaseMapSet<std::pair<K, V>>
constexpr OneElementContainer_RangeNoDuplicates<std::pair<K, V>> {
  .initial = {{10, 'A'}}, .input = {{5, 'a'}, {1, 'e'}, {3, 'i'}, {8, 'o'}, {6, 'u'}},
  .expected = {{5, 'a'}, {1, 'e'}, {3, 'i'}, {8, 'o'}, {6, 'u'}, {10, 'A'}}
};

template <class T>
TestCaseMapSet<T> constexpr OneElementContainer_RangeWithDuplicates {
  .initial = {10},
  .input = {5, 1, 1, 3, 5, 8, 5, 6, 10},
  .expected = {5, 1, 3, 8, 6, 10},
  .expected_multi = {5, 1, 1, 3, 5, 8, 5, 6, 10, 10}
};
template <class K, class V> TestCaseMapSet<std::pair<K, V>>
constexpr OneElementContainer_RangeWithDuplicates<std::pair<K, V>> {
  .initial = {{10, 'A'}},
  .input = {{5, 'a'}, {1, 'a'}, {1, 'b'}, {3, 'a'}, {5, 'b'}, {8, 'a'}, {5, 'c'}, {6, 'a'}, {10, 'b'}},
  .expected = {{5, 'a'}, {1, 'a'}, {3, 'a'}, {8, 'a'}, {6, 'a'}, {10, 'A'}},
  .expected_multi = {
    {5, 'a'}, {1, 'a'}, {1, 'b'}, {3, 'a'}, {5, 'b'}, {8, 'a'}, {5, 'c'}, {6, 'a'}, {10, 'A'}, {10, 'b'}
  }
};

// N-elements container.

template <class T>
TestCaseMapSet<T> constexpr NElementsContainer_EmptyRange {
  .initial = {10, 15, 19, 16}, .input = {}, .expected = {10, 15, 19, 16}
};
template <class K, class V> TestCaseMapSet<std::pair<K, V>>
constexpr NElementsContainer_EmptyRange<std::pair<K, V>> {
  .initial = {{10, 'A'}, {15, 'B'}, {19, 'C'}, {16, 'D'}}, .input = {},
  .expected = {{10, 'A'}, {15, 'B'}, {19, 'C'}, {16, 'D'}}
};

template <class T>
TestCaseMapSet<T> constexpr NElementsContainer_OneElementRange {
  .initial = {10, 15, 19, 16}, .input = {1}, .expected = {1, 10, 15, 19, 16}
};
template <class K, class V> TestCaseMapSet<std::pair<K, V>>
constexpr NElementsContainer_OneElementRange<std::pair<K, V>> {
  .initial = {{10, 'A'}, {15, 'B'}, {19, 'C'}, {16, 'D'}}, .input = {{1, 'a'}},
  .expected = {{1, 'a'}, {10, 'A'}, {15, 'B'}, {19, 'C'}, {16, 'D'}}
};

template <class T>
TestCaseMapSet<T> constexpr NElementsContainer_RangeNoDuplicates {
  .initial = {10, 15, 19, 16}, .input = {5, 1, 3, 8, 6}, .expected = {5, 1, 3, 8, 6, 10, 15, 19, 16}
};
template <class K, class V> TestCaseMapSet<std::pair<K, V>>
constexpr NElementsContainer_RangeNoDuplicates<std::pair<K, V>> {
  .initial = {{10, 'A'}, {15, 'B'}, {19, 'C'}, {16, 'D'}},
  .input = {{5, 'a'}, {1, 'e'}, {3, 'i'}, {8, 'o'}, {6, 'u'}},
  .expected = {{5, 'a'}, {1, 'e'}, {3, 'i'}, {8, 'o'}, {6, 'u'}, {10, 'A'}, {15, 'B'}, {19, 'C'}, {16, 'D'}}
};

template <class T>
TestCaseMapSet<T> constexpr NElementsContainer_RangeWithDuplicates {
  .initial = {10, 15, 19, 16},
  .input = {5, 1, 1, 3, 5, 8, 5, 6, 10},
  .expected = {5, 1, 3, 8, 6, 10, 15, 19, 16},
  .expected_multi = {5, 1, 1, 3, 5, 8, 5, 6, 10, 10, 15, 19, 16}
};
template <class K, class V> TestCaseMapSet<std::pair<K, V>>
constexpr NElementsContainer_RangeWithDuplicates<std::pair<K, V>> {
  .initial = {{10, 'A'}, {15, 'B'}, {19, 'C'}, {16, 'D'}},
  .input = {{5, 'a'}, {1, 'a'}, {1, 'b'}, {3, 'a'}, {5, 'b'}, {8, 'a'}, {5, 'c'}, {6, 'a'}, {10, 'b'}},
  .expected = {{5, 'a'}, {1, 'a'}, {3, 'a'}, {8, 'a'}, {6, 'a'}, {10, 'A'}, {15, 'B'}, {19, 'C'}, {16, 'D'}},
  .expected_multi = {
    {5, 'a'}, {1, 'a'}, {1, 'b'}, {3, 'a'}, {5, 'b'}, {8, 'a'}, {5, 'c'}, {6, 'a'}, {10, 'b'},
    {10, 'A'}, {15, 'B'}, {19, 'C'}, {16, 'D'}
  }
};

template <class Container, class T, class Iter, class Sent>
void test_map_set_insert_range(bool allow_duplicates = false) {
  auto test = [&](const TestCaseMapSet<T>& test_case, bool check_multi = false) {
    Container c(test_case.initial.begin(), test_case.initial.end());
    auto in = wrap_input<Iter, Sent>(test_case.input);

    c.insert_range(in);
    if (check_multi) {
      return std::ranges::is_permutation(c, test_case.expected_multi);
    } else {
      return std::ranges::is_permutation(c, test_case.expected);
    }
  };

  { // Empty container.
    // empty_c.insert_range(empty_range)
    assert(test(EmptyContainer_EmptyRange<T>));
    // empty_c.insert_range(one_element_range)
    assert(test(EmptyContainer_OneElementRange<T>));
    // empty_c.insert_range(range_no_duplicates)
    assert(test(EmptyContainer_RangeNoDuplicates<T>));
    // empty_c.insert_range(range_with_duplicates)
    assert(test(EmptyContainer_RangeWithDuplicates<T>, allow_duplicates));
  }

  { // One-element container.
    // one_element_c.insert_range(empty_range)
    assert(test(OneElementContainer_EmptyRange<T>));
    // one_element_c.insert_range(one_element_range)
    assert(test(OneElementContainer_OneElementRange<T>));
    // one_element_c.insert_range(range_no_duplicates)
    assert(test(OneElementContainer_RangeNoDuplicates<T>));
    // one_element_c.insert_range(range_with_duplicates)
    assert(test(OneElementContainer_RangeWithDuplicates<T>, allow_duplicates));
  }

  { // N-elements container.
    // n_elements_c.insert_range(empty_range)
    assert(test(NElementsContainer_EmptyRange<T>));
    // n_elements_c.insert_range(one_element_range)
    assert(test(NElementsContainer_OneElementRange<T>));
    // n_elements_c.insert_range(range_no_duplicates)
    assert(test(NElementsContainer_RangeNoDuplicates<T>));
    // n_elements_c.insert_range(range_with_duplicates)
    assert(test(NElementsContainer_RangeWithDuplicates<T>, allow_duplicates));
  }
}

// Move-only types.

template <template <class ...> class Container>
void test_set_insert_range_move_only() {
  MoveOnly input[5];
  std::ranges::subrange in(std::move_iterator{input}, std::move_iterator{input + 5});

  Container<MoveOnly> c;
  c.insert_range(in);
}

template <template <class ...> class Container>
void test_map_insert_range_move_only() {
  using Value = std::pair<const int, MoveOnly>;
  Value input[5];
  std::ranges::subrange in(std::move_iterator{input}, std::move_iterator{input + 5});

  Container<int, MoveOnly> c;
  c.insert_range(in);
}

// Exception safety.

template <template <class ...> class Container>
void test_set_insert_range_exception_safety_throwing_copy() {
#if !defined(TEST_HAS_NO_EXCEPTIONS)
  using T = ThrowingCopy<3>;
  T::reset();
  T in[5] = {{1}, {2}, {3}, {4}, {5}};

  try {
    Container<T> c;
    c.insert_range(in);
    assert(false); // The constructor call above should throw.

  } catch (int) {
    assert(T::created_by_copying == 3);
    assert(T::destroyed == 2); // No destructor call for the partially-constructed element.
  }
#endif
}

template <template <class ...> class Container>
void test_map_insert_range_exception_safety_throwing_copy() {
#if !defined(TEST_HAS_NO_EXCEPTIONS)
  using K = int;
  using V = ThrowingCopy<3>;

  V::throwing_enabled = false;
  std::pair<const K, V> in[5] = {
    {1, {}}, {2, {}}, {3, {}}, {4, {}}, {5, {}}
  };
  V::throwing_enabled = true;
  V::reset();

  try {
    Container<K, V> c;
    c.insert_range(in);
    assert(false); // The function call above should throw.

  } catch (int) {
    assert(V::created_by_copying == 3);
    assert(V::destroyed == 2); // No destructor call for the partially-constructed element.
  }
#endif
}

template <template <class ...> class Container, class T>
void test_assoc_set_insert_range_exception_safety_throwing_allocator() {
#if !defined(TEST_HAS_NO_EXCEPTIONS)
  T in[] = {1, 2};

  try {
    ThrowingAllocator<T> alloc;

    globalMemCounter.reset();
    Container<T, test_less<T>, ThrowingAllocator<T>> c(alloc);
    c.insert_range(in);
    assert(false); // The function call above should throw.

  } catch (int) {
    assert(globalMemCounter.new_called == globalMemCounter.delete_called);
  }
#endif
}

template <template <class ...> class Container, class T>
void test_unord_set_insert_range_exception_safety_throwing_allocator() {
#if !defined(TEST_HAS_NO_EXCEPTIONS)
  T in[] = {1, 2};

  try {
    ThrowingAllocator<T> alloc;

    globalMemCounter.reset();
    Container<T, test_hash<T>, test_equal_to<T>, ThrowingAllocator<T>> c(alloc);
    c.insert_range(in);
    assert(false); // The function call above should throw.

  } catch (int) {
    assert(globalMemCounter.new_called == globalMemCounter.delete_called);
  }
#endif
}

template <template <class ...> class Container, class K, class V>
void test_assoc_map_insert_range_exception_safety_throwing_allocator() {
#if !defined(TEST_HAS_NO_EXCEPTIONS)
  using ValueType = std::pair<const K, V>;
  ValueType in[] = {
    ValueType{K{1}, V{1}}
  };

  try {
    ThrowingAllocator<ValueType> alloc;

    globalMemCounter.reset();
    Container<K, V, test_less<K>, ThrowingAllocator<ValueType>> c(alloc);
    c.insert_range(in);
    assert(false); // The function call above should throw.

  } catch (int) {
    assert(globalMemCounter.new_called == globalMemCounter.delete_called);
  }
#endif
}

template <template <class ...> class Container, class K, class V>
void test_unord_map_insert_range_exception_safety_throwing_allocator() {
#if !defined(TEST_HAS_NO_EXCEPTIONS)
  using ValueType = std::pair<const K, V>;
  ValueType in[] = {
    ValueType{K{1}, V{1}}
  };

  try {
    ThrowingAllocator<ValueType> alloc;

    globalMemCounter.reset();
    Container<K, V, test_hash<K>, test_equal_to<K>, ThrowingAllocator<ValueType>> c(alloc);
    c.insert_range(in);
    assert(false); // The function call above should throw.

  } catch (int) {
    assert(globalMemCounter.new_called == globalMemCounter.delete_called);
  }
#endif
}

#endif // SUPPORT_INSERT_RANGE_MAPS_SETS_H