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 // basic_string(size_type n, charT c, const Allocator& a = Allocator());
     13 
     14 #include <string>
     15 #include <stdexcept>
     16 #include <algorithm>
     17 #include <cassert>
     18 
     19 #include "test_allocator.h"
     20 #include "min_allocator.h"
     21 
     22 template <class charT>
     23 void
     24 test(unsigned n, charT c)
     25 {
     26     typedef std::basic_string<charT, std::char_traits<charT>, test_allocator<charT> > S;
     27     typedef typename S::traits_type T;
     28     typedef typename S::allocator_type A;
     29     S s2(n, c);
     30     assert(s2.__invariants());
     31     assert(s2.size() == n);
     32     for (unsigned i = 0; i < n; ++i)
     33         assert(s2[i] == c);
     34     assert(s2.get_allocator() == A());
     35     assert(s2.capacity() >= s2.size());
     36 }
     37 
     38 template <class charT, class A>
     39 void
     40 test(unsigned n, charT c, const A& a)
     41 {
     42     typedef std::basic_string<charT, std::char_traits<charT>, A> S;
     43     typedef typename S::traits_type T;
     44     S s2(n, c, a);
     45     assert(s2.__invariants());
     46     assert(s2.size() == n);
     47     for (unsigned i = 0; i < n; ++i)
     48         assert(s2[i] == c);
     49     assert(s2.get_allocator() == a);
     50     assert(s2.capacity() >= s2.size());
     51 }
     52 
     53 template <class Tp>
     54 void
     55 test(Tp n, Tp c)
     56 {
     57     typedef char charT;
     58     typedef std::basic_string<charT, std::char_traits<charT>, test_allocator<charT> > S;
     59     typedef typename S::traits_type T;
     60     typedef typename S::allocator_type A;
     61     S s2(n, c);
     62     assert(s2.__invariants());
     63     assert(s2.size() == n);
     64     for (unsigned i = 0; i < n; ++i)
     65         assert(s2[i] == c);
     66     assert(s2.get_allocator() == A());
     67     assert(s2.capacity() >= s2.size());
     68 }
     69 
     70 template <class Tp, class A>
     71 void
     72 test(Tp n, Tp c, const A& a)
     73 {
     74     typedef char charT;
     75     typedef std::basic_string<charT, std::char_traits<charT>, A> S;
     76     typedef typename S::traits_type T;
     77     S s2(n, c, a);
     78     assert(s2.__invariants());
     79     assert(s2.size() == n);
     80     for (unsigned i = 0; i < n; ++i)
     81         assert(s2[i] == c);
     82     assert(s2.get_allocator() == a);
     83     assert(s2.capacity() >= s2.size());
     84 }
     85 
     86 int main()
     87 {
     88     {
     89     typedef test_allocator<char> A;
     90     typedef std::basic_string<char, std::char_traits<char>, A> S;
     91 
     92     test(0, 'a');
     93     test(0, 'a', A(2));
     94 
     95     test(1, 'a');
     96     test(1, 'a', A(2));
     97 
     98     test(10, 'a');
     99     test(10, 'a', A(2));
    100 
    101     test(100, 'a');
    102     test(100, 'a', A(2));
    103 
    104     test(100, 65);
    105     test(100, 65, A(3));
    106     }
    107 #if __cplusplus >= 201103L
    108     {
    109     typedef min_allocator<char> A;
    110     typedef std::basic_string<char, std::char_traits<char>, A> S;
    111 
    112     test(0, 'a');
    113     test(0, 'a', A());
    114 
    115     test(1, 'a');
    116     test(1, 'a', A());
    117 
    118     test(10, 'a');
    119     test(10, 'a', A());
    120 
    121     test(100, 'a');
    122     test(100, 'a', A());
    123 
    124     test(100, 65);
    125     test(100, 65, A());
    126     }
    127 #endif
    128 }
    129