Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / utilities / memory / util.smartptr / util.smartptr.shared / util.smartptr.shared.create / allocate_shared.pass.cpp
blob341a442c90a16748fd6de650b459f6cd887e0c1d
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 // <memory>
11 // shared_ptr
13 // template<class T, class A, class... Args>
14 // shared_ptr<T> allocate_shared(const A& a, Args&&... args); // T is not an array
16 #include <memory>
17 #include <new>
18 #include <cstdlib>
19 #include <cassert>
21 #include "min_allocator.h"
22 #include "operator_hijacker.h"
23 #include "test_allocator.h"
24 #include "test_macros.h"
26 int new_count = 0;
28 struct A
30 static int count;
32 A(int i, char c) : int_(i), char_(c) {++count;}
33 A(const A& a)
34 : int_(a.int_), char_(a.char_)
35 {++count;}
36 ~A() {--count;}
38 int get_int() const {return int_;}
39 char get_char() const {return char_;}
41 A* operator& () = delete;
42 private:
43 int int_;
44 char char_;
47 int A::count = 0;
49 struct Zero
51 static int count;
52 Zero() {++count;}
53 Zero(Zero const &) {++count;}
54 ~Zero() {--count;}
57 int Zero::count = 0;
59 struct One
61 static int count;
62 int value;
63 explicit One(int v) : value(v) {++count;}
64 One(One const & o) : value(o.value) {++count;}
65 ~One() {--count;}
68 int One::count = 0;
71 struct Two
73 static int count;
74 int value;
75 Two(int v, int) : value(v) {++count;}
76 Two(Two const & o) : value(o.value) {++count;}
77 ~Two() {--count;}
80 int Two::count = 0;
82 struct Three
84 static int count;
85 int value;
86 Three(int v, int, int) : value(v) {++count;}
87 Three(Three const & o) : value(o.value) {++count;}
88 ~Three() {--count;}
91 int Three::count = 0;
93 template <class Alloc>
94 void test()
96 int const bad = -1;
98 std::shared_ptr<Zero> p = std::allocate_shared<Zero>(Alloc());
99 assert(Zero::count == 1);
101 assert(Zero::count == 0);
103 int const i = 42;
104 std::shared_ptr<One> p = std::allocate_shared<One>(Alloc(), i);
105 assert(One::count == 1);
106 assert(p->value == i);
108 assert(One::count == 0);
110 int const i = 42;
111 std::shared_ptr<Two> p = std::allocate_shared<Two>(Alloc(), i, bad);
112 assert(Two::count == 1);
113 assert(p->value == i);
115 assert(Two::count == 0);
117 int const i = 42;
118 std::shared_ptr<Three> p = std::allocate_shared<Three>(Alloc(), i, bad, bad);
119 assert(Three::count == 1);
120 assert(p->value == i);
122 assert(Three::count == 0);
125 int main(int, char**)
127 test<bare_allocator<void> >();
128 test<test_allocator<void> >();
130 test_allocator_statistics alloc_stats;
132 int i = 67;
133 char c = 'e';
134 std::shared_ptr<A> p = std::allocate_shared<A>(test_allocator<A>(54, &alloc_stats), i, c);
135 assert(alloc_stats.alloc_count == 1);
136 assert(A::count == 1);
137 assert(p->get_int() == 67);
138 assert(p->get_char() == 'e');
140 assert(A::count == 0);
141 assert(alloc_stats.alloc_count == 0);
143 int i = 67;
144 char c = 'e';
145 std::shared_ptr<A> p = std::allocate_shared<A>(min_allocator<void>(), i, c);
146 assert(A::count == 1);
147 assert(p->get_int() == 67);
148 assert(p->get_char() == 'e');
150 assert(A::count == 0);
152 int i = 68;
153 char c = 'f';
154 std::shared_ptr<A> p = std::allocate_shared<A>(bare_allocator<void>(), i, c);
155 assert(A::count == 1);
156 assert(p->get_int() == 68);
157 assert(p->get_char() == 'f');
159 assert(A::count == 0);
161 // Make sure std::allocate_shared handles badly-behaved types properly
163 std::shared_ptr<operator_hijacker> p1 = std::allocate_shared<operator_hijacker>(min_allocator<operator_hijacker>());
164 std::shared_ptr<operator_hijacker> p2 = std::allocate_shared<operator_hijacker>(min_allocator<operator_hijacker>(), operator_hijacker());
165 assert(p1 != nullptr);
166 assert(p2 != nullptr);
169 return 0;