Home | History | Annotate | Download | only in string.view.ops
      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 
     11 // <string_view>
     12 
     13 // size_type copy(charT* s, size_type n, size_type pos = 0) const;
     14 
     15 // Throws: out_of_range if pos > size().
     16 // Remarks: Let rlen be the smaller of n and size() - pos.
     17 // Requires: [s, s+rlen) is a valid range.
     18 // Effects: Equivalent to std::copy_n(begin() + pos, rlen, s).
     19 // Returns: rlen.
     20 
     21 
     22 #include <experimental/string_view>
     23 #include <cassert>
     24 
     25 template<typename CharT>
     26 void test1 ( std::experimental::basic_string_view<CharT> sv, size_t n, size_t pos ) {
     27     const size_t rlen = std::min ( n, sv.size() - pos );
     28 
     29     CharT *dest1 = new CharT [rlen + 1];    dest1[rlen] = 0;
     30     CharT *dest2 = new CharT [rlen + 1];    dest2[rlen] = 0;
     31 
     32     try {
     33         sv.copy(dest1, n, pos);
     34         std::copy_n(sv.begin() + pos, rlen, dest2);
     35 
     36         for ( size_t i = 0; i <= rlen; ++i )
     37             assert ( dest1[i] == dest2[i] );
     38         }
     39     catch ( const std::out_of_range & ) { assert ( pos > sv.size()); }
     40     delete [] dest1;
     41     delete [] dest2;
     42 }
     43 
     44 
     45 template<typename CharT>
     46 void test ( const CharT *s ) {
     47     typedef std::experimental::basic_string_view<CharT> string_view_t;
     48 
     49     string_view_t sv1 ( s );
     50 
     51     test1(sv1,  0, 0);
     52     test1(sv1,  1, 0);
     53     test1(sv1, 20, 0);
     54     test1(sv1, sv1.size(), 0);
     55     test1(sv1, 20, string_view_t::npos);
     56 
     57     test1(sv1,   0, 3);
     58     test1(sv1,   2, 3);
     59     test1(sv1, 100, 3);
     60     test1(sv1, 100, string_view_t::npos);
     61 
     62     test1(sv1, sv1.size(), string_view_t::npos);
     63 
     64     test1(sv1, sv1.size() + 1, 0);
     65     test1(sv1, sv1.size() + 1, 1);
     66     test1(sv1, sv1.size() + 1, string_view_t::npos);
     67 
     68 }
     69 
     70 int main () {
     71     test ( "ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE" );
     72     test ( "ABCDE");
     73     test ( "a" );
     74     test ( "" );
     75 
     76     test ( L"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE" );
     77     test ( L"ABCDE" );
     78     test ( L"a" );
     79     test ( L"" );
     80 
     81 #if __cplusplus >= 201103L
     82     test ( u"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE" );
     83     test ( u"ABCDE" );
     84     test ( u"a" );
     85     test ( u"" );
     86 
     87     test ( U"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE" );
     88     test ( U"ABCDE" );
     89     test ( U"a" );
     90     test ( U"" );
     91 #endif
     92 }
     93