libcxx

libcxx mirror with random patches
git clone https://git.neptards.moe/neptards/libcxx.git
Log | Files | Refs

pointer_size.pass.cpp (2797B)


      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>
     11 
     12 // basic_string<charT,traits,Allocator>&
     13 //   assign(const charT* s, size_type n);
     14 
     15 #include <string>
     16 #include <stdexcept>
     17 #include <cassert>
     18 
     19 #include "test_macros.h"
     20 #include "min_allocator.h"
     21 
     22 template <class S>
     23 void
     24 test(S s, const typename S::value_type* str, typename S::size_type n, S expected)
     25 {
     26     s.assign(str, n);
     27     LIBCPP_ASSERT(s.__invariants());
     28     assert(s == expected);
     29 }
     30 
     31 int main()
     32 {
     33     {
     34     typedef std::string S;
     35     test(S(), "", 0, S());
     36     test(S(), "12345", 3, S("123"));
     37     test(S(), "12345", 4, S("1234"));
     38     test(S(), "12345678901234567890", 0, S());
     39     test(S(), "12345678901234567890", 1, S("1"));
     40     test(S(), "12345678901234567890", 3, S("123"));
     41     test(S(), "12345678901234567890", 20, S("12345678901234567890"));
     42 
     43     test(S("12345"), "", 0, S());
     44     test(S("12345"), "12345", 5, S("12345"));
     45     test(S("12345"), "1234567890", 10, S("1234567890"));
     46 
     47     test(S("12345678901234567890"), "", 0, S());
     48     test(S("12345678901234567890"), "12345", 5, S("12345"));
     49     test(S("12345678901234567890"), "12345678901234567890", 20,
     50          S("12345678901234567890"));
     51     }
     52 #if TEST_STD_VER >= 11
     53     {
     54     typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
     55     test(S(), "", 0, S());
     56     test(S(), "12345", 3, S("123"));
     57     test(S(), "12345", 4, S("1234"));
     58     test(S(), "12345678901234567890", 0, S());
     59     test(S(), "12345678901234567890", 1, S("1"));
     60     test(S(), "12345678901234567890", 3, S("123"));
     61     test(S(), "12345678901234567890", 20, S("12345678901234567890"));
     62 
     63     test(S("12345"), "", 0, S());
     64     test(S("12345"), "12345", 5, S("12345"));
     65     test(S("12345"), "1234567890", 10, S("1234567890"));
     66 
     67     test(S("12345678901234567890"), "", 0, S());
     68     test(S("12345678901234567890"), "12345", 5, S("12345"));
     69     test(S("12345678901234567890"), "12345678901234567890", 20,
     70          S("12345678901234567890"));
     71     }
     72 #endif
     73     { // test assign to self
     74     typedef std::string S;
     75     S s_short = "123/";
     76     S s_long  = "Lorem ipsum dolor sit amet, consectetur/";
     77 
     78     s_short.assign(s_short.data(), s_short.size());
     79     assert(s_short == "123/");
     80     s_short.assign(s_short.data() + 2, s_short.size() - 2);
     81     assert(s_short == "3/");
     82 
     83     s_long.assign(s_long.data(), s_long.size());
     84     assert(s_long == "Lorem ipsum dolor sit amet, consectetur/");
     85 
     86     s_long.assign(s_long.data() + 2, 8 );
     87     assert(s_long == "rem ipsu");
     88     }
     89 }