xref: /llvm-project/libcxx/test/std/strings/string.view/string.view.iterators/rbegin.pass.cpp (revision a40bada91aeda276a772acfbcae6e8de26755a11)
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 // UNSUPPORTED: !stdlib=libc++ && (c++03 || c++11 || c++14)
10 
11 // <string_view>
12 
13 // const_iterator rbegin() const;
14 
15 #include <string_view>
16 #include <cassert>
17 
18 #include "test_macros.h"
19 
20 template <class S>
test(S s)21 void test(S s) {
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     const std::size_t last = s.size() - 1;
28     assert(*b == s[last]);
29     assert(&*b == &s[last]);
30     assert(*cb1 == s[last]);
31     assert(&*cb1 == &s[last]);
32     assert(*cb2 == s[last]);
33     assert(&*cb2 == &s[last]);
34   }
35   assert(b == cb1);
36   assert(b == cb2);
37   assert(cb1 == cb2);
38 }
39 
main(int,char **)40 int main(int, char**) {
41   typedef std::string_view string_view;
42 #ifndef TEST_HAS_NO_CHAR8_T
43   typedef std::u8string_view u8string_view;
44 #endif
45   typedef std::u16string_view u16string_view;
46   typedef std::u32string_view u32string_view;
47 
48   test(string_view());
49   test(u16string_view());
50   test(u32string_view());
51   test(string_view("123"));
52 #ifndef TEST_HAS_NO_CHAR8_T
53   test(u8string_view{u8"123"});
54 #endif
55 #if TEST_STD_VER >= 11
56   test(u16string_view{u"123"});
57   test(u32string_view{U"123"});
58 #endif
59 
60 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
61   typedef std::wstring_view wstring_view;
62   test(wstring_view());
63   test(wstring_view(L"123"));
64 #endif
65 
66 #if TEST_STD_VER > 14
67   {
68     constexpr string_view sv{"123", 3};
69 #  ifndef TEST_HAS_NO_CHAR8_T
70     constexpr u8string_view u8sv{u8"123", 3};
71 #  endif
72     constexpr u16string_view u16sv{u"123", 3};
73     constexpr u32string_view u32sv{U"123", 3};
74 
75     static_assert(*sv.rbegin() == sv[2], "");
76 #  ifndef TEST_HAS_NO_CHAR8_T
77     static_assert(*u8sv.rbegin() == u8sv[2], "");
78 #  endif
79     static_assert(*u16sv.rbegin() == u16sv[2], "");
80     static_assert(*u32sv.rbegin() == u32sv[2], "");
81 
82     static_assert(*sv.crbegin() == sv[2], "");
83 #  ifndef TEST_HAS_NO_CHAR8_T
84     static_assert(*u8sv.crbegin() == u8sv[2], "");
85 #  endif
86     static_assert(*u16sv.crbegin() == u16sv[2], "");
87     static_assert(*u32sv.crbegin() == u32sv[2], "");
88 
89 #  ifndef TEST_HAS_NO_WIDE_CHARACTERS
90     {
91       constexpr wstring_view wsv{L"123", 3};
92       static_assert(*wsv.rbegin() == wsv[2], "");
93       static_assert(*wsv.crbegin() == wsv[2], "");
94     }
95 #  endif
96   }
97 #endif // TEST_STD_VER > 14
98 
99   return 0;
100 }
101