Home | History | Annotate | Download | only in string.view.iterators
      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 // constexpr const_iterator end() const;
     13 
     14 #include <experimental/string_view>
     15 #include <cassert>
     16 
     17 template <class S>
     18 void
     19 test(S s)
     20 {
     21     const S& cs = s;
     22     typename S::iterator e = s.end();
     23     typename S::const_iterator ce1 = cs.end();
     24     typename S::const_iterator ce2 = s.cend();
     25 
     26     if (s.empty())
     27     {
     28         assert(  e ==  s.begin());
     29         assert(ce1 == cs.begin());
     30         assert(ce2 ==  s.begin());
     31     }
     32     else
     33     {
     34         assert(  e !=  s.begin());
     35         assert(ce1 != cs.begin());
     36         assert(ce2 !=  s.begin());
     37     }
     38 
     39     assert(  e -  s.begin() == s.size());
     40     assert(ce1 - cs.begin() == cs.size());
     41     assert(ce2 - s.cbegin() == s.size());
     42 
     43     assert(  e == ce1);
     44     assert(  e == ce2);
     45     assert(ce1 == ce2);
     46 }
     47 
     48 
     49 int main()
     50 {
     51     typedef std::experimental::string_view    string_view;
     52     typedef std::experimental::u16string_view u16string_view;
     53     typedef std::experimental::u32string_view u32string_view;
     54     typedef std::experimental::wstring_view   wstring_view;
     55 
     56     test(string_view   ());
     57     test(u16string_view());
     58     test(u32string_view());
     59     test(wstring_view  ());
     60     test(string_view   ( "123"));
     61     test(wstring_view  (L"123"));
     62 #if __cplusplus >= 201103L
     63     test(u16string_view{u"123"});
     64     test(u32string_view{U"123"});
     65 #endif
     66 
     67 #if _LIBCPP_STD_VER > 11
     68     {
     69     constexpr string_view       sv { "123", 3 };
     70     constexpr u16string_view u16sv {u"123", 3 };
     71     constexpr u32string_view u32sv {U"123", 3 };
     72     constexpr wstring_view     wsv {L"123", 3 };
     73 
     74     static_assert (    sv.begin() !=    sv.end(), "" );
     75     static_assert ( u16sv.begin() != u16sv.end(), "" );
     76     static_assert ( u32sv.begin() != u32sv.end(), "" );
     77     static_assert (   wsv.begin() !=   wsv.end(), "" );
     78 
     79     static_assert (    sv.begin() !=    sv.cend(), "" );
     80     static_assert ( u16sv.begin() != u16sv.cend(), "" );
     81     static_assert ( u32sv.begin() != u32sv.cend(), "" );
     82     static_assert (   wsv.begin() !=   wsv.cend(), "" );
     83     }
     84 #endif
     85 }
     86