blob: 862a61d808201ebc1e138a8f7a1f68a0bbe1b320 [file] [log] [blame]
Marshall Clow5aa8fa22014-06-11 16:44:55 +00001//===----------------------------------------------------------------------===//
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
Marshall Clow5aa8fa22014-06-11 16:44:55 +000010// <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
Eric Fiselieraf4a5a72016-05-30 23:53:19 +000021#include "test_macros.h"
22
Marshall Clow5aa8fa22014-06-11 16:44:55 +000023template<typename CharT>
24void test1 ( std::experimental::basic_string_view<CharT> sv, size_t n, size_t pos ) {
Eric Fiselieraf4a5a72016-05-30 23:53:19 +000025 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 } catch (const std::out_of_range&) {
31 return;
32 } catch (...) {
33 assert(false);
34 }
35#endif
36 } else {
Marshall Clow5aa8fa22014-06-11 16:44:55 +000037 std::experimental::basic_string_view<CharT> sv1 = sv.substr(pos, n);
38 const size_t rlen = std::min ( n, sv.size() - pos );
39 assert ( sv1.size() == rlen );
40 for ( size_t i = 0; i <= rlen; ++i )
41 assert ( sv[pos+i] == sv1[i] );
Eric Fiselieraf4a5a72016-05-30 23:53:19 +000042 }
Marshall Clow5aa8fa22014-06-11 16:44:55 +000043}
44
45
46template<typename CharT>
47void test ( const CharT *s ) {
48 typedef std::experimental::basic_string_view<CharT> string_view_t;
49
50 string_view_t sv1 ( s );
51
52 test1(sv1, 0, 0);
53 test1(sv1, 1, 0);
54 test1(sv1, 20, 0);
55 test1(sv1, sv1.size(), 0);
56
57 test1(sv1, 0, 3);
58 test1(sv1, 2, 3);
59 test1(sv1, 100, 3);
60
61 test1(sv1, 0, string_view_t::npos);
62 test1(sv1, 2, string_view_t::npos);
63 test1(sv1, sv1.size(), string_view_t::npos);
64
65 test1(sv1, sv1.size() + 1, 0);
66 test1(sv1, sv1.size() + 1, 1);
67 test1(sv1, sv1.size() + 1, string_view_t::npos);
68}
69
70int main () {
71 test ( "ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE" );
72 test ( "ABCDE");
73 test ( "a" );
74 test ( "" );
75
76 test ( L"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE" );
77 test ( L"ABCDE" );
78 test ( L"a" );
79 test ( L"" );
80
Eric Fiselieraf4a5a72016-05-30 23:53:19 +000081#if TEST_STD_VER >= 11
Marshall Clow5aa8fa22014-06-11 16:44:55 +000082 test ( u"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE" );
83 test ( u"ABCDE" );
84 test ( u"a" );
85 test ( u"" );
86
87 test ( U"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE" );
88 test ( U"ABCDE" );
89 test ( U"a" );
90 test ( U"" );
91#endif
92
Eric Fiselieraf4a5a72016-05-30 23:53:19 +000093#if TEST_STD_VER > 11
Marshall Clow5aa8fa22014-06-11 16:44:55 +000094 {
95 constexpr std::experimental::string_view sv1 { "ABCDE", 5 };
96
97 {
98 constexpr std::experimental::string_view sv2 = sv1.substr ( 0, 3 );
99 static_assert ( sv2.size() == 3, "" );
100 static_assert ( sv2[0] == 'A', "" );
101 static_assert ( sv2[1] == 'B', "" );
102 static_assert ( sv2[2] == 'C', "" );
103 }
104
105 {
106 constexpr std::experimental::string_view sv2 = sv1.substr ( 3, 0 );
107 static_assert ( sv2.size() == 0, "" );
108 }
109
110 {
111 constexpr std::experimental::string_view sv2 = sv1.substr ( 3, 3 );
112 static_assert ( sv2.size() == 2, "" );
113 static_assert ( sv2[0] == 'D', "" );
114 static_assert ( sv2[1] == 'E', "" );
115 }
116 }
117#endif
Eric Fiseliercc2e1ab2015-02-10 17:32:49 +0000118}