blob: 8687fa8bbba59d2787b05d6eeb5940ddf354dd8e [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001//===----------------------------------------------------------------------===//
2//
3// ÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊThe LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10// <string>
11
12// basic_string(const basic_string<charT,traits,Allocator>& str,
13// size_type pos, size_type n = npos,
14// const Allocator& a = Allocator());
15
16#include <string>
17#include <stdexcept>
18#include <algorithm>
19#include <cassert>
20
21#include "../test_allocator.h"
22
23template <class S>
24void
25test(S str, unsigned pos)
26{
27 typedef typename S::traits_type T;
28 typedef typename S::allocator_type A;
29 try
30 {
31 S s2(str, pos);
32 assert(s2.__invariants());
33 assert(pos <= str.size());
34 unsigned rlen = str.size() - pos;
35 assert(s2.size() == rlen);
36 assert(T::compare(s2.data(), str.data() + pos, rlen) == 0);
37 assert(s2.get_allocator() == A());
38 assert(s2.capacity() >= s2.size());
39 }
40 catch (std::out_of_range&)
41 {
42 assert(pos > str.size());
43 }
44}
45
46template <class S>
47void
48test(S str, unsigned pos, unsigned n)
49{
50 typedef typename S::traits_type T;
51 typedef typename S::allocator_type A;
52 try
53 {
54 S s2(str, pos, n);
55 assert(s2.__invariants());
56 assert(pos <= str.size());
57 unsigned rlen = std::min(str.size() - pos, n);
58 assert(s2.size() == rlen);
59 assert(T::compare(s2.data(), str.data() + pos, rlen) == 0);
60 assert(s2.get_allocator() == A());
61 assert(s2.capacity() >= s2.size());
62 }
63 catch (std::out_of_range&)
64 {
65 assert(pos > str.size());
66 }
67}
68
69template <class S>
70void
71test(S str, unsigned pos, unsigned n, const typename S::allocator_type& a)
72{
73 typedef typename S::traits_type T;
74 typedef typename S::allocator_type A;
75 try
76 {
77 S s2(str, pos, n, a);
78 assert(s2.__invariants());
79 assert(pos <= str.size());
80 unsigned rlen = std::min(str.size() - pos, n);
81 assert(s2.size() == rlen);
82 assert(T::compare(s2.data(), str.data() + pos, rlen) == 0);
83 assert(s2.get_allocator() == a);
84 assert(s2.capacity() >= s2.size());
85 }
86 catch (std::out_of_range&)
87 {
88 assert(pos > str.size());
89 }
90}
91
92int main()
93{
94 typedef test_allocator<char> A;
95 typedef std::basic_string<char, std::char_traits<char>, A> S;
96
97 test(S(A(3)), 0);
98 test(S(A(3)), 1);
99 test(S("1", A(5)), 0);
100 test(S("1", A(5)), 1);
101 test(S("1", A(5)), 2);
102 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 0);
103 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 5);
104 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50);
105 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 500);
106
107 test(S(A(3)), 0, 0);
108 test(S(A(3)), 0, 1);
109 test(S(A(3)), 1, 0);
110 test(S(A(3)), 1, 1);
111 test(S(A(3)), 1, 2);
112 test(S("1", A(5)), 0, 0);
113 test(S("1", A(5)), 0, 1);
114 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 0);
115 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 1);
116 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 10);
117 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 100);
118
119 test(S(A(3)), 0, 0, A(4));
120 test(S(A(3)), 0, 1, A(4));
121 test(S(A(3)), 1, 0, A(4));
122 test(S(A(3)), 1, 1, A(4));
123 test(S(A(3)), 1, 2, A(4));
124 test(S("1", A(5)), 0, 0, A(6));
125 test(S("1", A(5)), 0, 1, A(6));
126 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 0, A(8));
127 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 1, A(8));
128 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 10, A(8));
129 test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 100, A(8));
130}