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 // <string>
11 
12 // template<class InputIterator>
13 //   basic_string(InputIterator begin, InputIterator end,
14 //   const Allocator& a = Allocator());
15 
16 
17 #include <string>
18 #include <iterator>
19 #include <cassert>
20 #include <cstddef>
21 
22 #include "test_macros.h"
23 #include "test_allocator.h"
24 #include "../input_iterator.h"
25 #include "min_allocator.h"
26 
27 template <class It>
28 void
test(It first,It last)29 test(It first, It last)
30 {
31     typedef typename std::iterator_traits<It>::value_type charT;
32     typedef std::basic_string<charT, std::char_traits<charT>, test_allocator<charT> > S;
33     typedef typename S::allocator_type A;
34     S s2(first, last);
35     LIBCPP_ASSERT(s2.__invariants());
36     assert(s2.size() == static_cast<std::size_t>(std::distance(first, last)));
37     unsigned i = 0;
38     for (It it = first; it != last; ++it, ++i)
39         assert(s2[i] == *it);
40     assert(s2.get_allocator() == A());
41     assert(s2.capacity() >= s2.size());
42 }
43 
44 template <class It, class A>
45 void
test(It first,It last,const A & a)46 test(It first, It last, const A& a)
47 {
48     typedef typename std::iterator_traits<It>::value_type charT;
49     typedef std::basic_string<charT, std::char_traits<charT>, A> S;
50     S s2(first, last, a);
51     LIBCPP_ASSERT(s2.__invariants());
52     assert(s2.size() == static_cast<std::size_t>(std::distance(first, last)));
53     unsigned i = 0;
54     for (It it = first; it != last; ++it, ++i)
55         assert(s2[i] == *it);
56     assert(s2.get_allocator() == a);
57     assert(s2.capacity() >= s2.size());
58 }
59 
main()60 int main()
61 {
62     {
63     typedef test_allocator<char> A;
64     const char* s = "12345678901234567890123456789012345678901234567890";
65 
66     test(s, s);
67     test(s, s, A(2));
68 
69     test(s, s+1);
70     test(s, s+1, A(2));
71 
72     test(s, s+10);
73     test(s, s+10, A(2));
74 
75     test(s, s+50);
76     test(s, s+50, A(2));
77 
78     test(input_iterator<const char*>(s), input_iterator<const char*>(s));
79     test(input_iterator<const char*>(s), input_iterator<const char*>(s), A(2));
80 
81     test(input_iterator<const char*>(s), input_iterator<const char*>(s+1));
82     test(input_iterator<const char*>(s), input_iterator<const char*>(s+1), A(2));
83 
84     test(input_iterator<const char*>(s), input_iterator<const char*>(s+10));
85     test(input_iterator<const char*>(s), input_iterator<const char*>(s+10), A(2));
86 
87     test(input_iterator<const char*>(s), input_iterator<const char*>(s+50));
88     test(input_iterator<const char*>(s), input_iterator<const char*>(s+50), A(2));
89     }
90 #if TEST_STD_VER >= 11
91     {
92     typedef min_allocator<char> A;
93     const char* s = "12345678901234567890123456789012345678901234567890";
94 
95     test(s, s);
96     test(s, s, A());
97 
98     test(s, s+1);
99     test(s, s+1, A());
100 
101     test(s, s+10);
102     test(s, s+10, A());
103 
104     test(s, s+50);
105     test(s, s+50, A());
106 
107     test(input_iterator<const char*>(s), input_iterator<const char*>(s));
108     test(input_iterator<const char*>(s), input_iterator<const char*>(s), A());
109 
110     test(input_iterator<const char*>(s), input_iterator<const char*>(s+1));
111     test(input_iterator<const char*>(s), input_iterator<const char*>(s+1), A());
112 
113     test(input_iterator<const char*>(s), input_iterator<const char*>(s+10));
114     test(input_iterator<const char*>(s), input_iterator<const char*>(s+10), A());
115 
116     test(input_iterator<const char*>(s), input_iterator<const char*>(s+50));
117     test(input_iterator<const char*>(s), input_iterator<const char*>(s+50), A());
118     }
119 #endif
120 }
121