Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / containers / from_range_helpers.h
blob7fff99da1e15e3378ae8679852534875774063c0
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #ifndef SUPPORT_FROM_RANGE_HELPERS_H
10 #define SUPPORT_FROM_RANGE_HELPERS_H
12 #include <cstddef>
13 #include <iterator>
14 #include <type_traits>
16 #include "min_allocator.h"
17 #include "test_allocator.h"
18 #include "test_iterators.h"
19 #include "test_macros.h"
20 #include "type_algorithms.h"
22 struct Empty {};
24 template <class T>
25 struct InputRange {
26 cpp20_input_iterator<T*> begin();
27 sentinel_wrapper<cpp20_input_iterator<T*>> end();
30 template <class Iter, class Sent, std::ranges::input_range Range>
31 constexpr auto wrap_input(Range&& input) {
32 auto b = Iter(std::ranges::begin(input));
33 auto e = Sent(Iter(std::ranges::end(input)));
34 return std::ranges::subrange(std::move(b), std::move(e));
37 template <class Iter, class Sent, class T>
38 constexpr auto wrap_input(std::vector<T>& input) {
39 auto b = Iter(input.data());
40 auto e = Sent(Iter(input.data() + input.size()));
41 return std::ranges::subrange(std::move(b), std::move(e));
44 struct KeyValue {
45 int key; // Only the key is considered for equality comparison.
46 char value; // Allows distinguishing equivalent instances.
48 bool operator<(const KeyValue& other) const { return key < other.key; }
49 bool operator==(const KeyValue& other) const { return key == other.key; }
52 template <>
53 struct std::hash<KeyValue> {
54 std::size_t operator()(const KeyValue& kv) const {
55 return kv.key;
59 #if !defined(TEST_HAS_NO_EXCEPTIONS)
61 template <class T>
62 struct ThrowingAllocator {
63 using value_type = T;
64 using char_type = T;
65 using is_always_equal = std::false_type;
67 ThrowingAllocator() = default;
69 template <class U>
70 ThrowingAllocator(const ThrowingAllocator<U>&) {}
72 T* allocate(std::size_t) { throw 1; }
73 void deallocate(T*, std::size_t) {}
75 template <class U>
76 friend bool operator==(const ThrowingAllocator&, const ThrowingAllocator<U>&) {
77 return true;
80 #endif
82 template <class T, class Func>
83 constexpr void for_all_iterators_and_allocators(Func f) {
84 using Iterators = types::type_list<
85 cpp20_input_iterator<T*>,
86 forward_iterator<T*>,
87 bidirectional_iterator<T*>,
88 random_access_iterator<T*>,
89 contiguous_iterator<T*>,
93 types::for_each(Iterators{}, [=]<class Iter>() {
94 f.template operator()<Iter, sentinel_wrapper<Iter>, std::allocator<T>>();
95 f.template operator()<Iter, sentinel_wrapper<Iter>, test_allocator<T>>();
96 f.template operator()<Iter, sentinel_wrapper<Iter>, min_allocator<T>>();
97 f.template operator()<Iter, sentinel_wrapper<Iter>, safe_allocator<T>>();
99 if constexpr (std::sentinel_for<Iter, Iter>) {
100 f.template operator()<Iter, Iter, std::allocator<T>>();
101 f.template operator()<Iter, Iter, test_allocator<T>>();
102 f.template operator()<Iter, Iter, min_allocator<T>>();
103 f.template operator()<Iter, Iter, safe_allocator<T>>();
108 #endif // SUPPORT_FROM_RANGE_HELPERS_H