blob: a3a1dbf418b842b363220d5cb069747746d11f8b [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);
Eric Fiselierfd838222016-12-23 23:37:52 +000030 ((void)sv1);
Eric Fiselieraf4a5a72016-05-30 23:53:19 +000031 } catch (const std::out_of_range&) {
32 return;
33 } catch (...) {
34 assert(false);
35 }
36#endif
37 } else {
Marshall Clow5aa8fa22014-06-11 16:44:55 +000038 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] );
Eric Fiselieraf4a5a72016-05-30 23:53:19 +000043 }
Marshall Clow5aa8fa22014-06-11 16:44:55 +000044}
45
46
47template<typename CharT>
48void test ( const CharT *s ) {
49 typedef std::experimental::basic_string_view<CharT> string_view_t;
Eric Fiselierd04c6852016-06-01 21:35:39 +000050
Marshall Clow5aa8fa22014-06-11 16:44:55 +000051 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);
Eric Fiselierd04c6852016-06-01 21:35:39 +000057
Marshall Clow5aa8fa22014-06-11 16:44:55 +000058 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
71int 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
Eric Fiselieraf4a5a72016-05-30 23:53:19 +000082#if TEST_STD_VER >= 11
Marshall Clow5aa8fa22014-06-11 16:44:55 +000083 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
Eric Fiselierd04c6852016-06-01 21:35:39 +000093
Eric Fiselieraf4a5a72016-05-30 23:53:19 +000094#if TEST_STD_VER > 11
Marshall Clow5aa8fa22014-06-11 16:44:55 +000095 {
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 }
Eric Fiselierd04c6852016-06-01 21:35:39 +0000105
Marshall Clow5aa8fa22014-06-11 16:44:55 +0000106 {
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
Eric Fiseliercc2e1ab2015-02-10 17:32:49 +0000119}