throwing_ptr
Smart pointers that throw on dereference if null
Functions
shared_ptr_reset.cpp File Reference

Go to the source code of this file.

Functions

 TEST_CASE ("shared_ptr reset with no arguments clears", "[shared_ptr][reset]")
 
 TEST_CASE ("shared_ptr reset with pointer", "[shared_ptr][reset]")
 
 TEST_CASE ("shared_ptr reset with pointer and deleter", "[shared_ptr][reset]")
 
 TEST_CASE ("shared_ptr reset with pointer, deleter and allocator", "[shared_ptr][reset]")
 

Function Documentation

◆ TEST_CASE() [1/4]

TEST_CASE ( "shared_ptr reset with no arguments clears"  ,
""  [shared_ptr][reset] 
)

Definition at line 28 of file shared_ptr_reset.cpp.

28  {
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 }
Wrapper aroung std::shared_ptr that throws when a wrapped null pointer is dereferenced.
Definition: shared_ptr.hpp:63

◆ TEST_CASE() [2/4]

TEST_CASE ( "shared_ptr reset with pointer"  ,
""  [shared_ptr][reset] 
)

Definition at line 39 of file shared_ptr_reset.cpp.

39  {
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 }
Wrapper aroung std::shared_ptr that throws when a wrapped null pointer is dereferenced.
Definition: shared_ptr.hpp:63

◆ TEST_CASE() [3/4]

TEST_CASE ( "shared_ptr reset with pointer and deleter"  ,
""  [shared_ptr][reset] 
)

Definition at line 53 of file shared_ptr_reset.cpp.

53  {
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 }
Wrapper aroung std::shared_ptr that throws when a wrapped null pointer is dereferenced.
Definition: shared_ptr.hpp:63

◆ TEST_CASE() [4/4]

TEST_CASE ( "shared_ptr reset with  pointer,
deleter and allocator"  ,
""  [shared_ptr][reset] 
)

Definition at line 80 of file shared_ptr_reset.cpp.

81  {
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 }
Wrapper aroung std::shared_ptr that throws when a wrapped null pointer is dereferenced.
Definition: shared_ptr.hpp:63