|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | + |
| 4 | +#include <cassert> |
| 5 | +#include <deque> |
| 6 | +#include <forward_list> |
| 7 | +#include <list> |
| 8 | +#include <map> |
| 9 | +#include <set> |
| 10 | +#include <string> |
| 11 | +#include <unordered_map> |
| 12 | +#include <unordered_set> |
| 13 | +#include <utility> |
| 14 | +#include <vector> |
| 15 | + |
| 16 | +// Note that the standard actually requires these to be copyable. As an extension, we want to ensure we don't copy them, |
| 17 | +// because copying some functors (e.g. std::function) is comparatively expensive, and even for relatively cheap to copy |
| 18 | +// function objects we care (somewhat) about debug mode perf. |
| 19 | +struct no_copy { |
| 20 | + no_copy() = default; |
| 21 | + no_copy(const no_copy&) = delete; |
| 22 | + no_copy(no_copy&&) = default; |
| 23 | + no_copy& operator=(const no_copy&) = delete; |
| 24 | + no_copy& operator=(no_copy&&) = delete; |
| 25 | +}; |
| 26 | + |
| 27 | +struct is_vowel : no_copy { |
| 28 | + constexpr bool operator()(const char c) const { |
| 29 | + return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'; |
| 30 | + } |
| 31 | +}; |
| 32 | + |
| 33 | +struct is_odd : no_copy { |
| 34 | + constexpr bool operator()(const int i) const { |
| 35 | + return i % 2 != 0; |
| 36 | + } |
| 37 | +}; |
| 38 | + |
| 39 | +struct is_first_odd : no_copy { |
| 40 | + bool operator()(const std::pair<const int, int>& p) const { |
| 41 | + return p.first % 2 != 0; |
| 42 | + } |
| 43 | +}; |
| 44 | + |
| 45 | +constexpr bool test_string() { |
| 46 | + std::string str1{"cute fluffy kittens"}; |
| 47 | + const auto str1_removed = std::erase_if(str1, is_vowel{}); |
| 48 | + assert(str1 == "ct flffy kttns"); |
| 49 | + assert(str1_removed == 5); |
| 50 | + |
| 51 | + std::string str2{"asynchronous beat"}; |
| 52 | + const auto str2_removed = std::erase(str2, 'a'); |
| 53 | + assert(str2 == "synchronous bet"); |
| 54 | + assert(str2_removed == 2); |
| 55 | + |
| 56 | + return true; |
| 57 | +} |
| 58 | + |
| 59 | +template <class SequenceContainer> |
| 60 | +constexpr bool test_sequence_container() { |
| 61 | + SequenceContainer c{3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3, 2, 3, 8, 4, 6, 2, 6, 4, 3, 3, 8, 3, 2, 7, 9, 5, |
| 62 | + 0, 2, 8, 8, 4, 1, 9, 7, 1, 6, 9, 3, 9, 9, 3, 7, 5, 1, 0}; |
| 63 | + |
| 64 | + { |
| 65 | + const auto removed1 = std::erase_if(c, is_odd{}); |
| 66 | + assert(removed1 == 31); |
| 67 | + const SequenceContainer expected1{4, 2, 6, 8, 2, 8, 4, 6, 2, 6, 4, 8, 2, 0, 2, 8, 8, 4, 6, 0}; |
| 68 | + assert(c == expected1); |
| 69 | + } |
| 70 | + |
| 71 | + { |
| 72 | + const auto removed2 = std::erase(c, 8); |
| 73 | + assert(removed2 == 5); |
| 74 | + const SequenceContainer expected2{4, 2, 6, 2, 4, 6, 2, 6, 4, 2, 0, 2, 4, 6, 0}; |
| 75 | + assert(c == expected2); |
| 76 | + } |
| 77 | + |
| 78 | + return true; |
| 79 | +} |
| 80 | + |
| 81 | +// Also test LWG-4135 "The helper lambda of std::erase for list should specify return type as bool" |
| 82 | + |
| 83 | +template <bool B> |
| 84 | +struct pinned_condition { |
| 85 | + explicit pinned_condition() = default; |
| 86 | + pinned_condition(const pinned_condition&) = delete; |
| 87 | + pinned_condition& operator=(const pinned_condition&) = delete; |
| 88 | + |
| 89 | + operator bool() const { |
| 90 | + return B; |
| 91 | + } |
| 92 | + |
| 93 | + pinned_condition<!B> operator!() const { |
| 94 | + return {}; |
| 95 | + } |
| 96 | +}; |
| 97 | + |
| 98 | +struct lwg_4135_src { |
| 99 | + static constexpr pinned_condition<true> result{}; |
| 100 | + |
| 101 | + friend void operator==(int&, const lwg_4135_src&) = delete; |
| 102 | + friend void operator==(const lwg_4135_src&, int&) = delete; |
| 103 | + |
| 104 | + friend const pinned_condition<true>& operator==(const lwg_4135_src&, const int&) { |
| 105 | + return result; |
| 106 | + } |
| 107 | + friend const pinned_condition<true>& operator==(const int&, const lwg_4135_src&) { |
| 108 | + return result; |
| 109 | + } |
| 110 | +}; |
| 111 | + |
| 112 | +template <class ListContainer> |
| 113 | +void test_list_erase() { |
| 114 | + ListContainer ls2{42, 1729}; |
| 115 | + const auto ls2_removed = std::erase(ls2, lwg_4135_src{}); |
| 116 | + assert(ls2.empty()); |
| 117 | + assert(ls2_removed == 2); |
| 118 | +} |
| 119 | + |
| 120 | +static_assert(test_string()); |
| 121 | +static_assert(test_sequence_container<std::vector<int>>()); |
| 122 | + |
| 123 | +int main() { |
| 124 | + test_string(); |
| 125 | + test_sequence_container<std::deque<int>>(); |
| 126 | + test_sequence_container<std::forward_list<int>>(); |
| 127 | + test_sequence_container<std::list<int>>(); |
| 128 | + test_sequence_container<std::vector<int>>(); |
| 129 | + |
| 130 | + test_list_erase<std::forward_list<int>>(); |
| 131 | + test_list_erase<std::list<int>>(); |
| 132 | + |
| 133 | + std::map<int, int> m{{1, 10}, {2, 20}, {3, 30}, {4, 40}, {5, 50}, {6, 60}, {7, 70}}; |
| 134 | + const auto m_removed = std::erase_if(m, is_first_odd{}); |
| 135 | + assert((m == std::map<int, int>{{2, 20}, {4, 40}, {6, 60}})); |
| 136 | + assert(m_removed == 4); |
| 137 | + |
| 138 | + std::multimap<int, int> mm{{1, 10}, {2, 20}, {3, 30}, {4, 40}, {5, 50}, {6, 60}, {7, 70}}; |
| 139 | + const auto mm_removed = std::erase_if(mm, is_first_odd{}); |
| 140 | + assert((mm == std::multimap<int, int>{{2, 20}, {4, 40}, {6, 60}})); |
| 141 | + assert(mm_removed == 4); |
| 142 | + |
| 143 | + std::set<int> s{1, 2, 3, 4, 5, 6, 7}; |
| 144 | + const auto s_removed = std::erase_if(s, is_odd{}); |
| 145 | + assert((s == std::set<int>{2, 4, 6})); |
| 146 | + assert(s_removed == 4); |
| 147 | + |
| 148 | + std::multiset<int> ms{1, 2, 3, 4, 5, 6, 7}; |
| 149 | + const auto ms_removed = std::erase_if(ms, is_odd{}); |
| 150 | + assert((ms == std::multiset<int>{2, 4, 6})); |
| 151 | + assert(ms_removed == 4); |
| 152 | + |
| 153 | + // Note that unordered equality considers permutations. |
| 154 | + |
| 155 | + std::unordered_map<int, int> um{{1, 10}, {2, 20}, {3, 30}, {4, 40}, {5, 50}, {6, 60}, {7, 70}}; |
| 156 | + const auto um_removed = std::erase_if(um, is_first_odd{}); |
| 157 | + assert((um == std::unordered_map<int, int>{{2, 20}, {4, 40}, {6, 60}})); |
| 158 | + assert(um_removed == 4); |
| 159 | + |
| 160 | + std::unordered_multimap<int, int> umm{{1, 10}, {2, 20}, {3, 30}, {4, 40}, {5, 50}, {6, 60}, {7, 70}}; |
| 161 | + const auto umm_removed = std::erase_if(umm, is_first_odd{}); |
| 162 | + assert((umm == std::unordered_multimap<int, int>{{2, 20}, {4, 40}, {6, 60}})); |
| 163 | + assert(umm_removed == 4); |
| 164 | + |
| 165 | + std::unordered_set<int> us{1, 2, 3, 4, 5, 6, 7}; |
| 166 | + const auto us_removed = std::erase_if(us, is_odd{}); |
| 167 | + assert((us == std::unordered_set<int>{2, 4, 6})); |
| 168 | + assert(us_removed == 4); |
| 169 | + |
| 170 | + std::unordered_multiset<int> ums{1, 2, 3, 4, 5, 6, 7}; |
| 171 | + const auto ums_removed = std::erase_if(ums, is_odd{}); |
| 172 | + assert((ums == std::unordered_multiset<int>{2, 4, 6})); |
| 173 | + assert(ums_removed == 4); |
| 174 | +} |
0 commit comments