| 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 | //===----------------------------------------------------------------------===// |
| 8 | // UNSUPPORTED: c++03, c++11, c++14, c++17 |
| 9 | |
| 10 | // <forward_list> |
| 11 | |
| 12 | // template <class T, class Allocator, class U> |
| 13 | // typename forward_list<T, Allocator>::size_type |
| 14 | // erase(forward_list<T, Allocator>& c, const U& value); // constexpr since C++26 |
| 15 | |
| 16 | #include <forward_list> |
| 17 | #include <optional> |
| 18 | |
| 19 | #include "test_macros.h" |
| 20 | #include "test_allocator.h" |
| 21 | #include "min_allocator.h" |
| 22 | |
| 23 | template <class S, class U> |
| 24 | TEST_CONSTEXPR_CXX26 void test0(S s, U val, S expected, std::size_t expected_erased_count) { |
| 25 | ASSERT_SAME_TYPE(typename S::size_type, decltype(std::erase(s, val))); |
| 26 | assert(expected_erased_count == std::erase(s, val)); |
| 27 | assert(s == expected); |
| 28 | } |
| 29 | |
| 30 | template <class S> |
| 31 | TEST_CONSTEXPR_CXX26 void test() { |
| 32 | test0(S(), 1, S(), 0); |
| 33 | |
| 34 | test0(S({1}), 1, S(), 1); |
| 35 | test0(S({1}), 2, S({1}), 0); |
| 36 | |
| 37 | test0(S({1, 2}), 1, S({2}), 1); |
| 38 | test0(S({1, 2}), 2, S({1}), 1); |
| 39 | test0(S({1, 2}), 3, S({1, 2}), 0); |
| 40 | test0(S({1, 1}), 1, S(), 2); |
| 41 | test0(S({1, 1}), 3, S({1, 1}), 0); |
| 42 | |
| 43 | test0(S({1, 2, 3}), 1, S({2, 3}), 1); |
| 44 | test0(S({1, 2, 3}), 2, S({1, 3}), 1); |
| 45 | test0(S({1, 2, 3}), 3, S({1, 2}), 1); |
| 46 | test0(S({1, 2, 3}), 4, S({1, 2, 3}), 0); |
| 47 | |
| 48 | test0(S({1, 1, 1}), 1, S(), 3); |
| 49 | test0(S({1, 1, 1}), 2, S({1, 1, 1}), 0); |
| 50 | test0(S({1, 1, 2}), 1, S({2}), 2); |
| 51 | test0(S({1, 1, 2}), 2, S({1, 1}), 1); |
| 52 | test0(S({1, 1, 2}), 3, S({1, 1, 2}), 0); |
| 53 | test0(S({1, 2, 2}), 1, S({2, 2}), 1); |
| 54 | test0(S({1, 2, 2}), 2, S({1}), 2); |
| 55 | test0(S({1, 2, 2}), 3, S({1, 2, 2}), 0); |
| 56 | |
| 57 | // Test cross-type erasure |
| 58 | using opt = std::optional<typename S::value_type>; |
| 59 | test0(S({1, 2, 1}), opt(), S({1, 2, 1}), 0); |
| 60 | test0(S({1, 2, 1}), opt(1), S({2}), 2); |
| 61 | test0(S({1, 2, 1}), opt(2), S({1, 1}), 1); |
| 62 | test0(S({1, 2, 1}), opt(3), S({1, 2, 1}), 0); |
| 63 | } |
| 64 | |
| 65 | TEST_CONSTEXPR_CXX26 bool test() { |
| 66 | test<std::forward_list<int>>(); |
| 67 | test<std::forward_list<int, min_allocator<int>>>(); |
| 68 | test<std::forward_list<int, test_allocator<int>>>(); |
| 69 | test<std::forward_list<long>>(); |
| 70 | test<std::forward_list<double>>(); |
| 71 | |
| 72 | return true; |
| 73 | } |
| 74 | |
| 75 | int main(int, char**) { |
| 76 | assert(test()); |
| 77 | #if TEST_STD_VER >= 26 |
| 78 | static_assert(test()); |
| 79 | #endif |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |