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: !stdlib=libc++ && (c++03 || c++11 || c++14)
10
11// <string_view>
12
13// void swap(basic_string_view& _other) noexcept
14
15#include <cassert>
16#include <cstddef>
17#include <string_view>
18
19#include "test_macros.h"
20
21template <typename CharT>
22void test(const CharT* s, std::size_t len) {
23 typedef std::basic_string_view<CharT> SV;
24 {
25 SV sv1(s);
26 SV sv2;
27
28 assert(sv1.size() == len);
29 assert(sv1.data() == s);
30 assert(sv2.size() == 0);
31
32 sv1.swap(sv2);
33 assert(sv1.size() == 0);
34 assert(sv2.size() == len);
35 assert(sv2.data() == s);
36 }
37}
38
39#if TEST_STD_VER > 11
40constexpr std::size_t test_ce(size_t n, size_t k) {
41 typedef std::basic_string_view<char> SV;
42 SV sv1{"ABCDEFGHIJKL", n};
43 SV sv2{sv1.data(), k};
44 sv1.swap(sv2);
45 return sv1.size();
46}
47#endif
48
49int main(int, char**) {
50 test(s: "ABCDE", len: 5);
51 test(s: "a", len: 1);
52 test(s: "", len: 0);
53
54#ifndef TEST_HAS_NO_WIDE_CHARACTERS
55 test(s: L"ABCDE", len: 5);
56 test(s: L"a", len: 1);
57 test(s: L"", len: 0);
58#endif
59
60#if TEST_STD_VER >= 11
61 test(u"ABCDE", 5);
62 test(u"a", 1);
63 test(u"", 0);
64
65 test(U"ABCDE", 5);
66 test(U"a", 1);
67 test(U"", 0);
68#endif
69
70#if TEST_STD_VER > 11
71 {
72 static_assert(test_ce(2, 3) == 3, "");
73 static_assert(test_ce(5, 3) == 3, "");
74 static_assert(test_ce(0, 1) == 1, "");
75 }
76#endif
77
78 return 0;
79}
80

source code of libcxx/test/std/strings/string.view/string.view.modifiers/swap.pass.cpp