blob: 664cb80b10fe63458a71821609af3df1b5d5cbb5 [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
Howard Hinnant6e0a1f42010-08-22 00:47:54 +000012// template<class InputIterator>
13// basic_string(InputIterator begin, InputIterator end,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000014// const Allocator& a = Allocator());
15
16#include <string>
17#include <iterator>
18#include <cassert>
19
Marshall Clow1b921882013-12-03 00:18:10 +000020#include "test_allocator.h"
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000021#include "../input_iterator.h"
Marshall Clow061d0cc2013-11-26 20:58:02 +000022#include "min_allocator.h"
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000023
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);
Dan Albert1d4a1ed2016-05-25 22:36:09 -070033 assert(s2.__invariants());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000034 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
Howard Hinnant9dcdcde2013-06-28 16:59:19 +000042template <class It, class A>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000043void
Howard Hinnant9dcdcde2013-06-28 16:59:19 +000044test(It first, It last, const A& a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000045{
46 typedef typename std::iterator_traits<It>::value_type charT;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +000047 typedef std::basic_string<charT, std::char_traits<charT>, A> S;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000048 typedef typename S::traits_type T;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000049 S s2(first, last, a);
Dan Albert1d4a1ed2016-05-25 22:36:09 -070050 assert(s2.__invariants());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000051 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{
Howard Hinnant9dcdcde2013-06-28 16:59:19 +000061 {
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000062 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));
Howard Hinnant9dcdcde2013-06-28 16:59:19 +000088 }
Dan Albert1d4a1ed2016-05-25 22:36:09 -070089#if __cplusplus >= 201103L
Howard Hinnant9dcdcde2013-06-28 16:59:19 +000090 {
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
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000119}