libcxx

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

allocate_shared_cxx03.pass.cpp (2588B)


      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 // <memory>
     11 
     12 // shared_ptr
     13 
     14 // template<class T, class A, class... Args>
     15 //    shared_ptr<T> allocate_shared(const A& a, Args&&... args);
     16 
     17 
     18 #include <memory>
     19 #include <new>
     20 #include <cstdlib>
     21 #include <cassert>
     22 #include "test_allocator.h"
     23 #include "min_allocator.h"
     24 
     25 struct Zero
     26 {
     27     static int count;
     28     Zero() {++count;}
     29     Zero(Zero const &) {++count;}
     30     ~Zero() {--count;}
     31 };
     32 
     33 int Zero::count = 0;
     34 
     35 struct One
     36 {
     37     static int count;
     38     int value;
     39     explicit One(int v) : value(v) {++count;}
     40     One(One const & o) : value(o.value) {++count;}
     41     ~One() {--count;}
     42 };
     43 
     44 int One::count = 0;
     45 
     46 
     47 struct Two
     48 {
     49     static int count;
     50     int value;
     51     Two(int v, int) : value(v) {++count;}
     52     Two(Two const & o) : value(o.value) {++count;}
     53     ~Two() {--count;}
     54 };
     55 
     56 int Two::count = 0;
     57 
     58 struct Three
     59 {
     60     static int count;
     61     int value;
     62     Three(int v, int, int) : value(v) {++count;}
     63     Three(Three const & o) : value(o.value) {++count;}
     64     ~Three() {--count;}
     65 };
     66 
     67 int Three::count = 0;
     68 
     69 template <class Alloc>
     70 void test()
     71 {
     72     int const bad = -1;
     73     {
     74     std::shared_ptr<Zero> p = std::allocate_shared<Zero>(Alloc());
     75     assert(Zero::count == 1);
     76     }
     77     assert(Zero::count == 0);
     78     {
     79     int const i = 42;
     80     std::shared_ptr<One> p = std::allocate_shared<One>(Alloc(), i);
     81     assert(One::count == 1);
     82     assert(p->value == i);
     83     }
     84     assert(One::count == 0);
     85     {
     86     int const i = 42;
     87     std::shared_ptr<Two> p = std::allocate_shared<Two>(Alloc(), i, bad);
     88     assert(Two::count == 1);
     89     assert(p->value == i);
     90     }
     91     assert(Two::count == 0);
     92     {
     93     int const i = 42;
     94     std::shared_ptr<Three> p = std::allocate_shared<Three>(Alloc(), i, bad, bad);
     95     assert(Three::count == 1);
     96     assert(p->value == i);
     97     }
     98     assert(Three::count == 0);
     99 }
    100 
    101 int main()
    102 {
    103     {
    104     int i = 67;
    105     int const bad = -1;
    106     std::shared_ptr<Two> p = std::allocate_shared<Two>(test_allocator<Two>(54), i, bad);
    107     assert(test_allocator<Two>::alloc_count == 1);
    108     assert(Two::count == 1);
    109     assert(p->value == 67);
    110     }
    111     assert(Two::count == 0);
    112     assert(test_allocator<Two>::alloc_count == 0);
    113 
    114     test<bare_allocator<void> >();
    115 #if TEST_STD_VER >= 11
    116     test<min_allocator<void> >();
    117 #endif
    118 }