throwing_ptr
Smart pointers that throw on dereference if null
shared_ptr_assignment.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  int dummy_a;
12 
13 public:
14  int dummy() { return dummy_a; }
15 };
16 
17 class B : public A {
18  int dummy_b;
19 
20 public:
21  int dummy() { return dummy_b; }
22 };
23 
24 } // namespace
25 
26 TEST_CASE("assignment from throwing::shared_ptr", "[shared_ptr][assignment]") {
27  A *ptr1 = new A;
28  throwing::shared_ptr<A> t_ptr1(ptr1);
29  throwing::shared_ptr<A> t_ptr2;
30  t_ptr2 = t_ptr1;
31  REQUIRE(t_ptr2.get() == t_ptr1.get());
32  t_ptr1.reset();
33  REQUIRE(t_ptr1.get() == nullptr);
34  REQUIRE(t_ptr2.get() == ptr1);
35 }
36 
37 TEST_CASE("assignment from std::shared_ptr", "[shared_ptr][assignment]") {
38  A *ptr1 = new A;
39  std::shared_ptr<A> t_ptr1(ptr1);
40  throwing::shared_ptr<A> t_ptr2;
41  t_ptr2 = t_ptr1;
42  REQUIRE(t_ptr2.get() == t_ptr1.get());
43  t_ptr1.reset();
44  REQUIRE(t_ptr1.get() == nullptr);
45  REQUIRE(t_ptr2.get() == ptr1);
46 }
47 
48 TEST_CASE("assignment from throwing::shared_ptr to derived class",
49  "[shared_ptr][assignment]") {
50  throwing::shared_ptr<B> t_ptr1(new B);
51  throwing::shared_ptr<A> t_ptr2;
52  t_ptr2 = t_ptr1;
53  REQUIRE(t_ptr2.get() == t_ptr1.get());
54 }
55 
56 TEST_CASE("assignment from std::shared_ptr to derived class",
57  "[shared_ptr][assignment]") {
58  std::shared_ptr<B> t_ptr1(new B);
59  throwing::shared_ptr<A> t_ptr2;
60  t_ptr2 = t_ptr1;
61  REQUIRE(t_ptr2.get() == t_ptr1.get());
62 }
63 
64 TEST_CASE("move assignment from throwing::shared_ptr to derived class",
65  "[shared_ptr][assignment]") {
66  B *ptr = new B;
67  throwing::shared_ptr<B> t_ptr1(ptr);
68  throwing::shared_ptr<A> t_ptr2;
69  t_ptr2 = std::move(t_ptr1);
70  REQUIRE(t_ptr1.get() == nullptr);
71  REQUIRE(t_ptr2.get() == ptr);
72 }
73 
74 TEST_CASE("move assignment from std::shared_ptr to derived class",
75  "[shared_ptr][assignment]") {
76  B *ptr = new B;
77  std::shared_ptr<B> t_ptr1(ptr);
78  throwing::shared_ptr<A> t_ptr2;
79  t_ptr2 = std::move(t_ptr1);
80  REQUIRE(t_ptr1.get() == nullptr);
81  REQUIRE(t_ptr2.get() == ptr);
82 }
TEST_CASE("unique_ptr to array reset to convertible", "[unique_ptr][array][reset][conv.qual]")