Home | History | Annotate | Download | only in allocator.members
      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 // allocator:
     13 // template <class... Args> void construct(pointer p, Args&&... args);
     14 
     15 #include <memory>
     16 #include <cassert>
     17 
     18 #include "test_macros.h"
     19 #include "count_new.hpp"
     20 
     21 int A_constructed = 0;
     22 
     23 struct A
     24 {
     25     int data;
     26     A() {++A_constructed;}
     27 
     28     A(const A&) {++A_constructed;}
     29 
     30     explicit A(int) {++A_constructed;}
     31     A(int, int*) {++A_constructed;}
     32 
     33     ~A() {--A_constructed;}
     34 };
     35 
     36 int move_only_constructed = 0;
     37 
     38 #if TEST_STD_VER >= 11
     39 class move_only
     40 {
     41     move_only(const move_only&) = delete;
     42     move_only& operator=(const move_only&)= delete;
     43 
     44 public:
     45     move_only(move_only&&) {++move_only_constructed;}
     46     move_only& operator=(move_only&&) {return *this;}
     47 
     48     move_only() {++move_only_constructed;}
     49     ~move_only() {--move_only_constructed;}
     50 
     51 public:
     52     int data; // unused other than to make sizeof(move_only) == sizeof(int).
     53               // but public to suppress "-Wunused-private-field"
     54 };
     55 #endif // TEST_STD_VER >= 11
     56 
     57 int main()
     58 {
     59     {
     60     std::allocator<A> a;
     61     assert(globalMemCounter.checkOutstandingNewEq(0));
     62     assert(A_constructed == 0);
     63 
     64     globalMemCounter.last_new_size = 0;
     65     A* ap = a.allocate(3);
     66     assert(globalMemCounter.checkOutstandingNewEq(1));
     67     assert(globalMemCounter.checkLastNewSizeEq(3 * sizeof(int)));
     68     assert(A_constructed == 0);
     69 
     70     a.construct(ap);
     71     assert(globalMemCounter.checkOutstandingNewEq(1));
     72     assert(A_constructed == 1);
     73 
     74     a.destroy(ap);
     75     assert(globalMemCounter.checkOutstandingNewEq(1));
     76     assert(A_constructed == 0);
     77 
     78     a.construct(ap, A());
     79     assert(globalMemCounter.checkOutstandingNewEq(1));
     80     assert(A_constructed == 1);
     81 
     82     a.destroy(ap);
     83     assert(globalMemCounter.checkOutstandingNewEq(1));
     84     assert(A_constructed == 0);
     85 
     86     a.construct(ap, 5);
     87     assert(globalMemCounter.checkOutstandingNewEq(1));
     88     assert(A_constructed == 1);
     89 
     90     a.destroy(ap);
     91     assert(globalMemCounter.checkOutstandingNewEq(1));
     92     assert(A_constructed == 0);
     93 
     94     a.construct(ap, 5, (int*)0);
     95     assert(globalMemCounter.checkOutstandingNewEq(1));
     96     assert(A_constructed == 1);
     97 
     98     a.destroy(ap);
     99     assert(globalMemCounter.checkOutstandingNewEq(1));
    100     assert(A_constructed == 0);
    101 
    102     a.deallocate(ap, 3);
    103     assert(globalMemCounter.checkOutstandingNewEq(0));
    104     assert(A_constructed == 0);
    105     }
    106 #if TEST_STD_VER >= 11
    107     {
    108     std::allocator<move_only> a;
    109     assert(globalMemCounter.checkOutstandingNewEq(0));
    110     assert(move_only_constructed == 0);
    111 
    112     globalMemCounter.last_new_size = 0;
    113     move_only* ap = a.allocate(3);
    114     assert(globalMemCounter.checkOutstandingNewEq(1));
    115     assert(globalMemCounter.checkLastNewSizeEq(3 * sizeof(int)));
    116     assert(move_only_constructed == 0);
    117 
    118     a.construct(ap);
    119     assert(globalMemCounter.checkOutstandingNewEq(1));
    120     assert(move_only_constructed == 1);
    121 
    122     a.destroy(ap);
    123     assert(globalMemCounter.checkOutstandingNewEq(1));
    124     assert(move_only_constructed == 0);
    125 
    126     a.construct(ap, move_only());
    127     assert(globalMemCounter.checkOutstandingNewEq(1));
    128     assert(move_only_constructed == 1);
    129 
    130     a.destroy(ap);
    131     assert(globalMemCounter.checkOutstandingNewEq(1));
    132     assert(move_only_constructed == 0);
    133 
    134     a.deallocate(ap, 3);
    135     assert(globalMemCounter.checkOutstandingNewEq(0));
    136     assert(move_only_constructed == 0);
    137     }
    138 #endif
    139 }
    140