blob: c902531aca45485d7adae863a87dda5d54915817 [file] [log] [blame]
Howard Hinnant3e519522010-05-11 19:42:16 +00001//===----------------------------------------------------------------------===//
2//
Howard Hinnant5b08a8a2010-05-11 21:36:01 +00003// The LLVM Compiler Infrastructure
Howard Hinnant3e519522010-05-11 19:42:16 +00004//
Howard Hinnant412dbeb2010-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 Hinnant3e519522010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10// <string>
11
Howard Hinnantbf2897c2010-08-22 00:47:54 +000012// template<class InputIterator>
13// basic_string(InputIterator begin, InputIterator end,
Howard Hinnant3e519522010-05-11 19:42:16 +000014// 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
23template <class It>
24void
25test(It first, It last)
26{
27 typedef typename std::iterator_traits<It>::value_type charT;
28 typedef std::basic_string<charT, std::char_traits<charT>, test_allocator<charT> > S;
29 typedef typename S::traits_type T;
30 typedef typename S::allocator_type A;
31 S s2(first, last);
32 assert(s2.__invariants());
33 assert(s2.size() == std::distance(first, last));
34 unsigned i = 0;
35 for (It it = first; it != last; ++it, ++i)
36 assert(s2[i] == *it);
37 assert(s2.get_allocator() == A());
38 assert(s2.capacity() >= s2.size());
39}
40
41template <class It>
42void
43test(It first, It last, const test_allocator<typename std::iterator_traits<It>::value_type>& a)
44{
45 typedef typename std::iterator_traits<It>::value_type charT;
46 typedef std::basic_string<charT, std::char_traits<charT>, test_allocator<charT> > S;
47 typedef typename S::traits_type T;
48 typedef typename S::allocator_type A;
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 typedef test_allocator<char> A;
62 const char* s = "12345678901234567890123456789012345678901234567890";
63
64 test(s, s);
65 test(s, s, A(2));
66
67 test(s, s+1);
68 test(s, s+1, A(2));
69
70 test(s, s+10);
71 test(s, s+10, A(2));
72
73 test(s, s+50);
74 test(s, s+50, A(2));
75
76 test(input_iterator<const char*>(s), input_iterator<const char*>(s));
77 test(input_iterator<const char*>(s), input_iterator<const char*>(s), A(2));
78
79 test(input_iterator<const char*>(s), input_iterator<const char*>(s+1));
80 test(input_iterator<const char*>(s), input_iterator<const char*>(s+1), A(2));
81
82 test(input_iterator<const char*>(s), input_iterator<const char*>(s+10));
83 test(input_iterator<const char*>(s), input_iterator<const char*>(s+10), A(2));
84
85 test(input_iterator<const char*>(s), input_iterator<const char*>(s+50));
86 test(input_iterator<const char*>(s), input_iterator<const char*>(s+50), A(2));
87}