blob: 2fc286e96a5b638ce9623a3b27d45b7832e19804 [file] [log] [blame]
Marshall Clow053d81c2016-07-21 05:31:24 +00001//===----------------------------------------------------------------------===//
2//
Chandler Carruth57b08b02019-01-19 10:56:40 +00003// 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
Marshall Clow053d81c2016-07-21 05:31:24 +00006//
7//===----------------------------------------------------------------------===//
8
9
10// <string_view>
11
12// void swap(basic_string_view& _other) noexcept
13
14#include <string_view>
15#include <cassert>
16
17#include "test_macros.h"
18
19template<typename CharT>
20void test ( const CharT *s, size_t len ) {
21 typedef std::basic_string_view<CharT> SV;
22 {
23 SV sv1(s);
24 SV sv2;
25
26 assert ( sv1.size() == len );
27 assert ( sv1.data() == s );
28 assert ( sv2.size() == 0 );
29
30 sv1.swap ( sv2 );
31 assert ( sv1.size() == 0 );
32 assert ( sv2.size() == len );
33 assert ( sv2.data() == s );
34 }
35
36}
37
Stephan T. Lavavej0f901c72016-11-04 20:26:59 +000038#if TEST_STD_VER > 11
Marshall Clow053d81c2016-07-21 05:31:24 +000039constexpr size_t test_ce ( size_t n, size_t k ) {
40 typedef std::basic_string_view<char> SV;
41 SV sv1{ "ABCDEFGHIJKL", n };
42 SV sv2 { sv1.data(), k };
43 sv1.swap ( sv2 );
44 return sv1.size();
45}
46#endif
47
48
JF Bastien2df59c52019-02-04 20:31:13 +000049int main(int, char**) {
Marshall Clow053d81c2016-07-21 05:31:24 +000050 test ( "ABCDE", 5 );
51 test ( "a", 1 );
52 test ( "", 0 );
53
54 test ( L"ABCDE", 5 );
55 test ( L"a", 1 );
56 test ( L"", 0 );
57
58#if TEST_STD_VER >= 11
59 test ( u"ABCDE", 5 );
60 test ( u"a", 1 );
61 test ( u"", 0 );
62
63 test ( U"ABCDE", 5 );
64 test ( U"a", 1 );
65 test ( U"", 0 );
66#endif
67
Stephan T. Lavavej0f901c72016-11-04 20:26:59 +000068#if TEST_STD_VER > 11
Marshall Clow053d81c2016-07-21 05:31:24 +000069 {
70 static_assert ( test_ce (2, 3) == 3, "" );
71 static_assert ( test_ce (5, 3) == 3, "" );
72 static_assert ( test_ce (0, 1) == 1, "" );
73 }
74#endif
JF Bastien2df59c52019-02-04 20:31:13 +000075
76 return 0;
Marshall Clow053d81c2016-07-21 05:31:24 +000077}