| 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 | |
| 9 | // UNSUPPORTED: c++03 |
| 10 | |
| 11 | // <forward_list> |
| 12 | |
| 13 | // template <class... Args> |
| 14 | // iterator emplace_after(const_iterator p, Args&&... args); // constexpr since C++26 |
| 15 | |
| 16 | #include <forward_list> |
| 17 | #include <cassert> |
| 18 | |
| 19 | #include "test_macros.h" |
| 20 | #include "../../../Emplaceable.h" |
| 21 | #include "min_allocator.h" |
| 22 | |
| 23 | TEST_CONSTEXPR_CXX26 bool test() { |
| 24 | { |
| 25 | typedef Emplaceable T; |
| 26 | typedef std::forward_list<T> C; |
| 27 | typedef C::iterator I; |
| 28 | C c; |
| 29 | I i = c.emplace_after(pos: c.cbefore_begin()); |
| 30 | assert(i == c.begin()); |
| 31 | assert(c.front() == Emplaceable()); |
| 32 | assert(std::distance(c.begin(), c.end()) == 1); |
| 33 | |
| 34 | i = c.emplace_after(pos: c.cbegin(), args: 1, args: 2.5); |
| 35 | assert(i == std::next(c.begin())); |
| 36 | assert(c.front() == Emplaceable()); |
| 37 | assert(*std::next(c.begin()) == Emplaceable(1, 2.5)); |
| 38 | assert(std::distance(c.begin(), c.end()) == 2); |
| 39 | |
| 40 | i = c.emplace_after(pos: std::next(x: c.cbegin()), args: 2, args: 3.5); |
| 41 | assert(i == std::next(c.begin(), 2)); |
| 42 | assert(c.front() == Emplaceable()); |
| 43 | assert(*std::next(c.begin()) == Emplaceable(1, 2.5)); |
| 44 | assert(*std::next(c.begin(), 2) == Emplaceable(2, 3.5)); |
| 45 | assert(std::distance(c.begin(), c.end()) == 3); |
| 46 | |
| 47 | i = c.emplace_after(pos: c.cbegin(), args: 3, args: 4.5); |
| 48 | assert(i == std::next(c.begin())); |
| 49 | assert(c.front() == Emplaceable()); |
| 50 | assert(*std::next(c.begin(), 1) == Emplaceable(3, 4.5)); |
| 51 | assert(*std::next(c.begin(), 2) == Emplaceable(1, 2.5)); |
| 52 | assert(*std::next(c.begin(), 3) == Emplaceable(2, 3.5)); |
| 53 | assert(std::distance(c.begin(), c.end()) == 4); |
| 54 | } |
| 55 | { |
| 56 | typedef Emplaceable T; |
| 57 | typedef std::forward_list<T, min_allocator<T>> C; |
| 58 | typedef C::iterator I; |
| 59 | C c; |
| 60 | I i = c.emplace_after(c.cbefore_begin()); |
| 61 | assert(i == c.begin()); |
| 62 | assert(c.front() == Emplaceable()); |
| 63 | assert(std::distance(c.begin(), c.end()) == 1); |
| 64 | |
| 65 | i = c.emplace_after(c.cbegin(), 1, 2.5); |
| 66 | assert(i == std::next(c.begin())); |
| 67 | assert(c.front() == Emplaceable()); |
| 68 | assert(*std::next(c.begin()) == Emplaceable(1, 2.5)); |
| 69 | assert(std::distance(c.begin(), c.end()) == 2); |
| 70 | |
| 71 | i = c.emplace_after(std::next(c.cbegin()), 2, 3.5); |
| 72 | assert(i == std::next(c.begin(), 2)); |
| 73 | assert(c.front() == Emplaceable()); |
| 74 | assert(*std::next(c.begin()) == Emplaceable(1, 2.5)); |
| 75 | assert(*std::next(c.begin(), 2) == Emplaceable(2, 3.5)); |
| 76 | assert(std::distance(c.begin(), c.end()) == 3); |
| 77 | |
| 78 | i = c.emplace_after(c.cbegin(), 3, 4.5); |
| 79 | assert(i == std::next(c.begin())); |
| 80 | assert(c.front() == Emplaceable()); |
| 81 | assert(*std::next(c.begin(), 1) == Emplaceable(3, 4.5)); |
| 82 | assert(*std::next(c.begin(), 2) == Emplaceable(1, 2.5)); |
| 83 | assert(*std::next(c.begin(), 3) == Emplaceable(2, 3.5)); |
| 84 | assert(std::distance(c.begin(), c.end()) == 4); |
| 85 | } |
| 86 | |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | int main(int, char**) { |
| 91 | assert(test()); |
| 92 | #if TEST_STD_VER >= 26 |
| 93 | static_assert(test()); |
| 94 | #endif |
| 95 | |
| 96 | return 0; |
| 97 | } |
| 98 | |