• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===----------------------------------------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // <string_view>
10 
11 // const_iterator rbegin() const;
12 
13 #include <string_view>
14 #include <cassert>
15 
16 #include "test_macros.h"
17 
18 template <class S>
19 void
test(S s)20 test(S s)
21 {
22     const S& cs = s;
23     typename S::reverse_iterator b = s.rbegin();
24     typename S::const_reverse_iterator cb1 = cs.rbegin();
25     typename S::const_reverse_iterator cb2 = s.crbegin();
26     if (!s.empty())
27     {
28         const size_t last = s.size() - 1;
29         assert(   *b ==  s[last]);
30         assert(  &*b == &s[last]);
31         assert( *cb1 ==  s[last]);
32         assert(&*cb1 == &s[last]);
33         assert( *cb2 ==  s[last]);
34         assert(&*cb2 == &s[last]);
35 
36     }
37     assert(  b == cb1);
38     assert(  b == cb2);
39     assert(cb1 == cb2);
40 }
41 
42 
main(int,char **)43 int main(int, char**)
44 {
45     typedef std::string_view    string_view;
46 #if defined(__cpp_lib_char8_t) && __cpp_lib_char8_t >= 201811L
47     typedef std::u8string_view  u8string_view;
48 #endif
49     typedef std::u16string_view u16string_view;
50     typedef std::u32string_view u32string_view;
51     typedef std::wstring_view   wstring_view;
52 
53     test(string_view   ());
54     test(u16string_view());
55     test(u32string_view());
56     test(wstring_view  ());
57     test(string_view   ( "123"));
58     test(wstring_view  (L"123"));
59 #if defined(__cpp_lib_char8_t) && __cpp_lib_char8_t >= 201811L
60     test(u8string_view{u8"123"});
61 #endif
62 #if TEST_STD_VER >= 11
63     test(u16string_view{u"123"});
64     test(u32string_view{U"123"});
65 #endif
66 
67 #if TEST_STD_VER > 14
68     {
69     constexpr string_view       sv { "123", 3 };
70 #if defined(__cpp_lib_char8_t) && __cpp_lib_char8_t >= 201811L
71     constexpr u8string_view u8sv  {u8"123", 3 };
72 #endif
73     constexpr u16string_view u16sv {u"123", 3 };
74     constexpr u32string_view u32sv {U"123", 3 };
75     constexpr wstring_view     wsv {L"123", 3 };
76 
77     static_assert (    *sv.rbegin() ==    sv[2], "" );
78 #if defined(__cpp_lib_char8_t) && __cpp_lib_char8_t >= 201811L
79     static_assert (  *u8sv.rbegin() ==  u8sv[2], "" );
80 #endif
81     static_assert ( *u16sv.rbegin() == u16sv[2], "" );
82     static_assert ( *u32sv.rbegin() == u32sv[2], "" );
83     static_assert (   *wsv.rbegin() ==   wsv[2], "" );
84 
85     static_assert (    *sv.crbegin() ==    sv[2], "" );
86 #if defined(__cpp_lib_char8_t) && __cpp_lib_char8_t >= 201811L
87     static_assert (  *u8sv.crbegin() == u8sv[2], "" );
88 #endif
89     static_assert ( *u16sv.crbegin() == u16sv[2], "" );
90     static_assert ( *u32sv.crbegin() == u32sv[2], "" );
91     static_assert (   *wsv.crbegin() ==   wsv[2], "" );
92     }
93 #endif
94 
95   return 0;
96 }
97