1 /*
2  * Copyright © 2017 Collabora Ltd.
3  *
4  * This file is part of vkmark.
5  *
6  * vkmark is free software: you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation, either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * vkmark is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with vkmark. If not, see <http://www.gnu.org/licenses/>.
18  *
19  * Authors:
20  *   Alexandros Frantzis <alexandros.frantzis@collabora.com>
21  */
22 
23 #include "src/managed_resource.h"
24 
25 #include "catch.hpp"
26 
27 #include <memory>
28 
29 SCENARIO("managed resource", "")
30 {
31     GIVEN("A managed resource with a destructor")
32     {
33         int x = 0;
34         auto mr = std::make_unique<ManagedResource<int*>>(
__anon7b0f2d4f0102(auto const& p) 35             std::move(&x), [] (auto const& p) { *p = -1; });
36 
37         WHEN("the managed resource is destroyed")
38         {
39             mr.reset();
40 
41             THEN("the destructor is invoked")
42             {
43                 REQUIRE(x == -1);
44             }
45         }
46 
47         WHEN("the managed resource is moved with move-construction")
48         {
49             auto new_mr = std::make_unique<ManagedResource<int*>>(
50                 std::move(*mr));
51 
52             THEN("ownership is moved to new object")
53             {
54                 mr.reset();
55                 REQUIRE(x == 0);
56                 new_mr.reset();
57                 REQUIRE(x == -1);
58             }
59         }
60 
61         WHEN("the managed resource is moved with move-assignment")
62         {
63             int y = 0;
64             auto new_mr = std::make_unique<ManagedResource<int*>>(
__anon7b0f2d4f0202(auto const& p) 65                 std::move(&y), [] (auto const& p) { *p = -1; });
66 
67             *new_mr = std::move(*mr);
68 
69             THEN("old resource of new object is destroyed")
70             {
71                 REQUIRE(y == -1);
72             }
73 
74             THEN("ownership is moved to new object")
75             {
76                 mr.reset();
77                 REQUIRE(x == 0);
78                 new_mr.reset();
79                 REQUIRE(x == -1);
80             }
81         }
82 
83         WHEN("the resource is stolen")
84         {
85             mr->steal();
86 
87             THEN("ownership is removed")
88             {
89                 mr.reset();
90                 REQUIRE(x == 0);
91             }
92         }
93     }
94 }
95