[VPlan] Perform DT expensive input DT verification earlier (NFC).
[llvm-project.git] / libcxx / test / std / utilities / memory / util.smartptr / util.smartptr.shared / util.smartptr.shared.create / make_shared.pass.cpp
blobbe7505f218cdd0c66cdf342c675bd12b1713e042
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... Args>
14 // shared_ptr<T> make_shared(Args&&... args); // T is not an array
16 #include <memory>
17 #include <cassert>
19 #include "count_new.h"
20 #include "operator_hijacker.h"
21 #include "test_macros.h"
23 struct A
25 static int count;
27 A(int i, char c) : int_(i), char_(c) {++count;}
28 A(const A& a)
29 : int_(a.int_), char_(a.char_)
30 {++count;}
31 ~A() {--count;}
33 int get_int() const {return int_;}
34 char get_char() const {return char_;}
36 A* operator& () = delete;
38 private:
39 int int_;
40 char char_;
43 int A::count = 0;
46 struct Foo
48 Foo() = default;
49 virtual ~Foo() = default;
52 #ifdef _LIBCPP_VERSION
53 struct Result {};
54 static Result theFunction() { return Result(); }
55 static int resultDeletorCount;
56 static void resultDeletor(Result (*pf)()) {
57 assert(pf == theFunction);
58 ++resultDeletorCount;
61 void test_pointer_to_function() {
62 { // https://llvm.org/PR27566
63 std::shared_ptr<Result()> x(&theFunction, &resultDeletor);
64 std::shared_ptr<Result()> y(theFunction, resultDeletor);
66 assert(resultDeletorCount == 2);
68 #else // _LIBCPP_VERSION
69 void test_pointer_to_function() {}
70 #endif // _LIBCPP_VERSION
72 template <typename T>
73 void test(const T &t0)
76 T t1 = t0;
77 std::shared_ptr<T> p0 = std::make_shared<T>(t0);
78 std::shared_ptr<T> p1 = std::make_shared<T>(t1);
79 assert(*p0 == t0);
80 assert(*p1 == t1);
84 const T t1 = t0;
85 std::shared_ptr<const T> p0 = std::make_shared<const T>(t0);
86 std::shared_ptr<const T> p1 = std::make_shared<const T>(t1);
87 assert(*p0 == t0);
88 assert(*p1 == t1);
92 int main(int, char**)
94 int nc = globalMemCounter.outstanding_new;
96 int i = 67;
97 char c = 'e';
98 std::shared_ptr<A> p = std::make_shared<A>(i, c);
99 assert(globalMemCounter.checkOutstandingNewLessThanOrEqual(nc+1));
100 assert(A::count == 1);
101 assert(p->get_int() == 67);
102 assert(p->get_char() == 'e');
105 { // https://llvm.org/PR24137
106 std::shared_ptr<Foo> p1 = std::make_shared<Foo>();
107 assert(p1.get());
108 std::shared_ptr<const Foo> p2 = std::make_shared<const Foo>();
109 assert(p2.get());
112 test_pointer_to_function();
114 #if TEST_STD_VER >= 11
115 nc = globalMemCounter.outstanding_new;
117 char c = 'e';
118 std::shared_ptr<A> p = std::make_shared<A>(67, c);
119 assert(globalMemCounter.checkOutstandingNewLessThanOrEqual(nc+1));
120 assert(A::count == 1);
121 assert(p->get_int() == 67);
122 assert(p->get_char() == 'e');
124 #endif
125 assert(A::count == 0);
127 // Make sure std::make_shared handles badly-behaved types properly
129 std::shared_ptr<operator_hijacker> p1 = std::make_shared<operator_hijacker>();
130 std::shared_ptr<operator_hijacker> p2 = std::make_shared<operator_hijacker>(operator_hijacker());
131 assert(p1 != nullptr);
132 assert(p2 != nullptr);
135 test<bool>(true);
136 test<int>(3);
137 test<double>(5.0);
139 return 0;