File: test_auto_ptr.cpp

package info (click to toggle)
libtut 0.0.20070706-2
  • links: PTS
  • area: main
  • in suites: buster
  • size: 1,148 kB
  • ctags: 607
  • sloc: cpp: 3,588; xml: 137; makefile: 61; ansic: 9
file content (155 lines) | stat: -rw-r--r-- 3,356 bytes parent folder | download | duplicates (4)
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
#include <tut/tut.hpp>
#include <memory>

using std::auto_ptr;

/**
 * This example test group tests std::auto_ptr implementation.
 * Tests are far from full, of course.
 */
namespace tut
{
/**
 * Struct which may contain test data members.
 * Test object (class that contains test methods)
 * will inherite from it, so each test method can
 * access members directly.
 *
 * Additionally, for each test, test object is re-created
 * using defaut constructor. Thus, any prepare work can be put
 * into default constructor.
 *
 * Finally, after each test, test object is destroyed independently
 * of test result, so any cleanup work should be located in destructor.
 */
struct auto_ptr_data
{
    /**
     * Type used to check scope lifetime of auto_ptr object.
     * Sets extern boolean value into true at constructor, and
     * to false at destructor.
     */
    bool exists;
    struct existing
    {
        bool& s_;
        existing(bool& s) : s_(s)
        {
            s_ = true;
        }
        ~existing()
        {
            s_ = false;
        }
    };
};

/**
 * This group of declarations is just to register
 * test group in test-application-wide singleton.
 * Name of test group object (auto_ptr_group) shall
 * be unique in tut:: namespace. Alternatively, you
 * you may put it into anonymous namespace.
 */
typedef test_group<auto_ptr_data> tf;
typedef tf::object object;
tf auto_ptr_group("std::auto_ptr");

/**
 * Checks default constructor.
 */
template<>
template<>
void object::test<1>()
{
    auto_ptr<existing> ap;
    ensure(ap.get() == 0);
    ensure(ap.operator->() == 0);
}

/**
 * Checks constructor with object
 */
template<>
template<>
void object::test<2>()
{
    {
        auto_ptr<existing> ap(new existing(exists));
        ensure("get", ap.get() != 0);
        ensure_equals("constructed", exists, true);
    }
    // ptr left scope
    ensure_equals("destructed", exists, false);
}

/**
 * Checks operator -> and get()
 */
template<>
template<>
void object::test<3>()
{
    auto_ptr<existing> ap(new existing(exists));
    existing* p1 = ap.get();
    existing* p2 = ap.operator->();
    ensure("get equiv ->", p1 == p2);
    // ensure no losing ownership
    p1 = ap.get();
    ensure("still owner", p1 == p2);
}

/**
 * Checks release()
 */
template<>
template<>
void object::test<4>()
{
    {
        auto_ptr<existing> ap(new existing(exists));
        existing* p1 = ap.get();
        auto_ptr<existing> ap2(ap.release());
        ensure("same pointer", p1 == ap2.get());
        ensure("lost ownership", ap.get() == 0);
    }
    ensure("destructed", exists == false);
}

/**
 * Checks assignment.
 */
template<>
template<>
void object::test<5>()
{
    {
        auto_ptr<existing> ap(new existing(exists));
        existing* p1 = ap.get();
        auto_ptr<existing> ap2;
        ap2 = ap;
        ensure("same pointer", p1 == ap2.get());
        ensure("lost ownership", ap.get() == 0);
    }
    ensure("destructed", exists == false);
}

/**
 * Checks copy constructor.
 */
template<>
template<>
void object::test<6>()
{
    {
        auto_ptr<existing> ap(new existing(exists));
        existing* p1 = ap.get();
        auto_ptr<existing> ap2(ap);
        ensure("same pointer", p1 == ap2.get());
        ensure("lost ownership", ap.get() == 0);
    }
    ensure("destructed", exists == false);
}

}