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;
AA26     A() {++A_constructed;}
27 
AA28     A(const A&) {++A_constructed;}
29 
AA30     explicit A(int) {++A_constructed;}
AA31     A(int, int*) {++A_constructed;}
32 
~AA33     ~A() {--A_constructed;}
34 };
35 
36 int move_only_constructed = 0;
37 
38 #if TEST_STD_VER >= 11
39 class move_only
40 {
41     int data;
42 
43     move_only(const move_only&) = delete;
44     move_only& operator=(const move_only&)= delete;
45 
46 public:
move_only(move_only &&)47     move_only(move_only&&) {++move_only_constructed;}
operator =(move_only &&)48     move_only& operator=(move_only&&) {return *this;}
49 
move_only()50     move_only() {++move_only_constructed;}
~move_only()51     ~move_only() {--move_only_constructed;}
52 };
53 #endif // TEST_STD_VER >= 11
54 
main()55 int main()
56 {
57     {
58     std::allocator<A> a;
59     assert(globalMemCounter.checkOutstandingNewEq(0));
60     assert(A_constructed == 0);
61 
62     globalMemCounter.last_new_size = 0;
63     A* ap = a.allocate(3);
64     assert(globalMemCounter.checkOutstandingNewEq(1));
65     assert(globalMemCounter.checkLastNewSizeEq(3 * sizeof(int)));
66     assert(A_constructed == 0);
67 
68     a.construct(ap);
69     assert(globalMemCounter.checkOutstandingNewEq(1));
70     assert(A_constructed == 1);
71 
72     a.destroy(ap);
73     assert(globalMemCounter.checkOutstandingNewEq(1));
74     assert(A_constructed == 0);
75 
76     a.construct(ap, A());
77     assert(globalMemCounter.checkOutstandingNewEq(1));
78     assert(A_constructed == 1);
79 
80     a.destroy(ap);
81     assert(globalMemCounter.checkOutstandingNewEq(1));
82     assert(A_constructed == 0);
83 
84     a.construct(ap, 5);
85     assert(globalMemCounter.checkOutstandingNewEq(1));
86     assert(A_constructed == 1);
87 
88     a.destroy(ap);
89     assert(globalMemCounter.checkOutstandingNewEq(1));
90     assert(A_constructed == 0);
91 
92     a.construct(ap, 5, (int*)0);
93     assert(globalMemCounter.checkOutstandingNewEq(1));
94     assert(A_constructed == 1);
95 
96     a.destroy(ap);
97     assert(globalMemCounter.checkOutstandingNewEq(1));
98     assert(A_constructed == 0);
99 
100     a.deallocate(ap, 3);
101     assert(globalMemCounter.checkOutstandingNewEq(0));
102     assert(A_constructed == 0);
103     }
104 #if TEST_STD_VER >= 11
105     {
106     std::allocator<move_only> a;
107     assert(globalMemCounter.checkOutstandingNewEq(0));
108     assert(move_only_constructed == 0);
109 
110     globalMemCounter.last_new_size = 0;
111     move_only* ap = a.allocate(3);
112     assert(globalMemCounter.checkOutstandingNewEq(1));
113     assert(globalMemCounter.checkLastNewSizeEq(3 * sizeof(int)));
114     assert(move_only_constructed == 0);
115 
116     a.construct(ap);
117     assert(globalMemCounter.checkOutstandingNewEq(1));
118     assert(move_only_constructed == 1);
119 
120     a.destroy(ap);
121     assert(globalMemCounter.checkOutstandingNewEq(1));
122     assert(move_only_constructed == 0);
123 
124     a.construct(ap, move_only());
125     assert(globalMemCounter.checkOutstandingNewEq(1));
126     assert(move_only_constructed == 1);
127 
128     a.destroy(ap);
129     assert(globalMemCounter.checkOutstandingNewEq(1));
130     assert(move_only_constructed == 0);
131 
132     a.deallocate(ap, 3);
133     assert(globalMemCounter.checkOutstandingNewEq(0));
134     assert(move_only_constructed == 0);
135     }
136 #endif
137 }
138