diff --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.swap/iter_swap.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.swap/iter_swap.pass.cpp index b3a9f5fc259ef..283832ec656ab 100644 --- a/libcxx/test/std/algorithms/alg.modifying.operations/alg.swap/iter_swap.pass.cpp +++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.swap/iter_swap.pass.cpp @@ -10,35 +10,43 @@ // template // requires HasSwap -// void -// iter_swap(Iter1 a, Iter2 b); +// void iter_swap(Iter1 a, Iter2 b); // constexpr since C++20 #include #include #include "test_macros.h" - -#if TEST_STD_VER > 17 -constexpr bool test_swap_constexpr() -{ - int i = 1; - int j = 2; - std::iter_swap(&i, &j); - return i == 2 && j == 1; +#include "test_iterators.h" +#include "type_algorithms.h" + +struct TestIterators { + template + TEST_CONSTEXPR_CXX20 void operator()() { + types::for_each(types::forward_iterator_list(), TestImpl()); + } + + template + struct TestImpl { + template + TEST_CONSTEXPR_CXX20 void operator()() { + int i = 1; + int j = 2; + std::iter_swap(Iter1(&i), Iter2(&j)); + assert(i == 2 && j == 1); + } + }; +}; + +TEST_CONSTEXPR_CXX20 bool test() { + types::for_each(types::forward_iterator_list(), TestIterators()); + return true; } -#endif // TEST_STD_VER > 17 - -int main(int, char**) -{ - int i = 1; - int j = 2; - std::iter_swap(&i, &j); - assert(i == 2); - assert(j == 1); - -#if TEST_STD_VER > 17 - static_assert(test_swap_constexpr()); -#endif // TEST_STD_VER > 17 + +int main(int, char**) { + test(); +#if TEST_STD_VER >= 20 + static_assert(test()); +#endif return 0; } diff --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.swap/ranges.swap_ranges.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.swap/ranges.swap_ranges.pass.cpp index a8d69b2832b46..9b61768cffd97 100644 --- a/libcxx/test/std/algorithms/alg.modifying.operations/alg.swap/ranges.swap_ranges.pass.cpp +++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.swap/ranges.swap_ranges.pass.cpp @@ -24,51 +24,45 @@ #include #include +#include + #include "test_iterators.h" +#include "type_algorithms.h" constexpr void test_different_lengths() { - using Expected = std::ranges::swap_ranges_result; - int i[3] = {1, 2, 3}; - int j[1] = {4}; + using Expected = std::ranges::swap_ranges_result; + int i[3] = {1, 2, 3}; + int j[1] = {4}; std::same_as auto r = std::ranges::swap_ranges(i, i + 3, j, j + 1); assert(r.in1 == i + 1); assert(r.in2 == j + 1); - assert(i[0] == 4); - assert(i[1] == 2); - assert(i[2] == 3); - assert(j[0] == 1); + assert(std::ranges::equal(i, std::array{4, 2, 3})); + assert(std::ranges::equal(j, std::array{1})); std::same_as auto r2 = std::ranges::swap_ranges(i, j); assert(r2.in1 == i + 1); assert(r2.in2 == j + 1); - assert(i[0] == 1); - assert(i[1] == 2); - assert(i[2] == 3); - assert(j[0] == 4); + assert(std::ranges::equal(i, std::array{1, 2, 3})); + assert(std::ranges::equal(j, std::array{4})); std::same_as auto r3 = std::ranges::swap_ranges(j, j + 1, i, i + 3); assert(r3.in1 == j + 1); assert(r3.in2 == i + 1); - assert(i[0] == 4); - assert(i[1] == 2); - assert(i[2] == 3); - assert(j[0] == 1); + assert(std::ranges::equal(i, std::array{4, 2, 3})); + assert(std::ranges::equal(j, std::array{1})); std::same_as auto r4 = std::ranges::swap_ranges(j, i); assert(r4.in1 == j + 1); assert(r4.in2 == i + 1); - assert(i[0] == 1); - assert(i[1] == 2); - assert(i[2] == 3); - assert(j[0] == 4); + assert(std::ranges::equal(i, std::array{1, 2, 3})); + assert(std::ranges::equal(j, std::array{4})); } constexpr void test_range() { std::array r1 = {1, 2, 3}; std::array r2 = {4, 5, 6}; - - std::same_as::iterator, std::array::iterator>> auto r = std::ranges::swap_ranges(r1, r2); + std::same_as::iterator, std::array::iterator>> auto r = + std::ranges::swap_ranges(r1, r2); assert(r.in1 == r1.end()); assert(r.in2 == r2.end()); - assert((r1 == std::array{4, 5, 6})); assert((r2 == std::array{1, 2, 3})); } @@ -78,75 +72,54 @@ constexpr void test_borrowed_input_range() { int r1[] = {1, 2, 3}; int r2[] = {4, 5, 6}; std::ranges::swap_ranges(std::views::all(r1), r2); - assert(r1[0] == 4); - assert(r1[1] == 5); - assert(r1[2] == 6); - assert(r2[0] == 1); - assert(r2[1] == 2); - assert(r2[2] == 3); + assert(std::ranges::equal(r1, std::array{4, 5, 6})); + assert(std::ranges::equal(r2, std::array{1, 2, 3})); } { int r1[] = {1, 2, 3}; int r2[] = {4, 5, 6}; std::ranges::swap_ranges(r1, std::views::all(r2)); - assert(r1[0] == 4); - assert(r1[1] == 5); - assert(r1[2] == 6); - assert(r2[0] == 1); - assert(r2[1] == 2); - assert(r2[2] == 3); + assert(std::ranges::equal(r1, std::array{4, 5, 6})); + assert(std::ranges::equal(r2, std::array{1, 2, 3})); } { int r1[] = {1, 2, 3}; int r2[] = {4, 5, 6}; std::ranges::swap_ranges(std::views::all(r1), std::views::all(r2)); - assert(r1[0] == 4); - assert(r1[1] == 5); - assert(r1[2] == 6); - assert(r2[0] == 1); - assert(r2[1] == 2); - assert(r2[2] == 3); + assert(std::ranges::equal(r1, std::array{4, 5, 6})); + assert(std::ranges::equal(r2, std::array{1, 2, 3})); } } constexpr void test_sentinel() { - int i[3] = {1, 2, 3}; - int j[3] = {4, 5, 6}; - using It = cpp17_input_iterator; - using Sent = sentinel_wrapper; - using Expected = std::ranges::swap_ranges_result; - std::same_as auto r = - std::ranges::swap_ranges(It(i), Sent(It(i + 3)), It(j), Sent(It(j + 3))); + int i[3] = {1, 2, 3}; + int j[3] = {4, 5, 6}; + using It = cpp17_input_iterator; + using Sent = sentinel_wrapper; + using Expected = std::ranges::swap_ranges_result; + std::same_as auto r = std::ranges::swap_ranges(It(i), Sent(It(i + 3)), It(j), Sent(It(j + 3))); assert(base(r.in1) == i + 3); assert(base(r.in2) == j + 3); - assert(i[0] == 4); - assert(i[1] == 5); - assert(i[2] == 6); - assert(j[0] == 1); - assert(j[1] == 2); - assert(j[2] == 3); + assert(std::ranges::equal(i, std::array{4, 5, 6})); + assert(std::ranges::equal(j, std::array{1, 2, 3})); } template -constexpr void test_iterators() { +TEST_CONSTEXPR_CXX20 void test_iterators() { using Expected = std::ranges::swap_ranges_result; - int i[3] = {1, 2, 3}; - int j[3] = {4, 5, 6}; + int a[3] = {1, 2, 3}; + int b[3] = {4, 5, 6}; std::same_as auto r = - std::ranges::swap_ranges(Iter1(i), sentinel_wrapper(Iter1(i + 3)), Iter2(j), sentinel_wrapper(Iter2(j + 3))); - assert(base(r.in1) == i + 3); - assert(base(r.in2) == j + 3); - assert(i[0] == 4); - assert(i[1] == 5); - assert(i[2] == 6); - assert(j[0] == 1); - assert(j[1] == 2); - assert(j[2] == 3); + std::ranges::swap_ranges(Iter1(a), sentinel_wrapper(Iter1(a + 3)), Iter2(b), sentinel_wrapper(Iter2(b + 3))); + assert(base(r.in1) == a + 3); + assert(base(r.in2) == b + 3); + assert(std::ranges::equal(a, std::array{4, 5, 6})); + assert(std::ranges::equal(b, std::array{1, 2, 3})); } constexpr void test_rval_range() { { - using Expected = std::ranges::swap_ranges_result::iterator, std::ranges::dangling>; + using Expected = std::ranges::swap_ranges_result::iterator, std::ranges::dangling>; std::array r = {1, 2, 3}; std::same_as auto a = std::ranges::swap_ranges(r, std::array{4, 5, 6}); assert((r == std::array{4, 5, 6})); @@ -154,66 +127,27 @@ constexpr void test_rval_range() { } { std::array r = {1, 2, 3}; - using Expected = std::ranges::swap_ranges_result::iterator>; + using Expected = std::ranges::swap_ranges_result::iterator>; std::same_as auto b = std::ranges::swap_ranges(std::array{4, 5, 6}, r); assert((r == std::array{4, 5, 6})); assert(b.in2 == r.begin() + 3); } } -template -constexpr void test_proxy_in_iterators() { - test_iterators>, Out>(); - test_iterators>, Out>(); - test_iterators>, Out>(); - test_iterators>, Out>(); - test_iterators>, Out>(); -} - constexpr bool test() { test_range(); - - test_iterators, cpp20_input_iterator>(); - test_iterators, forward_iterator>(); - test_iterators, bidirectional_iterator>(); - test_iterators, random_access_iterator>(); - test_iterators, int*>(); - - test_iterators, cpp20_input_iterator>(); - test_iterators, forward_iterator>(); - test_iterators, bidirectional_iterator>(); - test_iterators, random_access_iterator>(); - test_iterators, int*>(); - - test_iterators, cpp20_input_iterator>(); - test_iterators, forward_iterator>(); - test_iterators, bidirectional_iterator>(); - test_iterators, random_access_iterator>(); - test_iterators, int*>(); - - test_iterators, cpp20_input_iterator>(); - test_iterators, forward_iterator>(); - test_iterators, bidirectional_iterator>(); - test_iterators, random_access_iterator>(); - test_iterators, int*>(); - - test_iterators>(); - test_iterators>(); - test_iterators>(); - test_iterators>(); - test_iterators(); - - test_proxy_in_iterators>>(); - test_proxy_in_iterators>>(); - test_proxy_in_iterators>>(); - test_proxy_in_iterators>>(); - test_proxy_in_iterators>>(); - test_sentinel(); test_different_lengths(); test_borrowed_input_range(); test_rval_range(); + types::for_each(types::cpp20_input_iterator_list(), []() { + types::for_each(types::cpp20_input_iterator_list(), []() { + test_iterators(); + test_iterators, ProxyIterator>(); + }); + }); + return true; } diff --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.swap/swap_ranges.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.swap/swap_ranges.pass.cpp index 8bfbcd755e39f..810aa93e076ee 100644 --- a/libcxx/test/std/algorithms/alg.modifying.operations/alg.swap/swap_ranges.pass.cpp +++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.swap/swap_ranges.pass.cpp @@ -14,162 +14,130 @@ // swap_ranges(Iter1 first1, Iter1 last1, Iter2 first2); #include +#include #include #include +#include #include #include "test_macros.h" #include "test_iterators.h" - -template -void -test() -{ - int i[3] = {1, 2, 3}; - int j[3] = {4, 5, 6}; - Iter2 r = std::swap_ranges(Iter1(i), Iter1(i+3), Iter2(j)); - assert(base(r) == j+3); - assert(i[0] == 4); - assert(i[1] == 5); - assert(i[2] == 6); - assert(j[0] == 1); - assert(j[1] == 2); - assert(j[2] == 3); -} +#include "type_algorithms.h" + +struct TestPtr { + template + TEST_CONSTEXPR_CXX20 void operator()() { + types::for_each(types::forward_iterator_list(), TestImpl()); + } + + template + struct TestImpl { + template + TEST_CONSTEXPR_CXX20 void operator()() { + int a[] = {1, 2, 3}; + int b[] = {4, 5, 6}; + Iter2 r = std::swap_ranges(Iter1(a), Iter1(a + 3), Iter2(b)); + assert(base(r) == b + 3); + assert(a[0] == 4 && a[1] == 5 && a[2] == 6); + assert(b[0] == 1 && b[1] == 2 && b[2] == 3); + } + }; +}; #if TEST_STD_VER >= 11 -template -void -test1() -{ - std::unique_ptr i[3]; - for (int k = 0; k < 3; ++k) - i[k].reset(new int(k+1)); - std::unique_ptr j[3]; - for (int k = 0; k < 3; ++k) - j[k].reset(new int(k+4)); - Iter2 r = std::swap_ranges(Iter1(i), Iter1(i+3), Iter2(j)); - assert(base(r) == j+3); - assert(*i[0] == 4); - assert(*i[1] == 5); - assert(*i[2] == 6); - assert(*j[0] == 1); - assert(*j[1] == 2); - assert(*j[2] == 3); +struct TestUniquePtr { + template + TEST_CONSTEXPR_CXX23 void operator()() { + types::for_each(types::forward_iterator_list*>(), TestImpl()); + } + + template + struct TestImpl { + template + TEST_CONSTEXPR_CXX23 void operator()() { + std::unique_ptr a[3]; + for (int k = 0; k < 3; ++k) + a[k].reset(new int(k + 1)); + std::unique_ptr b[3]; + for (int k = 0; k < 3; ++k) + b[k].reset(new int(k + 4)); + Iter2 r = std::swap_ranges(Iter1(a), Iter1(a + 3), Iter2(b)); + assert(base(r) == b + 3); + assert(*a[0] == 4 && *a[1] == 5 && *a[2] == 6); + assert(*b[0] == 1 && *b[1] == 2 && *b[2] == 3); + } + }; +}; +#endif + +template