throwing_ptr
Smart pointers that throw on dereference if null
shared_ptr_reset.cpp
Go to the documentation of this file.
1 // Copyright Claudio Bantaloukas 2017-2018.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
5 
6 #include <catch.hpp>
7 #include <throwing/shared_ptr.hpp>
8 
9 namespace {
10 class A {
11 public:
12  A(bool &deleted) : deleted_(deleted) {}
13  virtual ~A() { deleted_ = true; }
14  A &operator=(const A &) { return *this; }
15  bool &deleted_;
16 };
17 
18 class B : public A {
19  int dummy_b;
20 
21 public:
22  B(bool &deleted) : A(deleted) {}
23  B &operator=(const B &) { return *this; }
24  int dummy() { return dummy_b; }
25 };
26 } // namespace
27 
28 TEST_CASE("shared_ptr reset with no arguments clears", "[shared_ptr][reset]") {
29  bool deleted = false;
30  A *ptr1 = new A(deleted);
31  throwing::shared_ptr<A> t_ptr1(ptr1);
32  REQUIRE(t_ptr1.get() == ptr1);
33  REQUIRE_FALSE(deleted);
34  t_ptr1.reset();
35  REQUIRE(t_ptr1.get() == nullptr);
36  REQUIRE(deleted);
37 }
38 
39 TEST_CASE("shared_ptr reset with pointer", "[shared_ptr][reset]") {
40  bool deleted1 = false;
41  A *ptr1 = new A(deleted1);
42  bool deleted2 = false;
43  B *ptr2 = new B(deleted2);
44 
45  throwing::shared_ptr<A> t_ptr1(ptr1);
46  REQUIRE(t_ptr1.get() == ptr1);
47  REQUIRE_FALSE(deleted1);
48  t_ptr1.reset(ptr2);
49  REQUIRE(t_ptr1.get() == ptr2);
50  REQUIRE(deleted1);
51 }
52 
53 TEST_CASE("shared_ptr reset with pointer and deleter", "[shared_ptr][reset]") {
54  bool deleted1 = false;
55  A *ptr1 = new A(deleted1);
56  bool deleted2 = false;
57  B *ptr2 = new B(deleted2);
58 
59  bool lamda_called = false;
60  auto deleter = [&lamda_called](A *p) {
61  delete p;
62  lamda_called = true;
63  };
64 
65  throwing::shared_ptr<A> t_ptr1(ptr1);
66  REQUIRE(t_ptr1.get() == ptr1);
67  REQUIRE_FALSE(deleted1);
68 
69  t_ptr1.reset(ptr2, deleter);
70  REQUIRE_FALSE(lamda_called);
71  REQUIRE(deleted1);
72  REQUIRE(t_ptr1.get() == ptr2);
73 
74  t_ptr1.reset();
75  REQUIRE(deleted2);
76  REQUIRE(lamda_called);
77  REQUIRE(t_ptr1.get() == nullptr);
78 }
79 
80 TEST_CASE("shared_ptr reset with pointer, deleter and allocator",
81  "[shared_ptr][reset]") {
82  bool deleted1 = false;
83  A *ptr1 = new A(deleted1);
84  bool deleted2 = false;
85  B *ptr2 = new B(deleted2);
86 
87  bool lamda_called = false;
88  auto deleter = [&lamda_called](A *p) {
89  delete p;
90  lamda_called = true;
91  };
92 
93  throwing::shared_ptr<A> t_ptr1(ptr1);
94  REQUIRE(t_ptr1.get() == ptr1);
95  REQUIRE_FALSE(deleted1);
96 
97  std::allocator<void *> allocator;
98  t_ptr1.reset(ptr2, deleter, allocator);
99  REQUIRE_FALSE(lamda_called);
100  REQUIRE(deleted1);
101  REQUIRE(t_ptr1.get() == ptr2);
102 
103  t_ptr1.reset();
104  REQUIRE(deleted2);
105  REQUIRE(lamda_called);
106  REQUIRE(t_ptr1.get() == nullptr);
107 }
TEST_CASE("unique_ptr to array reset to convertible", "[unique_ptr][array][reset][conv.qual]")