blob: b9e5fa8bc0648699b2ca7304df2d52f95ceea967 [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
12// iterator insert(const_iterator p, charT c);
13
Dan Albert1d4a1ed2016-05-25 22:36:09 -070014#if _LIBCPP_DEBUG >= 1
15#define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::exit(0))
16#endif
17
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000018#include <string>
19#include <stdexcept>
20#include <cassert>
21
Marshall Clow061d0cc2013-11-26 20:58:02 +000022#include "min_allocator.h"
Howard Hinnant9dcdcde2013-06-28 16:59:19 +000023
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000024template <class S>
25void
26test(S& s, typename S::const_iterator p, typename S::value_type c, S expected)
27{
28 bool sufficient_cap = s.size() < s.capacity();
29 typename S::difference_type pos = p - s.begin();
30 typename S::iterator i = s.insert(p, c);
Dan Albert1d4a1ed2016-05-25 22:36:09 -070031 assert(s.__invariants());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000032 assert(s == expected);
33 assert(i - s.begin() == pos);
34 assert(*i == c);
35 if (sufficient_cap)
36 assert(i == p);
37}
38
39int main()
40{
Howard Hinnant9dcdcde2013-06-28 16:59:19 +000041 {
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000042 typedef std::string S;
43 S s;
44 test(s, s.begin(), '1', S("1"));
45 test(s, s.begin(), 'a', S("a1"));
46 test(s, s.end(), 'b', S("a1b"));
47 test(s, s.end()-1, 'c', S("a1cb"));
48 test(s, s.end()-2, 'd', S("a1dcb"));
49 test(s, s.end()-3, '2', S("a12dcb"));
50 test(s, s.end()-4, '3', S("a132dcb"));
51 test(s, s.end()-5, '4', S("a1432dcb"));
52 test(s, s.begin()+1, '5', S("a51432dcb"));
53 test(s, s.begin()+2, '6', S("a561432dcb"));
54 test(s, s.begin()+3, '7', S("a5671432dcb"));
55 test(s, s.begin()+4, 'A', S("a567A1432dcb"));
56 test(s, s.begin()+5, 'B', S("a567AB1432dcb"));
57 test(s, s.begin()+6, 'C', S("a567ABC1432dcb"));
Howard Hinnant9dcdcde2013-06-28 16:59:19 +000058 }
Dan Albert1d4a1ed2016-05-25 22:36:09 -070059#if __cplusplus >= 201103L
Howard Hinnant9dcdcde2013-06-28 16:59:19 +000060 {
61 typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
62 S s;
63 test(s, s.begin(), '1', S("1"));
64 test(s, s.begin(), 'a', S("a1"));
65 test(s, s.end(), 'b', S("a1b"));
66 test(s, s.end()-1, 'c', S("a1cb"));
67 test(s, s.end()-2, 'd', S("a1dcb"));
68 test(s, s.end()-3, '2', S("a12dcb"));
69 test(s, s.end()-4, '3', S("a132dcb"));
70 test(s, s.end()-5, '4', S("a1432dcb"));
71 test(s, s.begin()+1, '5', S("a51432dcb"));
72 test(s, s.begin()+2, '6', S("a561432dcb"));
73 test(s, s.begin()+3, '7', S("a5671432dcb"));
74 test(s, s.begin()+4, 'A', S("a567A1432dcb"));
75 test(s, s.begin()+5, 'B', S("a567AB1432dcb"));
76 test(s, s.begin()+6, 'C', S("a567ABC1432dcb"));
77 }
78#endif
Dan Albert1d4a1ed2016-05-25 22:36:09 -070079#if _LIBCPP_DEBUG >= 1
80 {
81 typedef std::string S;
82 S s;
83 S s2;
84 s.insert(s2.begin(), '1');
85 assert(false);
86 }
87#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000088}