blob: 476fe963de73640ccd1c533053a3acf1b2ddb2db [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>&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000013// assign(const charT* s, size_type n);
14
15#include <string>
16#include <stdexcept>
17#include <cassert>
18
Marshall Clow061d0cc2013-11-26 20:58:02 +000019#include "min_allocator.h"
Howard Hinnant9dcdcde2013-06-28 16:59:19 +000020
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000021template <class S>
22void
23test(S s, const typename S::value_type* str, typename S::size_type n, S expected)
24{
25 s.assign(str, n);
Dan Albert1d4a1ed2016-05-25 22:36:09 -070026 assert(s.__invariants());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000027 assert(s == expected);
28}
29
30int main()
31{
Howard Hinnant9dcdcde2013-06-28 16:59:19 +000032 {
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000033 typedef std::string S;
34 test(S(), "", 0, S());
35 test(S(), "12345", 3, S("123"));
36 test(S(), "12345", 4, S("1234"));
37 test(S(), "12345678901234567890", 0, S());
38 test(S(), "12345678901234567890", 1, S("1"));
39 test(S(), "12345678901234567890", 3, S("123"));
40 test(S(), "12345678901234567890", 20, S("12345678901234567890"));
41
42 test(S("12345"), "", 0, S());
43 test(S("12345"), "12345", 5, S("12345"));
44 test(S("12345"), "1234567890", 10, S("1234567890"));
45
46 test(S("12345678901234567890"), "", 0, S());
47 test(S("12345678901234567890"), "12345", 5, S("12345"));
48 test(S("12345678901234567890"), "12345678901234567890", 20,
49 S("12345678901234567890"));
Howard Hinnant9dcdcde2013-06-28 16:59:19 +000050 }
Dan Albert1d4a1ed2016-05-25 22:36:09 -070051#if __cplusplus >= 201103L
Howard Hinnant9dcdcde2013-06-28 16:59:19 +000052 {
53 typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
54 test(S(), "", 0, S());
55 test(S(), "12345", 3, S("123"));
56 test(S(), "12345", 4, S("1234"));
57 test(S(), "12345678901234567890", 0, S());
58 test(S(), "12345678901234567890", 1, S("1"));
59 test(S(), "12345678901234567890", 3, S("123"));
60 test(S(), "12345678901234567890", 20, S("12345678901234567890"));
61
62 test(S("12345"), "", 0, S());
63 test(S("12345"), "12345", 5, S("12345"));
64 test(S("12345"), "1234567890", 10, S("1234567890"));
65
66 test(S("12345678901234567890"), "", 0, S());
67 test(S("12345678901234567890"), "12345", 5, S("12345"));
68 test(S("12345678901234567890"), "12345678901234567890", 20,
69 S("12345678901234567890"));
70 }
71#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000072}