File: intrusive_ref_counter_test.cpp

package info (click to toggle)
boost1.74 1.74.0%2Bds1-21
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 463,588 kB
  • sloc: cpp: 3,338,117; xml: 131,293; python: 33,088; ansic: 14,292; asm: 4,038; sh: 3,353; makefile: 1,193; perl: 1,036; yacc: 478; php: 212; ruby: 102; lisp: 24; sql: 13; csh: 6
file content (156 lines) | stat: -rw-r--r-- 3,383 bytes parent folder | download | duplicates (9)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
 *             Copyright Andrey Semashev 2013.
 * 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)
 */
/*!
 * \file   intrusive_ref_counter_test.cpp
 * \author Andrey Semashev
 * \date   31.08.2013
 *
 * This file contains tests for the \c intrusive_ref_counter base class.
 */

#include <boost/config.hpp>

#if defined(BOOST_MSVC)

#pragma warning(disable: 4786)  // identifier truncated in debug info
#pragma warning(disable: 4710)  // function not inlined
#pragma warning(disable: 4711)  // function selected for automatic inline expansion
#pragma warning(disable: 4514)  // unreferenced inline removed
#pragma warning(disable: 4355)  // 'this' : used in base member initializer list
#pragma warning(disable: 4511)  // copy constructor could not be generated
#pragma warning(disable: 4512)  // assignment operator could not be generated

#if (BOOST_MSVC >= 1310)
#pragma warning(disable: 4675)  // resolved overload found with Koenig lookup
#endif

#endif

#include <cstddef>
#include <boost/smart_ptr/intrusive_ref_counter.hpp>
#include <boost/intrusive_ptr.hpp>
#include <boost/core/lightweight_test.hpp>

namespace N1 {

class my_class :
    public boost::intrusive_ref_counter< my_class >
{
public:
    static unsigned int destructor_count;

    ~my_class()
    {
        ++destructor_count;
    }
};

unsigned int my_class::destructor_count = 0;

} // namespace N1

namespace N2 {

class my_class :
    public boost::intrusive_ref_counter< my_class, boost::thread_unsafe_counter >
{
public:
    static unsigned int destructor_count;

    ~my_class()
    {
        ++destructor_count;
    }
};

unsigned int my_class::destructor_count = 0;

} // namespace N2

namespace N3 {

struct root :
    public boost::intrusive_ref_counter< root >
{
    virtual ~root() {}
};

} // namespace N3

namespace N4 {

struct X :
    public virtual N3::root
{
};

} // namespace N4

namespace N5 {

struct Y :
    public virtual N3::root
{
};

} // namespace N5

namespace N6 {

struct Z :
    public N4::X,
    public N5::Y
{
    static unsigned int destructor_count;

    ~Z()
    {
        ++destructor_count;
    }
};

unsigned int Z::destructor_count = 0;

} // namespace N6


int main()
{
    // The test check that ADL works
    {
        boost::intrusive_ptr< N1::my_class > p = new N1::my_class();
        p = NULL;
        BOOST_TEST(N1::my_class::destructor_count == 1);
    }
    {
        boost::intrusive_ptr< N2::my_class > p = new N2::my_class();
        p = NULL;
        BOOST_TEST(N2::my_class::destructor_count == 1);
    }
    {
        N1::my_class* p = new N1::my_class();
        intrusive_ptr_add_ref(p);
        intrusive_ptr_release(p);
        BOOST_TEST(N1::my_class::destructor_count == 2);
    }

    // The test checks that destroying through the base class works
    {
        boost::intrusive_ptr< N6::Z > p1 = new N6::Z();
        BOOST_TEST(p1->use_count() == 1);
        BOOST_TEST(N6::Z::destructor_count == 0);
        boost::intrusive_ptr< N3::root > p2 = p1;
        BOOST_TEST(p1->use_count() == 2);
        BOOST_TEST(N6::Z::destructor_count == 0);
        p1 = NULL;
        BOOST_TEST(N6::Z::destructor_count == 0);
        p2 = NULL;
        BOOST_TEST(N6::Z::destructor_count == 1);
    }

    return boost::report_errors();
}