libcxx

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

pointer_size.pass.cpp (3043B)


      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 //   append(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.append(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("12345"));
     44     test(S("12345"), "12345", 5, S("1234512345"));
     45     test(S("12345"), "1234567890", 10, S("123451234567890"));
     46 
     47     test(S("12345678901234567890"), "", 0, S("12345678901234567890"));
     48     test(S("12345678901234567890"), "12345", 5, S("1234567890123456789012345"));
     49     test(S("12345678901234567890"), "12345678901234567890", 20,
     50          S("1234567890123456789012345678901234567890"));
     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("12345"));
     64     test(S("12345"), "12345", 5, S("1234512345"));
     65     test(S("12345"), "1234567890", 10, S("123451234567890"));
     66 
     67     test(S("12345678901234567890"), "", 0, S("12345678901234567890"));
     68     test(S("12345678901234567890"), "12345", 5, S("1234567890123456789012345"));
     69     test(S("12345678901234567890"), "12345678901234567890", 20,
     70          S("1234567890123456789012345678901234567890"));
     71     }
     72 #endif
     73 
     74     { // test appending to self
     75     typedef std::string S;
     76     S s_short = "123/";
     77     S s_long  = "Lorem ipsum dolor sit amet, consectetur/";
     78 
     79     s_short.append(s_short.data(), s_short.size());
     80     assert(s_short == "123/123/");
     81     s_short.append(s_short.data(), s_short.size());
     82     assert(s_short == "123/123/123/123/");
     83     s_short.append(s_short.data(), s_short.size());
     84     assert(s_short == "123/123/123/123/123/123/123/123/");
     85 
     86     s_long.append(s_long.data(), s_long.size());
     87     assert(s_long == "Lorem ipsum dolor sit amet, consectetur/Lorem ipsum dolor sit amet, consectetur/");
     88     }
     89 }