1 // Copyright 2015, Tobias Hermann and the FunctionalPlus contributors.
2 // https://github.com/Dobiasd/FunctionalPlus
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 //  http://www.boost.org/LICENSE_1_0.txt)
6 
7 #include <doctest/doctest.h>
8 #include <fplus/fplus.hpp>
9 
10 namespace
11 {
12     std::vector<std::string> logs;
13 
log(const std::string & str)14     void log(const std::string& str)
15     {
16         logs.push_back(str);
17     }
18 
19     struct test
20     {
21         int m_x;
22 
test__anone3fc6ba80111::test23         test(int x)         :m_x(x)                 { log("test(" + fplus::show(m_x) + ")"); }
test__anone3fc6ba80111::test24         test(const test& t) :m_x(t.m_x)             { log("test(const test& " + fplus::show(m_x) + ")"); }
test__anone3fc6ba80111::test25         test(test&& t)      :m_x(std::move(t.m_x))  { log("test(test&& " + fplus::show(m_x) + ")"); }
26 
operator =__anone3fc6ba80111::test27         test& operator=(int x)          { m_x = x;                  log("test::operator=(" + fplus::show(m_x) + ")"); return *this;}
operator =__anone3fc6ba80111::test28         test& operator=(const test& t)  { m_x = t.m_x;              log("test::operator=(const test& " + fplus::show(m_x) + ")"); return *this;}
operator =__anone3fc6ba80111::test29         test& operator=(test&& t)       { m_x = std::move(t.m_x);   log("test::operator=(test&& " + fplus::show(m_x) + ")"); return *this;}
30 
~test__anone3fc6ba80111::test31         ~test()             { log("~test(" + fplus::show(m_x) + ")"); }
32     };
33 }
34 
35 TEST_CASE("shared_ref_test - full")
36 {
37     using namespace fplus;
38 
39     {
40         auto ref = make_shared_ref<test>(1);
41         auto ref2 = ref;
42 
43         *ref2 = test(5);
44     }
45     {
46         test o(2);
47         auto ref = make_shared_ref<test>(std::move(o));
48     }
49 
50     const std::vector<std::string> logs_dest = {
51         "test(1)",
52         "test(5)",
53         "test::operator=(test&& 5)",
54         "~test(5)",
55         "~test(5)",
56         "test(2)",
57         "test(test&& 2)",
58         "~test(2)",
59         "~test(2)"
60     };
61 
62     REQUIRE_EQ(logs, logs_dest);
63 }
64