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 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::traits_type T; 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 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 typedef typename S::traits_type T; 51 S s2(first, last, a); 52 LIBCPP_ASSERT(s2.__invariants()); 53 assert(s2.size() == static_cast<std::size_t>(std::distance(first, last))); 54 unsigned i = 0; 55 for (It it = first; it != last; ++it, ++i) 56 assert(s2[i] == *it); 57 assert(s2.get_allocator() == a); 58 assert(s2.capacity() >= s2.size()); 59 } 60 61 int main() 62 { 63 { 64 typedef test_allocator<char> A; 65 const char* s = "12345678901234567890123456789012345678901234567890"; 66 67 test(s, s); 68 test(s, s, A(2)); 69 70 test(s, s+1); 71 test(s, s+1, A(2)); 72 73 test(s, s+10); 74 test(s, s+10, A(2)); 75 76 test(s, s+50); 77 test(s, s+50, A(2)); 78 79 test(input_iterator<const char*>(s), input_iterator<const char*>(s)); 80 test(input_iterator<const char*>(s), input_iterator<const char*>(s), A(2)); 81 82 test(input_iterator<const char*>(s), input_iterator<const char*>(s+1)); 83 test(input_iterator<const char*>(s), input_iterator<const char*>(s+1), A(2)); 84 85 test(input_iterator<const char*>(s), input_iterator<const char*>(s+10)); 86 test(input_iterator<const char*>(s), input_iterator<const char*>(s+10), A(2)); 87 88 test(input_iterator<const char*>(s), input_iterator<const char*>(s+50)); 89 test(input_iterator<const char*>(s), input_iterator<const char*>(s+50), A(2)); 90 } 91 #if TEST_STD_VER >= 11 92 { 93 typedef min_allocator<char> A; 94 const char* s = "12345678901234567890123456789012345678901234567890"; 95 96 test(s, s); 97 test(s, s, A()); 98 99 test(s, s+1); 100 test(s, s+1, A()); 101 102 test(s, s+10); 103 test(s, s+10, A()); 104 105 test(s, s+50); 106 test(s, s+50, A()); 107 108 test(input_iterator<const char*>(s), input_iterator<const char*>(s)); 109 test(input_iterator<const char*>(s), input_iterator<const char*>(s), A()); 110 111 test(input_iterator<const char*>(s), input_iterator<const char*>(s+1)); 112 test(input_iterator<const char*>(s), input_iterator<const char*>(s+1), A()); 113 114 test(input_iterator<const char*>(s), input_iterator<const char*>(s+10)); 115 test(input_iterator<const char*>(s), input_iterator<const char*>(s+10), A()); 116 117 test(input_iterator<const char*>(s), input_iterator<const char*>(s+50)); 118 test(input_iterator<const char*>(s), input_iterator<const char*>(s+50), A()); 119 } 120 #endif 121 } 122