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