blob: 5f5983e76c94abf4acc55d96ae4a1039f8c55834 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001//===----------------------------------------------------------------------===//
2//
Howard Hinnantf5256e12010-05-11 21:36:01 +00003// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10// <string>
11
Howard Hinnant6e0a1f42010-08-22 00:47:54 +000012// basic_string<charT,traits,Allocator>&
Marshall Clowa93b5e22014-03-04 19:17:19 +000013// assign(const basic_string<charT,traits>& str, size_type pos, size_type n=npos);
14// the =npos was added for C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000015
16#include <string>
17#include <stdexcept>
18#include <cassert>
19
Marshall Clow061d0cc2013-11-26 20:58:02 +000020#include "min_allocator.h"
Howard Hinnant9dcdcde2013-06-28 16:59:19 +000021
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000022template <class S>
23void
24test(S s, S str, typename S::size_type pos, typename S::size_type n, S expected)
25{
26 try
27 {
28 s.assign(str, pos, n);
Dan Albert1d4a1ed2016-05-25 22:36:09 -070029 assert(s.__invariants());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000030 assert(pos <= str.size());
31 assert(s == expected);
32 }
33 catch (std::out_of_range&)
34 {
35 assert(pos > str.size());
36 }
37}
38
Marshall Clowa93b5e22014-03-04 19:17:19 +000039template <class S>
40void
41test_npos(S s, S str, typename S::size_type pos, S expected)
42{
43 try
44 {
45 s.assign(str, pos);
Dan Albert1d4a1ed2016-05-25 22:36:09 -070046 assert(s.__invariants());
Marshall Clowa93b5e22014-03-04 19:17:19 +000047 assert(pos <= str.size());
48 assert(s == expected);
49 }
50 catch (std::out_of_range&)
51 {
52 assert(pos > str.size());
53 }
54}
55
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000056int main()
57{
Howard Hinnant9dcdcde2013-06-28 16:59:19 +000058 {
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000059 typedef std::string S;
60 test(S(), S(), 0, 0, S());
61 test(S(), S(), 1, 0, S());
62 test(S(), S("12345"), 0, 3, S("123"));
63 test(S(), S("12345"), 1, 4, S("2345"));
64 test(S(), S("12345"), 3, 15, S("45"));
65 test(S(), S("12345"), 5, 15, S(""));
66 test(S(), S("12345"), 6, 15, S("not happening"));
67 test(S(), S("12345678901234567890"), 0, 0, S());
68 test(S(), S("12345678901234567890"), 1, 1, S("2"));
69 test(S(), S("12345678901234567890"), 2, 3, S("345"));
70 test(S(), S("12345678901234567890"), 12, 13, S("34567890"));
71 test(S(), S("12345678901234567890"), 21, 13, S("not happening"));
72
73 test(S("12345"), S(), 0, 0, S());
74 test(S("12345"), S("12345"), 2, 2, S("34"));
75 test(S("12345"), S("1234567890"), 0, 100, S("1234567890"));
76
77 test(S("12345678901234567890"), S(), 0, 0, S());
78 test(S("12345678901234567890"), S("12345"), 1, 3, S("234"));
79 test(S("12345678901234567890"), S("12345678901234567890"), 5, 10,
80 S("6789012345"));
Howard Hinnant9dcdcde2013-06-28 16:59:19 +000081 }
Dan Albert1d4a1ed2016-05-25 22:36:09 -070082#if __cplusplus >= 201103L
Howard Hinnant9dcdcde2013-06-28 16:59:19 +000083 {
84 typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
85 test(S(), S(), 0, 0, S());
86 test(S(), S(), 1, 0, S());
87 test(S(), S("12345"), 0, 3, S("123"));
88 test(S(), S("12345"), 1, 4, S("2345"));
89 test(S(), S("12345"), 3, 15, S("45"));
90 test(S(), S("12345"), 5, 15, S(""));
91 test(S(), S("12345"), 6, 15, S("not happening"));
92 test(S(), S("12345678901234567890"), 0, 0, S());
93 test(S(), S("12345678901234567890"), 1, 1, S("2"));
94 test(S(), S("12345678901234567890"), 2, 3, S("345"));
95 test(S(), S("12345678901234567890"), 12, 13, S("34567890"));
96 test(S(), S("12345678901234567890"), 21, 13, S("not happening"));
97
98 test(S("12345"), S(), 0, 0, S());
99 test(S("12345"), S("12345"), 2, 2, S("34"));
100 test(S("12345"), S("1234567890"), 0, 100, S("1234567890"));
101
102 test(S("12345678901234567890"), S(), 0, 0, S());
103 test(S("12345678901234567890"), S("12345"), 1, 3, S("234"));
104 test(S("12345678901234567890"), S("12345678901234567890"), 5, 10,
105 S("6789012345"));
106 }
107#endif
Marshall Clowa93b5e22014-03-04 19:17:19 +0000108 {
109 typedef std::string S;
110 test_npos(S(), S(), 0, S());
111 test_npos(S(), S(), 1, S());
112 test_npos(S(), S("12345"), 0, S("12345"));
113 test_npos(S(), S("12345"), 1, S("2345"));
114 test_npos(S(), S("12345"), 3, S("45"));
115 test_npos(S(), S("12345"), 5, S(""));
116 test_npos(S(), S("12345"), 6, S("not happening"));
117 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000118}