]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/std/strings/basic.string/string.modifiers/string_assign/string_size_size.pass.cpp
Vendor import of libc++ release_39 branch r276489:
[FreeBSD/FreeBSD.git] / test / std / strings / basic.string / string.modifiers / string_assign / string_size_size.pass.cpp
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 // XFAIL: libcpp-no-exceptions
11 // <string>
12
13 // basic_string<charT,traits,Allocator>&
14 //   assign(const basic_string<charT,traits>& str, size_type pos, size_type n=npos);
15 // the =npos was added for C++14
16
17 #include <string>
18 #include <stdexcept>
19 #include <cassert>
20
21 #include "test_macros.h"
22 #include "min_allocator.h"
23
24 template <class S>
25 void
26 test(S s, S str, typename S::size_type pos, typename S::size_type n, S expected)
27 {
28     try
29     {
30         s.assign(str, pos, n);
31         LIBCPP_ASSERT(s.__invariants());
32         assert(pos <= str.size());
33         assert(s == expected);
34     }
35     catch (std::out_of_range&)
36     {
37         assert(pos > str.size());
38     }
39 }
40
41 template <class S>
42 void
43 test_npos(S s, S str, typename S::size_type pos, S expected)
44 {
45     try
46     {
47         s.assign(str, pos);
48         LIBCPP_ASSERT(s.__invariants());
49         assert(pos <= str.size());
50         assert(s == expected);
51     }
52     catch (std::out_of_range&)
53     {
54         assert(pos > str.size());
55     }
56 }
57
58 int main()
59 {
60     {
61     typedef std::string S;
62     test(S(), S(), 0, 0, S());
63     test(S(), S(), 1, 0, S());
64     test(S(), S("12345"), 0, 3, S("123"));
65     test(S(), S("12345"), 1, 4, S("2345"));
66     test(S(), S("12345"), 3, 15, S("45"));
67     test(S(), S("12345"), 5, 15, S(""));
68     test(S(), S("12345"), 6, 15, S("not happening"));
69     test(S(), S("12345678901234567890"), 0, 0, S());
70     test(S(), S("12345678901234567890"), 1, 1, S("2"));
71     test(S(), S("12345678901234567890"), 2, 3, S("345"));
72     test(S(), S("12345678901234567890"), 12, 13, S("34567890"));
73     test(S(), S("12345678901234567890"), 21, 13, S("not happening"));
74
75     test(S("12345"), S(), 0, 0, S());
76     test(S("12345"), S("12345"), 2, 2, S("34"));
77     test(S("12345"), S("1234567890"), 0, 100, S("1234567890"));
78
79     test(S("12345678901234567890"), S(), 0, 0, S());
80     test(S("12345678901234567890"), S("12345"), 1, 3, S("234"));
81     test(S("12345678901234567890"), S("12345678901234567890"), 5, 10,
82          S("6789012345"));
83     }
84 #if TEST_STD_VER >= 11
85     {
86     typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
87     test(S(), S(), 0, 0, S());
88     test(S(), S(), 1, 0, S());
89     test(S(), S("12345"), 0, 3, S("123"));
90     test(S(), S("12345"), 1, 4, S("2345"));
91     test(S(), S("12345"), 3, 15, S("45"));
92     test(S(), S("12345"), 5, 15, S(""));
93     test(S(), S("12345"), 6, 15, S("not happening"));
94     test(S(), S("12345678901234567890"), 0, 0, S());
95     test(S(), S("12345678901234567890"), 1, 1, S("2"));
96     test(S(), S("12345678901234567890"), 2, 3, S("345"));
97     test(S(), S("12345678901234567890"), 12, 13, S("34567890"));
98     test(S(), S("12345678901234567890"), 21, 13, S("not happening"));
99
100     test(S("12345"), S(), 0, 0, S());
101     test(S("12345"), S("12345"), 2, 2, S("34"));
102     test(S("12345"), S("1234567890"), 0, 100, S("1234567890"));
103
104     test(S("12345678901234567890"), S(), 0, 0, S());
105     test(S("12345678901234567890"), S("12345"), 1, 3, S("234"));
106     test(S("12345678901234567890"), S("12345678901234567890"), 5, 10,
107          S("6789012345"));
108     }
109 #endif
110     {
111     typedef std::string S;
112     test_npos(S(), S(), 0, S());
113     test_npos(S(), S(), 1, S());
114     test_npos(S(), S("12345"), 0, S("12345"));
115     test_npos(S(), S("12345"), 1, S("2345"));
116     test_npos(S(), S("12345"), 3, S("45"));
117     test_npos(S(), S("12345"), 5, S(""));
118     test_npos(S(), S("12345"), 6, S("not happening"));
119     }
120 }