Home | History | Annotate | Download | only in string.cons
      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 
     23 template <class It>
     24 void
     25 test(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 
     41 template <class It>
     42 void
     43 test(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 
     59 int 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 }
     88