Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
Chandler Carruth | 57b08b0 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 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 |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 6 | // |
| 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 | |
| 19 | template<typename CharT> |
| 20 | void 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. Lavavej | 0f901c7 | 2016-11-04 20:26:59 +0000 | [diff] [blame] | 38 | #if TEST_STD_VER > 11 |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 39 | constexpr 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 Bastien | 2df59c5 | 2019-02-04 20:31:13 +0000 | [diff] [blame] | 49 | int main(int, char**) { |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 50 | 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. Lavavej | 0f901c7 | 2016-11-04 20:26:59 +0000 | [diff] [blame] | 68 | #if TEST_STD_VER > 11 |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 69 | { |
| 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 Bastien | 2df59c5 | 2019-02-04 20:31:13 +0000 | [diff] [blame] | 75 | |
| 76 | return 0; |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 77 | } |