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
20#include "test_allocator.h"
21#include "../input_iterator.h"
22#include "min_allocator.h"
23
24template <class It>
25void
26test(It first, It last)
27{
28    typedef typename std::iterator_traits<It>::value_type charT;
29    typedef std::basic_string<charT, std::char_traits<charT>, test_allocator<charT> > S;
30    typedef typename S::traits_type T;
31    typedef typename S::allocator_type A;
32    S s2(first, last);
33    assert(s2.__invariants());
34    assert(s2.size() == std::distance(first, last));
35    unsigned i = 0;
36    for (It it = first; it != last; ++it, ++i)
37        assert(s2[i] == *it);
38    assert(s2.get_allocator() == A());
39    assert(s2.capacity() >= s2.size());
40}
41
42template <class It, class A>
43void
44test(It first, It last, const A& a)
45{
46    typedef typename std::iterator_traits<It>::value_type charT;
47    typedef std::basic_string<charT, std::char_traits<charT>, A> S;
48    typedef typename S::traits_type T;
49    S s2(first, last, a);
50    assert(s2.__invariants());
51    assert(s2.size() == 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
59int 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 __cplusplus >= 201103L
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