A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
Friends
Macros
Groups
Pages
ptr-test-suite.cc
Go to the documentation of this file.
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
* Copyright (c) 2005,2006 INRIA
4
*
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License version 2 as
7
* published by the Free Software Foundation;
8
*
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
13
*
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
*
18
* Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19
*/
20
21
#include "ns3/test.h"
22
#include "ns3/ptr.h"
23
24
using namespace
ns3;
25
26
class
PtrTestCase
;
27
28
class
Base
29
{
30
public
:
31
Base
();
32
virtual
~
Base
();
33
void
Ref (
void
)
const
;
34
void
Unref (
void
)
const
;
35
private
:
36
mutable
uint32_t
m_count
;
37
};
38
39
class
NoCount
:
public
Base
40
{
41
public
:
42
NoCount
(
PtrTestCase
*
test
);
43
~
NoCount
();
44
void
Nothing (
void
)
const
;
45
private
:
46
PtrTestCase
*
m_test
;
47
};
48
49
50
class
PtrTestCase
:
public
TestCase
51
{
52
public
:
53
PtrTestCase
();
54
void
DestroyNotify (
void
);
55
private
:
56
virtual
void
DoRun (
void
);
57
Ptr<NoCount>
CallTest (
Ptr<NoCount>
p);
58
Ptr<NoCount>
const
CallTestConst (
Ptr<NoCount>
const
p);
59
uint32_t
m_nDestroyed
;
60
};
61
62
63
Base::Base
()
64
: m_count (1)
65
{
66
}
67
Base::~Base
()
68
{
69
}
70
void
71
Base::Ref
(
void
)
const
72
{
73
m_count
++;
74
}
75
void
76
Base::Unref
(
void
)
const
77
{
78
m_count
--;
79
if
(
m_count
== 0)
80
{
81
delete
this
;
82
}
83
}
84
85
NoCount::NoCount
(
PtrTestCase
*
test
)
86
: m_test (test)
87
{
88
}
89
NoCount::~NoCount
()
90
{
91
m_test
->
DestroyNotify
();
92
}
93
void
94
NoCount::Nothing
()
const
95
{}
96
97
98
99
PtrTestCase::PtrTestCase
(
void
)
100
:
TestCase
(
"Sanity checking of Ptr<>"
)
101
{
102
}
103
void
104
PtrTestCase::DestroyNotify
(
void
)
105
{
106
m_nDestroyed
++;
107
}
108
Ptr<NoCount>
109
PtrTestCase::CallTest
(
Ptr<NoCount>
p)
110
{
111
return
p;
112
}
113
114
Ptr<NoCount>
const
115
PtrTestCase::CallTestConst
(
Ptr<NoCount>
const
p)
116
{
117
return
p;
118
}
119
120
121
void
122
PtrTestCase::DoRun
(
void
)
123
{
124
m_nDestroyed
=
false
;
125
{
126
Ptr<NoCount>
p = Create<NoCount> (
this
);
127
}
128
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 1,
"001"
);
129
130
m_nDestroyed
= 0;
131
{
132
Ptr<NoCount>
p;
133
p = Create<NoCount> (
this
);
134
p = p;
135
}
136
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 1,
"002"
);
137
138
m_nDestroyed
= 0;
139
{
140
Ptr<NoCount>
p1;
141
p1 = Create<NoCount> (
this
);
142
Ptr<NoCount>
p2 = p1;
143
}
144
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 1,
"003"
);
145
146
m_nDestroyed
= 0;
147
{
148
Ptr<NoCount>
p1;
149
p1 = Create<NoCount> (
this
);
150
Ptr<NoCount>
p2;
151
p2 = p1;
152
}
153
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 1,
"004"
);
154
155
m_nDestroyed
= 0;
156
{
157
Ptr<NoCount>
p1;
158
p1 = Create<NoCount> (
this
);
159
Ptr<NoCount>
p2 = Create<NoCount> (
this
);
160
p2 = p1;
161
}
162
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 2,
"005"
);
163
164
m_nDestroyed
= 0;
165
{
166
Ptr<NoCount>
p1;
167
p1 = Create<NoCount> (
this
);
168
Ptr<NoCount>
p2;
169
p2 = Create<NoCount> (
this
);
170
p2 = p1;
171
}
172
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 2,
"006"
);
173
174
m_nDestroyed
= 0;
175
{
176
Ptr<NoCount>
p1;
177
p1 = Create<NoCount> (
this
);
178
p1 = Create<NoCount> (
this
);
179
}
180
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 2,
"007"
);
181
182
m_nDestroyed
= 0;
183
{
184
Ptr<NoCount>
p1;
185
{
186
Ptr<NoCount>
p2;
187
p1 = Create<NoCount> (
this
);
188
p2 = Create<NoCount> (
this
);
189
p2 = p1;
190
}
191
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 1,
"008"
);
192
}
193
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 2,
"009"
);
194
195
m_nDestroyed
= 0;
196
{
197
Ptr<NoCount>
p1;
198
{
199
Ptr<NoCount>
p2;
200
p1 = Create<NoCount> (
this
);
201
p2 = Create<NoCount> (
this
);
202
p2 =
CallTest
(p1);
203
}
204
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 1,
"010"
);
205
}
206
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 2,
"011"
);
207
208
{
209
Ptr<NoCount>
p1;
210
Ptr<NoCount>
const
p2 =
CallTest
(p1);
211
Ptr<NoCount>
const
p3 =
CallTestConst
(p1);
212
Ptr<NoCount>
p4 =
CallTestConst
(p1);
213
Ptr<NoCount const>
p5 = p4;
214
//p4 = p5; You cannot make a const pointer be a non-const pointer.
215
// but if you use ConstCast, you can.
216
p4 = ConstCast<NoCount> (p5);
217
p5 = p1;
218
Ptr<NoCount>
p;
219
if
(p == 0)
220
{}
221
if
(p != 0)
222
{}
223
if
(0 == p)
224
{}
225
if
(0 != p)
226
{}
227
if
(p)
228
{}
229
if
(!p)
230
{}
231
}
232
233
m_nDestroyed
= 0;
234
{
235
NoCount
*raw;
236
{
237
Ptr<NoCount>
p = Create<NoCount> (
this
);
238
{
239
Ptr<NoCount const>
p1 = p;
240
}
241
raw =
GetPointer
(p);
242
p = 0;
243
}
244
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 0,
"012"
);
245
delete
raw;
246
}
247
248
m_nDestroyed
= 0;
249
{
250
Ptr<NoCount>
p = Create<NoCount> (
this
);
251
const
NoCount
*v1 =
PeekPointer
(p);
252
NoCount
*v2 =
PeekPointer
(p);
253
v1->Nothing ();
254
v2->
Nothing
();
255
}
256
NS_TEST_EXPECT_MSG_EQ
(
m_nDestroyed
, 1,
"013"
);
257
258
{
259
Ptr<Base>
p0 = Create<NoCount> (
this
);
260
Ptr<NoCount>
p1 = Create<NoCount> (
this
);
261
NS_TEST_EXPECT_MSG_EQ
((p0 == p1),
false
,
"operator == failed"
);
262
NS_TEST_EXPECT_MSG_EQ
((p0 != p1),
true
,
"operator != failed"
);
263
}
264
}
265
266
static
class
PtrTestSuite
:
public
TestSuite
267
{
268
public
:
269
PtrTestSuite
()
270
:
TestSuite
(
"ptr"
,
UNIT
)
271
{
272
AddTestCase
(
new
PtrTestCase
(), TestCase::QUICK);
273
}
274
}
g_ptrTestSuite
;
NoCount
Definition:
ptr-test-suite.cc:39
PtrTestCase::CallTest
Ptr< NoCount > CallTest(Ptr< NoCount > p)
Definition:
ptr-test-suite.cc:109
ns3::Ptr
smart pointer class similar to boost::intrusive_ptr
Definition:
ptr.h:59
g_ptrTestSuite
PtrTestSuite g_ptrTestSuite
Base
Definition:
ptr-test-suite.cc:28
ns3::TestSuite
A suite of tests to run.
Definition:
test.h:1025
ns3::GetPointer
T * GetPointer(const Ptr< T > &p)
Definition:
ptr.h:285
Base::Unref
void Unref(void) const
Definition:
ptr-test-suite.cc:76
PtrTestCase::DestroyNotify
void DestroyNotify(void)
Definition:
ptr-test-suite.cc:104
ns3::TestCase
encapsulates test code
Definition:
test.h:849
NoCount::~NoCount
~NoCount()
Definition:
ptr-test-suite.cc:89
PtrTestSuite
Definition:
ptr-test-suite.cc:266
NS_TEST_EXPECT_MSG_EQ
#define NS_TEST_EXPECT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report if not.
Definition:
test.h:244
NoCount::Nothing
void Nothing(void) const
Definition:
ptr-test-suite.cc:94
ns3::PeekPointer
T * PeekPointer(const Ptr< T > &p)
Definition:
ptr.h:279
PtrTestCase::PtrTestCase
PtrTestCase()
Definition:
ptr-test-suite.cc:99
Base::Base
Base()
Definition:
ptr-test-suite.cc:63
Base::Ref
void Ref(void) const
Definition:
ptr-test-suite.cc:71
NoCount::NoCount
NoCount(PtrTestCase *test)
Definition:
ptr-test-suite.cc:85
NoCount::m_test
PtrTestCase * m_test
Definition:
ptr-test-suite.cc:46
PtrTestCase::DoRun
virtual void DoRun(void)
Implementation to actually run this TestCase.
Definition:
ptr-test-suite.cc:122
ns3::TestCase::AddTestCase
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual child TestCase case to this TestCase.
Definition:
test.cc:173
Base::m_count
uint32_t m_count
Definition:
ptr-test-suite.cc:36
Base::~Base
virtual ~Base()
Definition:
ptr-test-suite.cc:67
PtrTestCase
Definition:
ptr-test-suite.cc:50
PtrTestCase::CallTestConst
Ptr< NoCount > const CallTestConst(Ptr< NoCount > const p)
Definition:
ptr-test-suite.cc:115
ns3::TestSuite::UNIT
This test suite implements a Unit Test.
Definition:
test.h:1035
PtrTestCase::m_nDestroyed
uint32_t m_nDestroyed
Definition:
ptr-test-suite.cc:59
test
void test(void)
Definition:
main-test-sync.cc:75
PtrTestSuite::PtrTestSuite
PtrTestSuite()
Definition:
ptr-test-suite.cc:269
src
core
test
ptr-test-suite.cc
Generated on Sat Apr 19 2014 14:06:53 for ns-3 by
1.8.6