File: shared_ref_test.cpp

package info (click to toggle)
libfplus 0.2.13-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,904 kB
  • sloc: cpp: 27,543; javascript: 634; sh: 105; python: 103; makefile: 6
file content (64 lines) | stat: -rw-r--r-- 1,899 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright 2015, Tobias Hermann and the FunctionalPlus contributors.
// https://github.com/Dobiasd/FunctionalPlus
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
//  http://www.boost.org/LICENSE_1_0.txt)

#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest/doctest.h"
#include <fplus/fplus.hpp>

namespace
{
    std::vector<std::string> logs;

    void log(const std::string& str)
    {
        logs.push_back(str);
    }

    struct test
    {
        int m_x;

        test(int x)         :m_x(x)                 { log("test(" + fplus::show(m_x) + ")"); }
        test(const test& t) :m_x(t.m_x)             { log("test(const test& " + fplus::show(m_x) + ")"); }
        test(test&& t)      :m_x(std::move(t.m_x))  { log("test(test&& " + fplus::show(m_x) + ")"); }

        test& operator=(int x)          { m_x = x;                  log("test::operator=(" + fplus::show(m_x) + ")"); return *this;}
        test& operator=(const test& t)  { m_x = t.m_x;              log("test::operator=(const test& " + fplus::show(m_x) + ")"); return *this;}
        test& operator=(test&& t)       { m_x = std::move(t.m_x);   log("test::operator=(test&& " + fplus::show(m_x) + ")"); return *this;}

        ~test()             { log("~test(" + fplus::show(m_x) + ")"); }
    };
}

TEST_CASE("shared_ref_test, full")
{
    using namespace fplus;

    {
        auto ref = make_shared_ref<test>(1);
        auto ref2 = ref;

        *ref2 = test(5);
    }
    {
        test o(2);
        auto ref = make_shared_ref<test>(std::move(o));
    }

    const std::vector<std::string> logs_dest = {
        "test(1)",
        "test(5)",
        "test::operator=(test&& 5)",
        "~test(5)",
        "~test(5)",
        "test(2)",
        "test(test&& 2)",
        "~test(2)",
        "~test(2)"
    };

    REQUIRE_EQ(logs, logs_dest);
}