libcxx

libcxx mirror with random patches
git clone https://git.neptards.moe/neptards/libcxx.git
Log | Files | Refs

size_char_alloc.pass.cpp (3005B)


      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 #include <cstddef>
     19 
     20 #include "test_macros.h"
     21 #include "test_allocator.h"
     22 #include "min_allocator.h"
     23 
     24 template <class charT>
     25 void
     26 test(unsigned n, charT c)
     27 {
     28     typedef std::basic_string<charT, std::char_traits<charT>, test_allocator<charT> > S;
     29     typedef typename S::allocator_type A;
     30     S s2(n, c);
     31     LIBCPP_ASSERT(s2.__invariants());
     32     assert(s2.size() == n);
     33     for (unsigned i = 0; i < n; ++i)
     34         assert(s2[i] == c);
     35     assert(s2.get_allocator() == A());
     36     assert(s2.capacity() >= s2.size());
     37 }
     38 
     39 template <class charT, class A>
     40 void
     41 test(unsigned n, charT c, const A& a)
     42 {
     43     typedef std::basic_string<charT, std::char_traits<charT>, A> S;
     44     S s2(n, c, a);
     45     LIBCPP_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::allocator_type A;
     60     S s2(n, c);
     61     LIBCPP_ASSERT(s2.__invariants());
     62     assert(s2.size() == static_cast<std::size_t>(n));
     63     for (int i = 0; i < n; ++i)
     64         assert(s2[i] == c);
     65     assert(s2.get_allocator() == A());
     66     assert(s2.capacity() >= s2.size());
     67 }
     68 
     69 template <class Tp, class A>
     70 void
     71 test(Tp n, Tp c, const A& a)
     72 {
     73     typedef char charT;
     74     typedef std::basic_string<charT, std::char_traits<charT>, A> S;
     75     S s2(n, c, a);
     76     LIBCPP_ASSERT(s2.__invariants());
     77     assert(s2.size() == static_cast<std::size_t>(n));
     78     for (int i = 0; i < n; ++i)
     79         assert(s2[i] == c);
     80     assert(s2.get_allocator() == a);
     81     assert(s2.capacity() >= s2.size());
     82 }
     83 
     84 int main()
     85 {
     86     {
     87     typedef test_allocator<char> A;
     88 
     89     test(0, 'a');
     90     test(0, 'a', A(2));
     91 
     92     test(1, 'a');
     93     test(1, 'a', A(2));
     94 
     95     test(10, 'a');
     96     test(10, 'a', A(2));
     97 
     98     test(100, 'a');
     99     test(100, 'a', A(2));
    100 
    101     test(static_cast<char>(100), static_cast<char>(65));
    102     test(static_cast<char>(100), static_cast<char>(65), A(3));
    103     }
    104 #if TEST_STD_VER >= 11
    105     {
    106     typedef min_allocator<char> A;
    107 
    108     test(0, 'a');
    109     test(0, 'a', A());
    110 
    111     test(1, 'a');
    112     test(1, 'a', A());
    113 
    114     test(10, 'a');
    115     test(10, 'a', A());
    116 
    117     test(100, 'a');
    118     test(100, 'a', A());
    119 
    120     test(static_cast<char>(100), static_cast<char>(65));
    121     test(static_cast<char>(100), static_cast<char>(65), A());
    122     }
    123 #endif
    124 }