7 #include <throwing/unique_ptr.hpp> 12 static int &object_count() {
16 static bool all_objects_deleted() {
return object_count() == 0; }
17 Foo() { ++object_count(); }
18 Foo(
const Foo &) { ++object_count(); }
20 ~Foo() { --object_count(); }
23 template <
int id>
struct Deleter {
24 static int &object_count() {
28 static int &deleter_calls() {
32 Deleter() { ++object_count(); }
33 Deleter(
const Deleter &) { ++object_count(); }
34 Deleter(Deleter &&) {}
35 ~Deleter() { --object_count(); }
37 void operator()(Foo *p)
const {
44 TEST_CASE(
"unique_ptr to array construction from pointer",
45 "[unique_ptr][array][constructor]") {
47 throwing::unique_ptr<
int[]> up(p);
48 REQUIRE(up.get() == p);
51 TEST_CASE(
"unique_ptr to array move constructor",
52 "[unique_ptr][array][constructor]") {
54 throwing::unique_ptr<
int[]> up(p);
55 throwing::unique_ptr<
int[]> up2(std::move(up));
56 REQUIRE(up2.get() == p);
59 TEST_CASE(
"unique_ptr to array move constructor from std::unique_ptr",
60 "[unique_ptr][array][constructor]") {
62 std::unique_ptr<
int[]> up(p);
63 throwing::unique_ptr<
int[]> up2(std::move(up));
64 REQUIRE(up2.get() == p);
67 TEST_CASE(
"unique_ptr to array construction from pointer and non reference " 69 "[unique_ptr][array][constructor]") {
70 REQUIRE(Foo::object_count() == 0);
72 REQUIRE(Deleter<1>::object_count() == 1);
74 REQUIRE(Deleter<2>::object_count() == 1);
76 std::unique_ptr<Foo[], Deleter<1>> foo1(
new Foo[10], d1);
77 REQUIRE(Foo::object_count() == 10);
79 throwing::unique_ptr<Foo[], Deleter<2>> foo2(
new Foo[10], d2);
80 REQUIRE(Foo::object_count() == 20);
81 REQUIRE(Deleter<2>::object_count() == Deleter<1>::object_count());
83 REQUIRE(foo2.get() !=
nullptr);
84 REQUIRE(Deleter<2>::deleter_calls() == 0);
86 REQUIRE(Foo::object_count() == 0);
87 REQUIRE(Deleter<2>::object_count() == Deleter<1>::object_count());
88 REQUIRE(Deleter<2>::deleter_calls() == 1);
91 #if !defined(_MSC_VER
) || _MSC_VER
> 1800
92 TEST_CASE(
"unique_ptr to array construction from pointer and reference deleter",
93 "[unique_ptr][array][constructor]") {
94 REQUIRE(Foo::object_count() == 0);
96 REQUIRE(Deleter<3>::object_count() == 1);
98 REQUIRE(Deleter<4>::object_count() == 1);
100 std::unique_ptr<Foo[], Deleter<3> &> foo1(
new Foo[10], d1);
101 REQUIRE(Foo::object_count() == 10);
102 REQUIRE(Deleter<3>::object_count() == 1);
104 throwing::unique_ptr<Foo[], Deleter<4> &> foo2(
new Foo[10], d2);
105 REQUIRE(Foo::object_count() == 20);
106 REQUIRE(Deleter<4>::object_count() == 1);
108 REQUIRE(foo2.get() !=
nullptr);
109 REQUIRE(Deleter<4>::deleter_calls() == 0);
111 REQUIRE(Deleter<3>::object_count() == 1);
112 REQUIRE(Deleter<4>::object_count() == 1);
113 REQUIRE(Deleter<4>::deleter_calls() == 1);
117 TEST_CASE(
"unique_ptr to array construction from pointer and move reference " 119 "[unique_ptr][array][constructor]") {
120 REQUIRE(Foo::object_count() == 0);
122 REQUIRE(Deleter<5>::object_count() == 1);
124 REQUIRE(Deleter<6>::object_count() == 1);
126 std::unique_ptr<Foo[], Deleter<5>> foo1(
new Foo[10], std::move(d1));
127 REQUIRE(Foo::object_count() == 10);
129 throwing::unique_ptr<Foo[], Deleter<6>> foo2(
new Foo[10],
131 REQUIRE(Foo::object_count() == 20);
132 REQUIRE(Deleter<5>::object_count() == Deleter<6>::object_count());
134 REQUIRE(foo2.get() !=
nullptr);
135 REQUIRE(Deleter<6>::deleter_calls() == 0);
137 REQUIRE(Deleter<6>::object_count() == Deleter<5>::object_count());
138 REQUIRE(Deleter<6>::deleter_calls() == 1);
141 TEST_CASE(
"unique_ptr to array construction from convertible pointer and copy " 143 "[unique_ptr][array][constructor]") {
144 REQUIRE(Foo::object_count() == 0);
146 REQUIRE(Deleter<7>::object_count() == 1);
148 REQUIRE(Deleter<8>::object_count() == 1);
150 std::unique_ptr<Foo[], Deleter<7>> foo1(
new Foo[10], d1);
151 REQUIRE(Foo::object_count() == 10);
152 throwing::unique_ptr<Foo[], Deleter<8>> foo2(
new Foo[10], d2);
153 REQUIRE(Foo::object_count() == 20);
154 REQUIRE(Deleter<8>::object_count() == Deleter<7>::object_count());
156 REQUIRE(foo2.get() !=
nullptr);
157 REQUIRE(Deleter<8>::deleter_calls() == 0);
159 std::unique_ptr<Foo[], Deleter<7>> moved_foo1(std::move(foo1));
160 REQUIRE(Foo::object_count() == 20);
161 throwing::unique_ptr<Foo[], Deleter<8>> moved_foo2(std::move(foo2));
162 REQUIRE(Foo::object_count() == 20);
163 REQUIRE(Deleter<8>::object_count() == Deleter<7>::object_count());
164 REQUIRE(Deleter<8>::deleter_calls() == 0);
165 REQUIRE(moved_foo2.get() !=
nullptr);
167 REQUIRE(Deleter<8>::object_count() == Deleter<7>::object_count());
168 REQUIRE(Deleter<8>::deleter_calls() == 1);
170 REQUIRE(Deleter<8>::object_count() == Deleter<7>::object_count());
171 REQUIRE(Deleter<8>::deleter_calls() == 1);
174 #if !defined(_MSC_VER
) || _MSC_VER
> 1800
175 TEST_CASE(
"unique_ptr to array construction from convertible pointer and move " 177 "[unique_ptr][array][constructor]") {
178 REQUIRE(Foo::object_count() == 0);
180 REQUIRE(Deleter<9>::object_count() == 1);
182 REQUIRE(Deleter<10>::object_count() == 1);
184 std::unique_ptr<Foo[], Deleter<9> &> foo1(
new Foo[10], d1);
185 REQUIRE(Foo::object_count() == 10);
187 throwing::unique_ptr<Foo[], Deleter<10> &> foo2(
new Foo[10], d2);
188 REQUIRE(Foo::object_count() == 20);
189 REQUIRE(Deleter<10>::object_count() == Deleter<9>::object_count());
191 REQUIRE(foo2.get() !=
nullptr);
192 REQUIRE(Deleter<10>::deleter_calls() == 0);
194 std::unique_ptr<Foo[], Deleter<9>> moved_foo1(std::move(foo1));
195 REQUIRE(Foo::object_count() == 20);
196 throwing::unique_ptr<Foo[], Deleter<10>> moved_foo2(
198 REQUIRE(Foo::object_count() == 20);
199 REQUIRE(Deleter<10>::object_count() == Deleter<9>::object_count());
200 REQUIRE(Deleter<10>::deleter_calls() == 0);
201 REQUIRE(moved_foo2.get() !=
nullptr);
203 REQUIRE(Deleter<10>::object_count() == Deleter<9>::object_count());
204 REQUIRE(Deleter<10>::deleter_calls() == 1);
206 REQUIRE(Deleter<10>::object_count() == Deleter<9>::object_count());
207 REQUIRE(Deleter<10>::deleter_calls() == 1);
TEST_CASE("unique_ptr to array reset to convertible", "[unique_ptr][array][reset][conv.qual]")