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// <algorithm>
10
11// template<Iterator Iter1, Iterator Iter2>
12// requires HasSwap<Iter1::reference, Iter2::reference>
13// void iter_swap(Iter1 a, Iter2 b); // constexpr since C++20
14
15#include <algorithm>
16#include <cassert>
17
18#include "test_macros.h"
19#include "test_iterators.h"
20#include "type_algorithms.h"
21
22struct TestIterators {
23 template <class Iter>
24 TEST_CONSTEXPR_CXX20 void operator()() {
25 types::for_each(types::forward_iterator_list<int*>(), TestImpl<Iter>());
26 }
27
28 template <class Iter1>
29 struct TestImpl {
30 template <class Iter2>
31 TEST_CONSTEXPR_CXX20 void operator()() {
32 int i = 1;
33 int j = 2;
34 std::iter_swap(Iter1(&i), Iter2(&j));
35 assert(i == 2 && j == 1);
36 }
37 };
38};
39
40TEST_CONSTEXPR_CXX20 bool test() {
41 types::for_each(types::forward_iterator_list<int*>(), TestIterators());
42 return true;
43}
44
45int main(int, char**) {
46 test();
47#if TEST_STD_VER >= 20
48 static_assert(test());
49#endif
50
51 return 0;
52}
53

source code of libcxx/test/std/algorithms/alg.modifying.operations/alg.swap/iter_swap.pass.cpp