1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // <string_view> 11 12 // constexpr basic_string_view substr(size_type pos = 0, size_type n = npos) const; 13 14 // Throws: out_of_range if pos > size(). 15 // Effects: Determines the effective length rlen of the string to reference as the smaller of n and size() - pos. 16 // Returns: basic_string_view(data()+pos, rlen). 17 18 #include <experimental/string_view> 19 #include <cassert> 20 21 #include "test_macros.h" 22 23 template<typename CharT> 24 void test1 ( std::experimental::basic_string_view<CharT> sv, size_t n, size_t pos ) { 25 if (pos > sv.size()) { 26 #ifndef TEST_HAS_NO_EXCEPTIONS 27 try { 28 std::experimental::basic_string_view<CharT> sv1 = sv.substr(pos, n); 29 assert(false); 30 ((void)sv1); 31 } catch (const std::out_of_range&) { 32 return; 33 } catch (...) { 34 assert(false); 35 } 36 #endif 37 } else { 38 std::experimental::basic_string_view<CharT> sv1 = sv.substr(pos, n); 39 const size_t rlen = std::min ( n, sv.size() - pos ); 40 assert ( sv1.size() == rlen ); 41 for ( size_t i = 0; i <= rlen; ++i ) 42 assert ( sv[pos+i] == sv1[i] ); 43 } 44 } 45 46 47 template<typename CharT> 48 void test ( const CharT *s ) { 49 typedef std::experimental::basic_string_view<CharT> string_view_t; 50 51 string_view_t sv1 ( s ); 52 53 test1(sv1, 0, 0); 54 test1(sv1, 1, 0); 55 test1(sv1, 20, 0); 56 test1(sv1, sv1.size(), 0); 57 58 test1(sv1, 0, 3); 59 test1(sv1, 2, 3); 60 test1(sv1, 100, 3); 61 62 test1(sv1, 0, string_view_t::npos); 63 test1(sv1, 2, string_view_t::npos); 64 test1(sv1, sv1.size(), string_view_t::npos); 65 66 test1(sv1, sv1.size() + 1, 0); 67 test1(sv1, sv1.size() + 1, 1); 68 test1(sv1, sv1.size() + 1, string_view_t::npos); 69 } 70 71 int main () { 72 test ( "ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE" ); 73 test ( "ABCDE"); 74 test ( "a" ); 75 test ( "" ); 76 77 test ( L"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE" ); 78 test ( L"ABCDE" ); 79 test ( L"a" ); 80 test ( L"" ); 81 82 #if TEST_STD_VER >= 11 83 test ( u"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE" ); 84 test ( u"ABCDE" ); 85 test ( u"a" ); 86 test ( u"" ); 87 88 test ( U"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE" ); 89 test ( U"ABCDE" ); 90 test ( U"a" ); 91 test ( U"" ); 92 #endif 93 94 #if TEST_STD_VER > 11 95 { 96 constexpr std::experimental::string_view sv1 { "ABCDE", 5 }; 97 98 { 99 constexpr std::experimental::string_view sv2 = sv1.substr ( 0, 3 ); 100 static_assert ( sv2.size() == 3, "" ); 101 static_assert ( sv2[0] == 'A', "" ); 102 static_assert ( sv2[1] == 'B', "" ); 103 static_assert ( sv2[2] == 'C', "" ); 104 } 105 106 { 107 constexpr std::experimental::string_view sv2 = sv1.substr ( 3, 0 ); 108 static_assert ( sv2.size() == 0, "" ); 109 } 110 111 { 112 constexpr std::experimental::string_view sv2 = sv1.substr ( 3, 3 ); 113 static_assert ( sv2.size() == 2, "" ); 114 static_assert ( sv2[0] == 'D', "" ); 115 static_assert ( sv2[1] == 'E', "" ); 116 } 117 } 118 #endif 119 } 120