throwing_ptr
Smart pointers that throw on dereference if null
shared_ptr_swap.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 TEST_CASE("shared_ptr swap swaps pointers", "[shared_ptr][swap]") {
10  int *ptr1 = new int;
11  throwing::shared_ptr<int> t_ptr1(ptr1);
12  int *ptr2 = new int;
13  throwing::shared_ptr<int> t_ptr2(ptr2);
14  REQUIRE(t_ptr1.get() == ptr1);
15  REQUIRE(t_ptr2.get() == ptr2);
16  t_ptr1.swap(t_ptr2);
17  REQUIRE(t_ptr1.get() == ptr2);
18  REQUIRE(t_ptr2.get() == ptr1);
19  std::swap(t_ptr1, t_ptr2);
20  REQUIRE(t_ptr1.get() == ptr1);
21  REQUIRE(t_ptr2.get() == ptr2);
22 }
23 
24 TEST_CASE("shared_ptr swap null pointers", "[shared_ptr][swap][nullptr]") {
25  throwing::shared_ptr<int> t_ptr1;
26  throwing::shared_ptr<int> t_ptr2;
27  REQUIRE(t_ptr1.get() == nullptr);
28  REQUIRE(t_ptr2.get() == nullptr);
29  t_ptr1.swap(t_ptr2);
30  REQUIRE(t_ptr1.get() == nullptr);
31  REQUIRE(t_ptr2.get() == nullptr);
32  std::swap(t_ptr1, t_ptr2);
33  REQUIRE(t_ptr1.get() == nullptr);
34  REQUIRE(t_ptr2.get() == nullptr);
35 }
TEST_CASE("unique_ptr to array reset to convertible", "[unique_ptr][array][reset][conv.qual]")