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